Skip to content

Commit

Permalink
Speedup image driver initialization - incorporate Bart's comments,
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
ymao committed Feb 22, 2017
1 parent 8e2019e commit 5cb7cba
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 344 deletions.
6 changes: 3 additions & 3 deletions vic/drivers/image/src/display_current_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down
33 changes: 20 additions & 13 deletions vic/drivers/image/src/get_global_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(&param_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(&param_set, 1);
}

Expand Down Expand Up @@ -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\".");
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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
Expand Down
72 changes: 46 additions & 26 deletions vic/drivers/image/src/vic_force.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
}

Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions vic/drivers/image/src/vic_image_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 0 additions & 11 deletions vic/drivers/image/src/vic_image_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit 5cb7cba

Please sign in to comment.