Пример #1
0
def statkraft_region_model_repo_constructor(region_config, model_config,
                                            region_model_id):
    from shyft.repository.service.gis_region_model_repository import GisRegionModelRepository
    from shyft.repository.service.gis_region_model_repository import RegionModelConfig
    from shyft.repository.service.gis_region_model_repository import GridSpecification
    from six import iteritems  # This replaces dictionary.iteritems() on Python 2 and dictionary.items() on Python 3

    repo_params = region_config.repository()['params']
    d = region_config.domain()
    grid_specification = GridSpecification(d['EPSG'], d['lower_left_x'],
                                           d['lower_left_y'], d['step_x'],
                                           d['step_y'], d['nx'], d['ny'])
    region_model_type = model_config.model_type()
    # Construct region parameter:
    name_map = {
        "gamma_snow": "gs",
        "priestley_taylor": "pt",
        "kirchner": "kirchner",
        "actual_evapotranspiration": "ae",
        "skaugen": "skaugen",
        "hbv_snow": "snow"
    }
    region_parameter = region_model_type.parameter_t()
    for p_type_name, value_ in iteritems(model_config.model_parameters()):
        if p_type_name in name_map:
            if hasattr(region_parameter, name_map[p_type_name]):
                sub_param = getattr(region_parameter, name_map[p_type_name])
                for p, v in iteritems(value_):
                    if hasattr(sub_param, p):
                        setattr(sub_param, p, v)
                    else:
                        raise ConfigError(
                            "Invalid parameter '{}' for parameter set '{}'".
                            format(p, p_type_name))
            else:
                raise ConfigError(
                    "Invalid parameter set '{}' for selected model '{}'".
                    format(p_type_name, region_model_type.__name__))
        elif p_type_name == "p_corr_scale_factor":
            region_parameter.p_corr.scale_factor = value_
        else:
            raise ConfigError("Unknown parameter set '{}'".format(p_type_name))

    cfg_list = [
        RegionModelConfig(region_model_id, region_model_type, region_parameter,
                          grid_specification,
                          repo_params['catchment_regulated_type'],
                          repo_params['service_id_field_name'],
                          region_config.catchments()),
    ]
    rm_cfg_dict = {x.name: x for x in cfg_list}
    return GisRegionModelRepository(rm_cfg_dict)
Пример #2
0
def create_tistel_simulator(model, geo_ts_repository):
    region_id = "Tistel-ptgsk"
    interpolation_id = 0
    cfg_list = [
        RegionModelConfig(region_id, model, tistel.pt_gs_k_parameters(),
                          tistel.grid_spec, tistel.gis_table_def[0],
                          tistel.gis_table_def[1], tistel.catchment_id_list)
    ]
    reg_model_repository = GisRegionModelRepository(
        {x.name: x
         for x in cfg_list})
    ptgsk = Simulator(region_id, interpolation_id, reg_model_repository,
                      geo_ts_repository, tistel.interpolation_repository, None)
    return ptgsk
Пример #3
0
 def region_model_repository(self):
     """
     Returns
     -------
      - RegionModelRepository - configured with 'Tistel-ptgsk' etc.
     """
     id_list = [1225]
     # parameters can be loaded from yaml_config Model parameters..
     pt_params = api.PriestleyTaylorParameter()  # *params["priestley_taylor"])
     gs_params = api.GammaSnowParameter()  # *params["gamma_snow"])
     ss_params = api.SkaugenParameter()
     ae_params = api.ActualEvapotranspirationParameter()  # *params["act_evap"])
     k_params = api.KirchnerParameter()  # *params["kirchner"])
     p_params = api.PrecipitationCorrectionParameter()  # TODO; default 1.0, is it used ??
     ptgsk_rm_params = pt_gs_k.PTGSKParameter(pt_params, gs_params, ae_params, k_params, p_params)
     ptssk_rm_params = pt_ss_k.PTSSKParameter(pt_params, ss_params, ae_params, k_params, p_params)
     # create the description for 2 models of tistel,ptgsk, ptssk
     tistel_grid_spec = self.grid_spec  #
     cfg_list = [
         RegionModelConfig("Tistel-ptgsk", pt_gs_k.PTGSKModel, ptgsk_rm_params, tistel_grid_spec, "unregulated", "FELTNR", id_list),
         RegionModelConfig("Tistel-ptssk", pt_ss_k.PTSSKModel, ptssk_rm_params, tistel_grid_spec, "unregulated", "FELTNR", id_list)
     ]
     rm_cfg_dict = {x.name: x for x in cfg_list}
     return GisRegionModelRepository(rm_cfg_dict)
