def main(nc_path_to_directions=""): ds = Dataset(nc_path_to_directions) margin = 20 var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][:][margin:-margin, margin:-margin] lkfr = ds.variables["lake_fraction"][:][margin:-margin, margin:-margin] lkouts = ds.variables["lake_outlet"][:][margin:-margin, margin:-margin] lkids = calculate_lake_ids(fldr, lkfr, lkouts) # plotting i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) lons, lats = [ds.variables[key][margin:-margin, margin:-margin] for key in ["lon", "lat"]] bsmap = gc.get_basemap(lons=lons, lats=lats) x, y = bsmap(lons, lats) fig = plt.figure(figsize=(15, 15)) img = bsmap.pcolormesh(x, y, lkids) bsmap.colorbar(img) bsmap.pcolormesh(x, y, lkouts, cmap="gray_r") nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] bsmap.quiver(x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1) bsmap.drawcoastlines(linewidth=0.5) bsmap.drawrivers(color="b") # plt.savefig(nc_path_to_directions[:-3] + "png", bbox_inches="tight") plt.show()
def plot_acc_area_with_glaciers(): gmask_vname = "VF" gmask_level = 2 # gmask_path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/geophys_West_NA_0.25deg_144x115_GLNM_PRSF_CanHR85" gmask_path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/geophys_CORDEX_NA_0.11deg_695x680_filled_grDes_barBor_Crop2Gras_peat" # stab reading of the glacier mask # r = RPN(gmask_path) # gmask = r.get_first_record_for_name_and_level(varname=gmask_vname, # level=gmask_level) r = RPN("/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/pm2013061400_00000000p") r.get_first_record_for_name("PR") # Because I almost sure that PR is there proj_params = r.get_proj_parameters_for_the_last_read_rec() rll = RotatedLatLon(**proj_params) lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec() basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="i") # gmask = np.ma.masked_where(gmask < 0.01, gmask) gmask = np.ma.masked_all(lons.shape) mask_value = 0.25 gmask[~gmask.mask] = mask_value path = "/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Java/DDM/directions_WestCaUs_dx0.11deg.nc" # path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/infocell_West_NA_0.25deg_104x75_GLNM_PRSF_CanHR85_104x75.nc" ds = Dataset(path) margin = 20 var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][:][margin:-margin, margin:-margin] i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) x, y = basemap(lons, lats) fig = plt.figure(figsize=(15, 15)) dx = (x[-1, -1] - x[0, 0]) / float(x.shape[0]) dy = (y[-1, -1] - y[0, 0]) / float(y.shape[1]) x1 = x - dx / 2.0 y1 = y - dy / 2.0 # im = basemap.pcolormesh(x1, y1, data, norm=LogNorm(vmin=1e3, vmax=1e7), cmap=cm.get_cmap("jet", 12)) # cb = basemap.colorbar(im) # cb.ax.tick_params(labelsize=25) cmap = cm.get_cmap("gray_r", 10) basemap.pcolormesh(x1, y1, gmask, cmap=cmap, vmin=0., vmax=1.) nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] basemap.quiver(x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1) basemap.drawcoastlines(linewidth=0.5) basemap.drawrivers(color="b") # plt.legend([Rectangle((0, 0), 5, 5, fc=cmap(mask_value)), ], ["Glaciers", ], loc=3) # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/athabasca/athabasca_dissolved", "atabaska", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/fraizer/fraizer", "frazier", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/South_sas/South_sas_dissolved", "south_sask", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/north_sas/north_sas", "north_sask", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/watersheds_up_sas/watershed_up_sas_proj", # "upsas", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network/network", "rivers", # zorder=2, linewidth=0.5, color="b") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network_up_sas/network_up_sout_sas_proj", "network", # zorder=2, linewidth=0.5, color="b") basemap.readshapefile("/skynet3_exec2/aganji/NE_can/bow_river/bow_projected", "basin", color="m", linewidth=2) # plt.savefig("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/directions.png", bbox_inches="tight") plt.savefig("/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/directions.png", bbox_inches="tight") plt.show()
def plot_positions_of_station_list(ax, stations, model_points, basemap, cell_manager, fill_upstream_areas=True): assert isinstance(ax, Axes) from util import direction_and_value delta = 1.0 / float(len(model_points)) darkness = 0 cmap = cm.get_cmap(name="jet", lut = len(model_points)) artists = [] labels = [] if None not in stations: st_to_mp = dict(list(zip(stations, model_points))) stlist_sorted = list(sorted(stations, key=lambda st: st.drainage_km2, reverse=True)) mplist_sorted = [st_to_mp[s] for s in stlist_sorted] else: mp_to_st = dict(list(zip(model_points, stations))) mplist_sorted = list(sorted(model_points, key=lambda mp: mp.latitude, reverse=True)) stlist_sorted = [mp_to_st[mp] for mp in mplist_sorted] for the_station, the_model_point in zip(stlist_sorted, mplist_sorted): assert the_station is None or isinstance(the_station, Station) assert isinstance(the_model_point, ModelPoint) assert isinstance(cell_manager, CellManager) x, y = basemap(cell_manager.lons2d, cell_manager.lats2d) if the_station is not None: x_station, y_station = basemap(the_station.longitude, the_station.latitude) point_info = "{0}".format(the_station.id) else: x_station, y_station = basemap(the_model_point.longitude, the_model_point.latitude) point_info = "({0}, {1})".format(the_model_point.ix, the_model_point.jy) basemap.scatter(x_station, y_station, c="r", s=20, ax=ax, linewidths=0.5, zorder=2) # show upstream cells if fill_upstream_areas: ups_mask = cell_manager.get_mask_of_upstream_cells_connected_with_by_indices(the_model_point.ix, the_model_point.jy) x1d_start = x[ups_mask == 1] y1d_start = y[ups_mask == 1] fld1d = cell_manager.flow_directions[ups_mask == 1] i_upstream, j_upstream = np.where(ups_mask == 1) labels.append(point_info) ishift, jshift = direction_and_value.flowdir_values_to_shift(fld1d) print(type(i_upstream[0]), type(ishift[0])) sub_i_upstream_next = i_upstream + ishift sub_j_upstream_next = j_upstream + jshift u = x[sub_i_upstream_next, sub_j_upstream_next] - x1d_start v = y[sub_i_upstream_next, sub_j_upstream_next] - y1d_start u2d = np.ma.masked_all_like(x) v2d = np.ma.masked_all_like(y) u2d[i_upstream, j_upstream] = u v2d[i_upstream, j_upstream] = v # basemap.quiver(x, y, u2d, v2d, angles="xy", scale_units="xy", scale=1, ax=ax) img = basemap.pcolormesh(x, y, np.ma.masked_where(ups_mask < 0.5, ups_mask) * darkness, cmap= cmap, ax=ax, vmax=1, vmin=0) p = Rectangle((0, 0), 1, 1, fc=cmap(darkness)) artists.append(p) va = "top" ha = "left" if the_station.id in ["093806", "081007"] else "right" yshift = -2 if the_station.id in ["093806", "081007"] else -2 xshift = -2 ax.annotate("{}".format(the_station.id), (x_station, y_station), ha=ha, va=va, textcoords="offset points", xytext=(xshift, yshift), font_properties=FontProperties(size=12)) # ax.text(x_station, y_station, point_info, bbox=dict(facecolor="white")) darkness += delta # ax.legend(artists, labels, handleheight = 1, ncol = 3, loc=2) basemap.drawcoastlines(linewidth=cpp.COASTLINE_WIDTH) basemap.drawrivers(cpp.COASTLINE_WIDTH)
def plot_acc_area_with_glaciers( gmask_vname: str = "VF", gmask_level=2, gmask_path="/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/geophys_West_NA_0.25deg_144x115_GLNM_PRSF_CanHR85", route_data_path="/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/directions_north_america_0.25deg_glaciers.nc", lons_target=None, lats_target=None, basin_shape_files=None): plot_scatter = False # stab reading of the glacier mask with RPN(gmask_path) as r: gmask = r.get_first_record_for_name_and_level(varname=gmask_vname, level=gmask_level) # r = RPN("/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/pm2013061400_00000000p") r.get_first_record_for_name( "VF") # Because I almost sure that PR is there proj_params = r.get_proj_parameters_for_the_last_read_rec() rll = RotatedLatLon(**proj_params) lons_gmask, lats_gmask = r.get_longitudes_and_latitudes_for_the_last_read_rec( ) gl_fraction_limit = 0.01 gmask = np.ma.masked_where(gmask < gl_fraction_limit, gmask) mask_value = 0.25 gmask[~gmask.mask] = mask_value if str(route_data_path).endswith(".nc"): print("route_data_path ends with .nc => assuming netcdf format: {}". format(route_data_path)) with Dataset(route_data_path) as ds: var_name = "accumulation_area" data = ds.variables[var_name][:] # flow directions fldr = ds.variables["flow_direction_value"][:] coord_names = ["lon", "lat"] if "lon" in ds.variables else [ "longitudes", "latitudes" ] lons_route, lats_route = [ds.variables[k] for k in coord_names] else: print( "route_data_path does not end with .nc => assuming rpn format: {}". format(route_data_path)) with RPN(route_data_path) as r: data = r.get_first_record_for_name("FACC") fldr = r.get_first_record_for_name("FLDR") lons_route, lats_route = r.get_longitudes_and_latitudes_for_the_last_read_rec( ) # do the spatial interpolation if required xg, yg, zg = lat_lon.lon_lat_to_cartesian(lons_gmask.flatten(), lats_gmask.flatten()) xr, yr, zr = lat_lon.lon_lat_to_cartesian(lons_route.flatten(), lats_route.flatten()) if lons_target is None or lats_target is None: lons_target, lats_target = lons_route, lats_route xt, yt, zt = xr, yr, zr else: xt, yt, zt = lat_lon.lon_lat_to_cartesian(lons_target.flatten(), lats_target.flatten()) basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons_target, lats2d=lats_target, resolution="i") ktree_route = KDTree(list(zip(xr, yr, zr))) dists_route, inds_route = ktree_route.query(list(zip(xt, yt, zt))) data = data.flatten()[inds_route].reshape(lons_target.shape) fldr = fldr.flatten()[inds_route].reshape(lons_target.shape) ktree_gmask = KDTree(list(zip(xg, yg, zg))) dists_gmask, inds_gmask = ktree_gmask.query(list(zip(xt, yt, zt))) gmask = gmask.flatten()[inds_gmask].reshape(lons_target.shape) data = np.ma.masked_where(data <= 0, data) i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) xx, yy = basemap(lons_target, lats_target) fig = plt.figure(figsize=(15, 15)) dx = (xx[-1, -1] - xx[0, 0]) / float(xx.shape[0]) dy = (yy[-1, -1] - yy[0, 0]) / float(yy.shape[1]) x1 = xx - dx / 2.0 y1 = yy - dy / 2.0 # Uncomment to plot the accumulation areas im = basemap.pcolormesh(x1, y1, data, norm=LogNorm(vmin=1e3, vmax=1e7), cmap=cm.get_cmap("jet", 12)) cb = basemap.colorbar(im) cmap = cm.get_cmap("gray_r", 10) basemap.pcolormesh(x1, y1, gmask, cmap=cmap, vmin=0., vmax=1.) nx, ny = xx.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(xx) v = np.ma.masked_all_like(xx) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = xx[inds_i_next[good], inds_j_next[good]] - xx[inds_i[good], inds_j[good]] v[good] = yy[inds_i_next[good], inds_j_next[good]] - yy[inds_i[good], inds_j[good]] basemap.quiver(xx, yy, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1) basemap.drawcoastlines(linewidth=0.5, zorder=5) basemap.drawrivers(color="lightcoral", zorder=5, linewidth=3) plt.legend([ Rectangle((0, 0), 5, 5, fc=cmap(mask_value)), ], [ r"Glacier ($\geq {}\%$)".format(gl_fraction_limit * 100), ], loc=3) watershed_bndry_width = 4 if basin_shape_files is not None: for i, the_shp in enumerate(basin_shape_files): basemap.readshapefile(the_shp[:-4], "basin_{}".format(i), zorder=2, color="m", linewidth=watershed_bndry_width) # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/athabasca/athabasca_dissolved", "atabaska", # zorder=2, linewidth=watershed_bndry_width, color="m") # # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/fraizer/fraizer", "frazier", # zorder=2, linewidth=watershed_bndry_width, color="m") # # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/South_sas/South_sas_dissolved", "south_sask", # zorder=2, linewidth=watershed_bndry_width, color="m") # # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/north_sas/north_sas", "north_sask", # zorder=2, linewidth=watershed_bndry_width, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/watersheds_up_sas/watershed_up_sas_proj", # "upsas", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network/network", "rivers", # zorder=2, linewidth=0.5, color="b") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network_up_sas/network_up_sout_sas_proj", "network", # zorder=2, linewidth=0.5, color="b") if plot_scatter: points_lat = [51.54, 49.2476] points_lon = [-122.277, -122.784] point_x, point_y = basemap(points_lon, points_lat) basemap.scatter(point_x, point_y, c="g", s=20, zorder=3) plt.savefig("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/directions_only.png", bbox_inches="tight", dpi=300) # plt.savefig("/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/directions.png", bbox_inches="tight") plt.show()
def plot_directions(nc_path_to_directions="", grid_config=gc, margin=20, shape_path_to_focus_polygons=None): """ :param margin: margin=20 corresponds to the usual free zone :param grid_config: :param nc_path_to_directions: :param shape_path_to_focus_polygons: if Not None, the path to the polygons Everything outside the specified polygons is masked """ fig = plt.figure(figsize=(15, 15)) ds = Dataset(nc_path_to_directions) var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][margin:-margin, margin:-margin] i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) lons, lats = [ds.variables[key][margin:-margin, margin:-margin] for key in ["lon", "lat"]] reg_of_interest = None if shape_path_to_focus_polygons is not None: reg_of_interest = get_mask(lons, lats, shp_path=shape_path_to_focus_polygons) > 0 i_list, j_list = np.where(reg_of_interest) mask_margin = 5 # margin of the mask in grid points i_min = min(i_list) - mask_margin i_max = max(i_list) + mask_margin j_min = min(j_list) - mask_margin j_max = max(j_list) + mask_margin bsmap = grid_config.get_basemap( lons=lons[i_min : i_max + 1, j_min : j_max + 1], lats=lats[i_min : i_max + 1, j_min : j_max + 1] ) assert isinstance(bsmap, Basemap) bsmap.readshapefile(shapefile=shape_path_to_focus_polygons[:-4], name="basin", linewidth=1, color="m") else: bsmap = grid_config.get_basemap(lons=lons, lats=lats) x, y = bsmap(lons, lats) nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] bsmap.fillcontinents(color="0.2", lake_color="aqua") bsmap.drawmapboundary(fill_color="aqua") bsmap.quiver( x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1, zorder=5 ) bsmap.drawcoastlines(linewidth=0.5) bsmap.drawrivers(color="b") plt.savefig(nc_path_to_directions[:-3] + ".png", bbox_inches="tight")
def get_model_points_for_stations(self, station_list, lake_fraction=None, drainaige_area_reldiff_limit=None, nneighbours=4): """ returns a map {station => modelpoint} for comparison modeled streamflows with observed :rtype dict """ # if drainaige_area_reldiff_limit is None: # drainaige_area_reldiff_limit = self.DEFAULT_DRAINAGE_AREA_RELDIFF_MIN # if nneighbours == 1: # raise Exception("Searching over 1 neighbor is not very secure and not implemented yet") station_to_model_point = {} model_acc_area = self.accumulation_area_km2 model_acc_area_1d = model_acc_area.flatten() grid = np.indices(model_acc_area.shape) for s in station_list: x, y, z = lat_lon.lon_lat_to_cartesian(s.longitude, s.latitude) if s.drainage_km2 is None or nneighbours == 1: # return the closest grid point dists, inds = self.kdtree.query((x, y, z), k=1) ix, jy = [g1.flatten()[inds] for g1 in grid] imin = 0 dists = [dists] if s.drainage_km2 is None: print("Using the closest grid point, since the station does not report its drainage area: {}".format(s)) else: if s.drainage_km2 < self.characteristic_distance ** 2 * 1e-12: print("skipping {0}, because drainage area is too small: {1} km**2".format(s.id, s.drainage_km2)) continue assert isinstance(s, Station) dists, inds = self.kdtree.query((x, y, z), k=nneighbours) deltaDaMin = np.min(np.abs(model_acc_area_1d[inds] - s.drainage_km2)) # this returns a list of numpy arrays imin = np.where(np.abs(model_acc_area_1d[inds] - s.drainage_km2) == deltaDaMin)[0][0] # deltaDa2D = np.abs(self.accumulation_area_km2 - s.drainage_km2) # ij = np.where(deltaDa2D == deltaDaMin) ix, jy = grid[0].flatten()[inds][imin], grid[1].flatten()[inds][imin] # check if it is not global lake cell (move downstream if it is) if lake_fraction is not None: while lake_fraction[ix, jy] >= infovar.GLOBAL_LAKE_FRACTION: di, dj = direction_and_value.flowdir_values_to_shift(self.flow_directions[ix, jy]) ix, jy = ix + di, jy + dj # check if the gridcell is not too far from the station # if dists[imin] > 2 * self.characteristic_distance: # continue # check if difference in drainage areas is not too big less than 10 % if drainaige_area_reldiff_limit is not None and deltaDaMin / s.drainage_km2 > drainaige_area_reldiff_limit: print("Drainage area relative difference is too high, skipping {}.".format(s.id)) print(deltaDaMin / s.drainage_km2, deltaDaMin, s.drainage_km2) continue mp = ModelPoint() mp.ix = ix mp.jy = jy mp.longitude = self.lons2d[ix, jy] mp.latitude = self.lats2d[ix, jy] mp.accumulation_area = self.accumulation_area_km2[ix, jy] try: mp.distance_to_station = dists[imin] except TypeError: mp.distance_to_station = float(dists) station_to_model_point[s] = mp print("mp.accumulation_area_km2={}; s.drainage_km2={}".format(mp.accumulation_area, s.drainage_km2)) print("Found model point for the station {0}".format(s)) return station_to_model_point
def plot_flow_directions_and_basin_boundaries(ax, s, sim_name_to_station_to_model_point, sim_name_to_manager=None): assert isinstance(ax, Axes) assert isinstance(s, Station) assert isinstance(sim_name_to_station_to_model_point, dict) mp_list = list(sim_name_to_station_to_model_point.items())[0][1][s] #selecting only one (the first model point) mp = mp_list[0] manager = list(sim_name_to_manager.items())[0][1] assert isinstance(manager, Crcm5ModelDataManager) flow_in_mask = manager.get_mask_for_cells_upstream(mp.ix, mp.jy) lons2d, lats2d = manager.lons2D, manager.lats2D i_upstream, j_upstream = np.where(flow_in_mask == 1) nx_total, ny_total = lons2d.shape imin, imax = np.min(i_upstream), np.max(i_upstream) jmin, jmax = np.min(j_upstream), np.max(j_upstream) margin = 8 imin = max(0, imin - margin) jmin = max(0, jmin - margin) imax = min(nx_total - 1, imax + margin) jmax = min(ny_total - 1, jmax + margin) sub_lons2d = lons2d[imin:imax + 1, jmin:jmax + 1] sub_lats2d = lats2d[imin:imax + 1, jmin:jmax + 1] sub_flow_directions = manager.flow_directions[imin:imax + 1, jmin:jmax + 1] sub_flow_in_mask = flow_in_mask[imin:imax + 1, jmin:jmax + 1] sub_i_upstream, sub_j_upstream = np.where(sub_flow_in_mask == 1) basemap = Crcm5ModelDataManager.get_rotpole_basemap_using_lons_lats( lons2d=sub_lons2d, lats2d=sub_lats2d, resolution="h" ) #plot all stations #stations = sim_name_to_station_to_model_point.items()[0][1].keys() x_list = [the_station.longitude for the_station in (s,)] y_list = [the_station.latitude for the_station in (s,)] basemap_big = Crcm5ModelDataManager.get_rotpole_basemap_using_lons_lats( lons2d=lons2d, lats2d=lats2d ) x_list, y_list = basemap_big(x_list, y_list) basemap_big.scatter(x_list, y_list, c="r", s=40, linewidths=0, ax=ax) basemap_big.drawcoastlines(ax=ax) basemap_big.drawrivers(ax=ax) ax.annotate(s.id, xy=basemap(s.longitude, s.latitude), xytext=(3, 3), textcoords='offset points', font_properties=FontProperties(weight="bold"), bbox=dict(facecolor='w', alpha=1), ha="left", va="bottom", zorder=2) x_big, y_big = basemap_big(manager.lons2D, manager.lats2D) ####zoom to the area of interest #axins = zoomed_inset_axes(ax, 3, loc=2) displayCoords = ax.transData.transform((x_big[imin, jmin], y_big[imin, jmin])) x_shift_fig, y_shift_fig = ax.figure.transFigure.inverted().transform(displayCoords) print("After transData", displayCoords) print("xshift and yshift", x_shift_fig, y_shift_fig) #ax.annotate("yuyu", xy = ( 0.733264985153, 0.477182994408), xycoords = "figure fraction" ) rect = [0.1, y_shift_fig + 0.1, 0.4, 0.4] axins = ax.figure.add_axes(rect) #assert isinstance(axins, Axes) x, y = basemap(sub_lons2d, sub_lats2d) x1d_start = x[sub_flow_in_mask == 1] y1d_start = y[sub_flow_in_mask == 1] fld1d = sub_flow_directions[sub_flow_in_mask == 1] from util import direction_and_value ishift, jshift = direction_and_value.flowdir_values_to_shift(fld1d) sub_i_upstream_next = sub_i_upstream + ishift sub_j_upstream_next = sub_j_upstream + jshift u = x[sub_i_upstream_next, sub_j_upstream_next] - x1d_start v = y[sub_i_upstream_next, sub_j_upstream_next] - y1d_start u2d = np.ma.masked_all_like(x) v2d = np.ma.masked_all_like(y) u2d[sub_i_upstream, sub_j_upstream] = u v2d[sub_i_upstream, sub_j_upstream] = v basemap.quiver(x, y, u2d, v2d, angles="xy", scale_units="xy", scale=1, ax=axins) x_list = [the_station.longitude for the_station in (s,)] y_list = [the_station.latitude for the_station in (s,)] x_list, y_list = basemap(x_list, y_list) basemap.scatter(x_list, y_list, c="r", s=40, linewidths=0) basemap.drawcoastlines(ax=axins) basemap.drawrivers(ax=axins) #read waterbase file, and plot only the polygons which contain at least one upstream point shp_path = "/skynet3_exec1/huziy/Netbeans Projects/Java/DDM/data/shape/waterbase/na_bas_ll_r500m/na_bas_ll_r500m.shp" c = fiona.open(shp_path) hits = c.filter(bbox=(sub_lons2d[0, 0], sub_lats2d[0, 0], sub_lons2d[-1, -1], sub_lats2d[-1, -1])) points = [Point(the_x, the_y) for the_x, the_y in zip(x1d_start, y1d_start)] selected_polygons = [] for feature in hits: new_coords = [] old_coords = feature["geometry"]["coordinates"] #transform to the map coordinates for ring in old_coords: x1 = [tup[0] for tup in ring] y1 = [tup[1] for tup in ring] x2, y2 = basemap(x1, y1) new_coords.append(list(zip(x2, y2))) feature["geometry"]["coordinates"] = new_coords poly = shape(feature["geometry"]) #print poly, type(poly) #print feature.keys() #print feature["properties"] prep_poly = prep.prep(poly) hits = list(filter(prep_poly.contains, points)) if len(hits) > 2: selected_polygons.append(feature["geometry"]) for p in selected_polygons: axins.add_patch(PolygonPatch(p, fc="none", ec="b", lw=1.5)) zoom_lines_color = "#6600FF" #set color of the frame for child in axins.get_children(): if isinstance(child, Spine): child.set_color(zoom_lines_color) child.set_linewidth(3) # draw a bbox of the region of the inset axes in the parent axes and # connecting lines between the bbox and the inset axes area mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec=zoom_lines_color, lw=3) #basemap.readshapefile(, "basin") pass
def _plot_station_position(ax, the_station, basemap, cell_manager, the_model_point): assert the_station is None or isinstance(the_station, Station) assert isinstance(the_model_point, ModelPoint) assert isinstance(cell_manager, CellManager) assert isinstance(ax, Axes) x, y = basemap(cell_manager.lons2d, cell_manager.lats2d) # plot the arrows for upstream cells ups_mask = cell_manager.get_mask_of_upstream_cells_connected_with_by_indices(the_model_point.ix, the_model_point.jy) x1d_start = x[ups_mask == 1] y1d_start = y[ups_mask == 1] fld1d = cell_manager.flow_directions[ups_mask == 1] i_upstream, j_upstream = np.where(ups_mask == 1) imin, jmin = i_upstream.min() - 60, j_upstream.min() - 60 imax, jmax = i_upstream.max() + 40, j_upstream.max() + 40 nx, ny = x.shape imax, jmax = min(imax, nx - 1), min(jmax, ny - 1) imin, jmin = max(imin, 0), max(jmin, 0) basemap_initial_corners = [ basemap.llcrnrx, basemap.llcrnry, basemap.urcrnrx, basemap.urcrnry ] basemap.llcrnrx = x[imin, jmin] basemap.llcrnry = y[imin, jmin] basemap.urcrnrx = x[imax, jmax] basemap.urcrnry = y[imax, jmax] if the_station is not None: x_station, y_station = basemap(the_station.longitude, the_station.latitude) else: x_station, y_station = basemap(the_model_point.longitude, the_model_point.latitude) basemap.scatter(x_station, y_station, c="b", s=100, ax=ax, linewidths=1, zorder=2) ax.annotate("{}".format(the_station.id), (x_station, y_station), va="top", ha="right") from util import direction_and_value ishift, jshift = direction_and_value.flowdir_values_to_shift(fld1d) sub_i_upstream_next = i_upstream + ishift sub_j_upstream_next = j_upstream + jshift u = x[sub_i_upstream_next, sub_j_upstream_next] - x1d_start v = y[sub_i_upstream_next, sub_j_upstream_next] - y1d_start u2d = np.ma.masked_all_like(x) v2d = np.ma.masked_all_like(y) u2d[i_upstream, j_upstream] = u v2d[i_upstream, j_upstream] = v # basemap.quiver(x, y, u2d, v2d, angles="xy", scale_units="xy", scale=1, ax=ax) basemap.pcolormesh(x, y, np.ma.masked_where(ups_mask < 0.5, ups_mask) * 0.5, cmap=cm.get_cmap(name="gray"), ax=ax, vmax=1, vmin=0, zorder=1) basemap.drawcoastlines(linewidth=cpp.COASTLINE_WIDTH) # put back the initial corners of the basemap basemap.llcrnrx, basemap.llcrnry, basemap.urcrnrx, basemap.urcrnry = basemap_initial_corners return ups_mask
def plot_directions(nc_path_to_directions="", grid_config=gc, margin=20, shape_path_to_focus_polygons=None): """ :param margin: margin=20 corresponds to the usual free zone :param grid_config: :param nc_path_to_directions: :param shape_path_to_focus_polygons: if Not None, the path to the polygons Everything outside the specified polygons is masked """ fig = plt.figure(figsize=(15, 15)) ds = Dataset(nc_path_to_directions) var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][margin:-margin, margin:-margin] i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) lons, lats = [ds.variables[key][margin:-margin, margin:-margin] for key in ["lon", "lat"]] reg_of_interest = None if shape_path_to_focus_polygons is not None: reg_of_interest = get_mask(lons, lats, shp_path=shape_path_to_focus_polygons) > 0 i_list, j_list = np.where(reg_of_interest) mask_margin = 5 # margin of the mask in grid points i_min = min(i_list) - mask_margin i_max = max(i_list) + mask_margin j_min = min(j_list) - mask_margin j_max = max(j_list) + mask_margin bsmap = grid_config.get_basemap(lons=lons[i_min:i_max + 1, j_min:j_max + 1], lats=lats[i_min:i_max + 1, j_min:j_max + 1]) assert isinstance(bsmap, Basemap) bsmap.readshapefile(shapefile=shape_path_to_focus_polygons[:-4], name="basin", linewidth=1, color="m") else: bsmap = grid_config.get_basemap(lons=lons, lats=lats) x, y = bsmap(lons, lats) nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] bsmap.fillcontinents(color='0.2', lake_color='aqua') bsmap.drawmapboundary(fill_color="aqua") bsmap.quiver(x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1, zorder=5) bsmap.drawcoastlines(linewidth=0.5) bsmap.drawrivers(color="b") plt.savefig(nc_path_to_directions[:-3] + ".png", bbox_inches="tight")
def main(nc_path_to_directions=""): ds = Dataset(nc_path_to_directions) margin = 20 var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][:][margin:-margin, margin:-margin] lkfr = ds.variables["lake_fraction"][:][margin:-margin, margin:-margin] lkouts = ds.variables["lake_outlet"][:][margin:-margin, margin:-margin] lkids = calculate_lake_ids(fldr, lkfr, lkouts) # plotting i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) lons, lats = [ ds.variables[key][margin:-margin, margin:-margin] for key in ["lon", "lat"] ] bsmap = gc.get_basemap(lons=lons, lats=lats) x, y = bsmap(lons, lats) fig = plt.figure(figsize=(15, 15)) img = bsmap.pcolormesh(x, y, lkids) bsmap.colorbar(img) bsmap.pcolormesh(x, y, lkouts, cmap="gray_r") nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] bsmap.quiver(x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1) bsmap.drawcoastlines(linewidth=0.5) bsmap.drawrivers(color="b") # plt.savefig(nc_path_to_directions[:-3] + "png", bbox_inches="tight") plt.show()
def plot_acc_area_with_glaciers(): gmask_vname = "VF" gmask_level = 2 # gmask_path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/geophys_West_NA_0.25deg_144x115_GLNM_PRSF_CanHR85" gmask_path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/geophys_CORDEX_NA_0.11deg_695x680_filled_grDes_barBor_Crop2Gras_peat" # stab reading of the glacier mask # r = RPN(gmask_path) # gmask = r.get_first_record_for_name_and_level(varname=gmask_vname, # level=gmask_level) r = RPN( "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/pm2013061400_00000000p" ) r.get_first_record_for_name("PR") # Because I almost sure that PR is there proj_params = r.get_proj_parameters_for_the_last_read_rec() rll = RotatedLatLon(**proj_params) lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec() basemap = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="i") # gmask = np.ma.masked_where(gmask < 0.01, gmask) gmask = np.ma.masked_all(lons.shape) mask_value = 0.25 gmask[~gmask.mask] = mask_value path = "/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Java/DDM/directions_WestCaUs_dx0.11deg.nc" # path = "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/infocell_West_NA_0.25deg_104x75_GLNM_PRSF_CanHR85_104x75.nc" ds = Dataset(path) margin = 20 var_name = "accumulation_area" data = ds.variables[var_name][margin:-margin, margin:-margin] data = np.ma.masked_where(data <= 0, data) # flow directions fldr = ds.variables["flow_direction_value"][:][margin:-margin, margin:-margin] i_shifts, j_shifts = direction_and_value.flowdir_values_to_shift(fldr) x, y = basemap(lons, lats) fig = plt.figure(figsize=(15, 15)) dx = (x[-1, -1] - x[0, 0]) / float(x.shape[0]) dy = (y[-1, -1] - y[0, 0]) / float(y.shape[1]) x1 = x - dx / 2.0 y1 = y - dy / 2.0 # im = basemap.pcolormesh(x1, y1, data, norm=LogNorm(vmin=1e3, vmax=1e7), cmap=cm.get_cmap("jet", 12)) # cb = basemap.colorbar(im) # cb.ax.tick_params(labelsize=25) cmap = cm.get_cmap("gray_r", 10) basemap.pcolormesh(x1, y1, gmask, cmap=cmap, vmin=0., vmax=1.) nx, ny = x.shape inds_j, inds_i = np.meshgrid(range(ny), range(nx)) inds_i_next = inds_i + i_shifts inds_j_next = inds_j + j_shifts inds_i_next = np.ma.masked_where((inds_i_next == nx) | (inds_i_next == -1), inds_i_next) inds_j_next = np.ma.masked_where((inds_j_next == ny) | (inds_j_next == -1), inds_j_next) u = np.ma.masked_all_like(x) v = np.ma.masked_all_like(x) good = (~inds_i_next.mask) & (~inds_j_next.mask) u[good] = x[inds_i_next[good], inds_j_next[good]] - x[inds_i[good], inds_j[good]] v[good] = y[inds_i_next[good], inds_j_next[good]] - y[inds_i[good], inds_j[good]] basemap.quiver(x, y, u, v, pivot="tail", width=0.0005, scale_units="xy", headlength=20, headwidth=15, scale=1) basemap.drawcoastlines(linewidth=0.5) basemap.drawrivers(color="b") # plt.legend([Rectangle((0, 0), 5, 5, fc=cmap(mask_value)), ], ["Glaciers", ], loc=3) # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/athabasca/athabasca_dissolved", "atabaska", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/fraizer/fraizer", "frazier", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/South_sas/South_sas_dissolved", "south_sask", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/north_sas/north_sas", "north_sask", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/watersheds_up_sas/watershed_up_sas_proj", # "upsas", # zorder=2, linewidth=3, color="m") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network/network", "rivers", # zorder=2, linewidth=0.5, color="b") # basemap.readshapefile("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/lat_lon/network_up_sas/network_up_sout_sas_proj", "network", # zorder=2, linewidth=0.5, color="b") basemap.readshapefile( "/skynet3_exec2/aganji/NE_can/bow_river/bow_projected", "basin", color="m", linewidth=2) # plt.savefig("/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/directions.png", bbox_inches="tight") plt.savefig( "/RESCUE/skynet3_rech1/huziy/CNRCWP/Calgary_flood/directions.png", bbox_inches="tight") plt.show()