def test_cell_data_fetcher_ranalangvatn(self): gs = GridSpecification(32632, x0=704000, y0=7431000, dx=1000, dy=1000, nx=98, ny=105) pwrplants = [236] cdf = CellDataFetcher(catchment_type="regulated", identifier="POWER_PLANT_ID", grid_specification=gs, id_list=pwrplants) cd = cdf.fetch() self.assertIsNotNone(cd) self.assertIsNotNone(cd['cell_data']) self.assertIsNotNone(cd['cell_data'][pwrplants[0]]) self.assertIsNotNone(cd['catchment_land_types']) self.assertIsNotNone(cd['elevation_raster'])
def plot_region_model(self, catchment_type, identifier, x0, y0, dx, dy, nx, ny, catch_indicies, station_ids, epsg_id): grid_spec = GridSpecification(epsg_id, x0, y0, dx, dy, nx, ny) cf = CellDataFetcher(catchment_type, identifier, grid_spec, id_list=catch_indicies) print("Start fetching data") cf.fetch() print("Done, now preparing plot") # Plot the extracted data fig, ax = plt.subplots(1) color_map = {"forest": 'g', "lake": 'b', "glacier": 'r', "cell": "0.75", "reservoir": "purple"} extent = grid_spec.geometry[0], grid_spec.geometry[2], grid_spec.geometry[1], grid_spec.geometry[3] ax.imshow(cf.elevation_raster, origin='upper', extent=extent, cmap=cm.gray) for catchment_cells in iter(cf.cell_data.values()): self.add_plot_polygons(ax, [cell["cell"] for cell in catchment_cells], color=color_map["cell"]) for catchment_land_type in iter(cf.catchment_land_types.values()): for k, v in iter(catchment_land_type.items()): self.add_plot_polygons(ax, v, color=color_map[k]) geometry = grid_spec.geometry if station_ids is not None: glr = GisLocationService() stations = glr.get_locations(station_ids, epsg_id) if stations: points = stations.values() ax.scatter([pt[0] for pt in points], [pt[1] for pt in points], alpha=0.5) station_min_x = min([v[0] for v in points]) - 1000 station_max_x = max([v[0] for v in points]) + 1000 station_min_y = min([v[1] for v in points]) - 1000 station_max_y = max([v[1] for v in points]) + 1000 geometry[0] = min(station_min_x, geometry[0]) geometry[1] = min(station_min_y, geometry[1]) geometry[2] = max(station_max_x, geometry[2]) geometry[3] = max(station_max_y, geometry[3]) base_dir = path.join(shyftdata_dir, "repository", "arome_data_repository") EPSG = grid_spec.epsg() bbox = grid_spec.bounding_box(EPSG) arome4 = AromeDataRepository(EPSG, base_dir, filename="arome_metcoop_default2_5km_*.nc", bounding_box=bbox, allow_subset=True) # data_names = ("temperature", "wind_speed", "precipitation", "relative_humidity","radiation") # utc_period = api.UtcPeriod( # api.Calendar().time(api.YMDhms(2015, 10, 1, 0, 0, 0)), # api.Calendar().time(api.YMDhms(2015, 10, 2, 0, 0, 0)) # ) # arome_ts=arome4.get_timeseries(["temperature"],utc_period) # arome_points=[gts.mid_point() for gts in arome_ts['temperature']] # ax.scatter( [pt.x for pt in arome_points ],[pt.y for pt in arome_points],c=[pt.z for pt in arome_points],alpha=0.5,cmap='gray',s=100)#, facecolors=('r')) ax.set_xlim(geometry[0], geometry[2]) ax.set_ylim(geometry[1], geometry[3]) plt.show()
def plot_region_model(self, catchment_type, identifier ,x0, y0, dx, dy, nx, ny, catch_indicies, station_ids,epsg_id): grid_spec = GridSpecification(epsg_id, x0, y0, dx, dy, nx, ny) cf = CellDataFetcher(catchment_type, identifier, grid_spec, id_list=catch_indicies) print( "Start fetching data") cf.fetch() print ("Done, now preparing plot") # Plot the extracted data fig, ax = plt.subplots(1) color_map = {"forest": 'g', "lake": 'b', "glacier": 'r', "cell": "0.75", "reservoir": "purple"} extent = grid_spec.geometry[0], grid_spec.geometry[2], grid_spec.geometry[1], grid_spec.geometry[3] ax.imshow(cf.elevation_raster, origin='upper', extent=extent, cmap=cm.gray) for catchment_cells in iter(cf.cell_data.values()): self.add_plot_polygons(ax, [cell["cell"] for cell in catchment_cells], color=color_map["cell"]) for catchment_land_type in iter(cf.catchment_land_types.values()): for k,v in iter(catchment_land_type.items()): self.add_plot_polygons(ax, v, color=color_map[k]) geometry = grid_spec.geometry if station_ids is not None: glr = GisLocationService() stations = glr.get_locations(station_ids, epsg_id) if stations: points = stations.values() ax.scatter([pt[0] for pt in points], [pt[1] for pt in points], alpha=0.5) station_min_x = min([v[0] for v in points]) - 1000 station_max_x = max([v[0] for v in points]) + 1000 station_min_y = min([v[1] for v in points]) - 1000 station_max_y = max([v[1] for v in points]) + 1000 geometry[0] = min(station_min_x, geometry[0]) geometry[1] = min(station_min_y, geometry[1]) geometry[2] = max(station_max_x, geometry[2]) geometry[3] = max(station_max_y, geometry[3]) base_dir = path.join(shyftdata_dir, "repository", "arome_data_repository") EPSG = grid_spec.epsg_id bbox = grid_spec.bounding_box(EPSG) arome4 = AromeDataRepository(EPSG, base_dir, filename="arome_metcoop_default2_5km_*.nc", bounding_box=bbox, allow_subset=True) #data_names = ("temperature", "wind_speed", "precipitation", "relative_humidity","radiation") utc_period = api.UtcPeriod( api.Calendar().time(api.YMDhms(2015,10,1,0,0,0)), api.Calendar().time(api.YMDhms(2015,10,2,0,0,0)) ) #arome_ts=arome4.get_timeseries(["temperature"],utc_period) #arome_points=[gts.mid_point() for gts in arome_ts['temperature']] #ax.scatter( [pt.x for pt in arome_points ],[pt.y for pt in arome_points],c=[pt.z for pt in arome_points],alpha=0.5,cmap='gray',s=100)#, facecolors=('r')) ax.set_xlim(geometry[0], geometry[2]) ax.set_ylim(geometry[1], geometry[3]) plt.show()