Пример #4
0
        def test_serialize_and_deserialize(self):
            rm_type = pt_gs_k.PTGSKModel
            reg_param = rm_type.parameter_t()
            epsg_id = 32633
            catchment_type = 'regulated'
            identifier = 'SUBCATCH_ID'
            dxy = 1000.
            pad = 5
            # LTM5-Tya
            id_list = [172, 174, 177, 185, 187, 190]
            grid_specification = get_grid_spec_from_catch_poly(
                id_list, catchment_type, identifier, epsg_id, dxy, pad)
            # LTM5-Nea
            id_list_1 = [180, 188, 191, 196]
            grid_specification_1 = get_grid_spec_from_catch_poly(
                id_list_1, catchment_type, identifier, epsg_id, dxy, pad)

            cfg_list = [
                RegionModelConfig('Tya', rm_type, reg_param,
                                  grid_specification, catchment_type,
                                  identifier, id_list),
                RegionModelConfig('Nea', rm_type, reg_param,
                                  grid_specification_1, catchment_type,
                                  identifier, id_list_1),
            ]
            rm_cfg_dict = {x.name: x for x in cfg_list}
            rm_repo = GisRegionModelRepository(rm_cfg_dict, use_cache=True)

            # Start by removing existing cache file
            GisRegionModelRepository.remove_cache(identifier,
                                                  grid_specification)
            # Update cache with Nea cell_data
            GisRegionModelRepository.update_cache(catchment_type, identifier,
                                                  grid_specification_1,
                                                  id_list_1)
            # Construct a region_model for Tya - this process should automatically append Tya cell_data to cache
            rm = rm_repo.get_region_model('Tya')
            cells_from_rm = rm.get_cells()
            # Get Tya cell_data from the region_model
            cell_data_from_region_model = cells_from_rm.geo_cell_data_vector(
                cells_from_rm).to_numpy().reshape(-1, 11)
            # Get Tya cell_data from auto generated cache
            cell_data_from_auto_cache = GisRegionModelRepository.get_cell_data_from_cache(
                identifier, grid_specification, id_list)
            # Remove the cache file
            GisRegionModelRepository.remove_cache(identifier,
                                                  grid_specification)
            # Update cache with Tya cell_data
            GisRegionModelRepository.update_cache(catchment_type, identifier,
                                                  grid_specification, id_list)
            # Update cache with Nea cell_data
            GisRegionModelRepository.update_cache(catchment_type, identifier,
                                                  grid_specification_1,
                                                  id_list_1)
            # Get Tya cell_data from updated cache
            cell_data_from_updated_cache = GisRegionModelRepository.get_cell_data_from_cache(
                identifier, grid_specification, id_list)
            # Get Nea cell_data from updated cache
            cell_data_from_updated_cache_1 = GisRegionModelRepository.get_cell_data_from_cache(
                identifier, grid_specification_1, id_list_1)
            # Remove the cache file
            GisRegionModelRepository.remove_cache(identifier,
                                                  grid_specification)
            # Get Nea cell_data from GIS
            cell_data_from_gis_1 = GisRegionModelRepository.get_cell_data_from_gis(
                catchment_type, identifier, grid_specification_1, id_list_1)
            # Get Tya cell_data from GIS
            cell_data_from_gis = GisRegionModelRepository.get_cell_data_from_gis(
                catchment_type, identifier, grid_specification, id_list)
            # Update cache with Tya cell_data
            GisRegionModelRepository.update_cache(catchment_type, identifier,
                                                  grid_specification, id_list)
            # Construct a region_model for Nea - this process should automatically append Nea cell_data to cache')
            rm_1 = rm_repo.get_region_model('Nea')
            cells_from_rm_1 = rm_1.get_cells()
            # Get Nea cell_data from the region_model
            cell_data_from_region_model_1 = cells_from_rm_1.geo_cell_data_vector(
                cells_from_rm_1).to_numpy().reshape(-1, 11)
            # Get Nea cell_data from auto generated cache
            cell_data_from_auto_cache_1 = GisRegionModelRepository.get_cell_data_from_cache(
                identifier, grid_specification_1, id_list_1)
            # Remove the cache file
            GisRegionModelRepository.remove_cache(identifier,
                                                  grid_specification)

            # Comparing results...
            # Arrays to compare
            # Tya
            # cell_data_from_region_model, cell_data_from_auto_cache, cell_data_from_updated_cache, cell_data_from_gis
            atol = 1e-08
            self.assertTrue(
                np.allclose(cell_data_from_region_model,
                            cell_data_from_gis['geo_data'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_auto_cache['geo_data'],
                            cell_data_from_gis['geo_data'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_updated_cache['geo_data'],
                            cell_data_from_gis['geo_data'],
                            atol=atol))

            self.assertTrue(
                np.allclose(rm.catchment_id_map,
                            cell_data_from_gis['cid_map'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_auto_cache['cid_map'],
                            cell_data_from_gis['cid_map'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_updated_cache['cid_map'],
                            cell_data_from_gis['cid_map'],
                            atol=atol))
            # Nea
            # cell_data_from_region_model_1, cell_data_from_auto_cache_1, cell_data_from_updated_cache_1, cell_data_from_gis_1
            self.assertTrue(
                np.allclose(cell_data_from_region_model_1,
                            cell_data_from_gis_1['geo_data'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_auto_cache_1['geo_data'],
                            cell_data_from_gis_1['geo_data'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_updated_cache_1['geo_data'],
                            cell_data_from_gis_1['geo_data'],
                            atol=atol))

            self.assertTrue(
                np.allclose(rm_1.catchment_id_map,
                            cell_data_from_gis_1['cid_map'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_auto_cache_1['cid_map'],
                            cell_data_from_gis_1['cid_map'],
                            atol=atol))
            self.assertTrue(
                np.allclose(cell_data_from_updated_cache_1['cid_map'],
                            cell_data_from_gis_1['cid_map'],
                            atol=atol))