From d7f0cffee93adc6147a157b1b5bf8d980e6fd762 Mon Sep 17 00:00:00 2001 From: ymao Date: Mon, 13 Feb 2017 17:36:46 -0800 Subject: [PATCH 1/7] Deleted unused file handles (for text files) for nc input files --- vic/drivers/shared_image/include/vic_driver_shared_image.h | 5 ----- vic/drivers/shared_image/src/initialize_files.c | 6 ------ 2 files changed, 11 deletions(-) diff --git a/vic/drivers/shared_image/include/vic_driver_shared_image.h b/vic/drivers/shared_image/include/vic_driver_shared_image.h index 1e7513a12..ace7c7054 100644 --- a/vic/drivers/shared_image/include/vic_driver_shared_image.h +++ b/vic/drivers/shared_image/include/vic_driver_shared_image.h @@ -160,13 +160,8 @@ typedef struct { * @brief file structures *****************************************************************************/ typedef struct { - FILE *forcing[MAX_FORCE_FILES]; /**< forcing data files */ FILE *globalparam; /**< global parameters file */ FILE *constants; /**< model constants parameter file */ - FILE *domain; /**< domain file */ - FILE *init_state; /**< initial model state file */ - FILE *paramfile; /**< parameter file */ - FILE *statefile; /**< output model state file */ FILE *logfile; /**< log file */ } filep_struct; diff --git a/vic/drivers/shared_image/src/initialize_files.c b/vic/drivers/shared_image/src/initialize_files.c index 22b97f651..a4191b140 100644 --- a/vic/drivers/shared_image/src/initialize_files.c +++ b/vic/drivers/shared_image/src/initialize_files.c @@ -61,11 +61,5 @@ initialize_fileps() filep.globalparam = NULL; filep.constants = NULL; - filep.init_state = NULL; - filep.paramfile = NULL; - filep.statefile = NULL; filep.logfile = NULL; - for (i = 0; i < 2; i++) { - filep.forcing[i] = NULL; - } } From 8a9a4cf22ab9fe9d880859988ab8a059201ff073 Mon Sep 17 00:00:00 2001 From: ymao Date: Thu, 16 Feb 2017 12:28:02 -0800 Subject: [PATCH 2/7] Fixed some remaining bugs related to reseting forceskip to zero that were missing in PR 671 --- vic/drivers/image/src/vic_force.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vic/drivers/image/src/vic_force.c b/vic/drivers/image/src/vic_force.c index 11caca455..70fc35995 100644 --- a/vic/drivers/image/src/vic_force.c +++ b/vic/drivers/image/src/vic_force.c @@ -73,7 +73,7 @@ vic_force(void) // global_param.forceoffset[0] resets every year since the met file restarts // every year // global_param.forceskip[0] should also reset to 0 after the first year - if (current > 1 && (dmy[current].year != dmy[current - 1].year)) { + if (current > 0 && (dmy[current].year != dmy[current - 1].year)) { global_param.forceoffset[0] = 0; global_param.forceskip[0] = 0; } @@ -264,8 +264,10 @@ vic_force(void) // global_param.forceoffset[1] resets every year since the met file restarts // every year - if (current > 1 && (dmy[current].year != dmy[current - 1].year)) { + // global_param.forceskip[1] should also reset to 0 after the first year + if (current > 0 && (dmy[current].year != dmy[current - 1].year)) { global_param.forceoffset[1] = 0; + global_param.forceskip[0] = 0; } // only the time slice changes for the met file reads. The rest is constant From fbbf0765a5918ddf60986536f8eebc9edfe05569 Mon Sep 17 00:00:00 2001 From: ymao Date: Thu, 16 Feb 2017 12:46:40 -0800 Subject: [PATCH 3/7] Fixed a typo --- vic/drivers/image/src/vic_force.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vic/drivers/image/src/vic_force.c b/vic/drivers/image/src/vic_force.c index 70fc35995..ce88a3e37 100644 --- a/vic/drivers/image/src/vic_force.c +++ b/vic/drivers/image/src/vic_force.c @@ -267,7 +267,7 @@ vic_force(void) // global_param.forceskip[1] should also reset to 0 after the first year if (current > 0 && (dmy[current].year != dmy[current - 1].year)) { global_param.forceoffset[1] = 0; - global_param.forceskip[0] = 0; + global_param.forceskip[1] = 0; } // only the time slice changes for the met file reads. The rest is constant From 0c3609fe796eb8afd89347ec7f77ba05651a9f1f Mon Sep 17 00:00:00 2001 From: ymao Date: Thu, 16 Feb 2017 15:00:50 -0800 Subject: [PATCH 4/7] First commit for speeding up image driver initialization - by not opening and closing files again and again - Runs successfully without MPI specified - Error with MPI --- vic/drivers/image/src/get_global_param.c | 21 ++++--- vic/drivers/image/src/vic_force.c | 33 ++++++++--- vic/drivers/image/src/vic_image_init.c | 4 ++ vic/drivers/image/src/vic_image_start.c | 13 ++++- .../include/vic_driver_shared_image.h | 34 ++++++----- vic/drivers/shared_image/include/vic_mpi.h | 14 ++++- .../shared_image/src/check_domain_info.c | 4 +- .../shared_image/src/get_global_domain.c | 50 ++++++++-------- .../shared_image/src/get_nc_dimension.c | 21 ++----- vic/drivers/shared_image/src/get_nc_field.c | 57 +++++------------- .../shared_image/src/get_nc_var_attr.c | 22 +++---- .../shared_image/src/get_nc_var_type.c | 21 ++----- .../shared_image/src/get_nc_varndimensions.c | 19 ++---- .../shared_image/src/initialize_files.c | 6 +- vic/drivers/shared_image/src/nc_utils.c | 58 +++++++++++++++++++ .../shared_image/src/vic_image_timing.c | 2 +- .../shared_image/src/vic_mpi_support.c | 12 ++-- vic/drivers/shared_image/src/vic_restore.c | 35 +++++------ 18 files changed, 233 insertions(+), 193 deletions(-) create mode 100644 vic/drivers/shared_image/src/nc_utils.c diff --git a/vic/drivers/image/src/get_global_param.c b/vic/drivers/image/src/get_global_param.c index 3e8dc38f0..ec396542d 100644 --- a/vic/drivers/image/src/get_global_param.c +++ b/vic/drivers/image/src/get_global_param.c @@ -296,7 +296,7 @@ get_global_param(FILE *gp) } else { options.INIT_STATE = true; - strcpy(filenames.init_state, flgstr); + strcpy(filenames.init_state.nc_file, flgstr); } } else if (strcasecmp("STATENAME", optstr) == 0) { @@ -374,13 +374,13 @@ get_global_param(FILE *gp) sscanf(cmdstr, "%*s %s", filenames.constants); } else if (strcasecmp("DOMAIN", optstr) == 0) { - sscanf(cmdstr, "%*s %s", filenames.domain); + sscanf(cmdstr, "%*s %s", filenames.domain.nc_file); } else if (strcasecmp("DOMAIN_TYPE", optstr) == 0) { get_domain_type(cmdstr); } else if (strcasecmp("PARAMETERS", optstr) == 0) { - sscanf(cmdstr, "%*s %s", filenames.params); + sscanf(cmdstr, "%*s %s", filenames.params.nc_file); } else if (strcasecmp("ARNO_PARAMS", optstr) == 0) { sscanf(cmdstr, "%*s %s", flgstr); @@ -787,12 +787,15 @@ get_global_param(FILE *gp) } // Get information from the forcing file(s) - sprintf(filenames.forcing[0], "%s%4d.nc", filenames.f_path_pfx[0], + // Open first-year forcing files and get info + sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], global_param.startyear); + filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); get_forcing_file_info(¶m_set, 0); if (param_set.N_TYPES[1] != MISSING) { - sprintf(filenames.forcing[1], "%s%4d.nc", filenames.f_path_pfx[1], + sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], global_param.startyear); + filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); get_forcing_file_info(¶m_set, 1); } @@ -829,7 +832,7 @@ get_global_param(FILE *gp) } // Validate parameter file information - if (strcmp(filenames.params, "MISSING") == 0) { + if (strcmp(filenames.params.nc_file, "MISSING") == 0) { log_err("A parameters file has not been defined. Make sure that the " "global file defines the parameters parameter file on the line " "that begins with \"PARAMETERS\"."); @@ -868,7 +871,7 @@ get_global_param(FILE *gp) // Validate the input state file information if (options.INIT_STATE) { - if (strcmp(filenames.init_state, "MISSING") == 0) { + if (strcmp(filenames.init_state.nc_file, "MISSING") == 0) { log_err("\"INIT_STATE\" was specified, but no input state file " "has been defined. Make sure that the global file " "defines the inputstate file on the line that begins " @@ -919,11 +922,11 @@ get_global_param(FILE *gp) global_param.statesec); } if (options.INIT_STATE && options.SAVE_STATE && - (strcmp(filenames.init_state, flgstr2) == 0)) { + (strcmp(filenames.init_state.nc_file, flgstr2) == 0)) { log_err("The save state file (%s) has the same name as the " "initialize state file (%s). The initialize state file " "will be destroyed when the save state file is opened.", - filenames.statefile, filenames.init_state); + filenames.statefile, filenames.init_state.nc_file); } // Validate soil parameter/simulation mode combinations diff --git a/vic/drivers/image/src/vic_force.c b/vic/drivers/image/src/vic_force.c index ce88a3e37..d8129a19c 100644 --- a/vic/drivers/image/src/vic_force.c +++ b/vic/drivers/image/src/vic_force.c @@ -66,16 +66,20 @@ vic_force(void) dvar = malloc(local_domain.ncells_active * sizeof(*dvar)); check_alloc_status(dvar, "Memory allocation error."); - // for now forcing file is determined by the year - sprintf(filenames.forcing[0], "%s%4d.nc", filenames.f_path_pfx[0], - dmy[current].year); - // global_param.forceoffset[0] resets every year since the met file restarts // every year // global_param.forceskip[0] should also reset to 0 after the first year if (current > 0 && (dmy[current].year != dmy[current - 1].year)) { global_param.forceoffset[0] = 0; global_param.forceskip[0] = 0; + // close the forcing file for the previous year and open the forcing + // file for the current new year + // (forcing file for the first year should already be open in + // get_global_param) + close_nc(filenames.forcing[0]); + sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], + dmy[current].year); + filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); } // only the time slice changes for the met file reads. The rest is constant @@ -228,6 +232,11 @@ vic_force(void) } } + // Close forcing file if it is the last time step + if (current == global_param.nrecs) { + close_nc(filenames.forcing[0]); + } + // Update the offset counter global_param.forceoffset[0] += NF; @@ -258,9 +267,6 @@ vic_force(void) if (options.LAI_SRC == FROM_VEGHIST || options.FCAN_SRC == FROM_VEGHIST || options.ALB_SRC == FROM_VEGHIST) { - // for now forcing file is determined by the year - sprintf(filenames.forcing[1], "%s%4d.nc", filenames.f_path_pfx[1], - dmy[current].year); // global_param.forceoffset[1] resets every year since the met file restarts // every year @@ -268,6 +274,14 @@ vic_force(void) if (current > 0 && (dmy[current].year != dmy[current - 1].year)) { global_param.forceoffset[1] = 0; global_param.forceskip[1] = 0; + // close the forcing file for the previous year and open the forcing + // file for the current new year + // (forcing file for the first year should already be open in + // get_global_param) + close_nc(filenames.forcing[1]); + sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], + dmy[current].year); + filenames.forcing[1].nc_id = open_nc(filenames.forcing[1].nc_file); } // only the time slice changes for the met file reads. The rest is constant @@ -335,6 +349,11 @@ vic_force(void) } } + // Close forcing file if it is the last time step + if (current == global_param.nrecs) { + close_nc(filenames.forcing[1]); + } + // Update the offset counter global_param.forceoffset[1] += NF; } diff --git a/vic/drivers/image/src/vic_image_init.c b/vic/drivers/image/src/vic_image_init.c index cbb29eb93..1e47d9603 100644 --- a/vic/drivers/image/src/vic_image_init.c +++ b/vic/drivers/image/src/vic_image_init.c @@ -34,10 +34,14 @@ vic_image_init(void) { extern dmy_struct *dmy; extern global_param_struct global_param; + extern filenames_struct filenames; // make_dmy() initialize_time(); dmy = make_dmy(&global_param); vic_init(); + + // close parameter netCDF file + close_nc(filenames.params); } diff --git a/vic/drivers/image/src/vic_image_start.c b/vic/drivers/image/src/vic_image_start.c index 5a2ee4f12..17fee6123 100644 --- a/vic/drivers/image/src/vic_image_start.c +++ b/vic/drivers/image/src/vic_image_start.c @@ -32,9 +32,9 @@ void vic_image_start(void) { - extern filep_struct filep; - extern filenames_struct filenames; - extern int mpi_rank; + extern filep_struct filep; + extern filenames_struct filenames; + extern int mpi_rank; // Initialize structures initialize_global_structures(); @@ -45,6 +45,13 @@ vic_image_start(void) get_global_param(filep.globalparam); } + // Open domain and parameter netCDF files + filenames.domain.nc_id = open_nc(filenames.domain.nc_file); + filenames.params.nc_id = open_nc(filenames.params.nc_file); + // initialize image mode structures and settings vic_start(); + + // Close domain file + close_nc(filenames.domain); } diff --git a/vic/drivers/shared_image/include/vic_driver_shared_image.h b/vic/drivers/shared_image/include/vic_driver_shared_image.h index ace7c7054..dfae537f3 100644 --- a/vic/drivers/shared_image/include/vic_driver_shared_image.h +++ b/vic/drivers/shared_image/include/vic_driver_shared_image.h @@ -169,42 +169,46 @@ typedef struct { * @brief This structure stores input and output filenames. *****************************************************************************/ typedef struct { - char forcing[MAX_FORCE_FILES][MAXSTRING]; /**< atmospheric forcing data file names */ + nameid_struct forcing[MAX_FORCE_FILES]; /**< atmospheric forcing data file names */ char f_path_pfx[MAX_FORCE_FILES][MAXSTRING]; /**< path and prefix for atmospheric forcing data file names */ char global[MAXSTRING]; /**< global control file name */ - char domain[MAXSTRING]; /**< domain file name */ + nameid_struct domain; /**< domain file name and nc_id*/ char constants[MAXSTRING]; /**< model constants file name */ - char params[MAXSTRING]; /**< model parameters file name */ - char init_state[MAXSTRING]; /**< initial model state file name */ + nameid_struct params; /**< model parameters file name and nc_id */ + nameid_struct init_state; /**< initial model state file name and nc_id */ char result_dir[MAXSTRING]; /**< directory where results will be written */ char statefile[MAXSTRING]; /**< name of file in which to store model state */ char log_path[MAXSTRING]; /**< Location to write log file to */ } filenames_struct; -void add_nveg_to_global_domain(char *nc_name, domain_struct *global_domain); +void add_nveg_to_global_domain(nameid_struct nc_nameid, + domain_struct *global_domain); void alloc_force(force_data_struct *force); void alloc_veg_hist(veg_hist_struct *veg_hist); double air_density(double t, double p); double average(double *ar, size_t n); void check_init_state_file(void); -void compare_ncdomain_with_global_domain(char *ncfile); +void compare_ncdomain_with_global_domain(nameid_struct nc_nameid); void free_force(force_data_struct *force); void free_veg_hist(veg_hist_struct *veg_hist); void get_domain_type(char *cmdstr); -size_t get_global_domain(char *domain_nc_name, char *param_nc_name, +size_t get_global_domain(nameid_struct domain_nc_nameid, + nameid_struct param_nc_nameid, domain_struct *global_domain); void copy_domain_info(domain_struct *domain_from, domain_struct *domain_to); -void get_nc_latlon(char *nc_name, domain_struct *nc_domain); -size_t get_nc_dimension(char *nc_name, char *dim_name); -void get_nc_var_attr(char *nc_name, char *var_name, char *attr_name, +int open_nc(char *nc_name); +int close_nc(nameid_struct nc_nameid); +void get_nc_latlon(nameid_struct nc_nameid, domain_struct *nc_domain); +size_t get_nc_dimension(nameid_struct nc_nameid, char *dim_name); +void get_nc_var_attr(nameid_struct nc_nameid, char *var_name, char *attr_name, char **attr); -int get_nc_var_type(char *nc_name, char *var_name); -int get_nc_varndimensions(char *nc_name, char *var_name); -int get_nc_field_double(char *nc_name, char *var_name, size_t *start, +int get_nc_var_type(nameid_struct nc_nameid, char *var_name); +int get_nc_varndimensions(nameid_struct nc_nameid, char *var_name); +int get_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, double *var); -int get_nc_field_float(char *nc_name, char *var_name, size_t *start, +int get_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, float *var); -int get_nc_field_int(char *nc_name, char *var_name, size_t *start, +int get_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, int *var); int get_nc_dtype(unsigned short int dtype); int get_nc_mode(unsigned short int format); diff --git a/vic/drivers/shared_image/include/vic_mpi.h b/vic/drivers/shared_image/include/vic_mpi.h index c72d5131e..13c25a3dc 100644 --- a/vic/drivers/shared_image/include/vic_mpi.h +++ b/vic/drivers/shared_image/include/vic_mpi.h @@ -32,6 +32,14 @@ #define VIC_MPI_ROOT 0 +/****************************************************************************** + * * @brief This structure stores netCDF file name and corresponding nc_id + * *****************************************************************************/ +typedef struct { + char nc_file[MAXSTRING]; + int nc_id; +} nameid_struct; + void create_MPI_filenames_struct_type(MPI_Datatype *mpi_type); void create_MPI_global_struct_type(MPI_Datatype *mpi_type); void create_MPI_location_struct_type(MPI_Datatype *mpi_type); @@ -48,11 +56,11 @@ void gather_put_nc_field_short(int nc_id, int var_id, short int fillval, size_t *start, size_t *count, short int *var); void gather_put_nc_field_schar(int nc_id, int var_id, char fillval, size_t *start, size_t *count, char *var); -void get_scatter_nc_field_double(char *nc_name, char *var_name, size_t *start, +void get_scatter_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, double *var); -void get_scatter_nc_field_float(char *nc_name, char *var_name, size_t *start, +void get_scatter_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, float *var); -void get_scatter_nc_field_int(char *nc_name, char *var_name, size_t *start, +void get_scatter_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, int *var); void initialize_mpi(void); void map(size_t size, size_t n, size_t *from_map, size_t *to_map, void *from, diff --git a/vic/drivers/shared_image/src/check_domain_info.c b/vic/drivers/shared_image/src/check_domain_info.c index d67e9bc90..e0efdf0be 100644 --- a/vic/drivers/shared_image/src/check_domain_info.c +++ b/vic/drivers/shared_image/src/check_domain_info.c @@ -32,7 +32,7 @@ the global domain. *****************************************************************************/ void -compare_ncdomain_with_global_domain(char *ncfile) +compare_ncdomain_with_global_domain(nameid_struct nc_nameid) { extern domain_struct global_domain; @@ -47,7 +47,7 @@ compare_ncdomain_with_global_domain(char *ncfile) sizeof(*(ncfile_domain.locations))); check_alloc_status(ncfile_domain.locations, "Memory allocation error."); copy_domain_info(&global_domain, &ncfile_domain); - get_nc_latlon(ncfile, &ncfile_domain); + get_nc_latlon(nc_nameid, &ncfile_domain); // using the ncfile_domain, we can compare the values to the global domain. diff --git a/vic/drivers/shared_image/src/get_global_domain.c b/vic/drivers/shared_image/src/get_global_domain.c index 07e6a243c..cb9441b89 100644 --- a/vic/drivers/shared_image/src/get_global_domain.c +++ b/vic/drivers/shared_image/src/get_global_domain.c @@ -30,8 +30,8 @@ * @brief Get global domain information. *****************************************************************************/ size_t -get_global_domain(char *domain_nc_name, - char *param_nc_name, +get_global_domain(nameid_struct domain_nc_nameid, + nameid_struct param_nc_nameid, domain_struct *global_domain) { int *run = NULL; @@ -43,9 +43,9 @@ get_global_domain(char *domain_nc_name, size_t d2count[2]; size_t d2start[2]; - global_domain->n_nx = get_nc_dimension(domain_nc_name, + global_domain->n_nx = get_nc_dimension(domain_nc_nameid, global_domain->info.x_dim); - global_domain->n_ny = get_nc_dimension(domain_nc_name, + global_domain->n_ny = get_nc_dimension(domain_nc_nameid, global_domain->info.y_dim); d2start[0] = 0; @@ -64,20 +64,20 @@ get_global_domain(char *domain_nc_name, // Get mask variable from the domain file // (check whether mask variable is int type) - typeid = get_nc_var_type(domain_nc_name, global_domain->info.mask_var); + typeid = get_nc_var_type(domain_nc_nameid, global_domain->info.mask_var); if (typeid != NC_INT) { log_err("Mask variable in the domain file must be integer type."); } - get_nc_field_int(domain_nc_name, global_domain->info.mask_var, d2start, d2count, + get_nc_field_int(domain_nc_nameid, global_domain->info.mask_var, d2start, d2count, mask); // Get run_cell variable from the parameter file // (check whether run_cell variable is int type) - typeid = get_nc_var_type(param_nc_name, "run_cell"); + typeid = get_nc_var_type(param_nc_nameid, "run_cell"); if (typeid != NC_INT) { log_err("Run_cell variable in the parameter file must be integer type."); } - get_nc_field_int(param_nc_name, "run_cell", d2start, d2count, + get_nc_field_int(param_nc_nameid, "run_cell", d2start, d2count, run); // Check whether cells with run_cell == 1 are all within the mask domain @@ -124,7 +124,7 @@ get_global_domain(char *domain_nc_name, // get area // TBD: read var id from file - get_nc_field_double(domain_nc_name, global_domain->info.area_var, + get_nc_field_double(domain_nc_nameid, global_domain->info.area_var, d2start, d2count, var); for (i = 0; i < global_domain->ncells_total; i++) { global_domain->locations[i].area = var[i]; @@ -132,18 +132,18 @@ get_global_domain(char *domain_nc_name, // get fraction // TBD: read var id from file - get_nc_field_double(domain_nc_name, global_domain->info.frac_var, + get_nc_field_double(domain_nc_nameid, global_domain->info.frac_var, d2start, d2count, var); for (i = 0; i < global_domain->ncells_total; i++) { global_domain->locations[i].frac = var[i]; } // get lat and lon coordinates - get_nc_latlon(domain_nc_name, global_domain); + get_nc_latlon(domain_nc_nameid, global_domain); // check whether lat and lon coordinates in the parameter file match those // in the domain file - compare_ncdomain_with_global_domain(param_nc_name); + compare_ncdomain_with_global_domain(param_nc_nameid); // free memory free(var); @@ -158,7 +158,7 @@ get_global_domain(char *domain_nc_name, store in nc_domain structure *****************************************************************************/ void -get_nc_latlon(char *nc_name, +get_nc_latlon(nameid_struct nc_nameid, domain_struct *nc_domain) { double *var = NULL; @@ -172,19 +172,19 @@ get_nc_latlon(char *nc_name, size_t d1start[1]; - nc_domain->n_nx = get_nc_dimension(nc_name, + nc_domain->n_nx = get_nc_dimension(nc_nameid, nc_domain->info.x_dim); - nc_domain->n_ny = get_nc_dimension(nc_name, + nc_domain->n_ny = get_nc_dimension(nc_nameid, nc_domain->info.y_dim); // Get number of lat/lon dimensions. - nc_domain->info.n_coord_dims = get_nc_varndimensions(nc_name, + nc_domain->info.n_coord_dims = get_nc_varndimensions(nc_nameid, nc_domain->info.lon_var); if (nc_domain->info.n_coord_dims != - (size_t) get_nc_varndimensions(nc_name, nc_domain->info.lat_var)) { + (size_t) get_nc_varndimensions(nc_nameid, nc_domain->info.lat_var)) { log_err("Un even number of dimensions for %s and %s in: %s", nc_domain->info.lon_var, nc_domain->info.lat_var, - nc_name); + nc_nameid.nc_file); } if (nc_domain->info.n_coord_dims == 1) { @@ -199,7 +199,7 @@ get_nc_latlon(char *nc_name, d1count[0] = nc_domain->n_nx; // get longitude for unmasked grid - get_nc_field_double(nc_name, nc_domain->info.lon_var, + get_nc_field_double(nc_nameid, nc_domain->info.lon_var, d1start, d1count, var_lon); for (j = 0; j < nc_domain->n_ny; j++) { for (i = 0; i < nc_domain->n_nx; i++) { @@ -217,7 +217,7 @@ get_nc_latlon(char *nc_name, d1count[0] = nc_domain->n_ny; // get latitude for unmasked grid - get_nc_field_double(nc_name, nc_domain->info.lat_var, + get_nc_field_double(nc_nameid, nc_domain->info.lat_var, d1start, d1count, var_lat); for (i = 0; i < nc_domain->n_ny; i++) { for (j = 0; j < nc_domain->n_nx; j++) { @@ -242,7 +242,7 @@ get_nc_latlon(char *nc_name, d2count[1] = nc_domain->n_nx; // get longitude for unmasked grid - get_nc_field_double(nc_name, nc_domain->info.lon_var, + get_nc_field_double(nc_nameid, nc_domain->info.lon_var, d2start, d2count, var); for (i = 0; i < nc_domain->ncells_total; i++) { // rescale to [-180., 180]. Note that the if statement is not strictly @@ -254,7 +254,7 @@ get_nc_latlon(char *nc_name, } // get latitude for unmasked grid - get_nc_field_double(nc_name, nc_domain->info.lat_var, + get_nc_field_double(nc_nameid, nc_domain->info.lat_var, d2start, d2count, var); for (i = 0; i < nc_domain->ncells_total; i++) { nc_domain->locations[i].latitude = var[i]; @@ -265,7 +265,7 @@ get_nc_latlon(char *nc_name, else { log_err("Number of dimensions for %s and %s should be 1 or 2 in: %s", nc_domain->info.lon_var, nc_domain->info.lat_var, - nc_name); + nc_nameid.nc_file); } } @@ -331,7 +331,7 @@ initialize_location(location_struct *location) * @brief Read the number of vegetation type per grid cell from file *****************************************************************************/ void -add_nveg_to_global_domain(char *nc_name, +add_nveg_to_global_domain(nameid_struct nc_nameid, domain_struct *global_domain) { size_t d2count[2]; @@ -346,7 +346,7 @@ add_nveg_to_global_domain(char *nc_name, d2start[1] = 0; d2count[0] = global_domain->n_ny; d2count[1] = global_domain->n_nx; - get_nc_field_int(nc_name, "Nveg", d2start, d2count, ivar); + get_nc_field_int(nc_nameid, "Nveg", d2start, d2count, ivar); for (i = 0; i < global_domain->ncells_total; i++) { global_domain->locations[i].nveg = (size_t) ivar[i]; diff --git a/vic/drivers/shared_image/src/get_nc_dimension.c b/vic/drivers/shared_image/src/get_nc_dimension.c index cdbbb3943..96fb2527a 100644 --- a/vic/drivers/shared_image/src/get_nc_dimension.c +++ b/vic/drivers/shared_image/src/get_nc_dimension.c @@ -30,32 +30,23 @@ * @brief Get netCDF dimension. *****************************************************************************/ size_t -get_nc_dimension(char *nc_name, - char *dim_name) +get_nc_dimension(nameid_struct nc_nameid, + char *dim_name) { - int nc_id; int dim_id; size_t dim_size; int status; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - // get dimension id - status = nc_inq_dimid(nc_id, dim_name, &dim_id); + status = nc_inq_dimid(nc_nameid.nc_id, dim_name, &dim_id); check_nc_status(status, "Error getting dimension id %s in %s", dim_name, - nc_name); + nc_nameid.nc_file); // get dimension size - status = nc_inq_dimlen(nc_id, dim_id, &dim_size); + status = nc_inq_dimlen(nc_nameid.nc_id, dim_id, &dim_size); check_nc_status(status, "Error getting dimension size for dim %s in %s", dim_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return dim_size; } diff --git a/vic/drivers/shared_image/src/get_nc_field.c b/vic/drivers/shared_image/src/get_nc_field.c index af3d48ea7..d672106cd 100644 --- a/vic/drivers/shared_image/src/get_nc_field.c +++ b/vic/drivers/shared_image/src/get_nc_field.c @@ -30,32 +30,23 @@ * @brief Read double precision netCDF field from file. *****************************************************************************/ int -get_nc_field_double(char *nc_name, +get_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, double *var) { - int nc_id; int status; int var_id; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - /* get NetCDF variable */ - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_name); + nc_nameid.nc_file); - status = nc_get_vara_double(nc_id, var_id, start, count, var); + status = nc_get_vara_double(nc_nameid.nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return status; } @@ -64,32 +55,23 @@ get_nc_field_double(char *nc_name, * @brief Read single precision netCDF field from file. *****************************************************************************/ int -get_nc_field_float(char *nc_name, +get_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, float *var) { - int nc_id; int status; int var_id; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - /* get NetCDF variable */ - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_name); + nc_nameid.nc_file); - status = nc_get_vara_float(nc_id, var_id, start, count, var); + status = nc_get_vara_float(nc_nameid.nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return status; } @@ -98,32 +80,23 @@ get_nc_field_float(char *nc_name, * @brief Read integer netCDF field from file. *****************************************************************************/ int -get_nc_field_int(char *nc_name, +get_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, int *var) { - int nc_id; int status; int var_id; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - /* get NetCDF variable */ - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_name); + nc_nameid.nc_file); - status = nc_get_vara_int(nc_id, var_id, start, count, var); + status = nc_get_vara_int(nc_nameid.nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return status; } diff --git a/vic/drivers/shared_image/src/get_nc_var_attr.c b/vic/drivers/shared_image/src/get_nc_var_attr.c index 6a91c8ebc..ee8dc537f 100644 --- a/vic/drivers/shared_image/src/get_nc_var_attr.c +++ b/vic/drivers/shared_image/src/get_nc_var_attr.c @@ -30,7 +30,7 @@ * @brief Get netCDF variable attributes. *****************************************************************************/ void -get_nc_var_attr(char *nc_name, +get_nc_var_attr(nameid_struct nc_nameid, char *var_name, char *attr_name, char **attr) @@ -40,36 +40,28 @@ get_nc_var_attr(char *nc_name, int status; size_t attr_len; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - // get variable id - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_name); + nc_nameid.nc_file); // get size of the attribute - status = nc_inq_attlen(nc_id, var_id, attr_name, &attr_len); + status = nc_inq_attlen(nc_nameid.nc_id, var_id, attr_name, &attr_len); check_nc_status(status, "Error getting attribute length for %s:%s in %s", var_name, - attr_name, nc_name); + attr_name, nc_nameid.nc_file); // allocate memory for attribute *attr = malloc((attr_len + 1) * sizeof(**attr)); check_alloc_status(*attr, "Memory allocation error."); // read attribute text - status = nc_get_att_text(nc_id, var_id, attr_name, *attr); + status = nc_get_att_text(nc_nameid.nc_id, var_id, attr_name, *attr); check_nc_status(status, "Error getting netCDF attribute %s for var %s in %s", attr_name, - var_name, nc_name); + var_name, nc_nameid.nc_file); // we need to null terminate the string ourselves according to NetCDF docs (*attr)[attr_len] = '\0'; - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); } diff --git a/vic/drivers/shared_image/src/get_nc_var_type.c b/vic/drivers/shared_image/src/get_nc_var_type.c index 80a674de6..e3fdd0253 100644 --- a/vic/drivers/shared_image/src/get_nc_var_type.c +++ b/vic/drivers/shared_image/src/get_nc_var_type.c @@ -30,31 +30,22 @@ * @brief Get netCDF variable type. *****************************************************************************/ int -get_nc_var_type(char *nc_name, - char *var_name) +get_nc_var_type(nameid_struct nc_nameid, + char *var_name) { - int nc_id; int var_id; int status; int xtypep; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - // get variable id - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_name); + nc_nameid.nc_file); // get type ID - status = nc_inq_var(nc_id, var_id, NULL, &xtypep, NULL, NULL, NULL); + status = nc_inq_var(nc_nameid.nc_id, var_id, NULL, &xtypep, NULL, NULL, NULL); check_nc_status(status, "Error getting variable type %s in %s", var_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return(xtypep); } diff --git a/vic/drivers/shared_image/src/get_nc_varndimensions.c b/vic/drivers/shared_image/src/get_nc_varndimensions.c index 1f4131eae..c3a0ebe38 100644 --- a/vic/drivers/shared_image/src/get_nc_varndimensions.c +++ b/vic/drivers/shared_image/src/get_nc_varndimensions.c @@ -30,33 +30,24 @@ * @brief Get netCDF dimension. *****************************************************************************/ int -get_nc_varndimensions(char *nc_name, +get_nc_varndimensions(nameid_struct nc_nameid, char *var_name) { - int nc_id; int var_id; int ndims; int status; - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - // get variable id - status = nc_inq_varid(nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_name); + nc_nameid.nc_file); // get number of dimensions - status = nc_inq_varndims(nc_id, var_id, &ndims); + status = nc_inq_varndims(nc_nameid.nc_id, var_id, &ndims); check_nc_status(status, "Error getting number of dimensions for var %s in %s", var_name, - nc_name); - - // close the netcdf file - status = nc_close(nc_id); - check_nc_status(status, "Error closing %s", nc_name); + nc_nameid.nc_file); return ndims; } diff --git a/vic/drivers/shared_image/src/initialize_files.c b/vic/drivers/shared_image/src/initialize_files.c index a4191b140..3357cb44c 100644 --- a/vic/drivers/shared_image/src/initialize_files.c +++ b/vic/drivers/shared_image/src/initialize_files.c @@ -38,10 +38,10 @@ initialize_filenames() size_t i; - strcpy(filenames.init_state, "MISSING"); + strcpy(filenames.init_state.nc_file, "MISSING"); strcpy(filenames.statefile, "MISSING"); strcpy(filenames.constants, "MISSING"); - strcpy(filenames.params, "MISSING"); + strcpy(filenames.params.nc_file, "MISSING"); strcpy(filenames.result_dir, "MISSING"); strcpy(filenames.log_path, "MISSING"); for (i = 0; i < 2; i++) { @@ -57,8 +57,6 @@ initialize_fileps() { extern filep_struct filep; - size_t i; - filep.globalparam = NULL; filep.constants = NULL; filep.logfile = NULL; diff --git a/vic/drivers/shared_image/src/nc_utils.c b/vic/drivers/shared_image/src/nc_utils.c new file mode 100644 index 000000000..7cdcdc263 --- /dev/null +++ b/vic/drivers/shared_image/src/nc_utils.c @@ -0,0 +1,58 @@ +/****************************************************************************** + * @section DESCRIPTION + * + * Functions related to opening and closing netCDF files. + * + * @section LICENSE + * + * The Variable Infiltration Capacity (VIC) macroscale hydrological model + * Copyright (C) 2016 The Computational Hydrology Group, Department of Civil + * and Environmental Engineering, University of Washington. + * + * The VIC model is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + *****************************************************************************/ + +#include + +/****************************************************************************** + * @brief Open a netCDF file + *****************************************************************************/ +int +open_nc(char *nc_name) +{ + int nc_id; + int status; + + // open the netcdf file + status = nc_open(nc_name, NC_NOWRITE, &nc_id); + check_nc_status(status, "Error opening %s", nc_name); + + return(nc_id); +} + +/****************************************************************************** + * @brief Close a netCDF file + *****************************************************************************/ +int +close_nc(nameid_struct nc_nameid) +{ + int status; + + // close the netcdf file + status = nc_close(nc_nameid.nc_id); + check_nc_status(status, "Error closing %s", nc_nameid.nc_file); + + return(status); +} diff --git a/vic/drivers/shared_image/src/vic_image_timing.c b/vic/drivers/shared_image/src/vic_image_timing.c index 64034ddd7..6289ddfeb 100644 --- a/vic/drivers/shared_image/src/vic_image_timing.c +++ b/vic/drivers/shared_image/src/vic_image_timing.c @@ -89,7 +89,7 @@ write_vic_timing_table(timer_struct *timers, fprintf(LOG_DEST, " VIC_DRIVER : %s\n", driver); fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, " Global Param File : %s\n", filenames.global); - fprintf(LOG_DEST, " Domain File : %s\n", filenames.domain); + fprintf(LOG_DEST, " Domain File : %s\n", filenames.domain.nc_file); fprintf(LOG_DEST, " Start Date : %04hu-%02hu-%02hu-%05u\n", global_param.startyear, global_param.startmonth, global_param.startday, global_param.startsec); diff --git a/vic/drivers/shared_image/src/vic_mpi_support.c b/vic/drivers/shared_image/src/vic_mpi_support.c index c1d678981..7c60b63e2 100644 --- a/vic/drivers/shared_image/src/vic_mpi_support.c +++ b/vic/drivers/shared_image/src/vic_mpi_support.c @@ -2042,7 +2042,7 @@ gather_put_nc_field_schar(int nc_id, * nodes *****************************************************************************/ void -get_scatter_nc_field_double(char *nc_name, +get_scatter_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, @@ -2073,7 +2073,7 @@ get_scatter_nc_field_double(char *nc_name, malloc(global_domain.ncells_active * sizeof(*dvar_mapped)); check_alloc_status(dvar_mapped, "Memory allocation error."); - get_nc_field_double(nc_name, var_name, start, count, dvar); + get_nc_field_double(nc_nameid, var_name, start, count, dvar); // filter the active cells only map(sizeof(double), global_domain.ncells_active, filter_active_cells, NULL, dvar, dvar_filtered); @@ -2103,7 +2103,7 @@ get_scatter_nc_field_double(char *nc_name, * nodes *****************************************************************************/ void -get_scatter_nc_field_float(char *nc_name, +get_scatter_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, @@ -2134,7 +2134,7 @@ get_scatter_nc_field_float(char *nc_name, malloc(global_domain.ncells_active * sizeof(*fvar_mapped)); check_alloc_status(fvar_mapped, "Memory allocation error."); - get_nc_field_float(nc_name, var_name, start, count, fvar); + get_nc_field_float(nc_nameid, var_name, start, count, fvar); // filter the active cells only map(sizeof(float), global_domain.ncells_active, filter_active_cells, NULL, @@ -2166,7 +2166,7 @@ get_scatter_nc_field_float(char *nc_name, * nodes *****************************************************************************/ void -get_scatter_nc_field_int(char *nc_name, +get_scatter_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, size_t *count, @@ -2196,7 +2196,7 @@ get_scatter_nc_field_int(char *nc_name, malloc(global_domain.ncells_active * sizeof(*ivar_mapped)); check_alloc_status(ivar_mapped, "Memory allocation error."); - get_nc_field_int(nc_name, var_name, start, count, ivar); + get_nc_field_int(nc_nameid, var_name, start, count, ivar); // filter the active cells only map(sizeof(int), global_domain.ncells_active, filter_active_cells, NULL, ivar, ivar_filtered); diff --git a/vic/drivers/shared_image/src/vic_restore.c b/vic/drivers/shared_image/src/vic_restore.c index f23cc3f92..a2eea26bb 100644 --- a/vic/drivers/shared_image/src/vic_restore.c +++ b/vic/drivers/shared_image/src/vic_restore.c @@ -59,6 +59,9 @@ vic_restore(void) size_t d6count[6]; size_t d6start[6]; + // open initial state file + filenames.init_state.nc_id = open_nc(filenames.init_state.nc_file); + // validate state file dimensions and coordinate variables check_init_state_file(); // read state variables @@ -866,6 +869,9 @@ vic_restore(void) free(ivar); free(dvar); + + // close initial state file + close_nc(filenames.init_state); } /****************************************************************************** @@ -897,11 +903,6 @@ check_init_state_file(void) double *dvar; double rtol = 0.0; // maybe move this to a .h file double abs_tol = 0.0001; // maybe move this to a .h file - nc_file_struct nc; - - // open the netcdf file - status = nc_open(filenames.init_state, NC_SHARE, &(nc.nc_id)); - check_nc_status(status, "Error opening %s", filenames.init_state); // read and validate dimension lengths if (mpi_rank == VIC_MPI_ROOT) { @@ -953,22 +954,22 @@ check_init_state_file(void) // lat/lon if (mpi_rank == VIC_MPI_ROOT) { - status = nc_inq_varid(nc.nc_id, global_domain.info.lon_var, &lon_var_id); + status = nc_inq_varid(filenames.init_state.nc_id, global_domain.info.lon_var, &lon_var_id); check_nc_status(status, "Unable to find variable \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state); - status = nc_inq_varid(nc.nc_id, global_domain.info.lat_var, &lat_var_id); + global_domain.info.lon_var, filenames.init_state.nc_file); + status = nc_inq_varid(filenames.init_state.nc_id, global_domain.info.lat_var, &lat_var_id); check_nc_status(status, "Unable to find variable \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state); + global_domain.info.lat_var, filenames.init_state.nc_file); if (global_domain.info.n_coord_dims == 1) { d1start[0] = 0; dvar = calloc(global_domain.n_nx, sizeof(*dvar)); check_alloc_status(dvar, "Memory allocation error"); d1count[0] = global_domain.n_nx; - status = nc_get_vara_double(nc.nc_id, lon_var_id, + status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id, d1start, d1count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state); + global_domain.info.lon_var, filenames.init_state.nc_file); // implicitly nested loop over ni and nj with j set to 0 for (i = 0; i < global_domain.n_nx; i++) { if (!assert_close_double(dvar[i], @@ -984,10 +985,10 @@ check_init_state_file(void) check_alloc_status(dvar, "Memory allocation error"); d1count[0] = global_domain.n_ny; - status = nc_get_vara_double(nc.nc_id, lat_var_id, + status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id, d1start, d1count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state); + global_domain.info.lat_var, filenames.init_state.nc_file); // implicitly nested loop over ni and nj with i set to 0; // j stride = n_nx for (j = 0; j < global_domain.n_ny; j++) { @@ -1010,20 +1011,20 @@ check_init_state_file(void) d2count[0] = global_domain.n_ny; d2count[1] = global_domain.n_nx; - status = nc_get_vara_double(nc.nc_id, lon_var_id, + status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id, d2start, d2count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state); + global_domain.info.lon_var, filenames.init_state.nc_file); for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) { if (dvar[i] != (double) global_domain.locations[i].longitude) { log_err("Longitudes in initial state file do not " "match parameter file"); } } - status = nc_get_vara_double(nc.nc_id, lat_var_id, + status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id, d2start, d2count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state); + global_domain.info.lat_var, filenames.init_state.nc_file); for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) { if (dvar[i] != (double) global_domain.locations[i].latitude) { log_err("Latitudes in initial state file do not " From de656cddac2557de43d8f1960f9e8192a1149b50 Mon Sep 17 00:00:00 2001 From: ymao Date: Thu, 16 Feb 2017 15:16:01 -0800 Subject: [PATCH 5/7] Image initialization speedup - fixed MPI related bugs so now MPI run works --- vic/drivers/image/src/vic_force.c | 37 ++++++++++++++-------- vic/drivers/image/src/vic_image_init.c | 7 ++-- vic/drivers/image/src/vic_image_start.c | 14 +++++--- vic/drivers/shared_image/src/vic_restore.c | 11 +++++-- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/vic/drivers/image/src/vic_force.c b/vic/drivers/image/src/vic_force.c index d8129a19c..b8af32edf 100644 --- a/vic/drivers/image/src/vic_force.c +++ b/vic/drivers/image/src/vic_force.c @@ -35,6 +35,7 @@ vic_force(void) extern size_t NF; extern size_t NR; extern size_t current; + extern int mpi_rank; extern force_data_struct *force; extern dmy_struct *dmy; extern domain_struct global_domain; @@ -76,10 +77,12 @@ vic_force(void) // file for the current new year // (forcing file for the first year should already be open in // get_global_param) - close_nc(filenames.forcing[0]); - sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], - dmy[current].year); - filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); + if (mpi_rank == VIC_MPI_ROOT) { + close_nc(filenames.forcing[0]); + sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], + dmy[current].year); + filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); + } } // only the time slice changes for the met file reads. The rest is constant @@ -232,9 +235,11 @@ vic_force(void) } } - // Close forcing file if it is the last time step - if (current == global_param.nrecs) { - close_nc(filenames.forcing[0]); + if (mpi_rank == VIC_MPI_ROOT) { + // Close forcing file if it is the last time step + if (current == global_param.nrecs) { + close_nc(filenames.forcing[0]); + } } // Update the offset counter @@ -278,10 +283,12 @@ vic_force(void) // file for the current new year // (forcing file for the first year should already be open in // get_global_param) - close_nc(filenames.forcing[1]); - sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], - dmy[current].year); - filenames.forcing[1].nc_id = open_nc(filenames.forcing[1].nc_file); + if (mpi_rank == VIC_MPI_ROOT) { + close_nc(filenames.forcing[1]); + sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], + dmy[current].year); + filenames.forcing[1].nc_id = open_nc(filenames.forcing[1].nc_file); + } } // only the time slice changes for the met file reads. The rest is constant @@ -349,9 +356,11 @@ vic_force(void) } } - // Close forcing file if it is the last time step - if (current == global_param.nrecs) { - close_nc(filenames.forcing[1]); + if (mpi_rank == VIC_MPI_ROOT) { + // Close forcing file if it is the last time step + if (current == global_param.nrecs) { + close_nc(filenames.forcing[1]); + } } // Update the offset counter diff --git a/vic/drivers/image/src/vic_image_init.c b/vic/drivers/image/src/vic_image_init.c index 1e47d9603..6fc06208a 100644 --- a/vic/drivers/image/src/vic_image_init.c +++ b/vic/drivers/image/src/vic_image_init.c @@ -35,6 +35,7 @@ vic_image_init(void) extern dmy_struct *dmy; extern global_param_struct global_param; extern filenames_struct filenames; + extern int mpi_rank; // make_dmy() initialize_time(); @@ -42,6 +43,8 @@ vic_image_init(void) vic_init(); - // close parameter netCDF file - close_nc(filenames.params); + if (mpi_rank == VIC_MPI_ROOT) { + // close parameter netCDF file + close_nc(filenames.params); + } } diff --git a/vic/drivers/image/src/vic_image_start.c b/vic/drivers/image/src/vic_image_start.c index 17fee6123..7bda6bc1f 100644 --- a/vic/drivers/image/src/vic_image_start.c +++ b/vic/drivers/image/src/vic_image_start.c @@ -45,13 +45,17 @@ vic_image_start(void) get_global_param(filep.globalparam); } - // Open domain and parameter netCDF files - filenames.domain.nc_id = open_nc(filenames.domain.nc_file); - filenames.params.nc_id = open_nc(filenames.params.nc_file); + if (mpi_rank == VIC_MPI_ROOT) { + // Open domain and parameter netCDF files + filenames.domain.nc_id = open_nc(filenames.domain.nc_file); + filenames.params.nc_id = open_nc(filenames.params.nc_file); + } // initialize image mode structures and settings vic_start(); - // Close domain file - close_nc(filenames.domain); + if (mpi_rank == VIC_MPI_ROOT) { + // Close domain file + close_nc(filenames.domain); + } } diff --git a/vic/drivers/shared_image/src/vic_restore.c b/vic/drivers/shared_image/src/vic_restore.c index a2eea26bb..2293eae2f 100644 --- a/vic/drivers/shared_image/src/vic_restore.c +++ b/vic/drivers/shared_image/src/vic_restore.c @@ -32,6 +32,7 @@ void vic_restore(void) { + extern int mpi_rank; extern all_vars_struct *all_vars; extern domain_struct global_domain; extern domain_struct local_domain; @@ -59,8 +60,10 @@ vic_restore(void) size_t d6count[6]; size_t d6start[6]; - // open initial state file - filenames.init_state.nc_id = open_nc(filenames.init_state.nc_file); + if (mpi_rank == VIC_MPI_ROOT) { + // open initial state file + filenames.init_state.nc_id = open_nc(filenames.init_state.nc_file); + } // validate state file dimensions and coordinate variables check_init_state_file(); @@ -871,7 +874,9 @@ vic_restore(void) free(dvar); // close initial state file - close_nc(filenames.init_state); + if (mpi_rank == VIC_MPI_ROOT) { + close_nc(filenames.init_state); + } } /****************************************************************************** From 1f3a85d208e0647d0d695e5a1f88c689d038a5b6 Mon Sep 17 00:00:00 2001 From: ymao Date: Thu, 16 Feb 2017 18:03:56 -0800 Subject: [PATCH 6/7] Fixed bugs corresponding to initialization speedup in some printing commands --- vic/drivers/image/src/display_current_settings.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vic/drivers/image/src/display_current_settings.c b/vic/drivers/image/src/display_current_settings.c index 6df978b2e..8c30a0b9e 100644 --- a/vic/drivers/image/src/display_current_settings.c +++ b/vic/drivers/image/src/display_current_settings.c @@ -265,11 +265,11 @@ display_current_settings(int mode) fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Input Domain Data:\n"); - fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain); + fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain.nc_file); fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Constants File\t\t%s\n", filenames.constants); - fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params); + fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params.nc_file); if (options.BASEFLOW == ARNO) { fprintf(LOG_DEST, "BASEFLOW\t\tARNO\n"); } @@ -378,7 +378,7 @@ display_current_settings(int mode) fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Input State File:\n"); if (options.INIT_STATE) { - fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state); + fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state.nc_file); if (options.STATE_FORMAT == NETCDF3_CLASSIC) { fprintf(LOG_DEST, "STATE_FORMAT\t\tNETCDF3_CLASSIC\n"); } From 5cb7cbaa3b031b77ed0b08e862099065d34bb70b Mon Sep 17 00:00:00 2001 From: ymao Date: Wed, 22 Feb 2017 14:27:39 -0800 Subject: [PATCH 7/7] Speedup image driver initialization - incorporate Bart's comments, including: - pass the pointer of the new nameid_struct variables into functions (instead of passing in the structure itself) - rename the filename element in the nameid_struct from "nc_file" to "nc_filename" - remove the "open_nc" and "close_nc" wrapping functions - move the opening and closing of the domain and parameter nc files into either "vic_start" or "vic_init" --- samples/data | 2 +- .../image/src/display_current_settings.c | 6 +- vic/drivers/image/src/get_global_param.c | 33 ++-- vic/drivers/image/src/vic_force.c | 72 +++++--- vic/drivers/image/src/vic_image_init.c | 7 - vic/drivers/image/src/vic_image_start.c | 11 -- .../include/vic_driver_shared_image.h | 26 ++- vic/drivers/shared_image/include/vic_mpi.h | 8 +- .../shared_image/src/check_domain_info.c | 2 +- .../shared_image/src/get_global_domain.c | 12 +- .../shared_image/src/get_nc_dimension.c | 10 +- vic/drivers/shared_image/src/get_nc_field.c | 30 ++-- .../shared_image/src/get_nc_var_attr.c | 15 +- .../shared_image/src/get_nc_var_type.c | 12 +- .../shared_image/src/get_nc_varndimensions.c | 10 +- .../shared_image/src/initialize_files.c | 5 +- vic/drivers/shared_image/src/nc_utils.c | 58 ------ .../shared_image/src/vic_image_timing.c | 2 +- vic/drivers/shared_image/src/vic_init.c | 145 ++++++++------- .../shared_image/src/vic_mpi_support.c | 6 +- vic/drivers/shared_image/src/vic_restore.c | 166 +++++++++--------- vic/drivers/shared_image/src/vic_start.c | 28 ++- 22 files changed, 322 insertions(+), 344 deletions(-) delete mode 100644 vic/drivers/shared_image/src/nc_utils.c diff --git a/samples/data b/samples/data index 1554cb017..3f2dcb445 160000 --- a/samples/data +++ b/samples/data @@ -1 +1 @@ -Subproject commit 1554cb017c71e59e114b853af5808649f80ad836 +Subproject commit 3f2dcb445e3e985456589a29711a2fee59f8faaf diff --git a/vic/drivers/image/src/display_current_settings.c b/vic/drivers/image/src/display_current_settings.c index 8c30a0b9e..d3994dceb 100644 --- a/vic/drivers/image/src/display_current_settings.c +++ b/vic/drivers/image/src/display_current_settings.c @@ -265,11 +265,11 @@ display_current_settings(int mode) fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Input Domain Data:\n"); - fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain.nc_file); + fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain.nc_filename); fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Constants File\t\t%s\n", filenames.constants); - fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params.nc_file); + fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params.nc_filename); if (options.BASEFLOW == ARNO) { fprintf(LOG_DEST, "BASEFLOW\t\tARNO\n"); } @@ -378,7 +378,7 @@ display_current_settings(int mode) fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, "Input State File:\n"); if (options.INIT_STATE) { - fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state.nc_file); + fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state.nc_filename); if (options.STATE_FORMAT == NETCDF3_CLASSIC) { fprintf(LOG_DEST, "STATE_FORMAT\t\tNETCDF3_CLASSIC\n"); } diff --git a/vic/drivers/image/src/get_global_param.c b/vic/drivers/image/src/get_global_param.c index 652600791..538ae1b55 100644 --- a/vic/drivers/image/src/get_global_param.c +++ b/vic/drivers/image/src/get_global_param.c @@ -46,6 +46,7 @@ get_global_param(FILE *gp) char flgstr2[MAXSTRING]; size_t file_num; int field; + int status; unsigned int tmpstartdate; unsigned int tmpenddate; unsigned short int lastday[MONTHS_PER_YEAR]; @@ -296,7 +297,7 @@ get_global_param(FILE *gp) } else { options.INIT_STATE = true; - strcpy(filenames.init_state.nc_file, flgstr); + strcpy(filenames.init_state.nc_filename, flgstr); } } else if (strcasecmp("STATENAME", optstr) == 0) { @@ -374,13 +375,13 @@ get_global_param(FILE *gp) sscanf(cmdstr, "%*s %s", filenames.constants); } else if (strcasecmp("DOMAIN", optstr) == 0) { - sscanf(cmdstr, "%*s %s", filenames.domain.nc_file); + sscanf(cmdstr, "%*s %s", filenames.domain.nc_filename); } else if (strcasecmp("DOMAIN_TYPE", optstr) == 0) { get_domain_type(cmdstr); } else if (strcasecmp("PARAMETERS", optstr) == 0) { - sscanf(cmdstr, "%*s %s", filenames.params.nc_file); + sscanf(cmdstr, "%*s %s", filenames.params.nc_filename); } else if (strcasecmp("ARNO_PARAMS", optstr) == 0) { sscanf(cmdstr, "%*s %s", flgstr); @@ -788,14 +789,20 @@ get_global_param(FILE *gp) // Get information from the forcing file(s) // Open first-year forcing files and get info - sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], - global_param.startyear); - filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); + sprintf(filenames.forcing[0].nc_filename, "%s%4d.nc", + filenames.f_path_pfx[0], global_param.startyear); + status = nc_open(filenames.forcing[0].nc_filename, NC_NOWRITE, + &(filenames.forcing[0].nc_id)); + check_nc_status(status, "Error opening %s", + filenames.forcing[0].nc_filename); get_forcing_file_info(¶m_set, 0); if (param_set.N_TYPES[1] != 0) { - sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], - global_param.startyear); - filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); + sprintf(filenames.forcing[1].nc_filename, "%s%4d.nc", + filenames.f_path_pfx[1], global_param.startyear); + status = nc_open(filenames.forcing[1].nc_filename, NC_NOWRITE, + &(filenames.forcing[1].nc_id)); + check_nc_status(status, "Error opening %s", + filenames.forcing[1].nc_filename); get_forcing_file_info(¶m_set, 1); } @@ -832,7 +839,7 @@ get_global_param(FILE *gp) } // Validate parameter file information - if (strcmp(filenames.params.nc_file, "MISSING") == 0) { + if (strcmp(filenames.params.nc_filename, "MISSING") == 0) { log_err("A parameters file has not been defined. Make sure that the " "global file defines the parameters parameter file on the line " "that begins with \"PARAMETERS\"."); @@ -871,7 +878,7 @@ get_global_param(FILE *gp) // Validate the input state file information if (options.INIT_STATE) { - if (strcmp(filenames.init_state.nc_file, "MISSING") == 0) { + if (strcmp(filenames.init_state.nc_filename, "MISSING") == 0) { log_err("\"INIT_STATE\" was specified, but no input state file " "has been defined. Make sure that the global file " "defines the inputstate file on the line that begins " @@ -922,11 +929,11 @@ get_global_param(FILE *gp) global_param.statesec); } if (options.INIT_STATE && options.SAVE_STATE && - (strcmp(filenames.init_state.nc_file, flgstr2) == 0)) { + (strcmp(filenames.init_state.nc_filename, flgstr2) == 0)) { log_err("The save state file (%s) has the same name as the " "initialize state file (%s). The initialize state file " "will be destroyed when the save state file is opened.", - filenames.statefile, filenames.init_state.nc_file); + filenames.statefile, filenames.init_state.nc_filename); } // Validate soil parameter/simulation mode combinations diff --git a/vic/drivers/image/src/vic_force.c b/vic/drivers/image/src/vic_force.c index 99126fb72..be0c80017 100644 --- a/vic/drivers/image/src/vic_force.c +++ b/vic/drivers/image/src/vic_force.c @@ -57,6 +57,7 @@ vic_force(void) size_t v; size_t band; int vidx; + int status; size_t d3count[3]; size_t d3start[3]; size_t d4count[4]; @@ -78,10 +79,17 @@ vic_force(void) // (forcing file for the first year should already be open in // get_global_param) if (mpi_rank == VIC_MPI_ROOT) { - close_nc(filenames.forcing[0]); - sprintf(filenames.forcing[0].nc_file, "%s%4d.nc", filenames.f_path_pfx[0], - dmy[current].year); - filenames.forcing[0].nc_id = open_nc(filenames.forcing[0].nc_file); + // close previous forcing file + status = nc_close(filenames.forcing[0].nc_id); + check_nc_status(status, "Error closing %s", + filenames.forcing[0].nc_filename); + // open new forcing file + sprintf(filenames.forcing[0].nc_filename, "%s%4d.nc", + filenames.f_path_pfx[0], dmy[current].year); + status = nc_open(filenames.forcing[0].nc_filename, NC_NOWRITE, + &(filenames.forcing[0].nc_id)); + check_nc_status(status, "Error opening %s", + filenames.forcing[0].nc_filename); } } @@ -96,7 +104,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[AIR_TEMP].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -108,7 +116,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[PREC].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -120,7 +128,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[SWDOWN].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -132,7 +140,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[LWDOWN].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -144,7 +152,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[WIND].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -156,7 +164,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[VP].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -168,7 +176,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[PRESSURE].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -180,7 +188,7 @@ vic_force(void) // Channel inflow to lake d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[CHANNEL_IN].varname, d3start, d3count, dvar); for (j = 0; j < NF; j++) { @@ -194,7 +202,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[CATM].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -215,7 +223,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[FDIR].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -226,7 +234,7 @@ vic_force(void) for (j = 0; j < NF; j++) { d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] + j; - get_scatter_nc_field_double(filenames.forcing[0], + get_scatter_nc_field_double(&(filenames.forcing[0]), param_set.TYPE[PAR].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -238,7 +246,9 @@ vic_force(void) if (mpi_rank == VIC_MPI_ROOT) { // Close forcing file if it is the last time step if (current == global_param.nrecs) { - close_nc(filenames.forcing[0]); + status = nc_close(filenames.forcing[0].nc_id); + check_nc_status(status, "Error closing %s", + filenames.forcing[0].nc_filename); } } @@ -284,10 +294,18 @@ vic_force(void) // (forcing file for the first year should already be open in // get_global_param) if (mpi_rank == VIC_MPI_ROOT) { - close_nc(filenames.forcing[1]); - sprintf(filenames.forcing[1].nc_file, "%s%4d.nc", filenames.f_path_pfx[1], + // close previous forcing file + status = nc_close(filenames.forcing[1].nc_id); + check_nc_status(status, "Error closing %s", + filenames.forcing[1].nc_filename); + // open new forcing file + sprintf(filenames.forcing[1].nc_filename, "%s%4d.nc", + filenames.f_path_pfx[1], dmy[current].year); - filenames.forcing[1].nc_id = open_nc(filenames.forcing[1].nc_file); + status = nc_open(filenames.forcing[1].nc_filename, NC_NOWRITE, + &(filenames.forcing[1].nc_id)); + check_nc_status(status, "Error opening %s", + filenames.forcing[1].nc_filename); } } @@ -306,7 +324,7 @@ vic_force(void) global_param.forceoffset[1] + j; for (v = 0; v < options.NVEGTYPES; v++) { d4start[1] = v; - get_scatter_nc_field_double(filenames.forcing[1], "lai", + get_scatter_nc_field_double(&(filenames.forcing[1]), "lai", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[v]; @@ -325,7 +343,7 @@ vic_force(void) global_param.forceoffset[1] + j; for (v = 0; v < options.NVEGTYPES; v++) { d4start[1] = v; - get_scatter_nc_field_double(filenames.forcing[1], "fcov", + get_scatter_nc_field_double(&(filenames.forcing[1]), "fcov", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[v]; @@ -344,7 +362,7 @@ vic_force(void) global_param.forceoffset[1] + j; for (v = 0; v < options.NVEGTYPES; v++) { d4start[1] = v; - get_scatter_nc_field_double(filenames.forcing[1], "alb", + get_scatter_nc_field_double(&(filenames.forcing[1]), "alb", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[v]; @@ -359,7 +377,9 @@ vic_force(void) if (mpi_rank == VIC_MPI_ROOT) { // Close forcing file if it is the last time step if (current == global_param.nrecs) { - close_nc(filenames.forcing[1]); + status = nc_close(filenames.forcing[1].nc_id); + check_nc_status(status, "Error closing %s", + filenames.forcing[1].nc_filename); } } @@ -507,9 +527,9 @@ get_forcing_file_info(param_set_struct *param_set, dmy_struct nc_start_dmy; // read time info from netcdf file - get_nc_field_double(filenames.forcing[file_num], "time", &start, &count, nc_times); - get_nc_var_attr(filenames.forcing[file_num], "time", "units", &nc_unit_chars); - get_nc_var_attr(filenames.forcing[file_num], "time", "calendar", &calendar_char); + get_nc_field_double(&(filenames.forcing[file_num]), "time", &start, &count, nc_times); + get_nc_var_attr(&(filenames.forcing[file_num]), "time", "units", &nc_unit_chars); + get_nc_var_attr(&(filenames.forcing[file_num]), "time", "calendar", &calendar_char); // parse the calendar string and check to make sure it matches the global clock calendar = str_to_calendar(calendar_char); diff --git a/vic/drivers/image/src/vic_image_init.c b/vic/drivers/image/src/vic_image_init.c index 6fc06208a..cbb29eb93 100644 --- a/vic/drivers/image/src/vic_image_init.c +++ b/vic/drivers/image/src/vic_image_init.c @@ -34,17 +34,10 @@ vic_image_init(void) { extern dmy_struct *dmy; extern global_param_struct global_param; - extern filenames_struct filenames; - extern int mpi_rank; // make_dmy() initialize_time(); dmy = make_dmy(&global_param); vic_init(); - - if (mpi_rank == VIC_MPI_ROOT) { - // close parameter netCDF file - close_nc(filenames.params); - } } diff --git a/vic/drivers/image/src/vic_image_start.c b/vic/drivers/image/src/vic_image_start.c index 7bda6bc1f..401df8162 100644 --- a/vic/drivers/image/src/vic_image_start.c +++ b/vic/drivers/image/src/vic_image_start.c @@ -45,17 +45,6 @@ vic_image_start(void) get_global_param(filep.globalparam); } - if (mpi_rank == VIC_MPI_ROOT) { - // Open domain and parameter netCDF files - filenames.domain.nc_id = open_nc(filenames.domain.nc_file); - filenames.params.nc_id = open_nc(filenames.params.nc_file); - } - // initialize image mode structures and settings vic_start(); - - if (mpi_rank == VIC_MPI_ROOT) { - // Close domain file - close_nc(filenames.domain); - } } diff --git a/vic/drivers/shared_image/include/vic_driver_shared_image.h b/vic/drivers/shared_image/include/vic_driver_shared_image.h index dfae537f3..9106e02cc 100644 --- a/vic/drivers/shared_image/include/vic_driver_shared_image.h +++ b/vic/drivers/shared_image/include/vic_driver_shared_image.h @@ -181,34 +181,32 @@ typedef struct { char log_path[MAXSTRING]; /**< Location to write log file to */ } filenames_struct; -void add_nveg_to_global_domain(nameid_struct nc_nameid, +void add_nveg_to_global_domain(nameid_struct *nc_nameid, domain_struct *global_domain); void alloc_force(force_data_struct *force); void alloc_veg_hist(veg_hist_struct *veg_hist); double air_density(double t, double p); double average(double *ar, size_t n); void check_init_state_file(void); -void compare_ncdomain_with_global_domain(nameid_struct nc_nameid); +void compare_ncdomain_with_global_domain(nameid_struct *nc_nameid); void free_force(force_data_struct *force); void free_veg_hist(veg_hist_struct *veg_hist); void get_domain_type(char *cmdstr); -size_t get_global_domain(nameid_struct domain_nc_nameid, - nameid_struct param_nc_nameid, +size_t get_global_domain(nameid_struct *domain_nc_nameid, + nameid_struct *param_nc_nameid, domain_struct *global_domain); void copy_domain_info(domain_struct *domain_from, domain_struct *domain_to); -int open_nc(char *nc_name); -int close_nc(nameid_struct nc_nameid); -void get_nc_latlon(nameid_struct nc_nameid, domain_struct *nc_domain); -size_t get_nc_dimension(nameid_struct nc_nameid, char *dim_name); -void get_nc_var_attr(nameid_struct nc_nameid, char *var_name, char *attr_name, +void get_nc_latlon(nameid_struct *nc_nameid, domain_struct *nc_domain); +size_t get_nc_dimension(nameid_struct *nc_nameid, char *dim_name); +void get_nc_var_attr(nameid_struct *nc_nameid, char *var_name, char *attr_name, char **attr); -int get_nc_var_type(nameid_struct nc_nameid, char *var_name); -int get_nc_varndimensions(nameid_struct nc_nameid, char *var_name); -int get_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, +int get_nc_var_type(nameid_struct *nc_nameid, char *var_name); +int get_nc_varndimensions(nameid_struct *nc_nameid, char *var_name); +int get_nc_field_double(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, double *var); -int get_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, +int get_nc_field_float(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, float *var); -int get_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, +int get_nc_field_int(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, int *var); int get_nc_dtype(unsigned short int dtype); int get_nc_mode(unsigned short int format); diff --git a/vic/drivers/shared_image/include/vic_mpi.h b/vic/drivers/shared_image/include/vic_mpi.h index 13c25a3dc..63330ca0b 100644 --- a/vic/drivers/shared_image/include/vic_mpi.h +++ b/vic/drivers/shared_image/include/vic_mpi.h @@ -36,7 +36,7 @@ * * @brief This structure stores netCDF file name and corresponding nc_id * *****************************************************************************/ typedef struct { - char nc_file[MAXSTRING]; + char nc_filename[MAXSTRING]; int nc_id; } nameid_struct; @@ -56,11 +56,11 @@ void gather_put_nc_field_short(int nc_id, int var_id, short int fillval, size_t *start, size_t *count, short int *var); void gather_put_nc_field_schar(int nc_id, int var_id, char fillval, size_t *start, size_t *count, char *var); -void get_scatter_nc_field_double(nameid_struct nc_nameid, char *var_name, size_t *start, +void get_scatter_nc_field_double(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, double *var); -void get_scatter_nc_field_float(nameid_struct nc_nameid, char *var_name, size_t *start, +void get_scatter_nc_field_float(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, float *var); -void get_scatter_nc_field_int(nameid_struct nc_nameid, char *var_name, size_t *start, +void get_scatter_nc_field_int(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, int *var); void initialize_mpi(void); void map(size_t size, size_t n, size_t *from_map, size_t *to_map, void *from, diff --git a/vic/drivers/shared_image/src/check_domain_info.c b/vic/drivers/shared_image/src/check_domain_info.c index e0efdf0be..26d6ae26c 100644 --- a/vic/drivers/shared_image/src/check_domain_info.c +++ b/vic/drivers/shared_image/src/check_domain_info.c @@ -32,7 +32,7 @@ the global domain. *****************************************************************************/ void -compare_ncdomain_with_global_domain(nameid_struct nc_nameid) +compare_ncdomain_with_global_domain(nameid_struct *nc_nameid) { extern domain_struct global_domain; diff --git a/vic/drivers/shared_image/src/get_global_domain.c b/vic/drivers/shared_image/src/get_global_domain.c index cb9441b89..4291b79d8 100644 --- a/vic/drivers/shared_image/src/get_global_domain.c +++ b/vic/drivers/shared_image/src/get_global_domain.c @@ -30,8 +30,8 @@ * @brief Get global domain information. *****************************************************************************/ size_t -get_global_domain(nameid_struct domain_nc_nameid, - nameid_struct param_nc_nameid, +get_global_domain(nameid_struct *domain_nc_nameid, + nameid_struct *param_nc_nameid, domain_struct *global_domain) { int *run = NULL; @@ -158,7 +158,7 @@ get_global_domain(nameid_struct domain_nc_nameid, store in nc_domain structure *****************************************************************************/ void -get_nc_latlon(nameid_struct nc_nameid, +get_nc_latlon(nameid_struct *nc_nameid, domain_struct *nc_domain) { double *var = NULL; @@ -184,7 +184,7 @@ get_nc_latlon(nameid_struct nc_nameid, (size_t) get_nc_varndimensions(nc_nameid, nc_domain->info.lat_var)) { log_err("Un even number of dimensions for %s and %s in: %s", nc_domain->info.lon_var, nc_domain->info.lat_var, - nc_nameid.nc_file); + nc_nameid->nc_filename); } if (nc_domain->info.n_coord_dims == 1) { @@ -265,7 +265,7 @@ get_nc_latlon(nameid_struct nc_nameid, else { log_err("Number of dimensions for %s and %s should be 1 or 2 in: %s", nc_domain->info.lon_var, nc_domain->info.lat_var, - nc_nameid.nc_file); + nc_nameid->nc_filename); } } @@ -331,7 +331,7 @@ initialize_location(location_struct *location) * @brief Read the number of vegetation type per grid cell from file *****************************************************************************/ void -add_nveg_to_global_domain(nameid_struct nc_nameid, +add_nveg_to_global_domain(nameid_struct *nc_nameid, domain_struct *global_domain) { size_t d2count[2]; diff --git a/vic/drivers/shared_image/src/get_nc_dimension.c b/vic/drivers/shared_image/src/get_nc_dimension.c index 96fb2527a..2561a1e4e 100644 --- a/vic/drivers/shared_image/src/get_nc_dimension.c +++ b/vic/drivers/shared_image/src/get_nc_dimension.c @@ -30,7 +30,7 @@ * @brief Get netCDF dimension. *****************************************************************************/ size_t -get_nc_dimension(nameid_struct nc_nameid, +get_nc_dimension(nameid_struct *nc_nameid, char *dim_name) { int dim_id; @@ -38,15 +38,15 @@ get_nc_dimension(nameid_struct nc_nameid, int status; // get dimension id - status = nc_inq_dimid(nc_nameid.nc_id, dim_name, &dim_id); + status = nc_inq_dimid(nc_nameid->nc_id, dim_name, &dim_id); check_nc_status(status, "Error getting dimension id %s in %s", dim_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); // get dimension size - status = nc_inq_dimlen(nc_nameid.nc_id, dim_id, &dim_size); + status = nc_inq_dimlen(nc_nameid->nc_id, dim_id, &dim_size); check_nc_status(status, "Error getting dimension size for dim %s in %s", dim_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return dim_size; } diff --git a/vic/drivers/shared_image/src/get_nc_field.c b/vic/drivers/shared_image/src/get_nc_field.c index d672106cd..5b79f218e 100644 --- a/vic/drivers/shared_image/src/get_nc_field.c +++ b/vic/drivers/shared_image/src/get_nc_field.c @@ -30,7 +30,7 @@ * @brief Read double precision netCDF field from file. *****************************************************************************/ int -get_nc_field_double(nameid_struct nc_nameid, +get_nc_field_double(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, @@ -40,13 +40,13 @@ get_nc_field_double(nameid_struct nc_nameid, int var_id; /* get NetCDF variable */ - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); - status = nc_get_vara_double(nc_nameid.nc_id, var_id, start, count, var); + status = nc_get_vara_double(nc_nameid->nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return status; } @@ -55,7 +55,7 @@ get_nc_field_double(nameid_struct nc_nameid, * @brief Read single precision netCDF field from file. *****************************************************************************/ int -get_nc_field_float(nameid_struct nc_nameid, +get_nc_field_float(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, @@ -65,13 +65,13 @@ get_nc_field_float(nameid_struct nc_nameid, int var_id; /* get NetCDF variable */ - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); - status = nc_get_vara_float(nc_nameid.nc_id, var_id, start, count, var); + status = nc_get_vara_float(nc_nameid->nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return status; } @@ -80,7 +80,7 @@ get_nc_field_float(nameid_struct nc_nameid, * @brief Read integer netCDF field from file. *****************************************************************************/ int -get_nc_field_int(nameid_struct nc_nameid, +get_nc_field_int(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, @@ -90,13 +90,13 @@ get_nc_field_int(nameid_struct nc_nameid, int var_id; /* get NetCDF variable */ - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); - status = nc_get_vara_int(nc_nameid.nc_id, var_id, start, count, var); + status = nc_get_vara_int(nc_nameid->nc_id, var_id, start, count, var); check_nc_status(status, "Error getting values for %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return status; } diff --git a/vic/drivers/shared_image/src/get_nc_var_attr.c b/vic/drivers/shared_image/src/get_nc_var_attr.c index ee8dc537f..c16d24b50 100644 --- a/vic/drivers/shared_image/src/get_nc_var_attr.c +++ b/vic/drivers/shared_image/src/get_nc_var_attr.c @@ -30,37 +30,36 @@ * @brief Get netCDF variable attributes. *****************************************************************************/ void -get_nc_var_attr(nameid_struct nc_nameid, +get_nc_var_attr(nameid_struct *nc_nameid, char *var_name, char *attr_name, char **attr) { - int nc_id; int var_id; int status; size_t attr_len; // get variable id - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); // get size of the attribute - status = nc_inq_attlen(nc_nameid.nc_id, var_id, attr_name, &attr_len); + status = nc_inq_attlen(nc_nameid->nc_id, var_id, attr_name, &attr_len); check_nc_status(status, "Error getting attribute length for %s:%s in %s", var_name, - attr_name, nc_nameid.nc_file); + attr_name, nc_nameid->nc_filename); // allocate memory for attribute *attr = malloc((attr_len + 1) * sizeof(**attr)); check_alloc_status(*attr, "Memory allocation error."); // read attribute text - status = nc_get_att_text(nc_nameid.nc_id, var_id, attr_name, *attr); + status = nc_get_att_text(nc_nameid->nc_id, var_id, attr_name, *attr); check_nc_status(status, "Error getting netCDF attribute %s for var %s in %s", attr_name, - var_name, nc_nameid.nc_file); + var_name, nc_nameid->nc_filename); // we need to null terminate the string ourselves according to NetCDF docs (*attr)[attr_len] = '\0'; diff --git a/vic/drivers/shared_image/src/get_nc_var_type.c b/vic/drivers/shared_image/src/get_nc_var_type.c index e3fdd0253..67dc4d5ad 100644 --- a/vic/drivers/shared_image/src/get_nc_var_type.c +++ b/vic/drivers/shared_image/src/get_nc_var_type.c @@ -30,22 +30,22 @@ * @brief Get netCDF variable type. *****************************************************************************/ int -get_nc_var_type(nameid_struct nc_nameid, - char *var_name) +get_nc_var_type(nameid_struct *nc_nameid, + char *var_name) { int var_id; int status; int xtypep; // get variable id - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); // get type ID - status = nc_inq_var(nc_nameid.nc_id, var_id, NULL, &xtypep, NULL, NULL, NULL); + status = nc_inq_var(nc_nameid->nc_id, var_id, NULL, &xtypep, NULL, NULL, NULL); check_nc_status(status, "Error getting variable type %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return(xtypep); } diff --git a/vic/drivers/shared_image/src/get_nc_varndimensions.c b/vic/drivers/shared_image/src/get_nc_varndimensions.c index c3a0ebe38..de2eb5959 100644 --- a/vic/drivers/shared_image/src/get_nc_varndimensions.c +++ b/vic/drivers/shared_image/src/get_nc_varndimensions.c @@ -30,7 +30,7 @@ * @brief Get netCDF dimension. *****************************************************************************/ int -get_nc_varndimensions(nameid_struct nc_nameid, +get_nc_varndimensions(nameid_struct *nc_nameid, char *var_name) { int var_id; @@ -38,16 +38,16 @@ get_nc_varndimensions(nameid_struct nc_nameid, int status; // get variable id - status = nc_inq_varid(nc_nameid.nc_id, var_name, &var_id); + status = nc_inq_varid(nc_nameid->nc_id, var_name, &var_id); check_nc_status(status, "Error getting variable id %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); // get number of dimensions - status = nc_inq_varndims(nc_nameid.nc_id, var_id, &ndims); + status = nc_inq_varndims(nc_nameid->nc_id, var_id, &ndims); check_nc_status(status, "Error getting number of dimensions for var %s in %s", var_name, - nc_nameid.nc_file); + nc_nameid->nc_filename); return ndims; } diff --git a/vic/drivers/shared_image/src/initialize_files.c b/vic/drivers/shared_image/src/initialize_files.c index 3357cb44c..6c4a201d3 100644 --- a/vic/drivers/shared_image/src/initialize_files.c +++ b/vic/drivers/shared_image/src/initialize_files.c @@ -38,10 +38,11 @@ initialize_filenames() size_t i; - strcpy(filenames.init_state.nc_file, "MISSING"); + strcpy(filenames.init_state.nc_filename, "MISSING"); strcpy(filenames.statefile, "MISSING"); strcpy(filenames.constants, "MISSING"); - strcpy(filenames.params.nc_file, "MISSING"); + strcpy(filenames.params.nc_filename, "MISSING"); + strcpy(filenames.domain.nc_filename, "MISSING"); strcpy(filenames.result_dir, "MISSING"); strcpy(filenames.log_path, "MISSING"); for (i = 0; i < 2; i++) { diff --git a/vic/drivers/shared_image/src/nc_utils.c b/vic/drivers/shared_image/src/nc_utils.c deleted file mode 100644 index 7cdcdc263..000000000 --- a/vic/drivers/shared_image/src/nc_utils.c +++ /dev/null @@ -1,58 +0,0 @@ -/****************************************************************************** - * @section DESCRIPTION - * - * Functions related to opening and closing netCDF files. - * - * @section LICENSE - * - * The Variable Infiltration Capacity (VIC) macroscale hydrological model - * Copyright (C) 2016 The Computational Hydrology Group, Department of Civil - * and Environmental Engineering, University of Washington. - * - * The VIC model is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - *****************************************************************************/ - -#include - -/****************************************************************************** - * @brief Open a netCDF file - *****************************************************************************/ -int -open_nc(char *nc_name) -{ - int nc_id; - int status; - - // open the netcdf file - status = nc_open(nc_name, NC_NOWRITE, &nc_id); - check_nc_status(status, "Error opening %s", nc_name); - - return(nc_id); -} - -/****************************************************************************** - * @brief Close a netCDF file - *****************************************************************************/ -int -close_nc(nameid_struct nc_nameid) -{ - int status; - - // close the netcdf file - status = nc_close(nc_nameid.nc_id); - check_nc_status(status, "Error closing %s", nc_nameid.nc_file); - - return(status); -} diff --git a/vic/drivers/shared_image/src/vic_image_timing.c b/vic/drivers/shared_image/src/vic_image_timing.c index 6289ddfeb..39cf8ff11 100644 --- a/vic/drivers/shared_image/src/vic_image_timing.c +++ b/vic/drivers/shared_image/src/vic_image_timing.c @@ -89,7 +89,7 @@ write_vic_timing_table(timer_struct *timers, fprintf(LOG_DEST, " VIC_DRIVER : %s\n", driver); fprintf(LOG_DEST, "\n"); fprintf(LOG_DEST, " Global Param File : %s\n", filenames.global); - fprintf(LOG_DEST, " Domain File : %s\n", filenames.domain.nc_file); + fprintf(LOG_DEST, " Domain File : %s\n", filenames.domain.nc_filename); fprintf(LOG_DEST, " Start Date : %04hu-%02hu-%02hu-%05u\n", global_param.startyear, global_param.startmonth, global_param.startday, global_param.startsec); diff --git a/vic/drivers/shared_image/src/vic_init.c b/vic/drivers/shared_image/src/vic_init.c index d9e2a978b..44fef6f5a 100644 --- a/vic/drivers/shared_image/src/vic_init.c +++ b/vic/drivers/shared_image/src/vic_init.c @@ -44,6 +44,7 @@ vic_init(void) extern veg_lib_struct **veg_lib; extern lake_con_struct *lake_con; extern parameters_struct param; + extern int mpi_rank; bool found; char locstr[MAXSTRING]; @@ -52,6 +53,7 @@ vic_init(void) double *Cv_sum = NULL; double *dvar = NULL; int *ivar = NULL; + int status; size_t i; size_t j; size_t k; @@ -123,7 +125,7 @@ vic_init(void) // overstory for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_int(filenames.params, "overstory", + get_scatter_nc_field_int(&(filenames.params), "overstory", d3start, d3count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].overstory = ivar[i]; @@ -133,7 +135,7 @@ vic_init(void) // rarc for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "rarc", + get_scatter_nc_field_double(&(filenames.params), "rarc", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].rarc = (double) dvar[i]; @@ -143,7 +145,7 @@ vic_init(void) // rmin for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "rmin", + get_scatter_nc_field_double(&(filenames.params), "rmin", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].rmin = (double) dvar[i]; @@ -153,7 +155,7 @@ vic_init(void) // wind height for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "wind_h", + get_scatter_nc_field_double(&(filenames.params), "wind_h", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].wind_h = (double) dvar[i]; @@ -163,7 +165,7 @@ vic_init(void) // RGL for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "RGL", + get_scatter_nc_field_double(&(filenames.params), "RGL", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].RGL = (double)dvar[i]; @@ -173,7 +175,7 @@ vic_init(void) // rad_atten for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "rad_atten", + get_scatter_nc_field_double(&(filenames.params), "rad_atten", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].rad_atten = (double) dvar[i]; @@ -183,7 +185,7 @@ vic_init(void) // wind_atten for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "wind_atten", + get_scatter_nc_field_double(&(filenames.params), "wind_atten", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].wind_atten = (double) dvar[i]; @@ -193,7 +195,7 @@ vic_init(void) // trunk_ratio for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "trunk_ratio", + get_scatter_nc_field_double(&(filenames.params), "trunk_ratio", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].trunk_ratio = (double) dvar[i]; @@ -206,7 +208,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < MONTHS_PER_YEAR; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "LAI", + get_scatter_nc_field_double(&(filenames.params), "LAI", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].LAI[k] = (double) dvar[i]; @@ -223,7 +225,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < MONTHS_PER_YEAR; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "albedo", + get_scatter_nc_field_double(&(filenames.params), "albedo", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].albedo[k] = (double) dvar[i]; @@ -237,7 +239,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < MONTHS_PER_YEAR; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "veg_rough", + get_scatter_nc_field_double(&(filenames.params), "veg_rough", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].roughness[k] = (double) dvar[i]; @@ -250,7 +252,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < MONTHS_PER_YEAR; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "displacement", + get_scatter_nc_field_double(&(filenames.params), "displacement", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].displacement[k] = (double) dvar[i]; @@ -278,7 +280,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < MONTHS_PER_YEAR; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "fcanopy", + get_scatter_nc_field_double(&(filenames.params), "fcanopy", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].fcanopy[k] = (double) dvar[i]; @@ -292,7 +294,7 @@ vic_init(void) // Ctype for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_int(filenames.params, "Ctype", + get_scatter_nc_field_int(&(filenames.params), "Ctype", d3start, d3count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].Ctype = ivar[i]; @@ -308,7 +310,7 @@ vic_init(void) // MaxCarboxRate for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "MaxCarboxRate", + get_scatter_nc_field_double(&(filenames.params), "MaxCarboxRate", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].MaxCarboxRate = (double) dvar[i]; @@ -322,7 +324,7 @@ vic_init(void) // MaxETransport or CO2Specificity for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "MaxiE_or_CO2Spec", + get_scatter_nc_field_double(&(filenames.params), "MaxiE_or_CO2Spec", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { if (dvar[i] < 0) { @@ -342,7 +344,7 @@ vic_init(void) // LightUseEff for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "LUE", + get_scatter_nc_field_double(&(filenames.params), "LUE", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].LightUseEff = (double) dvar[i]; @@ -357,7 +359,7 @@ vic_init(void) // Nscale flag for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_int(filenames.params, "Nscale", + get_scatter_nc_field_int(&(filenames.params), "Nscale", d3start, d3count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].NscaleFlag = ivar[i]; @@ -372,7 +374,7 @@ vic_init(void) // Wnpp_inhib for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Wnpp_inhib", + get_scatter_nc_field_double(&(filenames.params), "Wnpp_inhib", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].Wnpp_inhib = (double) dvar[i]; @@ -387,7 +389,7 @@ vic_init(void) // NPPfactor_sat for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "NPPfactor_sat", + get_scatter_nc_field_double(&(filenames.params), "NPPfactor_sat", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_lib[i][j].NPPfactor_sat = (double) dvar[i]; @@ -429,35 +431,35 @@ vic_init(void) } // b_infilt - get_scatter_nc_field_double(filenames.params, "infilt", + get_scatter_nc_field_double(&(filenames.params), "infilt", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].b_infilt = (double) dvar[i]; } // Ds - get_scatter_nc_field_double(filenames.params, "Ds", + get_scatter_nc_field_double(&(filenames.params), "Ds", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Ds = (double) dvar[i]; } // Dsmax - get_scatter_nc_field_double(filenames.params, "Dsmax", + get_scatter_nc_field_double(&(filenames.params), "Dsmax", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Dsmax = (double) dvar[i]; } // Ws - get_scatter_nc_field_double(filenames.params, "Ws", + get_scatter_nc_field_double(&(filenames.params), "Ws", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Ws = (double) dvar[i]; } // c - get_scatter_nc_field_double(filenames.params, "c", + get_scatter_nc_field_double(&(filenames.params), "c", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].c = (double) dvar[i]; @@ -466,7 +468,7 @@ vic_init(void) // expt: unsaturated hydraulic conductivity exponent for each layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "expt", + get_scatter_nc_field_double(&(filenames.params), "expt", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].expt[j] = (double) dvar[i]; @@ -476,7 +478,7 @@ vic_init(void) // Ksat: saturated hydraulic conductivity for each layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Ksat", + get_scatter_nc_field_double(&(filenames.params), "Ksat", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Ksat[j] = (double) dvar[i]; @@ -486,7 +488,7 @@ vic_init(void) // init_moist: initial soil moisture for cold start for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "init_moist", + get_scatter_nc_field_double(&(filenames.params), "init_moist", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].init_moist[j] = (double) dvar[i]; @@ -496,7 +498,7 @@ vic_init(void) // phi_s for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "phi_s", + get_scatter_nc_field_double(&(filenames.params), "phi_s", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].phi_s[j] = (double) dvar[i]; @@ -504,7 +506,7 @@ vic_init(void) } // elevation: mean grid cell elevation - get_scatter_nc_field_double(filenames.params, "elev", + get_scatter_nc_field_double(&(filenames.params), "elev", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].elevation = (double) dvar[i]; @@ -513,7 +515,7 @@ vic_init(void) // depth: thickness for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "depth", + get_scatter_nc_field_double(&(filenames.params), "depth", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].depth[j] = (double) dvar[i]; @@ -521,14 +523,14 @@ vic_init(void) } // avg_temp: mean grid temperature - get_scatter_nc_field_double(filenames.params, "avg_T", + get_scatter_nc_field_double(&(filenames.params), "avg_T", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].avg_temp = (double) dvar[i]; } // dp: damping depth - get_scatter_nc_field_double(filenames.params, "dp", + get_scatter_nc_field_double(&(filenames.params), "dp", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].dp = (double) dvar[i]; @@ -537,7 +539,7 @@ vic_init(void) // bubble: bubbling pressure for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "bubble", + get_scatter_nc_field_double(&(filenames.params), "bubble", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].bubble[j] = (double) dvar[i]; @@ -547,7 +549,7 @@ vic_init(void) // quartz: quartz content for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "quartz", + get_scatter_nc_field_double(&(filenames.params), "quartz", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].quartz[j] = (double) dvar[i]; @@ -557,7 +559,7 @@ vic_init(void) // bulk_dens_min: mineral bulk density for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "bulk_density", + get_scatter_nc_field_double(&(filenames.params), "bulk_density", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].bulk_dens_min[j] = (double) dvar[i]; @@ -567,7 +569,7 @@ vic_init(void) // soil_dens_min: mineral soil density for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "soil_density", + get_scatter_nc_field_double(&(filenames.params), "soil_density", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].soil_dens_min[j] = (double) dvar[i]; @@ -580,7 +582,7 @@ vic_init(void) // organic for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "organic", + get_scatter_nc_field_double(&(filenames.params), "organic", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].organic[j] = (double) dvar[i]; @@ -590,7 +592,7 @@ vic_init(void) // bulk_dens_org: organic bulk density for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "bulk_density_org", + get_scatter_nc_field_double(&(filenames.params), "bulk_density_org", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].bulk_dens_org[j] = (double) dvar[i]; @@ -600,7 +602,7 @@ vic_init(void) // soil_dens_org: organic soil density for each soil layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "soil_density_org", + get_scatter_nc_field_double(&(filenames.params), "soil_density_org", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].soil_dens_org[j] = (double) dvar[i]; @@ -612,7 +614,7 @@ vic_init(void) // Note this value is multiplied with the maximum moisture in each layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Wcr_FRACT", + get_scatter_nc_field_double(&(filenames.params), "Wcr_FRACT", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Wcr[j] = (double) dvar[i]; @@ -623,7 +625,7 @@ vic_init(void) // Note this value is multiplied with the maximum moisture in each layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Wpwp_FRACT", + get_scatter_nc_field_double(&(filenames.params), "Wpwp_FRACT", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Wpwp[j] = (double) dvar[i]; @@ -631,21 +633,21 @@ vic_init(void) } // rough: soil roughness - get_scatter_nc_field_double(filenames.params, "rough", + get_scatter_nc_field_double(&(filenames.params), "rough", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].rough = (double) dvar[i]; } // snow_rough: snow roughness - get_scatter_nc_field_double(filenames.params, "snow_rough", + get_scatter_nc_field_double(&(filenames.params), "snow_rough", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].snow_rough = (double) dvar[i]; } // annual_prec: annual precipitation - get_scatter_nc_field_double(filenames.params, "annual_prec", + get_scatter_nc_field_double(&(filenames.params), "annual_prec", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].annual_prec = (double) dvar[i]; @@ -654,7 +656,7 @@ vic_init(void) // resid_moist: residual moisture content for each layer for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "resid_moist", + get_scatter_nc_field_double(&(filenames.params), "resid_moist", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].resid_moist[j] = (double) dvar[i]; @@ -662,7 +664,7 @@ vic_init(void) } // fs_active: frozen soil active flag - get_scatter_nc_field_int(filenames.params, "fs_active", + get_scatter_nc_field_int(&(filenames.params), "fs_active", d2start, d2count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].FS_ACTIVE = (char) ivar[i]; @@ -671,7 +673,7 @@ vic_init(void) // spatial snow if (options.SPATIAL_SNOW) { // max_snow_distrib_slope - get_scatter_nc_field_double(filenames.params, "max_snow_distrib_slope", + get_scatter_nc_field_double(&(filenames.params), "max_snow_distrib_slope", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].max_snow_distrib_slope = (double) dvar[i]; @@ -681,7 +683,7 @@ vic_init(void) // spatial frost if (options.SPATIAL_FROST) { // frost_slope: slope of frozen soil distribution - get_scatter_nc_field_double(filenames.params, "frost_slope", + get_scatter_nc_field_double(&(filenames.params), "frost_slope", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].frost_slope = (double) dvar[i]; @@ -948,7 +950,7 @@ vic_init(void) // AreaFract: fraction of grid cell in each snow band for (j = 0; j < options.SNOW_BAND; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "AreaFract", + get_scatter_nc_field_double(&(filenames.params), "AreaFract", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].AreaFract[j] = (double) dvar[i]; @@ -957,7 +959,7 @@ vic_init(void) // elevation: elevation of each snow band for (j = 0; j < options.SNOW_BAND; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "elevation", + get_scatter_nc_field_double(&(filenames.params), "elevation", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].BandElev[j] = (double) dvar[i]; @@ -966,7 +968,7 @@ vic_init(void) // Pfactor: precipitation multiplier for each snow band for (j = 0; j < options.SNOW_BAND; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Pfactor", + get_scatter_nc_field_double(&(filenames.params), "Pfactor", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { soil_con[i].Pfactor[j] = (double) dvar[i]; @@ -1085,7 +1087,7 @@ vic_init(void) for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "Cv", + get_scatter_nc_field_double(&(filenames.params), "Cv", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { veg_con_map[i].Cv[j] = (double) dvar[i]; @@ -1135,7 +1137,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < options.ROOT_ZONES; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "root_depth", + get_scatter_nc_field_double(&(filenames.params), "root_depth", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[j]; @@ -1151,7 +1153,7 @@ vic_init(void) d4start[0] = j; for (k = 0; k < options.ROOT_ZONES; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.params, "root_fract", + get_scatter_nc_field_double(&(filenames.params), "root_fract", d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[j]; @@ -1240,7 +1242,7 @@ vic_init(void) // sigma_slope for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "sigma_slope", + get_scatter_nc_field_double(&(filenames.params), "sigma_slope", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[j]; @@ -1257,7 +1259,7 @@ vic_init(void) // lag_one for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "lag_one", + get_scatter_nc_field_double(&(filenames.params), "lag_one", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[j]; @@ -1274,7 +1276,7 @@ vic_init(void) // fetch for (j = 0; j < options.NVEGTYPES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.params, "fetch", + get_scatter_nc_field_double(&(filenames.params), "fetch", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { vidx = veg_con_map[i].vidx[j]; @@ -1294,7 +1296,7 @@ vic_init(void) // read_lake parameters if (options.LAKES) { // lake_idx - get_scatter_nc_field_int(filenames.params, "lake_idx", + get_scatter_nc_field_int(&(filenames.params), "lake_idx", d2start, d2count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].lake_idx = ivar[i]; @@ -1311,7 +1313,7 @@ vic_init(void) } // numnod - get_scatter_nc_field_int(filenames.params, "numnod", + get_scatter_nc_field_int(&(filenames.params), "numnod", d2start, d2count, ivar); max_numnod = 0; for (i = 0; i < local_domain.ncells_active; i++) { @@ -1341,7 +1343,7 @@ vic_init(void) } // mindepth (minimum depth for which channel outflow occurs) - get_scatter_nc_field_double(filenames.params, "mindepth", + get_scatter_nc_field_double(&(filenames.params), "mindepth", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].mindepth = (double) dvar[i]; @@ -1361,7 +1363,7 @@ vic_init(void) } // wfrac - get_scatter_nc_field_double(filenames.params, "wfrac", + get_scatter_nc_field_double(&(filenames.params), "wfrac", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].wfrac = (double) dvar[i]; @@ -1380,7 +1382,7 @@ vic_init(void) } // depth_in (initial depth for a cold start) - get_scatter_nc_field_double(filenames.params, "depth_in", + get_scatter_nc_field_double(&(filenames.params), "depth_in", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].depth_in = (double) dvar[i]; @@ -1400,7 +1402,7 @@ vic_init(void) } // rpercent - get_scatter_nc_field_double(filenames.params, "rpercent", + get_scatter_nc_field_double(&(filenames.params), "rpercent", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].rpercent = (double) dvar[i]; @@ -1432,14 +1434,14 @@ vic_init(void) d3start[0] = j; // basin_depth - get_scatter_nc_field_double(filenames.params, "basin_depth", + get_scatter_nc_field_double(&(filenames.params), "basin_depth", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].z[j] = (double) dvar[i]; } // basin_area - get_scatter_nc_field_double(filenames.params, "basin_area", + get_scatter_nc_field_double(&(filenames.params), "basin_area", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].Cl[j] = (double) dvar[i]; @@ -1448,14 +1450,14 @@ vic_init(void) } else { // basin_depth - get_scatter_nc_field_double(filenames.params, "basin_depth", + get_scatter_nc_field_double(&(filenames.params), "basin_depth", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].z[0] = (double) dvar[i]; } // basin_area - get_scatter_nc_field_double(filenames.params, "basin_area", + get_scatter_nc_field_double(&(filenames.params), "basin_area", d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { lake_con[i].Cl[0] = (double) dvar[i]; @@ -1555,6 +1557,13 @@ vic_init(void) // set state metadata structure set_state_meta_data_info(); + // close parameter file + if (mpi_rank == VIC_MPI_ROOT) { + status = nc_close(filenames.params.nc_id); + check_nc_status(status, "Error closing %s", + filenames.params.nc_filename); + } + // cleanup free(dvar); free(ivar); diff --git a/vic/drivers/shared_image/src/vic_mpi_support.c b/vic/drivers/shared_image/src/vic_mpi_support.c index 7c60b63e2..294131758 100644 --- a/vic/drivers/shared_image/src/vic_mpi_support.c +++ b/vic/drivers/shared_image/src/vic_mpi_support.c @@ -2042,7 +2042,7 @@ gather_put_nc_field_schar(int nc_id, * nodes *****************************************************************************/ void -get_scatter_nc_field_double(nameid_struct nc_nameid, +get_scatter_nc_field_double(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, @@ -2103,7 +2103,7 @@ get_scatter_nc_field_double(nameid_struct nc_nameid, * nodes *****************************************************************************/ void -get_scatter_nc_field_float(nameid_struct nc_nameid, +get_scatter_nc_field_float(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, @@ -2166,7 +2166,7 @@ get_scatter_nc_field_float(nameid_struct nc_nameid, * nodes *****************************************************************************/ void -get_scatter_nc_field_int(nameid_struct nc_nameid, +get_scatter_nc_field_int(nameid_struct *nc_nameid, char *var_name, size_t *start, size_t *count, diff --git a/vic/drivers/shared_image/src/vic_restore.c b/vic/drivers/shared_image/src/vic_restore.c index 2293eae2f..46abea478 100644 --- a/vic/drivers/shared_image/src/vic_restore.c +++ b/vic/drivers/shared_image/src/vic_restore.c @@ -48,6 +48,7 @@ vic_restore(void) size_t m; size_t p; int *ivar = NULL; + int status; double *dvar = NULL; size_t d2count[2]; size_t d2start[2]; @@ -62,7 +63,10 @@ vic_restore(void) if (mpi_rank == VIC_MPI_ROOT) { // open initial state file - filenames.init_state.nc_id = open_nc(filenames.init_state.nc_file); + status = nc_open(filenames.init_state.nc_filename, NC_NOWRITE, + &(filenames.init_state.nc_id)); + check_nc_status(status, "Error opening %s", + filenames.init_state.nc_filename); } // validate state file dimensions and coordinate variables @@ -129,7 +133,7 @@ vic_restore(void) d5start[1] = k; for (j = 0; j < options.Nlayer; j++) { d5start[2] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SOIL_MOISTURE].varname, d5start, d5count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -151,7 +155,7 @@ vic_restore(void) d6start[2] = j; for (p = 0; p < options.Nfrost; p++) { d6start[3] = p; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SOIL_ICE].varname, d6start, d6count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -170,7 +174,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_CANOPY_WATER].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -188,7 +192,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_ANNUALNPP].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -205,7 +209,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_ANNUALNPPPREV].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -222,7 +226,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_CLITTER].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -239,7 +243,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_CINTER].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -256,7 +260,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_CSLOW].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -274,7 +278,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_int(filenames.init_state, + get_scatter_nc_field_int(&(filenames.init_state), state_metadata[STATE_SNOW_AGE].varname, d4start, d4count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -291,7 +295,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_int(filenames.init_state, + get_scatter_nc_field_int(&(filenames.init_state), state_metadata[STATE_SNOW_MELT_STATE].varname, d4start, d4count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -308,7 +312,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_COVERAGE].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -325,7 +329,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_SNOW_WATER_EQUIVALENT].varname, d4start, d4count, dvar); @@ -343,7 +347,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_SURF_TEMP].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -360,7 +364,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_SURF_WATER].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -377,7 +381,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_PACK_TEMP].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -394,7 +398,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_PACK_WATER].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -411,7 +415,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_DENSITY].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -428,7 +432,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_COLD_CONTENT].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -445,7 +449,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SNOW_CANOPY].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -464,7 +468,7 @@ vic_restore(void) d5start[1] = k; for (j = 0; j < options.Nnode; j++) { d5start[2] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_SOIL_NODE_TEMP].varname, d5start, d5count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -482,7 +486,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_FOLIAGE_TEMPERATURE].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -500,7 +504,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_ENERGY_LONGUNDEROUT].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -518,7 +522,7 @@ vic_restore(void) d4start[0] = m; for (k = 0; k < options.SNOW_BAND; k++) { d4start[1] = k; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_ENERGY_SNOW_FLUX].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -534,7 +538,7 @@ vic_restore(void) // total soil moisture for (j = 0; j < options.Nlayer; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SOIL_MOISTURE].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -547,7 +551,7 @@ vic_restore(void) d4start[0] = j; for (p = 0; p < options.Nfrost; p++) { d4start[1] = p; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SOIL_ICE].varname, d4start, d4count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -558,7 +562,7 @@ vic_restore(void) if (options.CARBON) { // litter carbon: tmpval = lake_var.soil.CLitter; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_CLITTER].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -566,7 +570,7 @@ vic_restore(void) } // intermediate carbon: tmpval = lake_var.soil.CInter; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_CINTER].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -574,7 +578,7 @@ vic_restore(void) } // slow carbon: tmpval = lake_var.soil.CSlow; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_CSLOW].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -583,7 +587,7 @@ vic_restore(void) } // snow age: lake_var.snow.last_snow - get_scatter_nc_field_int(filenames.init_state, + get_scatter_nc_field_int(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_AGE].varname, d2start, d2count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -591,7 +595,7 @@ vic_restore(void) } // melting state: (int)lake_var.snow.MELTING - get_scatter_nc_field_int(filenames.init_state, + get_scatter_nc_field_int(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_MELT_STATE].varname, d2start, d2count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -599,7 +603,7 @@ vic_restore(void) } // snow covered fraction: lake_var.snow.coverage - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_COVERAGE].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -607,7 +611,7 @@ vic_restore(void) } // snow water equivalent: lake_var.snow.swq - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_SNOW_WATER_EQUIVALENT].varname, d2start, d2count, dvar); @@ -616,7 +620,7 @@ vic_restore(void) } // snow surface temperature: lake_var.snow.surf_temp - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_SURF_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -624,7 +628,7 @@ vic_restore(void) } // snow surface water: lake_var.snow.surf_water - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_SURF_WATER].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -632,7 +636,7 @@ vic_restore(void) } // snow pack temperature: lake_var.snow.pack_temp - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_PACK_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -640,7 +644,7 @@ vic_restore(void) } // snow pack water: lake_var.snow.pack_water - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_PACK_WATER].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -648,7 +652,7 @@ vic_restore(void) } // snow density: lake_var.snow.density - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_SURF_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -656,7 +660,7 @@ vic_restore(void) } // snow cold content: lake_var.snow.coldcontent - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_COLD_CONTENT].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -664,7 +668,7 @@ vic_restore(void) } // snow canopy storage: lake_var.snow.snow_canopy - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SNOW_CANOPY].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -674,7 +678,7 @@ vic_restore(void) // soil node temperatures: lake_var.energy.T[nidx] for (j = 0; j < options.Nnode; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SOIL_NODE_TEMP].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -683,7 +687,7 @@ vic_restore(void) } // lake active layers: lake_var.activenod - get_scatter_nc_field_int(filenames.init_state, + get_scatter_nc_field_int(&(filenames.init_state), state_metadata[STATE_LAKE_ACTIVE_LAYERS].varname, d2start, d2count, ivar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -691,7 +695,7 @@ vic_restore(void) } // lake layer thickness: lake_var.dz - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_LAYER_DZ].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -699,7 +703,7 @@ vic_restore(void) } // lake surface layer thickness: lake_var.surfdz - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SURF_LAYER_DZ].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -707,7 +711,7 @@ vic_restore(void) } // lake depth: lake_var.ldepth - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_DEPTH].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -717,7 +721,7 @@ vic_restore(void) // lake layer surface areas: lake_var.surface[ndix] for (j = 0; j < options.NLAKENODES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_LAYER_SURF_AREA].varname, d3start, d3count, dvar); @@ -727,7 +731,7 @@ vic_restore(void) } // lake surface area: lake_var.sarea - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_SURF_AREA].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -735,7 +739,7 @@ vic_restore(void) } // lake volume: lake_var.volume - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_VOLUME].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -745,7 +749,7 @@ vic_restore(void) // lake layer temperatures: lake_var.temp[nidx] for (j = 0; j < options.NLAKENODES; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_LAYER_TEMP].varname, d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -754,7 +758,7 @@ vic_restore(void) } // vertical average lake temperature: lake_var.tempavg - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_AVERAGE_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -762,7 +766,7 @@ vic_restore(void) } // lake ice area fraction: lake_var.areai - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_AREA_FRAC].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -770,7 +774,7 @@ vic_restore(void) } // new lake ice area fraction: lake_var.new_ice_area - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_AREA_FRAC_NEW].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -778,7 +782,7 @@ vic_restore(void) } // lake ice water equivalent: lake_var.ice_water_eq - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_ICE_WATER_EQUIVALENT].varname, d2start, d2count, dvar); @@ -787,7 +791,7 @@ vic_restore(void) } // lake ice height: lake_var.hice - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_HEIGHT].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -795,7 +799,7 @@ vic_restore(void) } // lake ice temperature: lake_var.tempi - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -803,7 +807,7 @@ vic_restore(void) } // lake ice snow water equivalent: lake_var.swe - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_SWE].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -811,7 +815,7 @@ vic_restore(void) } // lake ice snow surface temperature: lake_var.surf_temp - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_SNOW_SURF_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -819,7 +823,7 @@ vic_restore(void) } // lake ice snow pack temperature: lake_var.pack_temp - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_SNOW_PACK_TEMP].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -827,7 +831,7 @@ vic_restore(void) } // lake ice snow coldcontent: lake_var.coldcontent - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_ICE_SNOW_COLD_CONTENT].varname, d2start, d2count, dvar); @@ -836,7 +840,7 @@ vic_restore(void) } // lake ice snow surface water: lake_var.surf_water - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_ICE_SNOW_SURF_WATER].varname, d2start, d2count, dvar); @@ -845,7 +849,7 @@ vic_restore(void) } // lake ice snow pack water: lake_var.pack_water - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[ STATE_LAKE_ICE_SNOW_PACK_WATER].varname, d2start, d2count, dvar); @@ -854,7 +858,7 @@ vic_restore(void) } // lake ice snow albedo: lake_var.SAlbedo - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_SNOW_ALBEDO].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -862,7 +866,7 @@ vic_restore(void) } // lake ice snow depth: lake_var.sdepth - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), state_metadata[STATE_LAKE_ICE_SNOW_DEPTH].varname, d2start, d2count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -875,7 +879,9 @@ vic_restore(void) // close initial state file if (mpi_rank == VIC_MPI_ROOT) { - close_nc(filenames.init_state); + status = nc_close(filenames.init_state.nc_id); + check_nc_status(status, "Error closing %s", + filenames.init_state.nc_filename); } } @@ -911,43 +917,43 @@ check_init_state_file(void) // read and validate dimension lengths if (mpi_rank == VIC_MPI_ROOT) { - dimlen = get_nc_dimension(filenames.init_state, global_domain.info.x_dim); + dimlen = get_nc_dimension(&(filenames.init_state), global_domain.info.x_dim); if (dimlen != global_domain.n_nx) { log_err("Number of grid columns in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, global_domain.info.y_dim); + dimlen = get_nc_dimension(&(filenames.init_state), global_domain.info.y_dim); if (dimlen != global_domain.n_ny) { log_err("Number of grid rows in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, "veg_class"); + dimlen = get_nc_dimension(&(filenames.init_state), "veg_class"); if (dimlen != options.NVEGTYPES) { log_err("Number of veg classes in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, "snow_band"); + dimlen = get_nc_dimension(&(filenames.init_state), "snow_band"); if (dimlen != options.SNOW_BAND) { log_err("Number of snow bands in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, "nlayer"); + dimlen = get_nc_dimension(&(filenames.init_state), "nlayer"); if (dimlen != options.Nlayer) { log_err("Number of soil layers in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, "frost_area"); + dimlen = get_nc_dimension(&(filenames.init_state), "frost_area"); if (dimlen != options.Nfrost) { log_err("Number of frost areas in state file does not " "match parameter file"); } - dimlen = get_nc_dimension(filenames.init_state, "soil_node"); + dimlen = get_nc_dimension(&(filenames.init_state), "soil_node"); if (dimlen != options.Nnode) { log_err("Number of soil nodes in state file does not " "match parameter file"); } if (options.LAKES) { - dimlen = get_nc_dimension(filenames.init_state, "lake_node"); + dimlen = get_nc_dimension(&(filenames.init_state), "lake_node"); if (dimlen != options.NLAKENODES) { log_err("Number of lake nodes in state file does not " "match parameter file"); @@ -961,10 +967,10 @@ check_init_state_file(void) if (mpi_rank == VIC_MPI_ROOT) { status = nc_inq_varid(filenames.init_state.nc_id, global_domain.info.lon_var, &lon_var_id); check_nc_status(status, "Unable to find variable \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state.nc_file); + global_domain.info.lon_var, filenames.init_state.nc_filename); status = nc_inq_varid(filenames.init_state.nc_id, global_domain.info.lat_var, &lat_var_id); check_nc_status(status, "Unable to find variable \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state.nc_file); + global_domain.info.lat_var, filenames.init_state.nc_filename); if (global_domain.info.n_coord_dims == 1) { d1start[0] = 0; dvar = calloc(global_domain.n_nx, sizeof(*dvar)); @@ -974,7 +980,7 @@ check_init_state_file(void) status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id, d1start, d1count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state.nc_file); + global_domain.info.lon_var, filenames.init_state.nc_filename); // implicitly nested loop over ni and nj with j set to 0 for (i = 0; i < global_domain.n_nx; i++) { if (!assert_close_double(dvar[i], @@ -993,7 +999,7 @@ check_init_state_file(void) status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id, d1start, d1count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state.nc_file); + global_domain.info.lat_var, filenames.init_state.nc_filename); // implicitly nested loop over ni and nj with i set to 0; // j stride = n_nx for (j = 0; j < global_domain.n_ny; j++) { @@ -1019,7 +1025,7 @@ check_init_state_file(void) status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id, d2start, d2count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lon_var, filenames.init_state.nc_file); + global_domain.info.lon_var, filenames.init_state.nc_filename); for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) { if (dvar[i] != (double) global_domain.locations[i].longitude) { log_err("Longitudes in initial state file do not " @@ -1029,7 +1035,7 @@ check_init_state_file(void) status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id, d2start, d2count, dvar); check_nc_status(status, "Error reading data from \"%s\" in %s", - global_domain.info.lat_var, filenames.init_state.nc_file); + global_domain.info.lat_var, filenames.init_state.nc_filename); for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) { if (dvar[i] != (double) global_domain.locations[i].latitude) { log_err("Latitudes in initial state file do not " @@ -1056,7 +1062,7 @@ check_init_state_file(void) d3count[2] = global_domain.n_nx; for (j = 0; j < options.Nnode; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), "dz_node", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { @@ -1076,7 +1082,7 @@ check_init_state_file(void) d3count[2] = global_domain.n_nx; for (j = 0; j < options.Nnode; j++) { d3start[0] = j; - get_scatter_nc_field_double(filenames.init_state, + get_scatter_nc_field_double(&(filenames.init_state), "node_depth", d3start, d3count, dvar); for (i = 0; i < local_domain.ncells_active; i++) { diff --git a/vic/drivers/shared_image/src/vic_start.c b/vic/drivers/shared_image/src/vic_start.c index 147647e90..8c36b216f 100644 --- a/vic/drivers/shared_image/src/vic_start.c +++ b/vic/drivers/shared_image/src/vic_start.c @@ -73,13 +73,27 @@ vic_start(void) fclose(filep.constants); } + // open parameter file + status = nc_open(filenames.params.nc_filename, NC_NOWRITE, + &(filenames.params.nc_id)); + check_nc_status(status, "Error opening %s", + filenames.params.nc_filename); + // open domain file + status = nc_open(filenames.domain.nc_filename, NC_NOWRITE, + &(filenames.domain.nc_id)); + check_nc_status(status, "Error opening %s", + filenames.domain.nc_filename); // read domain info - get_global_domain(filenames.domain, filenames.params, + get_global_domain(&(filenames.domain), &(filenames.params), &global_domain); + // close domain file + status = nc_close(filenames.domain.nc_id); + check_nc_status(status, "Error closing %s", + filenames.domain.nc_filename); // add the number of vegetation type to the location info in the // global domain struct. This just makes life easier - add_nveg_to_global_domain(filenames.params, &global_domain); + add_nveg_to_global_domain(&(filenames.params), &global_domain); // decompose the mask mpi_map_decomp_domain(global_domain.ncells_active, mpi_size, @@ -100,14 +114,14 @@ vic_start(void) } // get dimensions (number of vegetation types, soil zones, etc) - options.ROOT_ZONES = get_nc_dimension(filenames.params, "root_zone"); - options.Nlayer = get_nc_dimension(filenames.params, "nlayer"); - options.NVEGTYPES = get_nc_dimension(filenames.params, "veg_class"); + options.ROOT_ZONES = get_nc_dimension(&(filenames.params), "root_zone"); + options.Nlayer = get_nc_dimension(&(filenames.params), "nlayer"); + options.NVEGTYPES = get_nc_dimension(&(filenames.params), "veg_class"); if (options.SNOW_BAND == SNOW_BAND_TRUE_BUT_UNSET) { - options.SNOW_BAND = get_nc_dimension(filenames.params, "snow_band"); + options.SNOW_BAND = get_nc_dimension(&(filenames.params), "snow_band"); } if (options.LAKES) { - options.NLAKENODES = get_nc_dimension(filenames.params, + options.NLAKENODES = get_nc_dimension(&(filenames.params), "lake_node"); }