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_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_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_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))