<NME> vic_restore.c
<BEF> /******************************************************************************
 * @section DESCRIPTION
 *
 * Read initial model state.
 *****************************************************************************/

#include <vic_driver_shared_image.h>
#include <rout.h>

/******************************************************************************
 * @brief    Read initial model state.
 *****************************************************************************/
void
vic_restore(void)
{
    extern int                 mpi_rank;
    extern all_vars_struct    *all_vars;
    extern domain_struct       global_domain;
    extern domain_struct       local_domain;
    extern option_struct       options;
    extern veg_con_map_struct *veg_con_map;
    extern filenames_struct    filenames;
    extern metadata_struct     state_metadata[N_STATE_VARS + N_STATE_VARS_EXT];
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *****************************************************************************/

#include <vic_driver_image.h>

/******************************************************************************
 * @brief    Read initial model state.
    int                       *ivar = NULL;
    int                        status;
    double                    *dvar = NULL;
    size_t                     d2count[2];
    size_t                     d2start[2];
    size_t                     d3count[3];
    size_t                     d3start[3];
    size_t                     d4count[4];
    size_t                     d4start[4];
    size_t                     d5count[5];
    size_t                     d5start[5];
    size_t                     d6count[6];
    size_t                     d6start[6];

    if (mpi_rank == VIC_MPI_ROOT) {
        // open initial state 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
    check_init_state_file();
    // read state variables

    // allocate memory for variables to be stored
    ivar = malloc(local_domain.ncells_active * sizeof(*ivar));
    check_alloc_status(ivar, "Memory allocation error");

    dvar = malloc(local_domain.ncells_active * sizeof(*dvar));
    check_alloc_status(dvar, "Memory allocation error");

    // initialize starts and counts
    d2start[0] = 0;
    d2start[1] = 0;
    d2count[0] = global_domain.n_ny;
    d2count[1] = global_domain.n_nx;

    d3start[0] = 0;
    d3start[1] = 0;
    d3start[2] = 0;
    d3count[0] = 1;
    d3count[1] = global_domain.n_ny;
    d3count[2] = global_domain.n_nx;

    d4start[0] = 0;
    d4start[1] = 0;
    d4start[2] = 0;
    d4start[3] = 0;
    d4count[0] = 1;
    d4count[1] = 1;
    d4count[2] = global_domain.n_ny;
    d4count[3] = global_domain.n_nx;

    d5start[0] = 0;
    d5start[1] = 0;
    d5start[2] = 0;
    d5start[3] = 0;
    d5start[4] = 0;
    d5count[0] = 1;
    d5count[1] = 1;
    d5count[2] = 1;
    d5count[3] = global_domain.n_ny;
    d5count[4] = global_domain.n_nx;

    d6start[0] = 0;
    d6start[1] = 0;
    d6start[2] = 0;
    d6start[3] = 0;
    d6start[4] = 0;
    d6start[5] = 0;
    d6count[0] = 1;
    d6count[1] = 1;
    d6count[2] = 1;
    d6count[3] = 1;
    d6count[4] = global_domain.n_ny;
    d6count[5] = global_domain.n_nx;

    // total soil moisture
    for (m = 0; m < options.NVEGTYPES; m++) {
        d5start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d5start[1] = k;
            for (j = 0; j < options.Nlayer; j++) {
                d5start[2] = j;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].cell[v][k].layer[j].moist = dvar[i];
                    }
                }
            }
        }
    }

    // ice content
    for (m = 0; m < options.NVEGTYPES; m++) {
        d6start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d6start[1] = k;
            for (j = 0; j < options.Nlayer; j++) {
                d6start[2] = j;
                for (p = 0; p < options.Nfrost; p++) {
                    d6start[3] = p;
                    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++) {
                        v = veg_con_map[i].vidx[m];
                        if (v >= 0) {
                            all_vars[i].cell[v][k].layer[j].ice[p] = dvar[i];
                        }
                    }
                }
            }
        }
    }

    // dew storage: tmpval = veg_var[veg][band].Wdew;
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].veg_var[v][k].Wdew = dvar[i];
                }
            }
        }
    }

    if (options.CARBON) {
        // cumulative NPP: tmpval = veg_var[veg][band].AnnualNPP;
        for (m = 0; m < options.NVEGTYPES; m++) {
            d4start[0] = m;
            for (k = 0; k < options.SNOW_BAND; k++) {
                d4start[1] = k;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].veg_var[v][k].AnnualNPP = dvar[i];
                    }
                }
            }
        }

        // previous NPP: tmpval = veg_var[veg][band].AnnualNPPPrev;
        for (m = 0; m < options.NVEGTYPES; m++) {
            d4start[0] = m;
            for (k = 0; k < options.SNOW_BAND; k++) {
                d4start[1] = k;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].veg_var[v][k].AnnualNPPPrev = dvar[i];
                    }
                }
            }
        }

        // litter carbon: tmpval = cell[veg][band].CLitter;
        for (m = 0; m < options.NVEGTYPES; m++) {
            d4start[0] = m;
            for (k = 0; k < options.SNOW_BAND; k++) {
                d4start[1] = k;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].cell[v][k].CLitter = dvar[i];
                    }
                }
            }
        }

        // intermediate carbon: tmpval = cell[veg][band].CInter;
        for (m = 0; m < options.NVEGTYPES; m++) {
            d4start[0] = m;
            for (k = 0; k < options.SNOW_BAND; k++) {
                d4start[1] = k;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].cell[v][k].CInter = dvar[i];
                    }
                }
            }
        }

        // slow carbon: tmpval = cell[veg][band].CSlow;
        for (m = 0; m < options.NVEGTYPES; m++) {
            d4start[0] = m;
            for (k = 0; k < options.SNOW_BAND; k++) {
                d4start[1] = k;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].cell[v][k].CSlow = dvar[i];
                    }
                }
            }
        }
    }

    // snow age: snow[veg][band].last_snow
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].last_snow = ivar[i];
                }
            }
        }
    }

    // melting state: (int)snow[veg][band].MELTING
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].MELTING = ivar[i];
                }
            }
        }
    }

    // snow covered fraction: snow[veg][band].coverage
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].coverage = dvar[i];
                }
            }
        }
    }

    // snow water equivalent: snow[veg][band].swq
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            get_scatter_nc_field_double(&(filenames.init_state),
                                        state_metadata[
                                            STATE_SNOW_WATER_EQUIVALENT].varname,
                                        d4start, d4count, dvar);
            for (i = 0; i < local_domain.ncells_active; i++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].swq = dvar[i];
                }
            }
        }
    }

    // snow surface temperature: snow[veg][band].surf_temp
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].surf_temp = dvar[i];
                }
            }
        }
    }

    // snow surface water: snow[veg][band].surf_water
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].surf_water = dvar[i];
                }
            }
        }
    }

    // snow pack temperature: snow[veg][band].pack_temp
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].pack_temp = dvar[i];
                }
            }
        }
    }

    // snow pack water: snow[veg][band].pack_water
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].pack_water = dvar[i];
                }
            }
        }
    }

    // snow density: snow[veg][band].density
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].density = dvar[i];
                }
            }
        }
    }

    // snow cold content: snow[veg][band].coldcontent
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].coldcontent = dvar[i];
                }
            }
        }
    }

    // snow canopy storage: snow[veg][band].snow_canopy
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].snow[v][k].snow_canopy = dvar[i];
                }
            }
        }
    }

    // grid cell-averaged albedo: gridcell_avg.avg_albedo
    get_scatter_nc_field_double(&(filenames.init_state),
                                state_metadata[STATE_AVG_ALBEDO].varname,
                                d2start, d2count, dvar);
    for (i = 0; i < local_domain.ncells_active; i++) {
        all_vars[i].gridcell_avg.avg_albedo = dvar[i];
    }

    // soil node temperatures: energy[veg][band].T[nidx]
    for (m = 0; m < options.NVEGTYPES; m++) {
        d5start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d5start[1] = k;
            for (j = 0; j < options.Nnode; j++) {
                d5start[2] = j;
                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++) {
                    v = veg_con_map[i].vidx[m];
                    if (v >= 0) {
                        all_vars[i].energy[v][k].T[j] = dvar[i];
                    }
                }
            }
        }
    }

    // Foliage temperature: energy[veg][band].Tfoliage
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].energy[v][k].Tfoliage = dvar[i];
                }
            }
        }
    }

    // Outgoing longwave from understory: energy[veg][band].LongUnderOut
    // This is a flux. Saving it to state file is a temporary solution!!
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].energy[v][k].LongUnderOut = dvar[i];
                }
            }
        }
    }

    // Thermal flux through the snow pack: energy[veg][band].snow_flux
    // This is a flux. Saving it to state file is a temporary solution!!
    for (m = 0; m < options.NVEGTYPES; m++) {
        d4start[0] = m;
        for (k = 0; k < options.SNOW_BAND; k++) {
            d4start[1] = k;
            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++) {
                v = veg_con_map[i].vidx[m];
                if (v >= 0) {
                    all_vars[i].energy[v][k].snow_flux = dvar[i];
                }
            }
        }
    }

    if (options.LAKES) {
        // total soil moisture
        for (j = 0; j < options.Nlayer; j++) {
            d3start[0] = j;
            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++) {
                all_vars[i].lake_var.soil.layer[j].moist = dvar[i];
            }
        }

        // ice content
        for (j = 0; j < options.Nlayer; j++) {
            d4start[0] = j;
            for (p = 0; p < options.Nfrost; p++) {
                d4start[1] = p;
                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++) {
                    all_vars[i].lake_var.soil.layer[j].ice[p] = dvar[i];
                }
            }
        }

        if (options.CARBON) {
            // litter carbon: tmpval = lake_var.soil.CLitter;
            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++) {
                all_vars[i].lake_var.soil.CLitter = dvar[i];
            }

            // intermediate carbon: tmpval = lake_var.soil.CInter;
            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++) {
                all_vars[i].lake_var.soil.CInter = dvar[i];
            }

            // slow carbon: tmpval = lake_var.soil.CSlow;
            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++) {
                all_vars[i].lake_var.soil.CSlow = dvar[i];
            }
        }

        // snow age: lake_var.snow.last_snow
        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++) {
            all_vars[i].lake_var.snow.last_snow = ivar[i];
        }

        // melting state: (int)lake_var.snow.MELTING
        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++) {
            all_vars[i].lake_var.snow.MELTING = ivar[i];
        }

        // snow covered fraction: lake_var.snow.coverage
        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++) {
            all_vars[i].lake_var.snow.coverage = dvar[i];
        }

        // snow water equivalent: lake_var.snow.swq
        get_scatter_nc_field_double(&(filenames.init_state),
                                    state_metadata[
                                        STATE_LAKE_SNOW_WATER_EQUIVALENT].varname,
                                    d2start, d2count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            all_vars[i].lake_var.snow.swq = dvar[i];
        }

        // snow surface temperature: lake_var.snow.surf_temp
        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++) {
            all_vars[i].lake_var.snow.surf_temp = dvar[i];
        }

        // snow surface water: lake_var.snow.surf_water
        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++) {
            all_vars[i].lake_var.snow.surf_water = dvar[i];
        }

        // snow pack temperature: lake_var.snow.pack_temp
        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++) {
            all_vars[i].lake_var.snow.pack_temp = dvar[i];
        }

        // snow pack water: lake_var.snow.pack_water
        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++) {
            all_vars[i].lake_var.snow.pack_water = dvar[i];
        }

        // snow density: lake_var.snow.density
        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++) {
            all_vars[i].lake_var.snow.density = dvar[i];
        }

        // snow cold content: lake_var.snow.coldcontent
        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++) {
            all_vars[i].lake_var.snow.coldcontent = dvar[i];
        }

        // snow canopy storage: lake_var.snow.snow_canopy
        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++) {
            all_vars[i].lake_var.snow.snow_canopy = dvar[i];
        }

        // 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),
                                        state_metadata[STATE_LAKE_SOIL_NODE_TEMP].varname,
                                        d3start, d3count, dvar);
            for (i = 0; i < local_domain.ncells_active; i++) {
                all_vars[i].lake_var.soil.layer[j].moist = dvar[i];
            }
        }

        // lake active layers: lake_var.activenod
        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++) {
            all_vars[i].lake_var.activenod = ivar[i];
        }

        // lake layer thickness: lake_var.dz
        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++) {
            all_vars[i].lake_var.dz = dvar[i];
        }

        // lake surface layer thickness: lake_var.surfdz
        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++) {
            all_vars[i].lake_var.surfdz = dvar[i];
        }

        // lake depth: lake_var.ldepth
        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++) {
            all_vars[i].lake_var.ldepth = dvar[i];
        }

        // lake layer surface areas: lake_var.surface[ndix]
        for (j = 0; j < options.Nlakenode; j++) {
            d3start[0] = j;
            get_scatter_nc_field_double(&(filenames.init_state),
                                        state_metadata[
                                            STATE_LAKE_LAYER_SURF_AREA].varname,
                                        d3start, d3count, dvar);
            for (i = 0; i < local_domain.ncells_active; i++) {
                all_vars[i].lake_var.surface[j] = dvar[i];
            }
        }

        // lake surface area: lake_var.sarea
        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++) {
            all_vars[i].lake_var.sarea = dvar[i];
        }

        // lake volume: lake_var.volume
        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++) {
            all_vars[i].lake_var.volume = dvar[i];
        }

        // lake layer temperatures: lake_var.temp[nidx]
        for (j = 0; j < options.Nlakenode; j++) {
            d3start[0] = j;
            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++) {
                all_vars[i].lake_var.temp[j] = dvar[i];
            }
        }

        // vertical average lake temperature: lake_var.tempavg
        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++) {
            all_vars[i].lake_var.tempavg = dvar[i];
        }

        // lake ice area fraction: lake_var.areai
        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++) {
            all_vars[i].lake_var.areai = dvar[i];
        }

        // new lake ice area fraction: lake_var.new_ice_area
        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++) {
            all_vars[i].lake_var.new_ice_area = dvar[i];
        }

        // lake ice water equivalent: lake_var.ice_water_eq
        get_scatter_nc_field_double(&(filenames.init_state),
                                    state_metadata[
                                        STATE_LAKE_ICE_WATER_EQUIVALENT].varname,
                                    d2start, d2count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            all_vars[i].lake_var.ice_water_eq = dvar[i];
        }

        // lake ice height: lake_var.hice
        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++) {
            all_vars[i].lake_var.hice = dvar[i];
        }

        // lake ice temperature: lake_var.tempi
        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++) {
            all_vars[i].lake_var.tempi = dvar[i];
        }

        // lake ice snow water equivalent: lake_var.swe
        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++) {
            all_vars[i].lake_var.swe = dvar[i];
        }

        // lake ice snow surface temperature: lake_var.surf_temp
        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++) {
            all_vars[i].lake_var.surf_temp = dvar[i];
        }

        // lake ice snow pack temperature: lake_var.pack_temp
        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++) {
            all_vars[i].lake_var.pack_temp = dvar[i];
        }

        // lake ice snow coldcontent: lake_var.coldcontent
        get_scatter_nc_field_double(&(filenames.init_state),
                                    state_metadata[
                                        STATE_LAKE_ICE_SNOW_COLD_CONTENT].varname,
                                    d2start, d2count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            all_vars[i].lake_var.coldcontent = dvar[i];
        }

        // lake ice snow surface water: lake_var.surf_water
        get_scatter_nc_field_double(&(filenames.init_state),
                                    state_metadata[
                                        STATE_LAKE_ICE_SNOW_SURF_WATER].varname,
                                    d2start, d2count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            all_vars[i].lake_var.surf_water = dvar[i];
        }

        // lake ice snow pack water: lake_var.pack_water
        get_scatter_nc_field_double(&(filenames.init_state),
                                    state_metadata[
                                        STATE_LAKE_ICE_SNOW_PACK_WATER].varname,
                                    d2start, d2count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            all_vars[i].lake_var.pack_water = dvar[i];
        }

        // lake ice snow albedo: lake_var.SAlbedo
        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++) {
            all_vars[i].lake_var.SAlbedo = dvar[i];
        }

        // lake ice snow depth: lake_var.sdepth
        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++) {
            all_vars[i].lake_var.sdepth = dvar[i];
        }
    }

    // routing ring
    vic_restore_rout_extension(&(filenames.init_state), state_metadata);

    free(ivar);
    free(dvar);

    // close initial state file
    if (mpi_rank == VIC_MPI_ROOT) {
        status = nc_close(filenames.init_state.nc_id);
        check_nc_status(status, "Error closing %s",
                        filenames.init_state.nc_filename);
    }
}

/******************************************************************************
 * @brief    Check that the initial state file matches the global parameter
             settings
 *****************************************************************************/
void
check_init_state_file(void)
{
    extern filenames_struct filenames;
    extern domain_struct    global_domain;
    extern domain_struct    local_domain;
    extern option_struct    options;
    extern soil_con_struct *soil_con;
    extern int              mpi_rank;

    int                     status;
    size_t                  dimlen;
    size_t                  i;
    size_t                  j;
    size_t                  d1count[1];
    size_t                  d1start[1];
    size_t                  d2count[2];
    size_t                  d2start[2];
    size_t                  d3count[3];
    size_t                  d3start[3];
    int                     lon_var_id;
    int                     lat_var_id;
    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

    // read and validate dimension lengths
    if (mpi_rank == VIC_MPI_ROOT) {
        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);
        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");
        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");
        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");
        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");
        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");
        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");
            if (dimlen != options.Nlakenode) {
                log_err("Number of lake nodes in state file does not "
                        "match LAKE_NODES (%zu) in global parameter file",
                        options.Nlakenode);
            }
        }
    }

    // read dimension variables

    // lat/lon
    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_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_filename);
        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(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_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],
                                         global_domain.locations[i].longitude,
                                         rtol,
                                         abs_tol)) {
                    log_err("Longitudes in initial state file do not "
                            "match parameter file");
                }
            }
            free(dvar);

            dvar = calloc(global_domain.n_ny, sizeof(*dvar));
            check_alloc_status(dvar, "Memory allocation error");

            d1count[0] = global_domain.n_ny;
            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_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++) {
                if (!assert_close_double(dvar[j],
                                         global_domain.locations[j *
                                                                 global_domain.
                                                                 n_nx]
                                         .latitude, rtol,
                                         abs_tol)) {
                    log_err("Latitudes in initial state file do not "
                            "match parameter file");
                }
            }
            free(dvar);
        }
        else if (global_domain.info.n_coord_dims == 2) {
            d2start[0] = 0;
            d2start[1] = 0;
            dvar =
                calloc(global_domain.n_ny * global_domain.n_nx, sizeof(*dvar));
            check_alloc_status(dvar, "Memory allocation error");

            d2count[0] = global_domain.n_ny;
            d2count[1] = global_domain.n_nx;
            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_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 "
                            "match parameter file");
                }
            }
            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_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 "
                            "match parameter file");
                }
            }
            free(dvar);
        }
        else {
            log_err("global_domain.info.n_coord_dims should be 1 or 2");
        }
    }

    // initialize dvar for soil thermal node deltas and depths
    dvar = malloc(local_domain.ncells_active * sizeof(*dvar));
    check_alloc_status(dvar, "Memory allocation error");

    // soil thermal node deltas (dimension: node, lat, lon)
    d3start[0] = 0;
    d3start[1] = 0;
    d3start[2] = 0;
    d3count[0] = 1;
    d3count[1] = global_domain.n_ny;
    d3count[2] = global_domain.n_nx;
    for (j = 0; j < options.Nnode; j++) {
        d3start[0] = j;
        get_scatter_nc_field_double(&(filenames.init_state),
                                    "dz_node",
                                    d3start, d3count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            if (dvar[i] != soil_con[i].dz_node[j]) {
                log_err("Soil node intervals in state file do not match "
                        "those computed by VIC");
            }
        }
    }

    // soil thermal node depths
    d3start[0] = 0;
    d3start[1] = 0;
    d3start[2] = 0;
    d3count[0] = 1;
    d3count[1] = global_domain.n_ny;
    d3count[2] = global_domain.n_nx;
    for (j = 0; j < options.Nnode; j++) {
        d3start[0] = j;
        get_scatter_nc_field_double(&(filenames.init_state),
                                    "node_depth",
                                    d3start, d3count, dvar);
        for (i = 0; i < local_domain.ncells_active; i++) {
            if (dvar[i] != soil_con[i].Zsum_node[j]) {
                log_err("Soil node depths in state file do not match "
                        "those computed by VIC");
            }
        }
    }
    free(dvar);
}

