def plot_temperature_biases(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) cru_var_name = "tmp" cru_data_store, crcm4_data_store = _get_comparison_data(cru_var_name= cru_var_name, season_to_months=season_to_months) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,2) color_map = my_cm.get_red_blue_colormap(ncolors = 14, reversed=True) clevels = xrange(-8, 9, 2) all_plot_axes = [] for i, season in enumerate(seasons): if not i: ax = fig.add_subplot(gs[0,:]) else: row, col = (i - 1) // 2 + 1, (i - 1) % 2 ax = fig.add_subplot(gs[row, col]) all_plot_axes.append(ax) assert isinstance(ax, Axes) delta = crcm4_data_store[season] - cru_data_store[season] if cru_var_name == "tmp": delta -= 273.15 #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta[:, :] = np.ma.masked delta[i_array, j_array] = save img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = -7, vmax = 7) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") int_ticker = LinearLocator(numticks = color_map.N + 1) fig.colorbar(img, cax = cax, ticks = MultipleLocator(base = 2)) ax.set_title(season) for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) put_selected_stations(the_ax, basemap, i_array, j_array) #gs.tight_layout(fig) fig.suptitle("T(2m), degrees, CRCM4 - CRU") fig.savefig("seasonal_{0}_ccc.png".format(cru_var_name))
def plot_high_flows(period = 10, imagefile = 'figure.png', pars_set = None, indices_file = 'data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc'): print('generating %s ' % imagefile) plt.clf() levs = [] i_list, j_list = data_select.get_indices_from_file(indices_file) #iterate through all grid points for pars in pars_set: lev = get_high_ret_level_stationary(pars, period) if lev < 0: print('period = ', period) print('pars = ', pars) assert False, 'in plot_high_flows' assert lev >= 0, pars levs.append( lev ) to_plot = np.ma.masked_all(xs.shape) for lev, i, j in zip(levs, i_list, j_list): assert np.isfinite(lev) if isinf(lev): print(lev) to_plot[i,j] = lev nticks = 15 color_map = mpl.cm.get_cmap('RdBu',nticks) int_ticker = LinearLocator(numticks = color_map.N + 1) m.pcolormesh(xs, ys, to_plot.copy(), cmap = color_map) plt.title('high flow, return period is {0}'.format(period)) boundaries.plot_basin_boundaries_from_shape(m, plt, linewidth = 0.5) m.drawcoastlines() # plot_directions(data_mask = to_plot) plt.colorbar( ticks = int_ticker, format = "%.1f" ) zoom_to_qc() plt.savefig(imagefile, bbox_inches = 'tight')
def plot(self): basemap = polar_stereographic.basemap plt.subplot(3, 1, 1) gevfit.plot_data( self.ret_level_2yr, imagefile=None, units="m**3/s", minmax=(0, None), i_list=self._i_indices, j_list=self._j_indices, ) plt.title("2 year return levels") plt.subplot(3, 1, 2) gevfit.plot_data( self.median_field, imagefile=None, units="m**3/s", minmax=(0, None), i_list=self._i_indices, j_list=self._j_indices, ) plt.title("median") plt.subplot(3, 1, 3) gevfit.plot_data( np.array(self.median_field) - np.array(self.ret_level_2yr), imagefile=None, units="m**3/s", minmax=(None, None), i_list=self._i_indices, j_list=self._j_indices, ) plt.title("difference: median - 2 year return level") plot_basin_boundaries_from_shape(basemap, plotter=plt, linewidth=0.1) basemap.drawcoastlines() plt.savefig("median_test.png") pass
def _plot_map_as_subplots(i_indices, j_indices, extremetype_retperiod_cv_map, title = ""): plt.figure() i_subplot = 1 plt.subplots_adjust(hspace = 0.2, wspace = 0.2) #TODO: plot CV for the current and future climates plt.figtext(0.5, 0.05, title, horizontalalignment='center') max_value = 0.1 for extreme_type, ret_period_to_cv in extremetype_retperiod_cv_map.items(): #max_value = 0.5 if extreme_type == 'low' else 2.5 for ret_period, cv_field in ret_period_to_cv.items(): to_plot = np.ma.masked_all(csfb.xs.shape) for i, j, cv in zip(i_indices, j_indices, cv_field): to_plot[i, j] = cv plt.subplot(2,2, i_subplot) i_subplot += 1 cMap = mpl.cm.get_cmap('jet', 3) cMap.set_over(color = '#FF8C00') basin_boundaries.plot_basin_boundaries_from_shape(csfb.basemap, plotter = plt, linewidth = 1.6) image = csfb.basemap.pcolormesh(csfb.xs, csfb.ys, to_plot, vmin = 0, vmax = 1.5, cmap = cMap) plot_axes = plt.gca() divider = make_axes_locatable(plot_axes) cax = divider.append_axes("right", "8%", pad="3%") # extend choices are "both", "min", "max" and "neither" cb = plt.colorbar(image, extend = 'max', ticks = LinearLocator(numticks = 4), format = "%.2f", drawedges = True, cax = cax) cb.outline.set_visible(False) csfb.basemap.drawcoastlines(linewidth = 0.2) plt.title('Return period: {0} years,\n {1} flow event.'.format(ret_period, extreme_type)) x_min, x_max, y_min, y_max = plot_utils.get_ranges(csfb.xs[i_indices, j_indices], csfb.ys[i_indices, j_indices]) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max)
def plot_data(data = None, imagefile = 'image.png', units = 'm**3/s', minmax = (None, None), indices_file = 'data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc', color_map = mpl.cm.get_cmap('RdBu', 20), ticks_locator = LinearLocator(), i_list = None, j_list = None, title = ''): if imagefile != None: plt.clf() if i_list == None or j_list == None: i_list, j_list = data_select.get_indices_from_file(indices_file) to_plot = np.ma.masked_all(xs.shape) for i, j, value in zip(i_list, j_list, data): to_plot[i, j] = value plt.title(title) plt.pcolormesh(xs,ys,to_plot.copy(), cmap = color_map, edgecolors = 'None', antialiased = True, vmin = minmax[0], vmax = minmax[1]) m.drawcoastlines() boundaries.plot_basin_boundaries_from_shape(m, plotter = plt, linewidth = 0.5) draw_meridians_and_parallels(m, 10) # plot_directions(data_mask = to_plot) cb = plt.colorbar(ticks = ticks_locator, format = "%.1f") cb.ax.set_ylabel(units) ymin, ymax = plt.ylim() plt.ylim(ymin + 0.05 * (ymax - ymin) , ymax * 0.25) # xmin, xmax = plt.xlim() plt.xlim(xmin + (xmax - xmin) * 0.55, 0.72*xmax) if imagefile != None: plt.savefig(imagefile, bbox_inches = 'tight')
def plot_subplot(i_indices, j_indices, data_1d, mark = ''): basemap = polar_stereographic.basemap xs = polar_stereographic.xs ys = polar_stereographic.ys to_plot = np.ma.masked_all(xs.shape) for i, j, f_v in zip(i_indices, j_indices, data_1d): to_plot[i,j] = f_v basemap.pcolormesh(xs, ys, to_plot, cmap = mpl.cm.get_cmap('jet', 7) ) basemap.drawcoastlines() plt.colorbar(ticks = LinearLocator(numticks = 8), format = '%.1f') bb.plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 2) #zoom to domain selected_x = xs[~to_plot.mask] selected_y = ys[~to_plot.mask] marginx = abs(np.min(selected_x) * 5.0e-2) marginy = abs(np.min(selected_y) * 5.0e-2) plt.xlim(np.min(selected_x) - marginx, np.max(selected_x) + marginx) plt.ylim(np.min(selected_y) - marginy, np.max(selected_y) + marginy) title = '(%s)' % mark plt.title(title)
def plot_signal_to_noise_ratio(): low_periods = [2, 5] high_periods = [10, 30] plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5) extreme_types = ['high', 'low'] extreme_type_to_periods = dict(list(zip(extreme_types, [high_periods, low_periods]))) i_indices, j_indices = data_select.get_indices_from_file() i_subplot = 0 #plt.subplots_adjust(wspace = 0.1) cMap = mpl.cm.get_cmap('jet', 3) cMap.set_over(color = '#FF8C00') gs = gridspec.GridSpec(3,2) for extreme_type in extreme_types: for return_period in extreme_type_to_periods[extreme_type]: changes = np.zeros((len(members.current_ids), len(i_indices))) for member_num, the_member in enumerate(members.current_ids): pars_current = csfb.get_pars_for_member_and_type(the_member, level_type = extreme_type) pars_future = csfb.get_pars_for_member_and_type(members.current2future[the_member], level_type = extreme_type) #calculate changes for each pair of members and each position npos = len(pars_current) for pos, pc, pf in zip(range(npos), pars_current, pars_future): if extreme_type == 'high': c = gevfit.get_high_ret_level_stationary(pc, return_period) f = gevfit.get_high_ret_level_stationary(pf, return_period) else: c = gevfit.get_low_ret_level_stationary(pc, return_period) f = gevfit.get_low_ret_level_stationary(pf, return_period) changes[member_num, pos] = f - c pass #calculate mean and stdev of the obtained changes the_mean = np.mean(changes, axis = 0) the_std = np.std(changes, axis = 0) #change if you want signal to noise ratio, currently it is cv (coefficient of variation 1/(signal-to-noise-ratio)) the_values = the_std / np.abs(the_mean) print(the_values.min(), the_values.max()) #the_values = the_mean to_plot = np.ma.masked_all(csfb.xs.shape) max_value = 1.5 for i, j, value, dev in zip(i_indices, j_indices, the_values, the_std): to_plot[i, j] = value #shaded = np.ma.masked_where(to_plot != 0, shaded) #to_plot = np.ma.masked_where(to_plot == 0, to_plot) plot_axes = plt.subplot(gs[i_subplot // 2, i_subplot % 2]) i_subplot += 1 print('just before plotting') basin_boundaries.plot_basin_boundaries_from_shape(csfb.basemap, plotter = plt, linewidth = 1.) image = csfb.basemap.pcolormesh(csfb.xs, csfb.ys, to_plot, vmin = 0, vmax = max_value, cmap = cMap, ax = plot_axes) csfb.basemap.drawcoastlines(linewidth = 0.2) plot_axes.set_title('T = {0}-year'.format(return_period)) x_min, x_max, y_min, y_max = plot_utils.get_ranges(csfb.xs[i_indices, j_indices], csfb.ys[i_indices, j_indices]) plot_axes.set_xlim(x_min, x_max) plot_axes.set_ylim(y_min, y_max) divider = make_axes_locatable(plot_axes) cax = divider.append_axes("right", "8%", pad="3%") # extend choices are "both", "min", "max" and "neither" cb = plt.colorbar(image, extend = 'max', format = "%.1f", drawedges = True, cax = cax) cb.set_ticks(LinearLocator(numticks = 4)) cb.outline.set_visible(False) #plt.show() plt.tight_layout() plt.savefig('cv_for_changes.png')
def plot_selected_stations(selected_stations, plot_ts = True, save_to_file = True, use_warpimage = True, basemap = None, i_list = None, j_list = None ): """ document me """ fig = plt.figure() if basemap is None: the_basemap = polar_stereographic.basemap else: the_basemap = basemap # basemap.warpimage() if use_warpimage: map_url = 'http://earthobservatory.nasa.gov/Features/BlueMarble/images_bmng/8km/world.topo.200407.3x5400x2700.jpg' the_basemap.warpimage(image = map_url) the_basemap.drawcoastlines() # draw a land-sea mask for a map background. # lakes=True means plot inland lakes with ocean color. #basemap.drawlsmask(land_color='none', ocean_color='aqua',lakes=True) # basemap.drawrivers(color = 'blue') the_basemap.drawcoastlines() the_xs = [] the_ys = [] xs = polar_stereographic.xs ys = polar_stereographic.ys dx = 0.01 * ( xs[i_list, j_list].max() - xs[i_list, j_list].min() ) dy = 0.01 * ( ys[i_list, j_list].max() - ys[i_list, j_list].min() ) for station in selected_stations: x, y = the_basemap(station.longitude, station.latitude) xtext = 1.005 * x ytext = y if station.id in ['061906']: xtext = 1.00 * x ytext = 0.97 * y if station.id in ['103603', '081002']: ytext = 0.98 * y if station.id in ['081007']: xtext = 0.97 * x if station.id in ["090602"]: ytext -= 7 * dy xtext -= 5 * dx if station.id in ["090613"]: ytext += 4 * dy xtext -= 6 * dx the_xs.append(x) the_ys.append(y) plt.annotate(station.id, xy = (x, y), xytext = (xtext, ytext), font_properties = FontProperties(size = 18), bbox = dict(facecolor = 'white'), weight = "bold" #arrowprops=dict(facecolor='black', shrink=0.001) ) plot_basin_boundaries_from_shape(the_basemap, plotter=plt, linewidth=2.1) the_basemap.scatter(the_xs,the_ys, c = 'r', s = 400, marker='^', linewidth = 0.5, alpha = 1, zorder = 5) #plot the field of bedrock depth in meters # x, y = the_basemap(polar_stereographic.lons, polar_stereographic.lats) # bedrock_field = bedrock_depth.get_bedrock_depth_amno_180x172() # bedrock_field = np.ma.masked_where(bedrock_field < 0, bedrock_field) # #the_basemap.contourf(x, y, bedrock_field) # the_basemap.pcolormesh(x, y, bedrock_field) # cb = plt.colorbar(shrink = 0.7) #cb.set_label("m") if save_to_file: x_interest = polar_stereographic.xs[i_list, j_list] y_interest = polar_stereographic.ys[i_list, j_list] x_min, x_max = x_interest.min(), x_interest.max() y_min, y_max = y_interest.min(), y_interest.max() dx = 0.1 * ( x_max - x_min ) dy = 0.1 * ( y_max - y_min ) plt.xlim(x_min - dx, x_max + dx) plt.ylim(y_min - dy, y_max + dy) fig.tight_layout() fig.savefig('selected_stations.png') if plot_ts: plt.figure() nstations = len(selected_stations) plt.subplots_adjust(hspace = 0.2) for i, station in enumerate( selected_stations ): plt.subplot(nstations / 2 + nstations % 2, 2, i + 1) x = map(lambda date: date.year, station.dates) plt.plot(x, station.values) plt.title(station.id) if save_to_file: plt.savefig('station_time_series.png')
def plot(data_1d, i_indices, j_indices, xs, ys, title = '', label = "", minmax = (None, None), color_map = mpl.cm.get_cmap('RdBu'), units = '', colorbar_orientation = 'vertical' , basemap = None, colorbar_tick_locator = LinearLocator(numticks = 6), colorbar_label_format = '%.1f', upper_limited = False, not_significant_mask = None, show_colorbar = True, impose_lower_limit = None, imagefile = None, ax = None): fig = None if imagefile is not None: fig = plt.figure() to_plot = np.ma.masked_all(xs.shape) sign_mask_2d = np.ma.masked_all(xs.shape) if not_significant_mask is not None: data_1d_masked = np.ma.masked_where(~not_significant_mask.mask, data_1d) the_zip = list(zip(i_indices, j_indices, data_1d_masked, not_significant_mask)) for i_index, j_index, the_data, significance in the_zip: to_plot[i_index, j_index] = the_data sign_mask_2d[i_index, j_index] = significance #plot not significant mask basemap.pcolormesh(xs, ys, sign_mask_2d.copy(), cmap = mpl.cm.get_cmap('gist_gray', 3), shading = 'flat', vmin = -1, vmax = 1, ax = ax ) else: the_zip = list(zip(i_indices, j_indices, data_1d)) for i_index, j_index, the_data in the_zip: to_plot[i_index, j_index] = the_data image = basemap.pcolormesh(xs, ys, to_plot.copy(), cmap = color_map, vmin = minmax[0], vmax = minmax[1], shading = 'flat', rasterized = False, ax = ax ) plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 1, edge_color = 'k', ax = ax) basemap.drawcoastlines(linewidth = 0.5, ax = ax) #plot_utils.draw_meridians_and_parallels(basemap, step_degrees = 30) if ax is None: ax = plt.gca() x_min, x_max, y_min, y_max = plot_utils.get_ranges(xs[i_indices, j_indices], ys[i_indices, j_indices]) ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) #draw a label #xmin, xmax = plt.xlim() #ymin, ymax = plt.ylim() # dx = xmax - xmin #dy = ymax - ymin #plt.annotate(label, xy = (xmax - 0.1 * dx, ymax - 0.1 * dy), font_properties = FontProperties(size = 25)) if title == "": ax.set_title(label) else: ax.set_title(title) #plot colorbar if show_colorbar: from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") cb = plt.colorbar(image, #ticks = colorbar_tick_locator, orientation = colorbar_orientation, format = colorbar_label_format, drawedges = True, cax=cax ) cb.set_ticks(colorbar_tick_locator) if impose_lower_limit is None: min_val = min( data_1d.min(), 0) else: min_val = impose_lower_limit lower_limit = (min_val - minmax[0]) / float(minmax[1] - minmax[0]) if lower_limit < 0: lower_limit = 0.0 else: lower_limit = plot_utils.get_closest_tick_value(color_map.N + 1, lower_limit) cb.ax.set_ylim(bottom = lower_limit - 1.0e-4) cb.outline.set_visible(False) cb.ax.set_title(units) if upper_limited: cl = cb.ax.get_yticklabels() labels = [] for text in cl: labels.append(text.get_text()) labels[-1] = '$\\geq$' + labels[-1] cb.ax.set_yticklabels(labels) #save to a file if necessary if fig is not None: fig.savefig(imagefile, bbox_inches = "tight")
def main(): start_year = 1970 end_year = 1999 stations = cehq_station.read_station_data(folder="data/cehq_measure_data_all") stations = list( itertools.ifilter( lambda s: s.is_natural, stations) ) for s in stations: s.delete_data_after_year(end_year) s.delete_data_before_year(start_year) pass stations = list( itertools.ifilter(lambda s: s.get_num_of_years_with_continuous_data() >= 10, stations) ) s = stations[0] assert isinstance(s, Station) #stations = list( itertools.ifilter(lambda s: s.is_natural, stations) ) x, y = polar_stereographic.lons, polar_stereographic.lats basemap = polar_stereographic.basemap x, y = basemap(x,y) sx = [s.longitude for s in stations] sy = [s.latitude for s in stations] sx, sy = basemap(sx, sy) #read model data model_file_path = "data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc" acc_area = data_select.get_field_from_file(path=model_file_path, field_name="drainage") i_indices, j_indices = data_select.get_indices_from_file(path=model_file_path) lons_1d = data_select.get_field_from_file(path=model_file_path, field_name="longitude") lats_1d = data_select.get_field_from_file(path=model_file_path, field_name="latitude") x1d, y1d, z1d = lat_lon.lon_lat_to_cartesian(lons_1d, lats_1d) kdtree = KDTree(zip(x1d, y1d, z1d)) print "Id: 4 DA (km2) <-> 4 dist (km) <-> 4 (i,j)" #basemap.scatter(sx, sy, c = "r", zorder = 5) for s, isx, isy in zip( stations, sx, sy ): assert isinstance(s, Station) plt.annotate(s.id, xy=(isx, isy), bbox = dict(facecolor = 'white'), weight = "bold", font_properties = FontProperties(size=0.5)) #get model drainaige areas for the four closest gridcells to the station x0, y0, z0 = lat_lon.lon_lat_to_cartesian(s.longitude, s.latitude) dists, indices = kdtree.query([x0, y0, z0], k = 4) dists /= 1000 print("{0}: {1:.1f}; {2:.1f}; {3:.1f}; {4:.1f} <-> {5:.1f}; {6:.1f}; {7:.1f}; {8:.1f} <-> {9};{10};{11};{12}".format( "{0} (S_DA = {1:.1f})".format(s.id, s.drainage_km2), float(acc_area[indices[0]]), float(acc_area[indices[1]]), float(acc_area[indices[2]]), float(acc_area[indices[3]]), float( dists[0] ), float(dists[1]), float(dists[2]), float(dists[3]), "({0}, {1})".format(i_indices[indices[0]] + 1, j_indices[indices[0]] + 1), "({0}, {1})".format(i_indices[indices[1]] + 1, j_indices[indices[1]] + 1), "({0}, {1})".format(i_indices[indices[2]] + 1, j_indices[indices[2]] + 1), "({0}, {1})".format(i_indices[indices[3]] + 1, j_indices[indices[3]] + 1) )) basemap.drawcoastlines(linewidth=0.5) xmin, xmax = min(sx), max(sx) ymin, ymax = min(sy), max(sy) marginx = (xmax - xmin) * 0.1 marginy = (ymax - ymin) * 0.1 xmin -= marginx * 1.5 xmax += marginx * 2 ymin -= marginy ymax += marginy * 2 plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.tight_layout() basin_boundaries.plot_basin_boundaries_from_shape(basemap=basemap, plotter=plt, linewidth=1) plt.savefig("10yr_cont_stations_natural_fs0.5.pdf") #plt.show() pass
def plot_data(data, i_array, j_array, name='AEX', title = None, digits = 1, color_map = mpl.cm.get_cmap('RdBu_r'), minmax = (None, None), units = '%', colorbar_orientation = 'vertical', draw_colorbar = True, basemap = None, axes = None, impose_lower_limit = None, upper_limited = False ): if name is not None: plt.figure() to_plot = np.ma.masked_all((n_cols, n_rows)) for index, i, j in zip( range(len(data)), i_array, j_array): to_plot[i, j] = data[index] print np.ma.min(data), np.ma.max(data) # m.pcolor(xs, ys, to_plot, cmap = mpl.cm.get_cmap('RdBu_r')) if basemap is None: the_basemap = m else: the_basemap = basemap image = the_basemap.pcolormesh(xs, ys, to_plot.copy(), cmap = color_map, vmin = minmax[0], vmax = minmax[1], ax = axes) #ads to m fields basins and basins_info which contain shapes and information # m.readshapefile('data/shape/contour_bv_MRCC/Bassins_MRCC_utm18', 'basins') # m.scatter(xs, ys, c=to_plot) plot_basin_boundaries_from_shape(m, axes=axes, linewidth = 2.1) #the_basemap.drawrivers(linewidth = 0.5, ax = axes) the_basemap.drawcoastlines(linewidth = 0.5, ax = axes) plot_utils.draw_meridians_and_parallels(the_basemap, step_degrees = 30) axes.set_title(title if title is not None else name) #zoom_to_qc() x_min, x_max, y_min, y_max = plot_utils.get_ranges(xs[i_array, j_array], ys[i_array, j_array]) axes.set_xlim(x_min, x_max) axes.set_ylim(y_min, y_max) if draw_colorbar: from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(axes) cax = divider.append_axes("right", "8%", pad="3%") int_ticker = LinearLocator(numticks = color_map.N + 1) cb = axes.figure.colorbar(image, ticks = int_ticker, orientation = colorbar_orientation, format = '%d', cax = cax, ax=axes, drawedges = True) cb.ax.set_title(units) #cb.outline.remove() bottom, top = cb.ax.get_ylim() left, right = cb.ax.get_xlim() print bottom, top, left, right if impose_lower_limit is None: new_bottom = min( np.min(data), 0 ) else: new_bottom = impose_lower_limit #new_bottom = np.floor( new_bottom / 10.0 ) * 10.0 new_bottom = np.abs((new_bottom - minmax[0]) / (float(minmax[1] - minmax[0]))) new_bottom = plot_utils.get_closest_tick_value(color_map.N + 1, new_bottom) - 1.0e-4 print new_bottom cb.ax.set_ylim(bottom = new_bottom) left, right = cb.ax.get_xlim() bottom, top = cb.ax.get_ylim() #cb.ax.set_xlim(left = 0, right = 0.8) cb.outline.set_visible( False ) if upper_limited: cl = cb.ax.get_yticklabels() labels = [] for text in cl: labels.append(text.get_text()) labels[-1] = '$\\geq$' + labels[-1] cb.ax.set_yticklabels(labels)
def compare_swe_2d(): """ Compare seasonal mean """ start = datetime(1980,01,01,00) end = datetime(1996, 12, 31,00) months = [12,1,2] #calculate mean for ccc data accounting for the mask domain_mask = get_domain_mask() cccData = getTemporalMeanCCCDataForMask(domain_mask, startDate = start, endDate = end, months = months) lon_selected = polar_stereographic.lons[domain_mask == 1] lat_selected = polar_stereographic.lats[domain_mask == 1] geopointList = [] for lon, lat in zip(lon_selected, lat_selected): geopointList.append(GeoPoint(longitude = lon, latitude = lat)) print 'Selecting obs data' sweObs = SweHolder() obsData = sweObs.getTemporalMeanDataFromNetCDFforPoints(geopointList, startDate = start, endDate = end, months = months) to_plot = np.ma.masked_all(polar_stereographic.lons.shape) condition = domain_mask == 1 to_plot[condition] = (cccData - obsData) / obsData * 100 xs = polar_stereographic.xs ys = polar_stereographic.ys basemap = polar_stereographic.basemap plot_utils.apply_plot_params() basemap.pcolormesh(xs, ys, to_plot, cmap = mpl.cm.get_cmap('jet', 9), vmin = -60, vmax = 120) basemap.drawcoastlines() plt.colorbar(ticks = LinearLocator(numticks = 10), format = '%.1f') plt.title('Snow Water Equivalent (%) \n $(S_{\\rm CRCM4} - S_{\\rm obs.})/S_{\\rm obs.}\\cdot100\%$\n') #zoom to domain selected_x = xs[~to_plot.mask] selected_y = ys[~to_plot.mask] marginx = abs(np.min(selected_x) * 5.0e-2) marginy = abs(np.min(selected_y) * 5.0e-2) plt.xlim(np.min(selected_x) - marginx, np.max(selected_x) + marginx) plt.ylim(np.min(selected_y) - marginy, np.max(selected_y) + marginy) bb.plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 2, edge_color = 'k') ##overlay flow directions cells = cpe.get_connected_cells('data/hydrosheds/directions_for_streamflow9.nc') read_infocell.plot_directions(cells, basemap = basemap, domain_mask = get_domain_mask()) selected_stations = [ 104001, 103715, 93806, 93801, 92715, 81006, 61502, 80718, 40830, 42607 ] selected_ids = map(lambda x: "%06d" % x, selected_stations) print selected_ids cpe.plot_station_positions(id_list=selected_ids, use_warpimage=False, save_to_file=False, the_basemap=basemap) plt.savefig('swe_djf_validation.pdf', bbox_inches = 'tight') pass
def plot_swe(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) var_name = "sno" year_range_c = xrange(1970,2000) year_range_f = xrange(2041,2071) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) _generate_mask_of_domain_of_interest(i_array, j_array) if True: return plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,4, height_ratios=[1,1,1], width_ratios=[1,1,1,1]) #color_map = my_cm.get_ color_map = my_cm.get_red_blue_colormap(ncolors = 10, reversed=True) #color_map = mpl.cm.get_cmap(name="jet_r", lut=10) clevels = xrange(-8, 9, 2) all_plot_axes = [] for i, season in enumerate(seasons): if not i: ax = fig.add_subplot(gs[0,1:3]) else: row, col = (i - 1) // 2 + 1, (i - 1) % 2 ax = fig.add_subplot(gs[row, col * 2 : col * 2 + 2 ]) all_plot_axes.append(ax) current = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) t, p = stats.ttest_ind(current, future, axis=0) #TODO: change it back to p <= 0.05 wheen doing real sign test significant = np.array(p <= 1) assert not np.all(~significant) assert not np.all(significant) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) delta = (future_m - current_m) delta = np.array(delta) assert isinstance(ax, Axes) #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save d_min = np.floor( np.min(save) ) d_max = np.ceil( np.max(save) ) bounds = plot_utils.get_boundaries_for_colobar(d_min, d_max, color_map.N, lambda x: np.round(x, decimals=10)) print bounds bn = BoundaryNorm(bounds, color_map.N) d = np.max( np.abs([d_min, d_max]) ) print season, np.min(delta), np.max(delta) #delta = np.ma.masked_where(delta < 0, delta ) img = basemap.pcolormesh(x, y, delta, cmap = color_map, norm = bn, vmin = bounds[0], vmax = bounds[-1]) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") assert isinstance(cax, Axes) int_ticker = LinearLocator(numticks = color_map.N + 1) cb = fig.colorbar(img, cax = cax, ticks = bounds, boundaries = bounds) where_significant = significant significant = np.ma.masked_all(significant.shape) significant[(~where_significant)] = 0 save = significant[i_array, j_array] significant = np.ma.masked_all(significant.shape) significant[i_array, j_array] = save basemap.pcolormesh( x, y, significant , cmap = mpl.cm.get_cmap(name = "gray", lut = 3), vmin = -1, vmax = 1, ax = ax) ax.set_title(season) for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) gs.update(wspace=0.5) #gs.tight_layout(fig) fig.suptitle("Projected changes, SWE, mm, CRCM4") fig.savefig("proj_change_{0}_ccc.png".format(var_name)) pass
def plot_temp(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) #take out annual seasons.pop(0) #put new numbering for the subplots new_numbering = ["a", "b", "c", "d"] var_name = "st" year_range_c = xrange(1970,2000) year_range_f = xrange(2041,2071) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,2) #color_map = mpl.cm.get_cmap(name="jet", lut=10) clevels = xrange(-8, 9, 2) all_plot_axes = [] #determine min an max for color scales min_val = np.inf max_val = -np.inf for season in seasons: current = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) delta = future_m[i_array, j_array] - current_m[i_array, j_array] the_min = delta.min() the_max = delta.max() min_val = min(min_val, the_min) max_val = max(max_val, the_max) min_val = np.floor(min_val) max_val = np.ceil(max_val) color_map = my_cm.get_red_blue_colormap(ncolors = 10, reversed=True) if min_val >= 0: color_map = my_cm.get_red_colormap(ncolors=10) for i, season in enumerate(seasons): row, col = i // 2, i % 2 ax = fig.add_subplot(gs[row, col ]) all_plot_axes.append(ax) current = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) # t, p = stats.ttest_ind(current, future, axis=0) # significant = np.array(p <= 0.05) significant = calculate_significance_using_bootstrap(current, future) assert not np.all(~significant) assert not np.all(significant) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) delta = (future_m - current_m) assert isinstance(ax, Axes) #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save d_min = np.floor( np.min(save) ) d_max = np.ceil( np.max(save) ) img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = min_val, vmax = max_val) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") assert isinstance(cax, Axes) int_ticker = LinearLocator(numticks = color_map.N + 1) cb = fig.colorbar(img, cax = cax, ticks = int_ticker) cax.set_title("$^{\\circ}{\\rm C}$") where_significant = significant significant = np.ma.masked_all(significant.shape) significant[~where_significant] = 0 basemap.pcolormesh( x, y, significant , cmap = mpl.cm.get_cmap(name = "gray", lut = 3), vmin = -1, vmax = 1, ax = ax) ax.set_title( season.replace( re.findall( "([a-z])", season)[0], new_numbering[i]) ) for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) #gs.update(wspace=0.5) fig.tight_layout() #fig.suptitle("Projected changes, T(2m), degrees, CRCM4") fig.savefig("proj_change_{0}_ccc.png".format(var_name))
def plot_precip(data_path = "/home/huziy/skynet1_rech3/crcm4_data"): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) #remove annual seasons.pop(0) #put new numbering for the subplots new_numbering = ["a", "b", "c", "d"] var_name = "pcp" year_range_c = xrange(1970,2000) year_range_f = xrange(2041,2071) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,2, height_ratios=[1,1,1], width_ratios=[1,1]) #determine min an max for color scales min_val = np.inf max_val = -np.inf for season in seasons: current = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) delta = future_m[i_array, j_array] - current_m[i_array, j_array] the_min = delta.min() the_max = delta.max() min_val = min(min_val, the_min) max_val = max(max_val, the_max) min_val = np.floor(min_val) max_val = np.ceil(max_val) color_map = my_cm.get_red_blue_colormap(ncolors = 10, reversed=False) #color_map = mpl.cm.get_cmap(name="jet_r", lut=10) clevels = xrange(-8, 9, 2) all_plot_axes = [] for i, season in enumerate(seasons): row, col = i // 2, i % 2 ax = fig.add_subplot(gs[row, col ]) all_plot_axes.append(ax) current = _get_data(data_folder=data_path, v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(data_folder=data_path, v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) #t, p = stats.ttest_ind(current, future, axis=0) #significant = np.array(p <= 0.05) significant = calculate_significance_using_bootstrap(current, future) assert not np.all(~significant) assert not np.all(significant) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) seconds_per_day = 24 * 60 * 60 delta = (future_m - current_m) * seconds_per_day delta = np.array(delta) assert isinstance(ax, Axes) #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save d_min = np.floor( min_val * 10 ) / 10.0 d_max = np.ceil( max_val *10 ) / 10.0 if d_min > 0: color_map = my_cm.get_blue_colormap(ncolors=10) img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = d_min, vmax = d_max) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") assert isinstance(cax, Axes) int_ticker = LinearLocator(numticks = color_map.N + 1) cb = fig.colorbar(img, cax = cax, ticks = int_ticker) cax.set_title("mm/d") where_significant = significant significant = np.ma.masked_all(significant.shape) significant[(~where_significant)] = 0 save = significant[i_array, j_array] significant = np.ma.masked_all(significant.shape) significant[i_array, j_array] = save basemap.pcolormesh( x, y, significant , cmap = mpl.cm.get_cmap(name = "gray", lut = 3), vmin = -1, vmax = 1, ax = ax) ax.set_title( season.replace( re.findall( "([a-z])", season)[0], new_numbering[i]) ) #plot djf swe change season = " (b) Winter (DJF)" ax = fig.add_subplot(gs[2, : ]) all_plot_axes.append(ax) var_name = "sno" current = _get_data(data_folder=data_path, v_name=var_name, months = season_to_months[season], member_list=members.current_ids, year_range=year_range_c) future = _get_data(data_folder=data_path, v_name=var_name, months = season_to_months[season], member_list=members.future_ids, year_range=year_range_f) #t, p = stats.ttest_ind(current, future, axis=0) #significant = np.array(p <= 0.05) significant = calculate_significance_using_bootstrap(current, future) assert not np.all(~significant) assert not np.all(significant) current_m = np.mean(current, axis=0) future_m = np.mean(future, axis= 0) delta = future_m - current_m delta = np.array(delta) assert isinstance(ax, Axes) #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save d_min = np.floor( np.min(save) * 10 ) / 10.0 d_max = np.ceil( np.max(save) *10 ) / 10.0 if d_min >= 0: color_map = my_cm.get_blue_colormap(ncolors=10) bounds = plot_utils.get_boundaries_for_colobar(d_min, d_max, color_map.N, lambda x: np.round(x, decimals=0)) bn = BoundaryNorm(bounds, color_map.N) img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = bounds[0], vmax = bounds[-1], norm = bn) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") assert isinstance(cax, Axes) int_ticker = LinearLocator(numticks = color_map.N + 1) cb = fig.colorbar(img, cax = cax, ticks = bounds) cax.set_title("mm") where_significant = significant significant = np.ma.masked_all(significant.shape) significant[(~where_significant)] = 0 save = significant[i_array, j_array] significant = np.ma.masked_all(significant.shape) significant[i_array, j_array] = save basemap.pcolormesh( x, y, significant , cmap = mpl.cm.get_cmap(name = "gray", lut = 3), vmin = -1, vmax = 1, ax = ax) ax.set_title( season.replace( re.findall( "([a-z])", season)[0], "e")) #finish swe djf for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) #gs.update(wspace=0.5) #gs.tight_layout(fig) #fig.suptitle("Projected changes, total precip (mm/day), CRCM4") fig.tight_layout() fig.savefig("proj_change_{0}_ccc.png".format(var_name)) pass
def plot_naveed_troubled_points(path = 'data/data_txt_naveed/TroubledGridCells_AtLeast3Sims.csv'): f = open(path) lines = f.readlines() if len(lines) == 1: lines = lines[0].split('\r') f.close() #skip header while len(lines) > 0: line = lines.pop(0) if 'stationsri' in line.lower(): break print(lines) folder_path = 'data/streamflows/hydrosheds_euler9/' i_indices, j_indices = data_select.get_indices_from_file(folder_path + 'aex_discharge_1970_01_01_00_00.nc') cols = [1,2,3] names = ['C', 'F', 'Both'] color_map = ListedColormap(['r','b', 'g']) #plt.subplots_adjust(hspace = 0.0001) for c, name in zip(cols, names): plt.subplot(2,2,c) to_plot = np.ma.masked_all(xs.shape) data = get_column(c, lines) print(data) for i, j, v in zip(i_indices, j_indices, data): if v: #no color for 0 to_plot[i, j] = v else: to_plot[i, j] = 3 basemap.pcolormesh(xs, ys, to_plot.copy(), cmap = color_map, shading = 'flat', rasterized = False ) plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 1, edge_color = 'k') basemap.drawcoastlines(linewidth = 0.5) plot_utils.draw_meridians_and_parallels(basemap, step_degrees = 30) plt.title(name) ymin, ymax = plt.ylim() plt.ylim(ymin + 0.05 * (ymax - ymin) , ymax * 0.25) xmin, xmax = plt.xlim() plt.xlim(xmin + (xmax - xmin) * 0.55, 0.72*xmax) cb = plt.colorbar(shrink = 0.5, format = '%d') cb.set_ticks([1.25, 2, 2.75]) cb.set_ticklabels([1,2,3]) plt.savefig('TroubledGridCells_AtLeast3Sims.pdf', bbox_inches = 'tight') pass
def plot_swe_biases(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) swe_obs_name = "swe" cru_data_store, crcm4_data_store = _get_comparison_data_swe(swe_var_name= swe_obs_name, season_to_months=season_to_months, start_date=datetime(1980,1,1), end_date=datetime(1996, 12, 31)) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,2) color_map = my_cm.get_red_blue_colormap(ncolors = 14, reversed=True) clevels = xrange(-8, 9, 2) all_plot_axes = [] for i, season in enumerate(seasons): if not i: ax = fig.add_subplot(gs[0,:]) else: row, col = (i - 1) // 2 + 1, (i -1) % 2 ax = fig.add_subplot(gs[row, col]) all_plot_axes.append(ax) assert isinstance(ax, Axes) delta = crcm4_data_store[season] - cru_data_store[season] #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save vmax = np.ceil( np.max(save) / 10.0) * 10 vmin = np.floor( np.min(save) / 10.0) * 10 bounds = plot_utils.get_boundaries_for_colobar(vmin, vmax, color_map.N, lambda x: np.round(x, decimals = 1)) bn = BoundaryNorm(bounds, color_map.N) img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = vmin, vmax = vmax, norm = bn) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") int_ticker = LinearLocator(numticks = color_map.N + 1) fig.colorbar(img, cax = cax, ticks = bounds, boundaries = bounds) ax.set_title(season) for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) # put_selected_stations(the_ax, basemap, i_array, j_array) #gs.tight_layout(fig) fig.suptitle("SWE (mm), CRCM4 - Ross Brown dataset (1981-1997)") fig.savefig("seasonal_{0}_ccc.png".format(swe_obs_name))
def plot_swe_and_temp_on_one_plot(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) cru_var_name = "tmp" temp_obs_data_store, temp_crcm4_data_store = _get_comparison_data(cru_var_name= cru_var_name, season_to_months=season_to_months) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) #get swe data swe_obs_name = "swe" swe_obs_data_store, swe_crcm4_data_store = _get_comparison_data_swe(swe_var_name= swe_obs_name, season_to_months=season_to_months, start_date=datetime(1980,1,1), end_date=datetime(1997, 1, 1)) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) gs = gridspec.GridSpec(2, 1) basemap = polar_stereographic.basemap swe_season = seasons[1] temp_season = seasons[2] var_names = [ "swe", cru_var_name] the_seasons = [ swe_season, temp_season ] labels = [ "(a) Winter (DJF)", "(b) Sping (MAM)"] units = [ "mm", "$^{\\circ}{\\rm C}$" ] data_stores = [ [swe_obs_data_store, swe_crcm4_data_store], [temp_obs_data_store, temp_crcm4_data_store] ] all_plot_axes = [] for i, season, var_name, store, label, unit in zip(xrange(len(seasons)), the_seasons, var_names, data_stores, labels, units): ax = fig.add_subplot(gs[i, 0]) all_plot_axes.append(ax) assert isinstance(ax, Axes) crcm4 = store[1][season] obs = store[0][season] delta = crcm4 - obs if var_name == "tmp": delta -= 273.15 if var_name == "swe": ax.annotate("(1979-1997)", (0.1, 0.1), xycoords = "axes fraction", font_properties = FontProperties(weight = "bold")) elif var_name == "tmp": ax.annotate("(1970-1999)", (0.1, 0.1), xycoords = "axes fraction", font_properties = FontProperties(weight = "bold")) color_map = my_cm.get_red_blue_colormap(ncolors = 16, reversed=(var_name == "tmp")) color_map.set_over("k") color_map.set_under("k") #delta = maskoceans(polar_stereographic.lons, polar_stereographic.lats, delta) save = delta[i_array, j_array] delta = np.ma.masked_all(delta.shape) delta[i_array, j_array] = save vmin = np.floor( np.min(save) ) vmax = np.ceil( np.max(save) ) decimals = 0 if var_name == "swe" else 1 round_func = lambda x: np.round(x, decimals= decimals) bounds = plot_utils.get_boundaries_for_colobar(vmin, vmax, color_map.N, round_func= round_func) bn = BoundaryNorm( bounds, color_map.N ) img = basemap.pcolormesh(x, y, delta, cmap = color_map, norm = bn) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") fig.colorbar(img, cax = cax, boundaries = bounds, ticks = bounds) ax.set_title(label) cax.set_title(unit) for the_ax in all_plot_axes: the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) put_selected_stations(the_ax, basemap, i_array, j_array) fig.tight_layout() fig.savefig("swe_temp_biases.png")
def plot_precip_biases(): seasons = ["(a) Annual", " (b) Winter (DJF)", "(c) Spring (MAM)", "(d) Summer (JJA)", "(e) Fall (SON)"] months = [range(1, 13), [12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] season_to_months = dict(zip(seasons, months)) cru_var_name = "pre" cru_data_store, crcm4_data_store = _get_comparison_data( crcm4_data_folder="/home/huziy/skynet1_rech3/crcm4_data/aex_p1pcp", cru_data_path = "data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.pre.dat.nc", cru_var_name=cru_var_name, season_to_months=season_to_months ) x, y = polar_stereographic.xs, polar_stereographic.ys i_array, j_array = _get_routing_indices() x_min, x_max, y_min, y_max = plot_utils.get_ranges(x[i_array, j_array], y[i_array, j_array]) plot_utils.apply_plot_params(width_pt= None, font_size=9, aspect_ratio=2.5) fig = plt.figure() assert isinstance(fig, Figure) basemap = polar_stereographic.basemap assert isinstance(basemap, Basemap) gs = gridspec.GridSpec(3,2, width_ratios=[1,1], height_ratios=[1, 1, 1]) color_map = my_cm.get_red_blue_colormap(ncolors = 16, reversed=False) color_map.set_over("k") color_map.set_under("k") all_plot_axes = [] img = None for i, season in enumerate(seasons): if not i: ax = fig.add_subplot(gs[0,:]) else: row, col = (i - 1) // 2 + 1, (i - 1) % 2 ax = fig.add_subplot(gs[row, col]) all_plot_axes.append(ax) assert isinstance(ax, Axes) delta = crcm4_data_store[season] - cru_data_store[season] save = delta[i_array, j_array] delta[:, :] = np.ma.masked delta[i_array, j_array] = save img = basemap.pcolormesh(x, y, delta, cmap = color_map, vmin = -2, vmax = 2) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") int_ticker = LinearLocator(numticks = color_map.N + 1) fig.colorbar(img, cax = cax, ticks = MultipleLocator(base = 0.5)) ax.set_title(season) for the_ax in all_plot_axes: assert isinstance(the_ax, Axes) the_ax.set_xlim(x_min, x_max) the_ax.set_ylim(y_min, y_max) the_ax.set_xmargin(0) the_ax.set_ymargin(0) basemap.drawcoastlines(ax = the_ax, linewidth = 0.1) plot_utils.draw_meridians_and_parallels(basemap, step_degrees=30.0, ax = the_ax) plot_basin_boundaries_from_shape(basemap, axes = the_ax, linewidth=0.4) # put_selected_stations(the_ax, basemap, i_array, j_array) # ax = fig.add_subplot(gs[3,:]) # assert isinstance(ax, Axes) # fig.colorbar(img, cax = ax, orientation = "horizontal", ticks = MultipleLocator(base = 0.5)) #gs.tight_layout(fig) fig.suptitle("Total precip, mm/day, CRCM4 - CRU") fig.savefig("seasonal_{0}_ccc.png".format(cru_var_name))