Beispiel #1
0
    def read_soil_parameters(self):
        soil_parameters = [
            'EvapZsurf', 'EvapZmin', 'EvapZmax', 'Kex', 'fevap', 'fWrelExp',
            'fwcc', 'CN', 'zCN', 'zGerm', 'zRes', 'fshape_cr'
        ]
        for param in soil_parameters:
            try:
                arr = open_hmdataarray(
                    self.model.config.SOIL_PARAMETERS['filename'],
                    param,
                    self.model.domain,
                    self.model.config.SOIL_PARAMETERS['is_1d'],
                    self.model.config.SOIL_PARAMETERS['xy_dimname'],
                )
                vars(self.model)[param] = np.require(
                    arr.values, requirements=['A', 'O', 'W', 'F'])

            except:
                try:
                    parameter_value = read_parameter_from_sqlite(
                        self.model.SoilParameterDatabase, param)
                    parameter_value = np.array(parameter_value[0],
                                               dtype=np.float64)
                    vars(self.model)[param] = np.require(
                        np.full(self.model.domain.nxy, parameter_value),
                        requirements=['A', 'O', 'W', 'F'])
                except:
                    pass
Beispiel #2
0
def test_open_hmdataarray():
    # os.chdir('test/release')
    modeltime = hm.set_modeltime(
        datetime.datetime(2003,6,1),
        datetime.datetime(2003,6,30),
        datetime.timedelta(days=1)
    )
    domain = hm.set_domain(
        'test_data/qrparm.soil_HWSD_cont_cosbyNew.nc',
        modeltime,
        'field342',
        is_1d=True,
        xy_dimname='land'
    )
    # domain.mask
    # domain.starttime
    # domain.endtime
    # domain.n_timestep
    # domain.dt
    da = hm.open_hmdataarray(
        'test_data/SWdown_WFDEI_land_200306.nc',
        'SWdown',
        domain,
        True,
        'land'
    )
    
    da = da.select(
        time=slice(
            datetime.datetime(2003,6,1),
            datetime.datetime(2003,6,10)
        )
    )
Beispiel #3
0
def test_open_hmdataarray():
    modeltime = hm.set_modeltime(datetime.datetime(2003, 6, 1, 0, 0),
                                 datetime.datetime(2003, 6, 10, 0, 0),
                                 datetime.timedelta(days=1))
    domain = hm.set_domain('test_data/ghana_landmask_0pt25degree.tif',
                           modeltime)
    da = hm.open_hmdataarray('test_data/AgMERRA_2003_tavg.nc4', 'tavg', domain)
    da.select(time=pd.Timestamp(2003, 6, 1), method='nearest')
 def set_initial_condition_from_file(self):
     self.var.th_init = open_hmdataarray(
         self.var.config.INITIAL_WATER_CONTENT['filename'],
         'th',
         self.var.domain,
         self.var.config.INITIAL_WATER_CONTENT['is_1d'],
         self.var.config.INITIAL_WATER_CONTENT['xy_dimname'],
         skip=['depth'])
     self.interpolate_initial_condition_to_compartments()
     self.var.th = np.broadcast_to(
         self.var.th_init.values[None, None, ...],
         (self.var.nFarm, self.var.nCrop, self.var.nComp,
          self.var.domain.nxy)).copy()
    def read_soil_hydraulic_parameters(self):

        # TODO: optionally read directly from config file

        soil_hydraulic_parameters = {
            'k_sat':
            self.model.config.SOIL_HYDRAULIC_PARAMETERS['k_sat_varname'],
            'th_sat':
            self.model.config.SOIL_HYDRAULIC_PARAMETERS['th_sat_varname'],
            'th_fc':
            self.model.config.SOIL_HYDRAULIC_PARAMETERS['th_fc_varname'],
            'th_wilt':
            self.model.config.SOIL_HYDRAULIC_PARAMETERS['th_wilt_varname']
        }
        for param, varname in soil_hydraulic_parameters.items():

            if param in self.model.config.SOIL_HYDRAULIC_PARAMETERS.keys():
                # 1 - Try to read from config file
                parameter_values = np.array(
                    self.model.config.SOIL_HYDRAULIC_PARAMETERS[param])
                if (len(parameter_values) == self.model.nLayer):
                    vars(self.model)[param] = np.require(np.broadcast_to(
                        parameter_values,
                        (self.model.nLayer, self.model.domain.nxy)),
                                                         requirements=[
                                                             'A', 'O', 'W', 'F'
                                                         ])
                else:
                    raise ValueError("Error reading parameter " + param +
                                     " from configuration file: length" +
                                     " of parameter list must equal number" +
                                     " of farms in simulation")

            else:
                # 2 - Try to read from netCDF file
                try:
                    arr = open_hmdataarray(
                        self.model.config.
                        SOIL_HYDRAULIC_PARAMETERS['filename'],
                        varname,
                        self.model.domain,
                        self.model.config.SOIL_HYDRAULIC_PARAMETERS['is_1d'],
                        self.model.config.
                        SOIL_HYDRAULIC_PARAMETERS['xy_dimname'],
                    )
                    vars(self.model)[param] = np.require(
                        arr.values, requirements=['A', 'O', 'W', 'F'])
                except:
                    raise ValueError()  # TODO
