From de54c2b776ccda84df0bbb7ef24b53c5f92144b8 Mon Sep 17 00:00:00 2001
From: Kyle Klenk <kyle.c.klenk@gmail.com>
Date: Thu, 18 Aug 2022 20:23:57 +0000
Subject: [PATCH] delete cppwrap_job.f90

---
 build/includes/job_actor/job_actor.hpp        |   2 +-
 .../job_actor_subroutine_wrappers.hpp         |   6 +-
 build/makefile                                |   1 -
 build/source/actors/job_actor/cppwrap_job.f90 | 184 ---------------
 build/source/actors/job_actor/job_actor.cpp   |  41 ++--
 build/source/actors/job_actor/job_actor.f90   |  71 +++++-
 build/source/netcdf/read_icondActors.f90      | 213 ++++++++++--------
 7 files changed, 211 insertions(+), 307 deletions(-)
 delete mode 100644 build/source/actors/job_actor/cppwrap_job.f90

diff --git a/build/includes/job_actor/job_actor.hpp b/build/includes/job_actor/job_actor.hpp
index b51be9e..1c89afb 100644
--- a/build/includes/job_actor/job_actor.hpp
+++ b/build/includes/job_actor/job_actor.hpp
@@ -45,7 +45,7 @@ struct job_state {
 behavior job_actor(stateful_actor<job_state>* self, int start_gru, int num_gru, 
     std::string config_path, int output_struct_size, actor parent);
 
-void initJob(stateful_actor<job_state>* self);
+void initCsvOutputFile(stateful_actor<job_state>* self);
 
 void initalizeGRU(stateful_actor<job_state>* self);
 
diff --git a/build/includes/job_actor/job_actor_subroutine_wrappers.hpp b/build/includes/job_actor/job_actor_subroutine_wrappers.hpp
index 9f30914..d215986 100644
--- a/build/includes/job_actor/job_actor_subroutine_wrappers.hpp
+++ b/build/includes/job_actor/job_actor_subroutine_wrappers.hpp
@@ -10,6 +10,10 @@ extern "C" {
 
     void readDimension(int* num_gru, int* num_hru, int* start_gru_index, int* err);
 
-    void cleanUpJobActor(int* err);
+    void readIcondNLayers(int* num_gru, int* err);
+
+    void allocateTimeStructure(int* err);
+
+    void deallocateJobActor(int* err);
     
 }
\ No newline at end of file
diff --git a/build/makefile b/build/makefile
index 8e3923e..bffef6f 100644
--- a/build/makefile
+++ b/build/makefile
@@ -148,7 +148,6 @@ SUMMA_FILEACCESS_INTERFACE = \
 FILEACCESS_INTERFACE = $(patsubst %, $(FILE_ACCESS_DIR)/%, $(SUMMA_FILEACCESS_INTERFACE))
 
 SUMMA_JOB_INTERFACE = \
-		cppwrap_job.f90 \
 		job_actor.f90
 
 JOB_INTERFACE =  $(patsubst %, $(JOB_ACTOR_DIR)/%, $(SUMMA_JOB_INTERFACE))
diff --git a/build/source/actors/job_actor/cppwrap_job.f90 b/build/source/actors/job_actor/cppwrap_job.f90
deleted file mode 100644
index c8a4221..0000000
--- a/build/source/actors/job_actor/cppwrap_job.f90
+++ /dev/null
@@ -1,184 +0,0 @@
-module cppwrap_job
-  USE, intrinsic :: iso_c_binding
-  USE nrtype
-  USE data_types
-  USE globalData
-
-  implicit none
-  public::initGlobals
-  public::cleanUpJobActor
-  private::allocTime
-
-  contains
-! **********************************************************************************************************
-! public subroutine initGlobals: set global vars and directories,
-! **********************************************************************************************************
-subroutine initGlobals(file_manager, totalGRUs, totalHRUs, numGRUs, numHRUs, startGRUIndex, err) bind(C, name='initGlobals')
-  ! subroutines and functions: initial priming
-  ! USE summa4chm_util, only:getCommandArguments4chm            ! process command line arguments
-  USE summaActors_FileManager,only:summa_SetTimesDirsAndFiles        ! sets directories and filenames
-  USE summa_globalData,only:summa_defineGlobalData            ! used to define global summa data structures
-  ! subroutines and functions: read dimensions (NOTE: NetCDF)
-  USE read_attribute_module,only:read_dimension               ! module to read dimensions of GRU and HRU
-  USE read_icond4chm_module,only:read_icond_nlayers           ! module to read initial condition dimensions
-  ! provide access to file paths
-  USE summaActors_FileManager,only:SETTINGS_PATH                     ! define path to settings files (e.g., parameters, soil and veg. tables)
-  USE summaActors_FileManager,only:STATE_PATH                        ! optional path to state/init. condition files (defaults to SETTINGS_PATH)
-  USE summaActors_FileManager,only:MODEL_INITCOND                    ! name of model initial conditions file
-  USE summaActors_FileManager,only:LOCAL_ATTRIBUTES                  ! name of model initial attributes file
-  ! set up forcing data, needed here to avoid concurrent access
-  USE ffile_info_module,only:ffile_info                       ! module to read information on forcing datafile
-  USE mDecisions_module,only:mDecisions                       ! module to read model decisions
-  USE allocspace4chm_module,only:allocLocal
-
-  USE cppwrap_auxiliary,only:c_f_string
-
-  implicit none
-  ! dummy variables
-  character(kind=c_char,len=1),intent(in)   :: file_manager(*)
-  integer(c_int),intent(inout)              :: totalGRUs            ! Variable to return total number of GRUs (not really needed)
-  integer(c_int),intent(inout)              :: totalHRUs            ! Variable to return total number of HRUs (not really needed)
-  integer(c_int),intent(in)                 :: numGRUs              ! Number of GRUs for this current run
-  integer(c_int),intent(out)                :: numHRUs              ! Number of HRUs for this current run
-  integer(c_int),intent(in)                 :: startGRUIndex        ! Index of the starting GRU
-  integer(c_int),intent(out)                :: err                  ! Error Code
-
-  ! local variables
-  character(len=256)                        :: file_manager_path    ! path/name of file defining directories and files
-  character(len=256)                        :: message              ! error message 
-  character(LEN=256)                        :: cmessage             ! error message of downwind routine
-  character(len=256)                        :: restartFile          ! restart file name
-  character(len=256)                        :: attrFile             ! attributes file name
-  integer(i4b)                              :: fileGRU              ! [used for filenames] number of GRUs in the input file
-  integer(i4b)                              :: fileHRU              ! [used for filenames] number of HRUs in the input file
-  character(len=256)                        :: summaFileManagerFile ! path/name of file defining directories and files
-
-  ! call c_f_string(file_manager, file_manager_path, 256)
-
-  ! Conver the fileManager path to format needed for summa_SetTimesDirsAndFIles
-  ! summaFileManagerFile = trim(file_manager_path)
-  ! set directories and files -- summaFileManager used as command-line argument
-  ! call summa_SetTimesDirsAndFiles(summaFileManagerFile,err,cmessage)
-  ! ! if(err/=0)then; message=trim(message)//trim(cmessage); return; endif;
-  ! if(err/=0)then
-  !   message=trim(message)//trim(cmessage)
-  !   print*, cmessage
-  !   return
-  ! endif
-
-  ! define global data (parameters, metadata)
-  ! call summa_defineGlobalData(err, cmessage)
-  ! ! if(err/=0)then; message=trim(message)//trim(cmessage); return; endif
-  ! if(err/=0)then
-  !   message=trim(message)//trim(cmessage)
-  !   print*, cmessage
-  !   return
-  ! endif
-  ! Set the index of the start GRU
-  ! startGRU = startGRUIndex
-
-  ! *****************************************************************************
-  ! *** read the number of GRUs and HRUs
-  ! *****************************************************************************
-  ! attrFile = trim(SETTINGS_PATH)//trim(LOCAL_ATTRIBUTES)
-  ! call read_dimension(trim(attrFile),fileGRU,fileHRU,numGRUs,numHRUs,startGRUIndex,err,cmessage)
-  ! if(err/=0)then
-  !   message=trim(message)//trim(cmessage)
-  !   print*, cmessage
-  ! return
-  ! endif
-  ! totalGRUs = fileGRU
-  ! totalHRUs = fileHRU
-
-  ! *****************************************************************************
-  ! *** read the number of snow and soil layers
-  ! *****************************************************************************
-  !  set restart filename and read the number of snow and soil layers from the initial conditions (restart) file
-  if(STATE_PATH == '') then
-    restartFile = trim(SETTINGS_PATH)//trim(MODEL_INITCOND)
-  else
-    restartFile = trim(STATE_PATH)//trim(MODEL_INITCOND)
-  endif
- 
-  call read_icond_nlayers(trim(restartFile),numGRUs,indx_meta,err,cmessage)
-  if(err/=0)then
-    message=trim(message)//trim(cmessage)
-    print*, cmessage
-    return
-  endif
-
-  ! Initalize time structures
-  call allocTime(err)
-  if(err/=0)then; message=trim(message)//trim(cmessage); return; endif
-  
-end subroutine initGlobals
-
-
-subroutine allocTime(err)
-  USE globalData,only:startTime,finshTime,refTime,oldTime
-  USE allocspace4chm_module,only:allocLocal
-  USE globalData,only:time_meta
-  
-  implicit none
-  ! dummy variables
-  integer(i4b),intent(inout)      :: err
-  ! local variables
-  character(len=256)              :: cmessage
-
-  call allocLocal(time_meta, startTime, err=err, message=cmessage)
-  call allocLocal(time_meta, finshTime, err=err, message=cmessage)
-  call allocLocal(time_meta, refTime,   err=err, message=cmessage)
-  call allocLocal(time_meta, oldTime,   err=err, message=cmessage)
-
-end subroutine allocTime
-
-
-subroutine cleanUpJobActor(err) bind(C, name='cleanUpJobActor')
-  USE globalData,only:structInfo                              ! information on the data structures
-  USE globalData,only:statForc_meta                           ! child metadata for stats
-  USE globalData,only:statProg_meta                           ! child metadata for stats
-  USE globalData,only:statDiag_meta                           ! child metadata for stats
-  USE globalData,only:statFlux_meta                           ! child metadata for stats
-  USE globalData,only:statIndx_meta                           ! child metadata for stats
-  USE globalData,only:statBvar_meta                           ! child metadata for stats
-  USE globalData,only:forcChild_map                           ! index of the child data structure: stats forc
-  USE globalData,only:progChild_map                           ! index of the child data structure: stats prog
-  USE globalData,only:diagChild_map                           ! index of the child data structure: stats diag
-  USE globalData,only:fluxChild_map                           ! index of the child data structure: stats flux
-  USE globalData,only:indxChild_map                           ! index of the child data structure: stats indx
-  USE globalData,only:bvarChild_map                           ! index of the child data structure: stats bvar
-  USE globalData,only:gru_struc                               ! gru->hru mapping structure
-  USE globalData,only:index_map
-  USE globalData,only:startTime,finshTime,refTime,oldTime
-  USE var_lookup,only:childFLUX_MEAN                          ! look-up values for timestep-average model fluxes
-
-  implicit none
-  integer(c_int), intent(inout)     :: err
-  err=0
-
-  ! Deallocate Time Varaibles
-  deallocate(startTime%var);
-  deallocate(finshTime%var);
-  deallocate(refTime%var);
-  deallocate(oldTime%var);
-
-  if(allocated(averageFlux_meta)) then; deallocate(averageFlux_meta); endif
-  if(allocated(statForc_meta)) then; deallocate(statForc_meta); endif
-  if(allocated(statProg_meta)) then; deallocate(statProg_meta); endif
-  if(allocated(statDiag_meta)) then; deallocate(statDiag_meta); endif
-  if(allocated(statFlux_meta)) then; deallocate(statFlux_meta); endif
-  if(allocated(statIndx_meta)) then; deallocate(statIndx_meta); endif
-  if(allocated(statBvar_meta)) then; deallocate(statBvar_meta); endif
-  if(allocated(forcChild_map)) then; deallocate(forcChild_map); endif
-  if(allocated(progChild_map)) then; deallocate(progChild_map); endif
-  if(allocated(diagChild_map)) then; deallocate(diagChild_map); endif
-  if(allocated(fluxChild_map)) then; deallocate(fluxChild_map); endif
-  if(allocated(indxChild_map)) then; deallocate(indxChild_map); endif
-  if(allocated(bvarChild_map)) then; deallocate(bvarChild_map); endif
-  if(allocated(childFLUX_MEAN))then; deallocate(childFLUX_MEAN);endif
-  if(allocated(gru_struc))then; deallocate(gru_struc);endif
-  if(allocated(index_map))then; deallocate(index_map);endif
-
-end subroutine cleanUpJobActor
-
-end module cppwrap_job
\ No newline at end of file
diff --git a/build/source/actors/job_actor/job_actor.cpp b/build/source/actors/job_actor/job_actor.cpp
index e1ff1c2..1c0924a 100644
--- a/build/source/actors/job_actor/job_actor.cpp
+++ b/build/source/actors/job_actor/job_actor.cpp
@@ -66,16 +66,33 @@ behavior job_actor(stateful_actor<job_state>* self, int start_gru, int num_gru,
     setTimesDirsAndFiles(self->state.file_manager.c_str(), &err);
     if (err != 0) {
         aout(self) << "ERROR: Job_Actor - setTimesDirsAndFiles\n";
+        return {}; // Failure
     }
     defineGlobalData(&self->state.start_gru, &err);
     if (err != 0) {
         aout(self) << "ERROR: Job_Actor - defineGlobalData\n";
+        return {}; // Failure
     }
     readDimension(&self->state.num_gru, &self->state.num_hru, &self->state.start_gru, &err);
     if (err != 0) {
         aout(self) << "ERROR: Job_Actor - readDimension\n";
+        return {}; // Failure
+    }
+    readIcondNLayers(&self->state.num_gru, &err);
+    if (err != 0) {
+        aout(self) << "ERROR: Job_Actor - readIcondNLayers\n";
+        return {}; // Failure
+    }
+    allocateTimeStructure(&err);
+    if (err != 0) {
+        aout(self) << "ERROR: Job_Actor - allocateTimeStructure\n";
+        return {}; // Failure
     }
-    initJob(self);
+
+
+
+
+    initCsvOutputFile(self);
 
     // Spawn the file_access_actor. This will return the number of forcing files we are working with
     self->state.file_access_actor = self->spawn(file_access_actor, self->state.start_gru, self->state.num_gru, 
@@ -183,7 +200,7 @@ behavior job_actor(stateful_actor<job_state>* self, int start_gru, int num_gru,
             aout(self) << "Total Duration = " << self->state.job_timing.getDuration("total_duration").value_or(-1.0) / 60 << " Minutes\n";
             aout(self) << "Total Duration = " << (self->state.job_timing.getDuration("total_duration").value_or(-1.0) / 60) / 60 << " Hours\n\n";
 
-            cleanUpJobActor(&err);
+            deallocateJobActor(&err);
             // Tell Parent we are done
             self->send(self->state.parent, 
                     done_job_v, 
@@ -215,7 +232,7 @@ behavior job_actor(stateful_actor<job_state>* self, int start_gru, int num_gru,
     };
 }
 
-void initJob(stateful_actor<job_state>* self) {
+void initCsvOutputFile(stateful_actor<job_state>* self) {
     std::string success = "Success"; // allows us to build the string
     if (self->state.output_csv) {
         std::ofstream file;
@@ -233,24 +250,6 @@ void initJob(stateful_actor<job_state>* self) {
             "numAttemtps\n";
         file.close();
     }
-
-    int totalGRUs           = 0;
-    int totalHRUs           = 0;
-    int numHRUs             = 0;
-    int err                 = 0;
-
-    // aout(self) << "Initalizing Globals \n";
-    initGlobals(self->state.file_manager.c_str(), 
-        &totalGRUs,    
-        &totalHRUs, 
-        &self->state.num_gru, 
-        &numHRUs,
-        &self->state.start_gru, 
-        &err);
-    if (err != 0) {
-        aout(self) << "Error: initGlobals" << std::endl;
-        self->quit();
-    }
 }
 
 void initalizeGRU(stateful_actor<job_state>* self) {
diff --git a/build/source/actors/job_actor/job_actor.f90 b/build/source/actors/job_actor/job_actor.f90
index 4b8e28c..eaffeb8 100644
--- a/build/source/actors/job_actor/job_actor.f90
+++ b/build/source/actors/job_actor/job_actor.f90
@@ -1,9 +1,78 @@
 module job_actor
+    USE, intrinsic :: iso_c_binding
+    
+    
     implicit none
-
+    public::allocateTimeStructure
+    public::deallocateJobActor
 
     contains
 
+subroutine allocateTimeStructure(err) bind(C, name="allocateTimeStructure")
+    USE globalData,only:startTime,finshTime,refTime,oldTime
+    USE allocspace4chm_module,only:allocLocal
+    USE globalData,only:time_meta
+
+    implicit none
+    ! dummy variables
+    integer(c_int),intent(inout)      :: err
+    ! local variables
+    character(len=256)              :: cmessage
+
+    call allocLocal(time_meta, startTime, err=err, message=cmessage)
+    call allocLocal(time_meta, finshTime, err=err, message=cmessage)
+    call allocLocal(time_meta, refTime,   err=err, message=cmessage)
+    call allocLocal(time_meta, oldTime,   err=err, message=cmessage)
+end subroutine
+
+subroutine deallocateJobActor(err) bind(C, name="deallocateJobActor")
+    USE globalData,only:structInfo                              ! information on the data structures
+    USE globalData,only:statForc_meta                           ! child metadata for stats
+    USE globalData,only:statProg_meta                           ! child metadata for stats
+    USE globalData,only:statDiag_meta                           ! child metadata for stats
+    USE globalData,only:statFlux_meta                           ! child metadata for stats
+    USE globalData,only:statIndx_meta                           ! child metadata for stats
+    USE globalData,only:statBvar_meta                           ! child metadata for stats
+    USE globalData,only:forcChild_map                           ! index of the child data structure: stats forc
+    USE globalData,only:progChild_map                           ! index of the child data structure: stats prog
+    USE globalData,only:diagChild_map                           ! index of the child data structure: stats diag
+    USE globalData,only:fluxChild_map                           ! index of the child data structure: stats flux
+    USE globalData,only:indxChild_map                           ! index of the child data structure: stats indx
+    USE globalData,only:bvarChild_map                           ! index of the child data structure: stats bvar
+    USE globalData,only:gru_struc                               ! gru->hru mapping structure
+    USE globalData,only:averageFlux_meta
+    USE globalData,only:index_map
+    USE globalData,only:startTime,finshTime,refTime,oldTime
+    USE var_lookup,only:childFLUX_MEAN                          ! look-up values for timestep-average model fluxes
+  
+    implicit none
+    integer(c_int), intent(out)     :: err
+    err=0
+  
+    ! Deallocate Time Varaibles
+    deallocate(startTime%var);
+    deallocate(finshTime%var);
+    deallocate(refTime%var);
+    deallocate(oldTime%var);
+  
+    if(allocated(averageFlux_meta)) then; deallocate(averageFlux_meta); endif
+    if(allocated(statForc_meta)) then; deallocate(statForc_meta); endif
+    if(allocated(statProg_meta)) then; deallocate(statProg_meta); endif
+    if(allocated(statDiag_meta)) then; deallocate(statDiag_meta); endif
+    if(allocated(statFlux_meta)) then; deallocate(statFlux_meta); endif
+    if(allocated(statIndx_meta)) then; deallocate(statIndx_meta); endif
+    if(allocated(statBvar_meta)) then; deallocate(statBvar_meta); endif
+    if(allocated(forcChild_map)) then; deallocate(forcChild_map); endif
+    if(allocated(progChild_map)) then; deallocate(progChild_map); endif
+    if(allocated(diagChild_map)) then; deallocate(diagChild_map); endif
+    if(allocated(fluxChild_map)) then; deallocate(fluxChild_map); endif
+    if(allocated(indxChild_map)) then; deallocate(indxChild_map); endif
+    if(allocated(bvarChild_map)) then; deallocate(bvarChild_map); endif
+    if(allocated(childFLUX_MEAN))then; deallocate(childFLUX_MEAN);endif
+    if(allocated(gru_struc))then; deallocate(gru_struc);endif
+    if(allocated(index_map))then; deallocate(index_map);endif
+end subroutine
+
 
 
 end module
\ No newline at end of file
diff --git a/build/source/netcdf/read_icondActors.f90 b/build/source/netcdf/read_icondActors.f90
index c0560a0..9dec43a 100644
--- a/build/source/netcdf/read_icondActors.f90
+++ b/build/source/netcdf/read_icondActors.f90
@@ -19,6 +19,7 @@
 ! along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 module read_icond4chm_module
+USE, intrinsic :: iso_c_binding
 USE nrtype
 USE netcdf
 USE globalData,only: ixHRUfile_min,ixHRUfile_max
@@ -36,104 +37,120 @@ contains
  ! ************************************************************************************************
  ! public subroutine read_icond_nlayers: read model initial conditions file for number of snow/soil layers
  ! ************************************************************************************************
- subroutine read_icond_nlayers(iconFile,nGRU,indx_meta,err,message)
- ! --------------------------------------------------------------------------------------------------------
- ! modules
- USE nrtype
- USE var_lookup,only:iLookIndex                        ! variable lookup structure
- USE globalData,only:gru_struc                         ! gru-hru mapping structures
- USE netcdf_util_module,only:nc_file_close             ! close netcdf file
- USE netcdf_util_module,only:nc_file_open              ! close netcdf file
- USE netcdf_util_module,only:netcdf_err                ! netcdf error handling
- USE data_types,only:gru_hru_intVec                    ! actual data
- USE data_types,only:var_info                          ! metadata
- implicit none
-
- ! --------------------------------------------------------------------------------------------------------
- ! variable declarations
- ! dummies
- character(*)        ,intent(in)     :: iconFile       ! name of input (restart) file
- integer(i4b)        ,intent(in)     :: nGRU           ! total # of GRUs in run domain
- type(var_info)      ,intent(in)     :: indx_meta(:)   ! metadata
- integer(i4b)        ,intent(out)    :: err            ! error code
- character(*)        ,intent(out)    :: message        ! returned error message
-
- ! locals
- integer(i4b)             :: ncID                       ! netcdf file id
- integer(i4b)             :: dimID                      ! netcdf file dimension id
- integer(i4b)             :: fileHRU                    ! number of HRUs in netcdf file
- integer(i4b)             :: snowID, soilID             ! netcdf variable ids
- integer(i4b)             :: iGRU, iHRU                 ! loop indexes
- integer(i4b)             :: iHRU_global                ! index of HRU in the netcdf file
- integer(i4b),allocatable :: snowData(:)                ! number of snow layers in all HRUs
- integer(i4b),allocatable :: soilData(:)                ! number of soil layers in all HRUs
- character(len=256)       :: cmessage                   ! downstream error message
-
- ! --------------------------------------------------------------------------------------------------------
- ! initialize error message
- err=0
- message = 'read_icond_nlayers/'
-
- ! open netcdf file
- call nc_file_open(iconFile,nf90_nowrite,ncid,err,cmessage);
- if (err/=0) then; message=trim(message)//trim(cmessage); return; end if
- 
-
- ! get number of HRUs in file (the GRU variable(s), if present, are processed at the end)
- err = nf90_inq_dimid(ncID,"hru",dimId);               if(err/=nf90_noerr)then; message=trim(message)//'problem finding hru dimension/'//trim(nf90_strerror(err)); return; end if
- err = nf90_inquire_dimension(ncID,dimId,len=fileHRU); if(err/=nf90_noerr)then; message=trim(message)//'problem reading hru dimension/'//trim(nf90_strerror(err)); return; end if
-
- ! allocate storage for reading from file (allocate entire file size, even when doing subdomain run)
- allocate(snowData(fileHRU))
- allocate(soilData(fileHRU))
- snowData = 0
- soilData = 0
-
- ! get netcdf ids for the variables holding number of snow and soil layers in each hru
- err = nf90_inq_varid(ncid,trim(indx_meta(iLookIndex%nSnow)%varName),snowid); call netcdf_err(err,message)
- err = nf90_inq_varid(ncid,trim(indx_meta(iLookIndex%nSoil)%varName),soilid); call netcdf_err(err,message)
-
- ! get nSnow and nSoil data (reads entire state file)
- err = nf90_get_var(ncid,snowid,snowData); call netcdf_err(err,message)
- err = nf90_get_var(ncid,soilid,soilData); call netcdf_err(err,message)
-
- ixHRUfile_min=huge(1)
- ixHRUfile_max=0
- ! find the min and max hru indices in the state file
- do iGRU = 1,nGRU
-  do iHRU = 1,gru_struc(iGRU)%hruCount
-   if(gru_struc(iGRU)%hruInfo(iHRU)%hru_nc < ixHRUfile_min) ixHRUfile_min = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
-   if(gru_struc(iGRU)%hruInfo(iHRU)%hru_nc > ixHRUfile_max) ixHRUfile_max = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
-  end do
- end do
- 
-
- ! loop over grus in current run to update snow/soil layer information
- do iGRU = 1,nGRU
-  do iHRU = 1,gru_struc(iGRU)%hruCount
-   iHRU_global = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
-
-   ! single HRU (Note: 'restartFileType' is hardwired above to multiHRU)
-   if(restartFileType==singleHRU) then
-    gru_struc(iGRU)%hruInfo(iHRU)%nSnow = snowData(1)
-    gru_struc(iGRU)%hruInfo(iHRU)%nSoil = soilData(1)
-
-   ! multi HRU
-   else
-    gru_struc(iGRU)%hruInfo(iHRU)%nSnow = snowData(iHRU_global)
-    gru_struc(iGRU)%hruInfo(iHRU)%nSoil = soilData(iHRU_global)
-   endif
-
-  end do
- end do
-
-
- ! close file
- call nc_file_close(ncid,err,cmessage)
- if(err/=0)then;message=trim(message)//trim(cmessage);return;end if
-
- ! cleanup
- deallocate(snowData,soilData)
+subroutine read_icond_nlayers(nGRU,err) bind(C, name="readIcondNLayers")
+   ! --------------------------------------------------------------------------------------------------------
+   ! modules
+    USE nrtype
+    USE var_lookup,only:iLookIndex                        ! variable lookup structure
+    USE globalData,only:gru_struc                         ! gru-hru mapping structures
+    USE netcdf_util_module,only:nc_file_close             ! close netcdf file
+    USE netcdf_util_module,only:nc_file_open              ! close netcdf file
+    USE netcdf_util_module,only:netcdf_err                ! netcdf error handling
+    USE data_types,only:gru_hru_intVec                    ! actual data
+    USE data_types,only:var_info                          ! metadata
+
+    USE globalData,only:indx_meta
+    
+    ! file paths
+    USE summaActors_FileManager,only:STATE_PATH                        ! optional path to state/init. condition files (defaults to SETTINGS_PATH)
+    USE summaActors_FileManager,only:SETTINGS_PATH                     ! define path to settings files (e.g., parameters, soil and veg. tables)
+    USE summaActors_FileManager,only:MODEL_INITCOND                    ! name of model initial conditions file
+
+    implicit none
+
+    ! --------------------------------------------------------------------------------------------------------
+    ! variable declarations
+    ! dummies
+    integer(i4b)        ,intent(in)     :: nGRU           ! total # of GRUs in run domain
+    integer(i4b)        ,intent(out)    :: err            ! error code
+
+    ! locals
+    integer(i4b)                        :: ncID                       ! netcdf file id
+    integer(i4b)                        :: dimID                      ! netcdf file dimension id
+    integer(i4b)                        :: fileHRU                    ! number of HRUs in netcdf file
+    integer(i4b)                        :: snowID, soilID             ! netcdf variable ids
+    integer(i4b)                        :: iGRU, iHRU                 ! loop indexes
+    integer(i4b)                        :: iHRU_global                ! index of HRU in the netcdf file
+    integer(i4b),allocatable            :: snowData(:)                ! number of snow layers in all HRUs
+    integer(i4b),allocatable            :: soilData(:)                ! number of soil layers in all HRUs
+    
+    character(len=256)                  :: iconFile          ! restart file name
+
+    
+    character(len=256)                  :: message        ! returned error message
+    character(len=256)                  :: cmessage                   ! downstream error message
+
+    ! --------------------------------------------------------------------------------------------------------
+    ! initialize error message
+    err=0
+    message = 'read_icond_nlayers/'
+
+    if(STATE_PATH == '') then
+        iconFile = trim(SETTINGS_PATH)//trim(MODEL_INITCOND)
+    else
+        iconFile = trim(STATE_PATH)//trim(MODEL_INITCOND)
+    endif
+
+    ! open netcdf file
+    call nc_file_open(iconFile,nf90_nowrite,ncid,err,cmessage);
+    if (err/=0) then; message=trim(message)//trim(cmessage); return; end if
+    
+
+    ! get number of HRUs in file (the GRU variable(s), if present, are processed at the end)
+    err = nf90_inq_dimid(ncID,"hru",dimId);               if(err/=nf90_noerr)then; message=trim(message)//'problem finding hru dimension/'//trim(nf90_strerror(err)); return; end if
+    err = nf90_inquire_dimension(ncID,dimId,len=fileHRU); if(err/=nf90_noerr)then; message=trim(message)//'problem reading hru dimension/'//trim(nf90_strerror(err)); return; end if
+
+    ! allocate storage for reading from file (allocate entire file size, even when doing subdomain run)
+    allocate(snowData(fileHRU))
+    allocate(soilData(fileHRU))
+    snowData = 0
+    soilData = 0
+
+    ! get netcdf ids for the variables holding number of snow and soil layers in each hru
+    err = nf90_inq_varid(ncid,trim(indx_meta(iLookIndex%nSnow)%varName),snowid); call netcdf_err(err,message)
+    err = nf90_inq_varid(ncid,trim(indx_meta(iLookIndex%nSoil)%varName),soilid); call netcdf_err(err,message)
+
+    ! get nSnow and nSoil data (reads entire state file)
+    err = nf90_get_var(ncid,snowid,snowData); call netcdf_err(err,message)
+    err = nf90_get_var(ncid,soilid,soilData); call netcdf_err(err,message)
+
+    ixHRUfile_min=huge(1)
+    ixHRUfile_max=0
+    ! find the min and max hru indices in the state file
+    do iGRU = 1,nGRU
+    do iHRU = 1,gru_struc(iGRU)%hruCount
+    if(gru_struc(iGRU)%hruInfo(iHRU)%hru_nc < ixHRUfile_min) ixHRUfile_min = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
+    if(gru_struc(iGRU)%hruInfo(iHRU)%hru_nc > ixHRUfile_max) ixHRUfile_max = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
+    end do
+    end do
+    
+
+    ! loop over grus in current run to update snow/soil layer information
+    do iGRU = 1,nGRU
+    do iHRU = 1,gru_struc(iGRU)%hruCount
+    iHRU_global = gru_struc(iGRU)%hruInfo(iHRU)%hru_nc
+
+    ! single HRU (Note: 'restartFileType' is hardwired above to multiHRU)
+    if(restartFileType==singleHRU) then
+        gru_struc(iGRU)%hruInfo(iHRU)%nSnow = snowData(1)
+        gru_struc(iGRU)%hruInfo(iHRU)%nSoil = soilData(1)
+
+    ! multi HRU
+    else
+        gru_struc(iGRU)%hruInfo(iHRU)%nSnow = snowData(iHRU_global)
+        gru_struc(iGRU)%hruInfo(iHRU)%nSoil = soilData(iHRU_global)
+    endif
+
+    end do
+    end do
+
+
+    ! close file
+    call nc_file_close(ncid,err,cmessage)
+    if(err/=0)then;message=trim(message)//trim(cmessage);return;end if
+
+    ! cleanup
+    deallocate(snowData,soilData)
 
  end subroutine read_icond_nlayers
 
-- 
GitLab