def test_region_model_repository(self):
     id_list = [1225]
     epsg_id = 32632
     # 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 = api.pt_gs_k.PTGSKParameter(pt_params, gs_params, ae_params, k_params, p_params)
     ptssk_rm_params = api.pt_ss_k.PTSSKParameter(pt_params, ss_params, ae_params, k_params, p_params)
     # create the description for 4 models of tistel,ptgsk, ptssk, full and optimized
     tistel_grid_spec = GridSpecification(epsg_id=epsg_id, x0=362000.0, y0=6765000.0, dx=1000, dy=1000, nx=8,
                                          ny=8)
     cfg_list = [
         RegionModelConfig("tistel-ptgsk", PTGSKModel, ptgsk_rm_params, tistel_grid_spec, "unregulated", "FELTNR", id_list),
         RegionModelConfig("tistel-ptgsk-opt", PTGSKOptModel, ptgsk_rm_params, tistel_grid_spec, "unregulated", "FELTNR", id_list),
         RegionModelConfig("tistel-ptssk", PTSSKModel, ptssk_rm_params, tistel_grid_spec, "unregulated", "FELTNR", id_list)
     ]
     rm_cfg_dict = {x.name: x for x in cfg_list}
     rmr = GisRegionModelRepository(rm_cfg_dict)  # ok, now we have a Gis RegionModelRepository that can handle all named entities we pass.
     cm1 = rmr.get_region_model("tistel-ptgsk")  # pull out a PTGSKModel for tistel
     cm2 = rmr.get_region_model("tistel-ptgsk-opt")
     # Does not work, fail on ct. model:
     cm3 = rmr.get_region_model("tistel-ptssk")  # pull out a PTGSKModel for tistel
     # cm4= rmr.get_region_model("tistel-ptssk",PTSSKOptModel)
     self.assertIsNotNone(cm3)
     self.assertIsNotNone(cm1)
     self.assertIsNotNone(cm2)
     self.assertIsNotNone(
         cm2.catchment_id_map)  # This one is needed in order to properly map catchment-id to internal id
     self.assertEqual(cm2.catchment_id_map[0], id_list[0])
 def test_region_model_nea_nidelv(self):
     nea_nidelv_grid_spec = GridSpecification(epsg_id=32633,
                                              x0=270000.0,
                                              y0=7035000.0,
                                              dx=1000,
                                              dy=1000,
                                              nx=105,
                                              ny=75)
     catch_ids = [
         1228, 1308, 1394, 1443, 1726, 1867, 1996, 2041, 2129, 2195,
         2198, 2277, 2402, 2446, 2465, 2545, 2640, 2718, 3002, 3536,
         3630
     ]  #  , 1000010, 1000011]
     ptgsk_params = self.std_ptgsk_parameters
     cfg_list = [
         RegionModelConfig("nea-nidelv-ptgsk", PTGSKModel, ptgsk_params,
                           nea_nidelv_grid_spec, "regulated",
                           "CATCH_ID", catch_ids)
     ]
     rm_cfg_dict = {x.name: x for x in cfg_list}
     rmr = GisRegionModelRepository(rm_cfg_dict)
     nea_nidelv = rmr.get_region_model("nea-nidelv-ptgsk")
     self.assertIsNotNone(nea_nidelv)
     self.assertEqual(len(nea_nidelv.catchment_id_map), len(catch_ids))
     print("nea-nidelv:{0}".format(nea_nidelv.catchment_id_map))
 def test_region_model_repository(self):
     id_list = [1225]
     epsg_id = 32632
     # 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 = api.pt_gs_k.PTGSKParameter(
         pt_params, gs_params, ae_params, k_params, p_params)
     ptssk_rm_params = api.pt_ss_k.PTSSKParameter(
         pt_params, ss_params, ae_params, k_params, p_params)
     # create the description for 4 models of tistel,ptgsk, ptssk, full and optimized
     tistel_grid_spec = GridSpecification(epsg_id=epsg_id,
                                          x0=362000.0,
                                          y0=6765000.0,
                                          dx=1000,
                                          dy=1000,
                                          nx=8,
                                          ny=8)
     cfg_list = [
         RegionModelConfig("tistel-ptgsk", PTGSKModel, ptgsk_rm_params,
                           tistel_grid_spec, "unregulated", "FELTNR",
                           id_list),
         RegionModelConfig("tistel-ptgsk-opt", PTGSKOptModel,
                           ptgsk_rm_params, tistel_grid_spec,
                           "unregulated", "FELTNR", id_list),
         RegionModelConfig("tistel-ptssk", PTSSKModel, ptssk_rm_params,
                           tistel_grid_spec, "unregulated", "FELTNR",
                           id_list)
     ]
     rm_cfg_dict = {x.name: x for x in cfg_list}
     rmr = GisRegionModelRepository(
         rm_cfg_dict
     )  # ok, now we have a Gis RegionModelRepository that can handle all named entities we pass.
     cm1 = rmr.get_region_model(
         "tistel-ptgsk")  # pull out a PTGSKModel for tistel
     cm2 = rmr.get_region_model("tistel-ptgsk-opt")
     # Does not work, fail on ct. model:
     cm3 = rmr.get_region_model(
         "tistel-ptssk")  # pull out a PTGSKModel for tistel
     # cm4= rmr.get_region_model("tistel-ptssk",PTSSKOptModel)
     self.assertIsNotNone(cm3)
     self.assertIsNotNone(cm1)
     self.assertIsNotNone(cm2)
     self.assertIsNotNone(
         cm2.catchment_id_map
     )  # This one is needed in order to properly map catchment-id to internal id
     self.assertEqual(cm2.catchment_id_map[0], id_list[0])
 def test_region_model_nea_nidelv(self):
     nea_nidelv_grid_spec = GridSpecification(epsg_id=32633, x0=270000.0, y0=7035000.0, dx=1000, dy=1000, nx=105, ny=75)
     catch_ids = [1228, 1308, 1394, 1443, 1726, 1867, 1996, 2041, 2129, 2195, 2198, 2277, 2402, 2446, 2465, 2545,
                  2640, 2718, 3002, 3536, 3630, 1000010, 1000011]
     ptgsk_params = self.std_ptgsk_parameters
     cfg_list = [
         RegionModelConfig("nea-nidelv-ptgsk", PTGSKModel, ptgsk_params, nea_nidelv_grid_spec, "regulated", "CATCH_ID", catch_ids)
     ]
     rm_cfg_dict = {x.name: x for x in cfg_list}
     rmr = GisRegionModelRepository(rm_cfg_dict)
     nea_nidelv = rmr.get_region_model("nea-nidelv-ptgsk")
     self.assertIsNotNone(nea_nidelv)
     self.assertEqual(len(nea_nidelv.catchment_id_map), len(catch_ids))
     print("nea-nidelv:{0}".format(nea_nidelv.catchment_id_map))
Exemple #5
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)
Exemple #6
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
Exemple #7
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)
Exemple #8
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))
        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))