<MSG> Fixed typo in vic_restore.c.

<DFF> @@ -24,7 +24,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *****************************************************************************/
 
-#include <vic_driver_image.h>
+#include <vic_driver_shared_image.h>
 
 /******************************************************************************
  * @brief    Read initial model state.

{'commit': 'diff --git a/vic/drivers/shared_image/src/vic_restore.c b/vic/drivers/shared_image/src/vic_restore.c\nindex ef2d55b9..cc09451c 100644\n--- a/vic/drivers/shared_image/src/vic_restore.c\n+++ b/vic/drivers/shared_image/src/vic_restore.c\n@@ -24,7 +24,7 @@\n  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n  *****************************************************************************/\n \n-#include <vic_driver_image.h>\n+#include <vic_driver_shared_image.h>\n \n /******************************************************************************\n  * @brief    Read initial model state.\n', 'commit_subject': 'Fixed typo in vic_restore.c.', 'repo_name': 'UW-Hydro/VIC', 'committer': {'name': 'Ted Bohn', 'email': '5f7413397de0c04dd98e57976c2fc555ab842292@gmail.com', 'time_sec': '1458862428', 'tz_offset': '-420', 'date': {'seconds': '1458862428'}}, 'commit_tree': '276814c66a5836a931987745c2e4e4fe16840c63', 'commit_message': 'Fixed typo in vic_restore.c.\n', 'language_name': 'C', 'license': 'mit', 'hunks': '@@ -24,7 +24,7 @@\n  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n  *****************************************************************************/\n \n-#include <vic_driver_image.h>\n+#include <vic_driver_shared_image.h>\n \n /******************************************************************************\n  * @brief    Read initial model state.\n', 'addition_count': 1, 'deletion_count': 1, 'src_file': 'a/vic/drivers/shared_image/src/vic_restore.c', 'tgt_file': 'b/vic/drivers/shared_image/src/vic_restore.c', 'file_extension': '.c', 'before_file': '/******************************************************************************\n * @section DESCRIPTION\n *\n * Read initial model state.\n *****************************************************************************/\n\n#include <vic_driver_shared_image.h>\n#include <rout.h>\n\n/******************************************************************************\n * @brief    Read initial model state.\n *****************************************************************************/\nvoid\nvic_restore(void)\n{\n    extern int                 mpi_rank;\n    extern all_vars_struct    *all_vars;\n    extern domain_struct       global_domain;\n    extern domain_struct       local_domain;\n    extern option_struct       options;\n    extern veg_con_map_struct *veg_con_map;\n    extern filenames_struct    filenames;\n    extern metadata_struct     state_metadata[N_STATE_VARS + N_STATE_VARS_EXT];\n * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n *****************************************************************************/\n\n#include <vic_driver_image.h>\n\n/******************************************************************************\n * @brief    Read initial model state.\n    int                       *ivar = NULL;\n    int                        status;\n    double                    *dvar = NULL;\n    size_t                     d2count[2];\n    size_t                     d2start[2];\n    size_t                     d3count[3];\n    size_t                     d3start[3];\n    size_t                     d4count[4];\n    size_t                     d4start[4];\n    size_t                     d5count[5];\n    size_t                     d5start[5];\n    size_t                     d6count[6];\n    size_t                     d6start[6];\n\n    if (mpi_rank == VIC_MPI_ROOT) {\n        // open initial state file\n        status = nc_open(filenames.init_state.nc_filename, NC_NOWRITE,\n                         &(filenames.init_state.nc_id));\n        check_nc_status(status, "Error opening %s",\n                        filenames.init_state.nc_filename);\n    }\n\n    // validate state file dimensions and coordinate variables\n    check_init_state_file();\n    // read state variables\n\n    // allocate memory for variables to be stored\n    ivar = malloc(local_domain.ncells_active * sizeof(*ivar));\n    check_alloc_status(ivar, "Memory allocation error");\n\n    dvar = malloc(local_domain.ncells_active * sizeof(*dvar));\n    check_alloc_status(dvar, "Memory allocation error");\n\n    // initialize starts and counts\n    d2start[0] = 0;\n    d2start[1] = 0;\n    d2count[0] = global_domain.n_ny;\n    d2count[1] = global_domain.n_nx;\n\n    d3start[0] = 0;\n    d3start[1] = 0;\n    d3start[2] = 0;\n    d3count[0] = 1;\n    d3count[1] = global_domain.n_ny;\n    d3count[2] = global_domain.n_nx;\n\n    d4start[0] = 0;\n    d4start[1] = 0;\n    d4start[2] = 0;\n    d4start[3] = 0;\n    d4count[0] = 1;\n    d4count[1] = 1;\n    d4count[2] = global_domain.n_ny;\n    d4count[3] = global_domain.n_nx;\n\n    d5start[0] = 0;\n    d5start[1] = 0;\n    d5start[2] = 0;\n    d5start[3] = 0;\n    d5start[4] = 0;\n    d5count[0] = 1;\n    d5count[1] = 1;\n    d5count[2] = 1;\n    d5count[3] = global_domain.n_ny;\n    d5count[4] = global_domain.n_nx;\n\n    d6start[0] = 0;\n    d6start[1] = 0;\n    d6start[2] = 0;\n    d6start[3] = 0;\n    d6start[4] = 0;\n    d6start[5] = 0;\n    d6count[0] = 1;\n    d6count[1] = 1;\n    d6count[2] = 1;\n    d6count[3] = 1;\n    d6count[4] = global_domain.n_ny;\n    d6count[5] = global_domain.n_nx;\n\n    // total soil moisture\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d5start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d5start[1] = k;\n            for (j = 0; j < options.Nlayer; j++) {\n                d5start[2] = j;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_SOIL_MOISTURE].varname,\n                                            d5start, d5count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].cell[v][k].layer[j].moist = dvar[i];\n                    }\n                }\n            }\n        }\n    }\n\n    // ice content\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d6start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d6start[1] = k;\n            for (j = 0; j < options.Nlayer; j++) {\n                d6start[2] = j;\n                for (p = 0; p < options.Nfrost; p++) {\n                    d6start[3] = p;\n                    get_scatter_nc_field_double(&(filenames.init_state),\n                                                state_metadata[STATE_SOIL_ICE].varname,\n                                                d6start, d6count, dvar);\n                    for (i = 0; i < local_domain.ncells_active; i++) {\n                        v = veg_con_map[i].vidx[m];\n                        if (v >= 0) {\n                            all_vars[i].cell[v][k].layer[j].ice[p] = dvar[i];\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    // dew storage: tmpval = veg_var[veg][band].Wdew;\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_CANOPY_WATER].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].veg_var[v][k].Wdew = dvar[i];\n                }\n            }\n        }\n    }\n\n    if (options.CARBON) {\n        // cumulative NPP: tmpval = veg_var[veg][band].AnnualNPP;\n        for (m = 0; m < options.NVEGTYPES; m++) {\n            d4start[0] = m;\n            for (k = 0; k < options.SNOW_BAND; k++) {\n                d4start[1] = k;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_ANNUALNPP].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].veg_var[v][k].AnnualNPP = dvar[i];\n                    }\n                }\n            }\n        }\n\n        // previous NPP: tmpval = veg_var[veg][band].AnnualNPPPrev;\n        for (m = 0; m < options.NVEGTYPES; m++) {\n            d4start[0] = m;\n            for (k = 0; k < options.SNOW_BAND; k++) {\n                d4start[1] = k;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_ANNUALNPPPREV].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].veg_var[v][k].AnnualNPPPrev = dvar[i];\n                    }\n                }\n            }\n        }\n\n        // litter carbon: tmpval = cell[veg][band].CLitter;\n        for (m = 0; m < options.NVEGTYPES; m++) {\n            d4start[0] = m;\n            for (k = 0; k < options.SNOW_BAND; k++) {\n                d4start[1] = k;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_CLITTER].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].cell[v][k].CLitter = dvar[i];\n                    }\n                }\n            }\n        }\n\n        // intermediate carbon: tmpval = cell[veg][band].CInter;\n        for (m = 0; m < options.NVEGTYPES; m++) {\n            d4start[0] = m;\n            for (k = 0; k < options.SNOW_BAND; k++) {\n                d4start[1] = k;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_CINTER].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].cell[v][k].CInter = dvar[i];\n                    }\n                }\n            }\n        }\n\n        // slow carbon: tmpval = cell[veg][band].CSlow;\n        for (m = 0; m < options.NVEGTYPES; m++) {\n            d4start[0] = m;\n            for (k = 0; k < options.SNOW_BAND; k++) {\n                d4start[1] = k;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_CSLOW].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].cell[v][k].CSlow = dvar[i];\n                    }\n                }\n            }\n        }\n    }\n\n    // snow age: snow[veg][band].last_snow\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_int(&(filenames.init_state),\n                                     state_metadata[STATE_SNOW_AGE].varname,\n                                     d4start, d4count, ivar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].last_snow = ivar[i];\n                }\n            }\n        }\n    }\n\n    // melting state: (int)snow[veg][band].MELTING\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_int(&(filenames.init_state),\n                                     state_metadata[STATE_SNOW_MELT_STATE].varname,\n                                     d4start, d4count, ivar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].MELTING = ivar[i];\n                }\n            }\n        }\n    }\n\n    // snow covered fraction: snow[veg][band].coverage\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_COVERAGE].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].coverage = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow water equivalent: snow[veg][band].swq\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[\n                                            STATE_SNOW_WATER_EQUIVALENT].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].swq = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow surface temperature: snow[veg][band].surf_temp\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_SURF_TEMP].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].surf_temp = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow surface water: snow[veg][band].surf_water\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_SURF_WATER].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].surf_water = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow pack temperature: snow[veg][band].pack_temp\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_PACK_TEMP].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].pack_temp = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow pack water: snow[veg][band].pack_water\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_PACK_WATER].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].pack_water = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow density: snow[veg][band].density\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_DENSITY].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].density = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow cold content: snow[veg][band].coldcontent\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_COLD_CONTENT].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].coldcontent = dvar[i];\n                }\n            }\n        }\n    }\n\n    // snow canopy storage: snow[veg][band].snow_canopy\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_SNOW_CANOPY].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].snow[v][k].snow_canopy = dvar[i];\n                }\n            }\n        }\n    }\n\n    // grid cell-averaged albedo: gridcell_avg.avg_albedo\n    get_scatter_nc_field_double(&(filenames.init_state),\n                                state_metadata[STATE_AVG_ALBEDO].varname,\n                                d2start, d2count, dvar);\n    for (i = 0; i < local_domain.ncells_active; i++) {\n        all_vars[i].gridcell_avg.avg_albedo = dvar[i];\n    }\n\n    // soil node temperatures: energy[veg][band].T[nidx]\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d5start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d5start[1] = k;\n            for (j = 0; j < options.Nnode; j++) {\n                d5start[2] = j;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_SOIL_NODE_TEMP].varname,\n                                            d5start, d5count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    v = veg_con_map[i].vidx[m];\n                    if (v >= 0) {\n                        all_vars[i].energy[v][k].T[j] = dvar[i];\n                    }\n                }\n            }\n        }\n    }\n\n    // Foliage temperature: energy[veg][band].Tfoliage\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_FOLIAGE_TEMPERATURE].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].energy[v][k].Tfoliage = dvar[i];\n                }\n            }\n        }\n    }\n\n    // Outgoing longwave from understory: energy[veg][band].LongUnderOut\n    // This is a flux. Saving it to state file is a temporary solution!!\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_ENERGY_LONGUNDEROUT].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].energy[v][k].LongUnderOut = dvar[i];\n                }\n            }\n        }\n    }\n\n    // Thermal flux through the snow pack: energy[veg][band].snow_flux\n    // This is a flux. Saving it to state file is a temporary solution!!\n    for (m = 0; m < options.NVEGTYPES; m++) {\n        d4start[0] = m;\n        for (k = 0; k < options.SNOW_BAND; k++) {\n            d4start[1] = k;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_ENERGY_SNOW_FLUX].varname,\n                                        d4start, d4count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                v = veg_con_map[i].vidx[m];\n                if (v >= 0) {\n                    all_vars[i].energy[v][k].snow_flux = dvar[i];\n                }\n            }\n        }\n    }\n\n    if (options.LAKES) {\n        // total soil moisture\n        for (j = 0; j < options.Nlayer; j++) {\n            d3start[0] = j;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_SOIL_MOISTURE].varname,\n                                        d3start, d3count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.soil.layer[j].moist = dvar[i];\n            }\n        }\n\n        // ice content\n        for (j = 0; j < options.Nlayer; j++) {\n            d4start[0] = j;\n            for (p = 0; p < options.Nfrost; p++) {\n                d4start[1] = p;\n                get_scatter_nc_field_double(&(filenames.init_state),\n                                            state_metadata[STATE_LAKE_SOIL_ICE].varname,\n                                            d4start, d4count, dvar);\n                for (i = 0; i < local_domain.ncells_active; i++) {\n                    all_vars[i].lake_var.soil.layer[j].ice[p] = dvar[i];\n                }\n            }\n        }\n\n        if (options.CARBON) {\n            // litter carbon: tmpval = lake_var.soil.CLitter;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_CLITTER].varname,\n                                        d2start, d2count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.soil.CLitter = dvar[i];\n            }\n\n            // intermediate carbon: tmpval = lake_var.soil.CInter;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_CINTER].varname,\n                                        d2start, d2count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.soil.CInter = dvar[i];\n            }\n\n            // slow carbon: tmpval = lake_var.soil.CSlow;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_CSLOW].varname,\n                                        d2start, d2count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.soil.CSlow = dvar[i];\n            }\n        }\n\n        // snow age: lake_var.snow.last_snow\n        get_scatter_nc_field_int(&(filenames.init_state),\n                                 state_metadata[STATE_LAKE_SNOW_AGE].varname,\n                                 d2start, d2count, ivar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.last_snow = ivar[i];\n        }\n\n        // melting state: (int)lake_var.snow.MELTING\n        get_scatter_nc_field_int(&(filenames.init_state),\n                                 state_metadata[STATE_LAKE_SNOW_MELT_STATE].varname,\n                                 d2start, d2count, ivar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.MELTING = ivar[i];\n        }\n\n        // snow covered fraction: lake_var.snow.coverage\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_COVERAGE].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.coverage = dvar[i];\n        }\n\n        // snow water equivalent: lake_var.snow.swq\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[\n                                        STATE_LAKE_SNOW_WATER_EQUIVALENT].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.swq = dvar[i];\n        }\n\n        // snow surface temperature: lake_var.snow.surf_temp\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_SURF_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.surf_temp = dvar[i];\n        }\n\n        // snow surface water: lake_var.snow.surf_water\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_SURF_WATER].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.surf_water = dvar[i];\n        }\n\n        // snow pack temperature: lake_var.snow.pack_temp\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_PACK_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.pack_temp = dvar[i];\n        }\n\n        // snow pack water: lake_var.snow.pack_water\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_PACK_WATER].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.pack_water = dvar[i];\n        }\n\n        // snow density: lake_var.snow.density\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_SURF_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.density = dvar[i];\n        }\n\n        // snow cold content: lake_var.snow.coldcontent\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_COLD_CONTENT].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.coldcontent = dvar[i];\n        }\n\n        // snow canopy storage: lake_var.snow.snow_canopy\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SNOW_CANOPY].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.snow.snow_canopy = dvar[i];\n        }\n\n        // soil node temperatures: lake_var.energy.T[nidx]\n        for (j = 0; j < options.Nnode; j++) {\n            d3start[0] = j;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_SOIL_NODE_TEMP].varname,\n                                        d3start, d3count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.soil.layer[j].moist = dvar[i];\n            }\n        }\n\n        // lake active layers: lake_var.activenod\n        get_scatter_nc_field_int(&(filenames.init_state),\n                                 state_metadata[STATE_LAKE_ACTIVE_LAYERS].varname,\n                                 d2start, d2count, ivar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.activenod = ivar[i];\n        }\n\n        // lake layer thickness: lake_var.dz\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_LAYER_DZ].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.dz = dvar[i];\n        }\n\n        // lake surface layer thickness: lake_var.surfdz\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SURF_LAYER_DZ].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.surfdz = dvar[i];\n        }\n\n        // lake depth: lake_var.ldepth\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_DEPTH].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.ldepth = dvar[i];\n        }\n\n        // lake layer surface areas: lake_var.surface[ndix]\n        for (j = 0; j < options.Nlakenode; j++) {\n            d3start[0] = j;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[\n                                            STATE_LAKE_LAYER_SURF_AREA].varname,\n                                        d3start, d3count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.surface[j] = dvar[i];\n            }\n        }\n\n        // lake surface area: lake_var.sarea\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_SURF_AREA].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.sarea = dvar[i];\n        }\n\n        // lake volume: lake_var.volume\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_VOLUME].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.volume = dvar[i];\n        }\n\n        // lake layer temperatures: lake_var.temp[nidx]\n        for (j = 0; j < options.Nlakenode; j++) {\n            d3start[0] = j;\n            get_scatter_nc_field_double(&(filenames.init_state),\n                                        state_metadata[STATE_LAKE_LAYER_TEMP].varname,\n                                        d3start, d3count, dvar);\n            for (i = 0; i < local_domain.ncells_active; i++) {\n                all_vars[i].lake_var.temp[j] = dvar[i];\n            }\n        }\n\n        // vertical average lake temperature: lake_var.tempavg\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_AVERAGE_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.tempavg = dvar[i];\n        }\n\n        // lake ice area fraction: lake_var.areai\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_AREA_FRAC].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.areai = dvar[i];\n        }\n\n        // new lake ice area fraction: lake_var.new_ice_area\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_AREA_FRAC_NEW].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.new_ice_area = dvar[i];\n        }\n\n        // lake ice water equivalent: lake_var.ice_water_eq\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[\n                                        STATE_LAKE_ICE_WATER_EQUIVALENT].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.ice_water_eq = dvar[i];\n        }\n\n        // lake ice height: lake_var.hice\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_HEIGHT].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.hice = dvar[i];\n        }\n\n        // lake ice temperature: lake_var.tempi\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.tempi = dvar[i];\n        }\n\n        // lake ice snow water equivalent: lake_var.swe\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_SWE].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.swe = dvar[i];\n        }\n\n        // lake ice snow surface temperature: lake_var.surf_temp\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_SNOW_SURF_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.surf_temp = dvar[i];\n        }\n\n        // lake ice snow pack temperature: lake_var.pack_temp\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_SNOW_PACK_TEMP].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.pack_temp = dvar[i];\n        }\n\n        // lake ice snow coldcontent: lake_var.coldcontent\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[\n                                        STATE_LAKE_ICE_SNOW_COLD_CONTENT].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.coldcontent = dvar[i];\n        }\n\n        // lake ice snow surface water: lake_var.surf_water\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[\n                                        STATE_LAKE_ICE_SNOW_SURF_WATER].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.surf_water = dvar[i];\n        }\n\n        // lake ice snow pack water: lake_var.pack_water\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[\n                                        STATE_LAKE_ICE_SNOW_PACK_WATER].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.pack_water = dvar[i];\n        }\n\n        // lake ice snow albedo: lake_var.SAlbedo\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_SNOW_ALBEDO].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.SAlbedo = dvar[i];\n        }\n\n        // lake ice snow depth: lake_var.sdepth\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    state_metadata[STATE_LAKE_ICE_SNOW_DEPTH].varname,\n                                    d2start, d2count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            all_vars[i].lake_var.sdepth = dvar[i];\n        }\n    }\n\n    // routing ring\n    vic_restore_rout_extension(&(filenames.init_state), state_metadata);\n\n    free(ivar);\n    free(dvar);\n\n    // close initial state file\n    if (mpi_rank == VIC_MPI_ROOT) {\n        status = nc_close(filenames.init_state.nc_id);\n        check_nc_status(status, "Error closing %s",\n                        filenames.init_state.nc_filename);\n    }\n}\n\n/******************************************************************************\n * @brief    Check that the initial state file matches the global parameter\n             settings\n *****************************************************************************/\nvoid\ncheck_init_state_file(void)\n{\n    extern filenames_struct filenames;\n    extern domain_struct    global_domain;\n    extern domain_struct    local_domain;\n    extern option_struct    options;\n    extern soil_con_struct *soil_con;\n    extern int              mpi_rank;\n\n    int                     status;\n    size_t                  dimlen;\n    size_t                  i;\n    size_t                  j;\n    size_t                  d1count[1];\n    size_t                  d1start[1];\n    size_t                  d2count[2];\n    size_t                  d2start[2];\n    size_t                  d3count[3];\n    size_t                  d3start[3];\n    int                     lon_var_id;\n    int                     lat_var_id;\n    double                 *dvar;\n    double                  rtol = 0.0; // maybe move this to a .h file\n    double                  abs_tol = 0.0001; // maybe move this to a .h file\n\n    // read and validate dimension lengths\n    if (mpi_rank == VIC_MPI_ROOT) {\n        dimlen = get_nc_dimension(&(filenames.init_state),\n                                  global_domain.info.x_dim);\n        if (dimlen != global_domain.n_nx) {\n            log_err("Number of grid columns in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state),\n                                  global_domain.info.y_dim);\n        if (dimlen != global_domain.n_ny) {\n            log_err("Number of grid rows in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state), "veg_class");\n        if (dimlen != options.NVEGTYPES) {\n            log_err("Number of veg classes in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state), "snow_band");\n        if (dimlen != options.SNOW_BAND) {\n            log_err("Number of snow bands in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state), "nlayer");\n        if (dimlen != options.Nlayer) {\n            log_err("Number of soil layers in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state), "frost_area");\n        if (dimlen != options.Nfrost) {\n            log_err("Number of frost areas in state file does not "\n                    "match parameter file");\n        }\n        dimlen = get_nc_dimension(&(filenames.init_state), "soil_node");\n        if (dimlen != options.Nnode) {\n            log_err("Number of soil nodes in state file does not "\n                    "match parameter file");\n        }\n        if (options.LAKES) {\n            dimlen = get_nc_dimension(&(filenames.init_state), "lake_node");\n            if (dimlen != options.Nlakenode) {\n                log_err("Number of lake nodes in state file does not "\n                        "match LAKE_NODES (%zu) in global parameter file",\n                        options.Nlakenode);\n            }\n        }\n    }\n\n    // read dimension variables\n\n    // lat/lon\n    if (mpi_rank == VIC_MPI_ROOT) {\n        status = nc_inq_varid(filenames.init_state.nc_id,\n                              global_domain.info.lon_var, &lon_var_id);\n        check_nc_status(status, "Unable to find variable \\"%s\\" in %s",\n                        global_domain.info.lon_var,\n                        filenames.init_state.nc_filename);\n        status = nc_inq_varid(filenames.init_state.nc_id,\n                              global_domain.info.lat_var, &lat_var_id);\n        check_nc_status(status, "Unable to find variable \\"%s\\" in %s",\n                        global_domain.info.lat_var,\n                        filenames.init_state.nc_filename);\n        if (global_domain.info.n_coord_dims == 1) {\n            d1start[0] = 0;\n            dvar = calloc(global_domain.n_nx, sizeof(*dvar));\n            check_alloc_status(dvar, "Memory allocation error");\n\n            d1count[0] = global_domain.n_nx;\n            status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id,\n                                        d1start, d1count, dvar);\n            check_nc_status(status, "Error reading data from \\"%s\\" in %s",\n                            global_domain.info.lon_var,\n                            filenames.init_state.nc_filename);\n            // implicitly nested loop over ni and nj with j set to 0\n            for (i = 0; i < global_domain.n_nx; i++) {\n                if (!assert_close_double(dvar[i],\n                                         global_domain.locations[i].longitude,\n                                         rtol,\n                                         abs_tol)) {\n                    log_err("Longitudes in initial state file do not "\n                            "match parameter file");\n                }\n            }\n            free(dvar);\n\n            dvar = calloc(global_domain.n_ny, sizeof(*dvar));\n            check_alloc_status(dvar, "Memory allocation error");\n\n            d1count[0] = global_domain.n_ny;\n            status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id,\n                                        d1start, d1count, dvar);\n            check_nc_status(status, "Error reading data from \\"%s\\" in %s",\n                            global_domain.info.lat_var,\n                            filenames.init_state.nc_filename);\n            // implicitly nested loop over ni and nj with i set to 0;\n            // j stride = n_nx\n            for (j = 0; j < global_domain.n_ny; j++) {\n                if (!assert_close_double(dvar[j],\n                                         global_domain.locations[j *\n                                                                 global_domain.\n                                                                 n_nx]\n                                         .latitude, rtol,\n                                         abs_tol)) {\n                    log_err("Latitudes in initial state file do not "\n                            "match parameter file");\n                }\n            }\n            free(dvar);\n        }\n        else if (global_domain.info.n_coord_dims == 2) {\n            d2start[0] = 0;\n            d2start[1] = 0;\n            dvar =\n                calloc(global_domain.n_ny * global_domain.n_nx, sizeof(*dvar));\n            check_alloc_status(dvar, "Memory allocation error");\n\n            d2count[0] = global_domain.n_ny;\n            d2count[1] = global_domain.n_nx;\n            status = nc_get_vara_double(filenames.init_state.nc_id, lon_var_id,\n                                        d2start, d2count, dvar);\n            check_nc_status(status, "Error reading data from \\"%s\\" in %s",\n                            global_domain.info.lon_var,\n                            filenames.init_state.nc_filename);\n            for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) {\n                if (dvar[i] != (double) global_domain.locations[i].longitude) {\n                    log_err("Longitudes in initial state file do not "\n                            "match parameter file");\n                }\n            }\n            status = nc_get_vara_double(filenames.init_state.nc_id, lat_var_id,\n                                        d2start, d2count, dvar);\n            check_nc_status(status, "Error reading data from \\"%s\\" in %s",\n                            global_domain.info.lat_var,\n                            filenames.init_state.nc_filename);\n            for (i = 0; i < global_domain.n_ny * global_domain.n_nx; i++) {\n                if (dvar[i] != (double) global_domain.locations[i].latitude) {\n                    log_err("Latitudes in initial state file do not "\n                            "match parameter file");\n                }\n            }\n            free(dvar);\n        }\n        else {\n            log_err("global_domain.info.n_coord_dims should be 1 or 2");\n        }\n    }\n\n    // initialize dvar for soil thermal node deltas and depths\n    dvar = malloc(local_domain.ncells_active * sizeof(*dvar));\n    check_alloc_status(dvar, "Memory allocation error");\n\n    // soil thermal node deltas (dimension: node, lat, lon)\n    d3start[0] = 0;\n    d3start[1] = 0;\n    d3start[2] = 0;\n    d3count[0] = 1;\n    d3count[1] = global_domain.n_ny;\n    d3count[2] = global_domain.n_nx;\n    for (j = 0; j < options.Nnode; j++) {\n        d3start[0] = j;\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    "dz_node",\n                                    d3start, d3count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            if (dvar[i] != soil_con[i].dz_node[j]) {\n                log_err("Soil node intervals in state file do not match "\n                        "those computed by VIC");\n            }\n        }\n    }\n\n    // soil thermal node depths\n    d3start[0] = 0;\n    d3start[1] = 0;\n    d3start[2] = 0;\n    d3count[0] = 1;\n    d3count[1] = global_domain.n_ny;\n    d3count[2] = global_domain.n_nx;\n    for (j = 0; j < options.Nnode; j++) {\n        d3start[0] = j;\n        get_scatter_nc_field_double(&(filenames.init_state),\n                                    "node_depth",\n                                    d3start, d3count, dvar);\n        for (i = 0; i < local_domain.ncells_active; i++) {\n            if (dvar[i] != soil_con[i].Zsum_node[j]) {\n                log_err("Soil node depths in state file do not match "\n                        "those computed by VIC");\n            }\n        }\n    }\n    free(dvar);\n}\n', 'message': 'Fixed typo in vic_restore.c.\n', 'author_name': 'Ted Bohn', 'lang': 'c'}