def plot_domain_for_different_margins(path, margins=None): if not margins: margins = [20, 40, 60] rpnObj = RPN(path) lons2d, lats2d = rpnObj.get_longitudes_and_latitudes() # projection parameters lon_1 = -68 lat_1 = 52 lon_2 = 16.65 lat_2 = 0.0 rot_lat_lon = RotatedLatLon(lon1=lon_1, lat1=lat_1, lon2=lon_2, lat2=lat_2) xll, yll = rot_lat_lon.toProjectionXY(lons2d[0, 0], lats2d[0, 0]) xur, yur = rot_lat_lon.toProjectionXY(lons2d[-1, -1], lats2d[-1, -1]) if xll < 0: xll += 360.0 if xur < 0: xur += 360.0 nx, ny = lons2d.shape dx = (xur - xll) / float(nx - 1) dy = (yur - yll) / float(ny - 1) print(dx, dy) print(xur, yur, xll, yll) x1 = xll - dx / 2.0 y1 = yll - dy / 2.0 x2 = xur + dx / 2.0 y2 = yur + dy / 2.0 x1lon, y1lat = rot_lat_lon.toGeographicLonLat(x1, y1) x2lon, y2lat = rot_lat_lon.toGeographicLonLat(x2, y2) llcrnrlon, llcrnrlat = rot_lat_lon.toGeographicLonLat(x1 - dx, y1 - dx) urcrnrlon, urcrnrlat = rot_lat_lon.toGeographicLonLat(x2 + dx, y2 + dx) basemap = Basemap(projection="omerc", lon_1=lon_1, lat_1=lat_1, lon_2=lon_2, lat_2=lat_2, llcrnrlon=llcrnrlon, llcrnrlat=llcrnrlat, urcrnrlon=urcrnrlon, urcrnrlat=urcrnrlat, no_rot=True, resolution="l") basemap.drawcoastlines() basemap.drawrivers() x1, y1 = basemap(x1lon, y1lat) x2, y2 = basemap(x2lon, y2lat) # add rectangle for the grid 220x220 # r1 = Rectangle((x1, y1), x2-x1, y2-y1, facecolor="none", edgecolor="r", linewidth=5 ) ax = plt.gca() assert isinstance(ax, Axes) # xr1_label, yr1_label = rot_lat_lon.toGeographicLonLat(xur - 2 * dx, yll + 2 * dy) # xr1_label, yr1_label = basemap( xr1_label, yr1_label ) # ax.annotate("{0}x{1}".format(nx, ny), xy = (xr1_label, yr1_label), va = "bottom", ha = "right", color = "r") # assert isinstance(ax, Axes) # ax.add_patch(r1) margins_all = [0] + margins for margin in margins_all: # mfree = margin - 20 xlli = xll + margin * dx ylli = yll + margin * dy xuri = xur - margin * dx yuri = yur - margin * dy x1lon, y1lat = rot_lat_lon.toGeographicLonLat(xlli, ylli) x2lon, y2lat = rot_lat_lon.toGeographicLonLat(xuri, yuri) x1, y1 = basemap(x1lon, y1lat) x2, y2 = basemap(x2lon, y2lat) ri = Rectangle((x1, y1), x2 - x1, y2 - y1, facecolor="none", edgecolor="r", linewidth=5) ax.add_patch(ri) xri_label, yri_label = rot_lat_lon.toGeographicLonLat( xlli + 2 * dx, yuri - 2 * dy) xri_label, yri_label = basemap(xri_label, yri_label) ax.annotate("{0}x{1}\nmarg. = {2}".format(nx - margin * 2, ny - margin * 2, margin + 20), xy=(xri_label, yri_label), va="top", ha="left", color="k", backgroundcolor="w") plt.show()
def diagnose(station_ids=None, model_data_path=None): manager = Crcm5ModelDataManager(samples_folder_path=model_data_path, file_name_prefix="pm", all_files_in_samples_folder=True, need_cell_manager=True) nx, ny = manager.lons2D.shape rot_lat_lon = RotatedLatLon(lon1=-68, lat1=52, lon2=16.65, lat2=0.0) x00, y00 = rot_lat_lon.toProjectionXY(manager.lons2D[0, 0], manager.lats2D[0, 0]) x10, y10 = rot_lat_lon.toProjectionXY(manager.lons2D[1, 0], manager.lats2D[1, 0]) x01, y01 = rot_lat_lon.toProjectionXY(manager.lons2D[0, 1], manager.lats2D[0, 1]) dx = x10 - x00 dy = y01 - y00 print("dx, dy = {0}, {1}".format(dx, dy)) areas = rot_lat_lon.get_areas_of_gridcells( dx, dy, nx, ny, y00, 1) #1 -since the index is starting from 1 print(areas[0, 0]) start_date = datetime(1986, 1, 1) end_date = datetime(1986, 12, 31) stations = cehq_station.read_station_data(selected_ids=station_ids, start_date=start_date, end_date=end_date) stations.sort(key=lambda x: x.latitude, reverse=True) for i, s in enumerate(stations): fig = plt.figure() #3 columns gs = GridSpec(5, 3, hspace=0.2, wspace=0.2, right=0.98, left=0.1, top=0.98) model_ts = manager.get_streamflow_timeseries_for_station( s, start_date=start_date, end_date=end_date, nneighbours=9) print(model_ts.time[0], model_ts.time[-1]) i_model0, j_model0 = model_ts.metadata["ix"], model_ts.metadata["jy"] mask = manager.get_mask_for_cells_upstream(i_model0, j_model0) #hydrographs ax = fig.add_subplot(gs[0, 0]) plot_streamflows(ax, s, model_ts) #relative error ax = fig.add_subplot(gs[1, 0]) plot_streamflow_re(ax, s, model_ts) #directions plot_directions_and_positions(fig.add_subplot(gs[:2, 1]), s, model_ts, manager, rot_lat_lon, mask=mask) #runoff ax = fig.add_subplot(gs[2, 0]) plot_runoff(ax, manager, areas, model_ts, mask=mask) #runoff from gldas ax = fig.add_subplot(gs[2, 1]) #plot_gldas_runoff(ax, manager, areas, model_ts, mask = mask) #temperature ax_temp = fig.add_subplot(gs[3, 0]) ax_prec = fig.add_subplot(gs[4, 0]) plot_total_precip_and_temp_re_1d(ax_prec, ax_temp, manager, rot_lat_lon, areas, model_ts, mask=mask) #swe timeseries ax = fig.add_subplot(gs[3, 1]) plot_swe_timeseries(ax, manager, areas, model_ts, mask=mask) #print np.where(mask == 1) print("(i, j) = ({0}, {1})".format(model_ts.metadata["ix"], model_ts.metadata["jy"])) fig.savefig("diagnose_{0}_{1:.2f}deg.pdf".format(s.id, dx))
def plot_domain_for_different_margins(path, margins=None): if not margins: margins = [20, 40, 60] rpnObj = RPN(path) lons2d, lats2d = rpnObj.get_longitudes_and_latitudes() # projection parameters lon_1 = -68 lat_1 = 52 lon_2 = 16.65 lat_2 = 0.0 rot_lat_lon = RotatedLatLon(lon1=lon_1, lat1=lat_1, lon2=lon_2, lat2=lat_2) xll, yll = rot_lat_lon.toProjectionXY(lons2d[0, 0], lats2d[0, 0]) xur, yur = rot_lat_lon.toProjectionXY(lons2d[-1, -1], lats2d[-1, -1]) if xll < 0: xll += 360.0 if xur < 0: xur += 360.0 nx, ny = lons2d.shape dx = (xur - xll) / float(nx - 1) dy = (yur - yll) / float(ny - 1) print(dx, dy) print(xur, yur, xll, yll) x1 = xll - dx / 2.0 y1 = yll - dy / 2.0 x2 = xur + dx / 2.0 y2 = yur + dy / 2.0 x1lon, y1lat = rot_lat_lon.toGeographicLonLat(x1, y1) x2lon, y2lat = rot_lat_lon.toGeographicLonLat(x2, y2) llcrnrlon, llcrnrlat = rot_lat_lon.toGeographicLonLat(x1 - dx, y1 - dx) urcrnrlon, urcrnrlat = rot_lat_lon.toGeographicLonLat(x2 + dx, y2 + dx) basemap = Basemap(projection="omerc", lon_1=lon_1, lat_1=lat_1, lon_2=lon_2, lat_2=lat_2, llcrnrlon=llcrnrlon, llcrnrlat=llcrnrlat, urcrnrlon=urcrnrlon, urcrnrlat=urcrnrlat, no_rot=True, resolution="l") basemap.drawcoastlines() basemap.drawrivers() x1, y1 = basemap(x1lon, y1lat) x2, y2 = basemap(x2lon, y2lat) # add rectangle for the grid 220x220 # r1 = Rectangle((x1, y1), x2-x1, y2-y1, facecolor="none", edgecolor="r", linewidth=5 ) ax = plt.gca() assert isinstance(ax, Axes) # xr1_label, yr1_label = rot_lat_lon.toGeographicLonLat(xur - 2 * dx, yll + 2 * dy) # xr1_label, yr1_label = basemap( xr1_label, yr1_label ) # ax.annotate("{0}x{1}".format(nx, ny), xy = (xr1_label, yr1_label), va = "bottom", ha = "right", color = "r") # assert isinstance(ax, Axes) # ax.add_patch(r1) margins_all = [0] + margins for margin in margins_all: # mfree = margin - 20 xlli = xll + margin * dx ylli = yll + margin * dy xuri = xur - margin * dx yuri = yur - margin * dy x1lon, y1lat = rot_lat_lon.toGeographicLonLat(xlli, ylli) x2lon, y2lat = rot_lat_lon.toGeographicLonLat(xuri, yuri) x1, y1 = basemap(x1lon, y1lat) x2, y2 = basemap(x2lon, y2lat) ri = Rectangle((x1, y1), x2 - x1, y2 - y1, facecolor="none", edgecolor="r", linewidth=5) ax.add_patch(ri) xri_label, yri_label = rot_lat_lon.toGeographicLonLat(xlli + 2 * dx, yuri - 2 * dy) xri_label, yri_label = basemap(xri_label, yri_label) ax.annotate("{0}x{1}\nmarg. = {2}".format(nx - margin * 2, ny - margin * 2, margin + 20), xy=(xri_label, yri_label), va="top", ha="left", color="k", backgroundcolor="w") plt.show()