Beispiel #6
0
    def read(self):
        # PlantPop currently real in Fortran
        # ETadj should be an option provided in config
        int_crop_params = [
            'CropType', 'PlantingDate', 'HarvestDate', 'PolHeatStress',
            'PolColdStress', 'BioTempStress', 'PlantPop', 'Determinant',
            'ETadj', 'LagAer'
        ]
        flt_crop_params = [
            'Emergence', 'MaxRooting', 'Senescence', 'Maturity', 'HIstart',
            'Flowering', 'YldForm', 'Tbase', 'Tupp', 'Tmax_up', 'Tmax_lo',
            'Tmin_up', 'Tmin_lo', 'GDD_up', 'GDD_lo', 'fshape_b', 'PctZmin',
            'Zmin', 'Zmax', 'fshape_r', 'fshape_ex', 'SxTopQ', 'SxBotQ',
            'a_Tr', 'SeedSize', 'CCmin', 'CCx', 'CDC', 'CGC', 'Kcb', 'fage',
            'WP', 'WPy', 'fsink', 'bsted', 'bface', 'HI0', 'HIini', 'dHI_pre',
            'a_HI', 'b_HI', 'dHI0', 'exc', 'MaxFlowPct', 'p_up1', 'p_up2',
            'p_up3', 'p_up4', 'p_lo1', 'p_lo2', 'p_lo3', 'p_lo4', 'fshape_w1',
            'fshape_w2', 'fshape_w3', 'fshape_w4', 'Aer', 'beta', 'GermThr'
        ]
        crop_params_to_read = int_crop_params + flt_crop_params
        if len(crop_params_to_read) > 0:
            for param in crop_params_to_read:

                if param in int_crop_params:
                    datatype = np.int32
                else:
                    datatype = np.float64

                # 1 Try to read from configuration file:
                if param in self.config.keys():
                    # TODO
                    parameter_values = np.array(self.config[param])

                    # ensure the parameter has a farm dimension
                    if len(parameter_values.shape) == 1:
                        if len(parameter_values) == self.model.nCrop:
                            parameter_values = np.broadcast_to(
                                parameter_values,
                                (self.model.nFarm, self.model.nCrop))

                    if (parameter_values.shape[0] == self.model.nFarm) & (
                            parameter_values.shape[1] == self.model.nCrop):
                        vars(self.model)[param] = np.require(
                            np.broadcast_to(
                                parameter_values[:, None],
                                (self.model.nFarm, self.model.nCrop,
                                 self.model.domain.nxy)),
                            dtype=datatype,
                            requirements=['A', 'O', 'W', 'F'])

                    else:
                        raise ValueError(
                            "Error reading parameter " + param +
                            " from configuration file: length" +
                            " of parameter list must equal number" +
                            " of crops in simulation")
                else:
                    # 2 Try to read from netCDF file
                    try:
                        arr = open_hmdataarray(
                            self.model.config.CROP_PARAMETERS['filename'],
                            param,
                            self.model.domain,
                            self.model.config.CROP_PARAMETERS['is_1d'],
                            self.model.config.CROP_PARAMETERS['xy_dimname'],
                            sample=self.model.currentSampleNumber)
                        vars(self.model)[param] = np.require(np.broadcast_to(
                            arr.values, (self.model.nFarm, self.model.nCrop,
                                         self.model.domain.nxy)),
                                                             dtype=datatype,
                                                             requirements=[
                                                                 'A', 'O', 'W',
                                                                 'F'
                                                             ])

                    # 3 - Read from default parameter database
                    except:
                        try:
                            parameter_values = np.zeros((self.model.nCrop))
                            for index, crop_id in enumerate(self.model.CropID):
                                parameter_values[
                                    index] = read_crop_parameter_from_sqlite(
                                        self.model.CropParameterDatabase,
                                        crop_id, param)[0]

                            vars(self.model)[param] = np.require(
                                np.broadcast_to(
                                    parameter_values[None, :, None],
                                    (self.model.nFarm, self.model.nCrop,
                                     self.model.domain.nxy)),
                                dtype=datatype,
                                requirements=['A', 'O', 'W', 'F'])

                        except:
                            raise KeyError("Error reading parameter " + param +
                                           " from crop parameter database")