def plot(reference, test, diff, metrics_dict, parameter): # Create figure, projection fig = plt.figure(figsize=[8.5, 11.0]) # proj = ccrs.PlateCarree(central_longitude=180) proj = None ax = fig.add_axes(panel[0]) ax.plot(test.getLatitude()[:], ma.squeeze(test.asma()), 'k', linewidth=2) ax.plot(reference.getLatitude()[:], ma.squeeze(reference.asma()), 'r', linewidth=2) ax1 = fig.add_axes(panel[1]) ax1.plot(diff.getLatitude()[:], ma.squeeze(diff.asma()), 'k', linewidth=2) fig.text(panel[0][0], panel[0][2] + 0.095, "Test: " + parameter.test_name, ha='left', fontdict=plotSideTitle, color='black') fig.text(panel[0][0], panel[0][2] + 0.07, "Reference: " + parameter.reference_name, ha='left', fontdict=plotSideTitle, color='red') fig.text(panel[1][0], panel[0][2] - 0.3, "Test-Reference", ha='left', fontdict=plotSideTitle) ax.set_xticks([-90, -60, -30, 0, 30, 60, 90]) #crs=ccrs.PlateCarree()) ax.set_xlim(-90, 90) ax1.set_xticks([-90, -60, -30, 0, 30, 60, 90]) #crs=ccrs.PlateCarree()) ax1.set_xlim(-90, 90) ## lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f') lat_formatter = LatitudeFormatter() # ax.xaxis.set_major_formatter(lon_formatter) #ax.xaxis.set_major_formatter(lat_formatter) #ax1.xaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=11.0, direction='out', pad=-2, width=1) ax1.tick_params(labelsize=11.0, direction='out', pad=-2, width=1) ax.xaxis.set_ticks_position('bottom') ax1.xaxis.set_ticks_position('bottom') ax.set_ylabel(test.long_name + ' (' + test.units + ')') ax1.set_ylabel(test.long_name + ' (' + test.units + ')') # ax.yaxis.set_ticks_position('left') # fig.suptitle(parameter.main_title, x=0.5, y=0.95, fontsize=18) # plt.show() # # Save figure for f in parameter.output_format: f = f.lower().split('.')[-1] fnm = os.path.join(get_output_dir('3', parameter), parameter.output_file) plt.savefig(fnm + '.' + f) print('Plot saved in: ' + fnm + '.' + f)
def plot(reference, test, diff, metrics_dict, parameter): # Create figure, projection fig = plt.figure(figsize=parameter.figsize, dpi=parameter.dpi) # proj = ccrs.PlateCarree(central_longitude=180) ax = fig.add_axes(panel[0]) ax.plot(test.getLatitude()[:], ma.squeeze(test.asma()), 'k', linewidth=2) ax.plot(reference.getLatitude()[:], ma.squeeze( reference.asma()), 'r', linewidth=2) ax1 = fig.add_axes(panel[1]) ax1.plot(diff.getLatitude()[:], ma.squeeze(diff.asma()), 'k', linewidth=2) ax1.axhline(y=0, color='0.5') test_title = "Test" if parameter.test_title == '' else parameter.test_title test_title += ' : {}'.format(parameter.test_name_yrs) fig.text(panel[0][0], panel[0][2] + 0.095, test_title, ha='left', fontdict=plotSideTitle, color='black') ref_title = "Reference" if parameter.reference_title == '' else parameter.reference_title ref_title += ' : {}'.format(parameter.reference_name) fig.text(panel[0][0], panel[0][2] + 0.07, ref_title, ha='left', fontdict=plotSideTitle, color='red') fig.text(panel[1][0], panel[0][2] - 0.3, parameter.diff_title, ha='left', fontdict=plotSideTitle) ax.set_xticks([-90, -60, -30, 0, 30, 60, 90]) # crs=ccrs.PlateCarree()) ax.set_xlim(-90, 90) ax1.set_xticks([-90, -60, -30, 0, 30, 60, 90]) # crs=ccrs.PlateCarree()) ax1.set_xlim(-90, 90) # lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f') LatitudeFormatter() # ax.xaxis.set_major_formatter(lon_formatter) # ax.xaxis.set_major_formatter(lat_formatter) # ax1.xaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=11.0, direction='out', width=1) ax1.tick_params(labelsize=11.0, direction='out', width=1) ax.xaxis.set_ticks_position('bottom') ax1.xaxis.set_ticks_position('bottom') ax.set_ylabel(test.long_name + ' (' + test.units + ')') ax1.set_ylabel(test.long_name + ' (' + test.units + ')') # ax.yaxis.set_ticks_position('left') fig.suptitle(parameter.main_title, x=0.5, y=0.95, fontsize=18) # Save figure for f in parameter.output_format: f = f.lower().split('.')[-1] fnm = os.path.join(get_output_dir( parameter.current_set, parameter), parameter.output_file) plt.savefig(fnm + '.' + f) _chown(fnm + '.' + f, parameter.user) print('Plot saved in: ' + fnm + '.' + f)
def calculatePosturalMeasurements(self): self.Xhead = ma.array(ma.squeeze(self.skeleton[:, 0, :])) self.Xhead = ((self.Xhead + self.h5ref['boundingBox'][:, :2]) / self.pixelsPerMicron) self.Xhead[np.logical_not(self.orientationFixed), :] = ma.masked self.Xtail = ma.array(np.squeeze(self.skeleton[:, -1, :])) self.Xtail = ((self.Xtail + self.h5ref['boundingBox'][:, :2]) / self.pixelsPerMicron) self.Xtail[np.logical_not(self.orientationFixed), :] = ma.masked self.psi = ma.arctan2(self.Xhead[:, 0]-self.X[:, 0], self.Xhead[:, 1]-self.X[:, 1]) dpsi = self.phi - self.psi self.dpsi = ma.mod(dpsi+np.pi, 2*np.pi)-np.pi self.psi[np.logical_not(self.orientationFixed)] = ma.masked self.dpsi[np.logical_or(np.logical_not(self.orientationFixed), self.badFrames)] = ma.masked self.Xhead[np.logical_or(np.logical_not(self.orientationFixed), self.badFrames), :] = ma.masked self.Xtail[np.logical_or(np.logical_not(self.orientationFixed), self.badFrames), :] = ma.masked skeleton = ma.array(self.skeleton) skeleton[np.logical_not(self.orientationFixed), :, :] = ma.masked posture = ma.array(self.posture) posture[np.logical_not(self.orientationFixed), :] = ma.masked missing = np.any(posture.mask, axis=1) if np.all(missing): self.Ctheta = None self.ltheta = None self.vtheta = None else: posture = posture[~missing, :].T self.Ctheta = np.cov(posture) self.ltheta, self.vtheta = LA.eig(self.Ctheta)
def _compute_variable_stats(variable, axis, weights, calc_avg, calc_min, calc_max, calc_stddev, calc_count): ''' Calculate statistics for a single variable. ''' # Get the data out. Note: scale_factor and add_offset are automatically # applied. data = variable[:] # Make sure data is a masked array data = ma.masked_array(data) # Broadcast the weights before we try to combine the masks for data and # weights weights = ma.masked_array(data=np.broadcast_to(weights.data, data.shape), mask=np.broadcast_to(ma.getmaskarray(weights), data.shape)) # We want all our calculations to happen over areas that are unmasked in # both the weights and data combined_mask = np.logical_or(ma.getmaskarray(data), ma.getmaskarray(weights)) data = ma.masked_array(data.data, mask=combined_mask) weights = ma.masked_array(weights.data, mask=combined_mask) out = {} if calc_count: # Irritatingly, the ma.count function can only take one value at a time # for the axis. So, instead, construct an array of ones ones = np.ones(data.shape) # Set the masked areas to 0 ones[combined_mask] = 0 out["count"] = ma.sum(ones, axis=axis) if calc_min: out["min"] = ma.min(data, axis=axis) if calc_max: out["max"] = ma.max(data, axis=axis) # Note: standard deviation needs the weighted average and the weights sum if calc_avg or calc_stddev: sum_weights = _add_axes_back(ma.sum(weights, axis=axis), axis) weighted_avg_numerator = _add_axes_back( ma.sum(weights * data, axis=axis), axis) weighted_avg = weighted_avg_numerator / sum_weights if calc_avg: out["avg"] = ma.squeeze(weighted_avg, axis=axis) if calc_stddev: # calculate the anomaly anomaly = data - weighted_avg # calculate the standard deviation variance = ma.sum(weights * (anomaly**2) / sum_weights, axis=axis) out["stddev"] = np.sqrt(variance) return out
def delete_singleton_axis( mv, vid=None ): """If mv depends on an axis with just one value, create a copy of mv without that axis, and without the corresponding data dimension. Normally this happens when time has been averaged out, but there is still a one-valued time axis left (thus one would normally use id='time'). You can specify the axis id if there might be more than one singleton.""" axes = allAxes(mv) saxis = None si = None for i in range(len(axes)): if len(axes[i])==1 and (vid==None or axes[i].id==vid): saxis = axes[i] si = i del axes[si] break if saxis==None: return mv data = ma.copy( mv.data ) if numpy.version.version >= '1.7.0': data = ma.squeeze( data, axis=si ) else: data = ma.squeeze( data ) # let's hope that there's only one singleton! mvnew = cdms2.createVariable ( data, axes=axes, id=mv.id ) if hasattr(mv,'units'): mvnew.units = mv.units return mvnew
def top_words_in_doc(doc_term_frequency: ndarray, features: list, row_id: int, top_n: int = 20) -> List[Tuple[str, str]]: """ Top TF IDF features in specific document (matrix row). :param doc_term_frequency: Document-term frequency matrix. :param features: Feature names for the document term matrix. :param row_id: Row index of the document. :param top_n: Top number of words to display on wordcloud. :return: List[(word, tf_idf)] """ row = squeeze(doc_term_frequency[row_id].toarray()) top_n_ids = argsort(row)[::-1][:top_n] return [(features[i], row[i]) for i in top_n_ids]
def _compute(self, blob_generator): # Design pattern: for blob in blob_generator: # Map all RGB values to colorname histograms blob.data *= 255 if len(blob.data.shape) == 2: blob.data = (blob.data.astype(np.uint8) / 8).astype(np.uint32) r = blob.data g = blob.data b = blob.data else: r, g, b = split( (blob.data.astype(np.uint8) / 8).astype(np.uint32), 3, axis=2) index = r + 32 * g + 32 * 32 * b blob.data = self.mapping[squeeze(index)] yield blob
def plot_panel(n, fig, proj, var, title): #var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) # Contour levels # Contour plot ax = fig.add_axes(panel[n], projection=proj) ax.set_global() p1 = ax.contourf( lon, lat, var, transform=ccrs.PlateCarree(), extend='both', ) ax.set_aspect('auto') ax.coastlines(lw=0.3) ax.set_title(title, fontdict=plotTitle) ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree()) ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree()) lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f') lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction='out', width=1) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # Color bar cbax = fig.add_axes( (panel[n][0] + 0.6635, panel[n][1] + 0.0215, 0.0326, 0.1792)) cbar = fig.colorbar(p1, cax=cbax) w, h = get_ax_size(fig, cbax) cbar.ax.tick_params(labelsize=9.0, length=0)
y_index = np.where(lat == target_lat)[0] #nc_files = glob('merra2_TQ_*.nc') nc_files = glob('merra2_TQH_*.nc') nc_files.sort() nyear = 26 nz = 25 nx = 63 MSE = ma.zeros([nyear * 365, nz, nx]) times = [] for ifile, file in enumerate(nc_files): f = Dataset(file) #MSE[365*ifile:365+365*ifile,:] = ma.squeeze(f.variables['T'][:,:,y_index,:]*1004.+f.variables['QV'][:,:,y_index,:]*2.5e6)/1.e+3 MSE[365 * ifile:365 + 365 * ifile, :] = ma.squeeze( f.variables['T'][:, :, y_index, :] * 1004. + f.variables['H'][:, :, y_index, :] * 9.8 + f.variables['QV'][:, :, y_index, :] * 2.5e6) / 1.e+3 times.extend( num2date(f.variables['time'][:], units='days since 1980-01-01 00:00:00')) times = np.array(times) # onset or demise dates dates = [ datetime(1980, 5, 18), datetime(1981, 4, 14), datetime(1982, 5, 17), datetime(1983, 5, 25), datetime(1984, 5, 8), datetime(1985, 6, 9),
def plot_panel(n, fig, proj, var, clevels, cmap, title, parameters, stats=None): # var_min = float(var.min()) # var_max = float(var.max()) # var_mean = cdutil.averager(var, axis='xy', weights='generate') # var = add_cyclic(var) var.getLongitude() lat = var.getLatitude() plev = var.getLevel() var = ma.squeeze(var.asma()) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # Contour plot ax = fig.add_axes(panel[n], projection=proj) cmap = get_colormap(cmap, parameters) p1 = ax.contourf( lat, plev, var, # transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend='both', ) ax.set_aspect('auto') # ax.coastlines(lw=0.3) if title[0] is not None: ax.set_title(title[0], loc='left', fontdict=plotSideTitle) if title[1] is not None: ax.set_title(title[1], fontdict=plotTitle) if title[2] is not None: ax.set_title(title[2], loc='right', fontdict=plotSideTitle) # ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree()) # ax.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree()) ax.set_xticks([-90, -60, -30, 0, 30, 60, 90]) # , crs=ccrs.PlateCarree()) ax.set_xlim(-90, 90) # lon_formatter = LongitudeFormatter( # zero_direction_label=True, number_format='.0f') LatitudeFormatter() # ax.xaxis.set_major_formatter(lon_formatter) # ax.xaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction='out', width=1) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.invert_yaxis() # Color bar cbax = fig.add_axes( (panel[n][0] + 0.6635, panel[n][1] + 0.0215, 0.0326, 0.1792)) cbar = fig.colorbar(p1, cax=cbax) w, h = get_ax_size(fig, cbax) if levels is None: cbar.ax.tick_params(labelsize=9.0, length=0) else: maxval = np.amax(np.absolute(levels[1:-1])) if maxval < 10.0: fmt = "%5.2f" pad = 25 elif maxval < 100.0: fmt = "%5.1f" pad = 25 else: fmt = "%6.1f" pad = 30 cbar.set_ticks(levels[1:-1]) labels = [fmt % l for l in levels[1:-1]] cbar.ax.set_yticklabels(labels, ha='right') cbar.ax.tick_params(labelsize=9.0, pad=pad, length=0) # Min, Mean, Max fig.text(panel[n][0] + 0.6635, panel[n][1] + 0.2107, "Max\nMean\nMin", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.7635, panel[n][1] + 0.2107, "%.2f\n%.2f\n%.2f" % stats[0:3], ha='right', fontdict=plotSideTitle) # RMSE, CORR if len(stats) == 5: fig.text(panel[n][0] + 0.6635, panel[n][1] - 0.0105, "RMSE\nCORR", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.7635, panel[n][1] - 0.0105, "%.2f\n%.2f" % stats[3:5], ha='right', fontdict=plotSideTitle)
def seawinds(lonStart=3, lonEnd=4, latStart=41, latEnd=42, \ wantedDate = datetime.datetime(2010,12,18,17,39,0), \ m=None, name='seawinds', contour=None): refDate = datetime.datetime(1978,1,1,0,0,0) #wantedDate = datetime.datetime(2010,12,18,17,39,0) base = '/media/SOLabNFS/store/satellite/seawinds/SI/uv/6hrly/netcdf/2000s/' fn = 'uv' + wantedDate.strftime('%Y%m%d') + '.nc' cdf = Dataset(base + fn) # Read lats/lons and time lats = cdf.variables["lat"][:] lons = cdf.variables["lon"][:] time=cdf.variables["time"][:] # Find which date fits us best resolution = [ datetime.timedelta(hours=0), datetime.timedelta(hours=6), \ datetime.timedelta(hours=12), datetime.timedelta(hours=18) ] roundTime = datetime.timedelta(seconds=wantedDate.second,\ minutes=wantedDate.minute, hours=wantedDate.hour) closestTime = sorted(resolution, key=lambda t: abs(roundTime - t)) t = closestTime.index(min(closestTime)) closestTime = refDate + datetime.timedelta(hours=int(time[t])) print "Closest time: ", closestTime # find subset not to import all the data res = findSubsetIndices(latStart,latEnd,lonStart,lonEnd,lats,lons) lon,lat=meshgrid(lons[res[0]:res[1]],lats[res[2]:res[3]]) u = cdf.variables["u"][t,:,int(res[2]):int(res[3]),int(res[0]):int(res[1])] v = cdf.variables["v"][t,:,int(res[2]):int(res[3]),int(res[0]):int(res[1])] print "Extracted data for area: (%s,%s) to (%s,%s)"%(lon.min(),lat.min(),lon.max(),lat.max()) # Squeezing u = ma.squeeze(u) v = ma.squeeze(v) # Get the speed w = ma.sqrt(u**2+v**2) print "Preparing Basemap" # setup nsper basemap # Lat/Lon coords of image corners if lonStart< 0 and lonEnd < 0: lon_0= - (abs(lonEnd)+abs(lonStart))/2.0 else: lon_0=(abs(lonEnd)+abs(lonStart))/2.0 if latStart< 0 and latEnd < 0: lat_0= - (abs(latEnd)+abs(latStart))/2.0 else: lat_0=(abs(latEnd)+abs(latStart))/2.0 if m is None: print ( "Using default NSPER Basemap \n") # m = Basemap(llcrnrlat=latStart,urcrnrlat=latEnd,\ # llcrnrlon=lonStart,urcrnrlon=lonEnd,\ # rsphere=(6378137.00,6356752.3142),\ # resolution='h',area_thresh=1000.,projection='lcc',\ # lat_1=latStart,lon_0=lon_0) m = Basemap(llcrnrlat=latStart,urcrnrlat=latEnd,\ llcrnrlon=lonStart,urcrnrlon=lonEnd,\ satellite_height=798000, \ resolution='h',area_thresh=1000.,projection='nsper',\ lat_1=latStart,lon_0=lon_0,lat_0=lat_0) x, y = m(lon,lat) #!!!Check!!! #Compared Q and QQ - no difference #uproj,vproj,xx,yy = \ #m.transform_vector(u[t],v[t],lon[-1,:],lat[:,-1],13,13,returnxy=True,masked=True) #Q = m.quiver(xx,yy,uproj,vproj) #QQ = m.quiver(x,y,u[t],v[t]) plt.close('all') plt.figure() # maximizing figure # mng = plt.get_current_fig_manager() # mng.resize(1920,1080) if contour is 'fill': CS1 = m.contourf(x,y,w) else: CS1 = m.pcolor(x,y,w) Q = m.quiver(x,y,u,v) #Checked in matlab should be right # make quiver key plt.quiverkey(Q, 0.07, -0.03, 10, r'$10 \frac{m}{s}$', labelpos='W') CS1.axis='tight' plt.jet() cb = plt.colorbar(CS1,orientation='vertical',extend='both', shrink=0.8) cb.set_label('Seawinds [m/s]') m.drawmeridians(arange(lon.min(),lon.max(),0.5),labels=[0,0,0,1]) m.drawparallels(arange(lat.min(),lat.max(),0.5),labels=[1,0,0,0]) m.drawcoastlines() plt.draw() plt.savefig('/home/mag/' + name + closestTime.strftime('%Y%m%d') + '.png', \ facecolor='w', edgecolor='w', dpi=100, bbox_inches="tight", pad_inches=1.75)
def plot_panel(n, fig, proj, var, amp, amp_ref, title, parameter): normalize_test_amp = parameter.normalize_test_amp lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) max_amp = amp.max() max_amp_ref = amp_ref.max() amp = ma.squeeze(amp.asma()) amp_ref = ma.squeeze(amp_ref.asma()) if normalize_test_amp: img = np.dstack(((var / 24 - 0.5) % 1, (amp / max_amp * (max_amp / max_amp_ref))**0.5, np.ones_like(amp))) max_amp = max_amp_ref else: img = np.dstack( ((var / 24 - 0.5) % 1, (amp / max_amp)**0.5, np.ones_like(amp))) img = hsv_to_rgb(img) # imshow plot ax = fig.add_axes(panel[n], projection=proj) region_str = parameter.regions[0] region = regions_specs[region_str] global_domain = True full_lon = True if 'domain' in region.keys(): # Get domain to plot domain = region['domain'] global_domain = False else: # Assume global domain domain = cdutil.region.domain(latitude=(-90., 90, 'ccb')) kargs = domain.components()[0].kargs #lon_west, lon_east, lat_south, lat_north = (0, 360, -90, 90) lon_west, lon_east, lat_south, lat_north = (-180, 180, -90, 90) if 'longitude' in kargs: full_lon = False lon_west, lon_east, _ = kargs['longitude'] # Note cartopy Problem with gridlines across the dateline:https://github.com/SciTools/cartopy/issues/821. Region cross dateline is not supported yet. if lon_west > 180 and lon_east > 180: lon_west = lon_west - 360 lon_east = lon_east - 360 if 'latitude' in kargs: lat_south, lat_north, _ = kargs['latitude'] lon_covered = lon_east - lon_west lon_step = determine_tick_step(lon_covered) xticks = np.arange(lon_west, lon_east, lon_step) # Subtract 0.50 to get 0 W to show up on the right side of the plot. # If less than 0.50 is subtracted, then 0 W will overlap 0 E on the left side of the plot. # If a number is added, then the value won't show up at all. if global_domain or full_lon: xticks = [0, 60, 120, 180, 240, 300, 359.99] else: xticks = np.append(xticks, lon_east) proj = ccrs.PlateCarree() lat_covered = lat_north - lat_south lat_step = determine_tick_step(lat_covered) yticks = np.arange(lat_south, lat_north, lat_step) yticks = np.append(yticks, lat_north) ax.set_extent([lon_west, lon_east, lat_south, lat_north]) #, crs=proj) # Full world would be aspect 360/(2*180) = 1 #ax.set_aspect((lon_east - lon_west)/(2*(lat_north - lat_south))) ax.coastlines(lw=0.3) if title[0] is not None: ax.set_title(title[0], loc='left', fontdict=plotSideTitle) if title[1] is not None: ax.set_title(title[1], fontdict=plotTitle) ax.set_xticks(xticks, crs=ccrs.PlateCarree()) #ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree()) ax.set_yticks(yticks, crs=ccrs.PlateCarree()) lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f') lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction='out', width=1) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # set a margin around the data ax.set_xmargin(0.05) ax.set_ymargin(0.10) ax.set_ylim([yticks[0], yticks[-1]]) # add the image. Because this image was a tif, the "origin" of the image is in the # upper left corner img_extent = [lon_west, lon_east, lat_south, lat_north] # When the requested region is inconsistent with what the data covered (`global` is secified but TRMM only has 50S-5N), set an arbitrary threhold. if global_domain and lat_covered - abs(lat[0] - lat[-1]) > 10: img_extent = [lon_west, lon_east, lat[0], lat[-1]] #ax.imshow(img, origin='lower', extent=img_extent, transform=ccrs.PlateCarree()) ax.imshow(img, origin='lower', extent=img_extent, transform=proj) if (region_str == 'CONUS'): ax.coastlines(resolution='50m', color='black', linewidth=0.3) state_borders = cfeature.NaturalEarthFeature( category='cultural', name='admin_1_states_provinces_lakes', scale='50m', facecolor='none') ax.add_feature(state_borders, edgecolor='black') # Color bar bar_ax = fig.add_axes((panel[n][0] + 0.67, panel[n][1] + 0.2, 0.07, 0.07), polar=True) theta, R = np.meshgrid(np.linspace(0, 2 * np.pi, 24), np.linspace(0, 1, 8)) H, S = np.meshgrid(np.linspace(0, 1, 24), np.linspace(0, 1, 8)) image = np.dstack(((H - 0.5) % 1, S**0.5, np.ones_like(S))) image = hsv_to_rgb(image) #bar_ax.set_theta_zero_location('N') bar_ax.set_theta_direction(-1) bar_ax.set_theta_offset(np.pi / 2) bar_ax.set_xticklabels( ['0h', '3h', '6h', '9h', '12h', '15h', '18h', '21h']) bar_ax.set_yticklabels(['', '', '{:.2f}'.format(max_amp)]) bar_ax.set_rlabel_position(340) bar_ax.get_yticklabels()[-2].set_weight("bold") # We change the fontsize of minor ticks label bar_ax.tick_params(axis='both', labelsize=7, pad=0, length=0) bar_ax.text(0.2, -0.3, 'Local Time', transform=bar_ax.transAxes, fontsize=7, verticalalignment='center') bar_ax.text(-0.1, -0.9, 'Max DC amp {:.2f}{}'.format(amp.max(), 'mm/d'), transform=bar_ax.transAxes, fontsize=7, fontweight='bold', verticalalignment='center') bar_ax.text(-0.1, -0.5, 'DC phase (Hue)', transform=bar_ax.transAxes, fontsize=7, verticalalignment='center') bar_ax.text(-0.1, -0.7, 'DC amplitude (Saturation)', transform=bar_ax.transAxes, fontsize=7, verticalalignment='center') color = image.reshape((image.shape[0] * image.shape[1], image.shape[2])) pc = bar_ax.pcolormesh(theta, R, np.zeros_like(R), color=color, shading='auto') pc.set_array(None)
if (v != 'TIME') & (v in var_list): print('Processing %s in file ' %v, end="") for path_file in files: #print("%d : %s file %s" % (filen, v, path_file)) print("%s " % (filen+1), end="") with Dataset(path_file, mode="r") as nc1: n_records = len(nc1.dimensions['TIME']) ## EK. check if the variable is present ## EK. if not, create an empty masked array of dimension TIME with the corresponding dtype if v in list(nc1.variables.keys()): ma_variable = nc1.variables[v][:] ma_variable = ma.squeeze(ma_variable) else: ma_variable = ma.array(numpy.repeat(999999, n_records), mask = numpy.repeat(True, n_records), dtype = var_list[v].dtype) varDims = var_list[v].dimensions var_order = len(varDims) if len(varDims) > 0: # need to replace the TIME dimension with the now extended OBS dimension # should we extend this to the CTD case where the variables have a DEPTH dimension and no TIME if var_list[v].dimensions[0] == 'TIME': if filen == 0: ma_variable_all = ma_variable
def seawinds(lonStart=3, lonEnd=4, latStart=41, latEnd=42, \ wantedDate = datetime.datetime(2010,12,18,17,39,0), \ m=None, name='seawinds', contour=None): refDate = datetime.datetime(1978, 1, 1, 0, 0, 0) #wantedDate = datetime.datetime(2010,12,18,17,39,0) base = '/media/SOLabNFS/store/satellite/seawinds/SI/uv/6hrly/netcdf/2000s/' fn = 'uv' + wantedDate.strftime('%Y%m%d') + '.nc' cdf = Dataset(base + fn) # Read lats/lons and time lats = cdf.variables["lat"][:] lons = cdf.variables["lon"][:] time = cdf.variables["time"][:] # Find which date fits us best resolution = [ datetime.timedelta(hours=0), datetime.timedelta(hours=6), \ datetime.timedelta(hours=12), datetime.timedelta(hours=18) ] roundTime = datetime.timedelta(seconds=wantedDate.second,\ minutes=wantedDate.minute, hours=wantedDate.hour) closestTime = sorted(resolution, key=lambda t: abs(roundTime - t)) t = closestTime.index(min(closestTime)) closestTime = refDate + datetime.timedelta(hours=int(time[t])) print "Closest time: ", closestTime # find subset not to import all the data res = findSubsetIndices(latStart, latEnd, lonStart, lonEnd, lats, lons) lon, lat = meshgrid(lons[res[0]:res[1]], lats[res[2]:res[3]]) u = cdf.variables["u"][t, :, int(res[2]):int(res[3]), int(res[0]):int(res[1])] v = cdf.variables["v"][t, :, int(res[2]):int(res[3]), int(res[0]):int(res[1])] print "Extracted data for area: (%s,%s) to (%s,%s)" % ( lon.min(), lat.min(), lon.max(), lat.max()) # Squeezing u = ma.squeeze(u) v = ma.squeeze(v) # Get the speed w = ma.sqrt(u**2 + v**2) print "Preparing Basemap" # setup nsper basemap # Lat/Lon coords of image corners if lonStart < 0 and lonEnd < 0: lon_0 = -(abs(lonEnd) + abs(lonStart)) / 2.0 else: lon_0 = (abs(lonEnd) + abs(lonStart)) / 2.0 if latStart < 0 and latEnd < 0: lat_0 = -(abs(latEnd) + abs(latStart)) / 2.0 else: lat_0 = (abs(latEnd) + abs(latStart)) / 2.0 if m is None: print("Using default NSPER Basemap \n") # m = Basemap(llcrnrlat=latStart,urcrnrlat=latEnd,\ # llcrnrlon=lonStart,urcrnrlon=lonEnd,\ # rsphere=(6378137.00,6356752.3142),\ # resolution='h',area_thresh=1000.,projection='lcc',\ # lat_1=latStart,lon_0=lon_0) m = Basemap(llcrnrlat=latStart,urcrnrlat=latEnd,\ llcrnrlon=lonStart,urcrnrlon=lonEnd,\ satellite_height=798000, \ resolution='h',area_thresh=1000.,projection='nsper',\ lat_1=latStart,lon_0=lon_0,lat_0=lat_0) x, y = m(lon, lat) #!!!Check!!! #Compared Q and QQ - no difference #uproj,vproj,xx,yy = \ #m.transform_vector(u[t],v[t],lon[-1,:],lat[:,-1],13,13,returnxy=True,masked=True) #Q = m.quiver(xx,yy,uproj,vproj) #QQ = m.quiver(x,y,u[t],v[t]) plt.close('all') plt.figure() # maximizing figure # mng = plt.get_current_fig_manager() # mng.resize(1920,1080) if contour is 'fill': CS1 = m.contourf(x, y, w) else: CS1 = m.pcolor(x, y, w) Q = m.quiver(x, y, u, v) #Checked in matlab should be right # make quiver key plt.quiverkey(Q, 0.07, -0.03, 10, r'$10 \frac{m}{s}$', labelpos='W') CS1.axis = 'tight' plt.jet() cb = plt.colorbar(CS1, orientation='vertical', extend='both', shrink=0.8) cb.set_label('Seawinds [m/s]') m.drawmeridians(arange(lon.min(), lon.max(), 0.5), labels=[0, 0, 0, 1]) m.drawparallels(arange(lat.min(), lat.max(), 0.5), labels=[1, 0, 0, 0]) m.drawcoastlines() plt.draw() plt.savefig('/home/mag/' + name + closestTime.strftime('%Y%m%d') + '.png', \ facecolor='w', edgecolor='w', dpi=100, bbox_inches="tight", pad_inches=1.75)
def plot_panel(n, fig, proj, pole, var, clevels, cmap, title, stats=None): # var_min = float(var.min()) # var_max = float(var.max()) # var_mean = cdutil.averager(var, axis='xy', weights='generate') var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze( var.asma() ) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # Contour plot ax = fig.add_axes(panel[n],projection=proj) ax.set_global() ax.gridlines() if pole == 'N': ax.set_extent([-180, 180, 50, 90], crs=ccrs.PlateCarree()) elif pole == 'S': ax.set_extent([-180, 180, -55, -90], crs=ccrs.PlateCarree()) p1 = ax.contourf(lon, lat, var, transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend='both', ) ax.set_aspect('auto') ax.coastlines(lw=0.3) theta = np.linspace(0, 2*np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) if title[0] != None: ax.set_title(title[0], loc='left', fontdict=plotSideTitle) if title[1] != None: t = ax.set_title(title[1], fontdict=plotTitle) if title[0] != None or title[2] != None: t.set_position([.5, 1.06]) if title[2] != None: ax.set_title(title[2], loc='right', fontdict=plotSideTitle) # Color bar cbax = fig.add_axes((panel[n][0]+0.35,panel[n][1]+0.0354,0.0326,0.1792)) cbar = fig.colorbar(p1, cax=cbax) w,h = get_ax_size(fig,cbax) if levels == None: cbar.ax.tick_params(labelsize=9.0, length=0) else: cbar.set_ticks(levels[1:-1]) labels = ["%4.1f" % l for l in levels[1:-1]] cbar.ax.set_yticklabels(labels,ha='right') cbar.ax.tick_params(labelsize=9.0, pad=25, length=0) # Min, Mean, Max fig.text(panel[n][0]+0.35,panel[n][1]+0.225,"Max\nMean\nMin",ha='left',fontdict=plotSideTitle) fig.text(panel[n][0]+0.45,panel[n][1]+0.225,"%.2f\n%.2f\n%.2f" % stats[0:3],ha='right',fontdict=plotSideTitle) # RMSE, CORR if len(stats) == 5: fig.text(panel[n][0]+0.35,panel[n][1]+0.,"RMSE\nCORR",ha='left',fontdict=plotSideTitle) fig.text(panel[n][0]+0.45,panel[n][1]+0.,"%.2f\n%.2f" % stats[3:5],ha='right',fontdict=plotSideTitle)
def inpolygon(polygon, xp, yp): return np.array([Point(x, y).intersects(polygon) for x, y in zip(xp, yp)], dtype=np.bool) # <codecell> mask = inpolygon(polygon, x.ravel(), y.ravel()) # <codecell> mask_grid = mask.reshape(x.shape) plt.imshow(mask_grid, origin ='lower') # <codecell> hind_avg = ma.squeeze(ma.masked_array(avg.variables['temp_latlon'][:])[0,0]) hind_avg.shape # <codecell> plt.imshow(hind_avg, origin = 'lower') # <codecell> x = [50, 100, 120] y = [50, 100, 25] coordlist=np.vstack((x,y)).T print coordlist # <codecell>
def plot_panel(n, fig, proj, pole, var, clevels, cmap, title, parameters, stats=None): var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # Contour plot ax = fig.add_axes(panel[n], projection=proj) ax.set_global() ax.gridlines() if pole == 'N': ax.set_extent([-180, 180, 50, 90], crs=ccrs.PlateCarree()) elif pole == 'S': ax.set_extent([-180, 180, -55, -90], crs=ccrs.PlateCarree()) cmap = get_colormap(cmap, parameters) p1 = ax.contourf( lon, lat, var, transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend='both', ) ax.set_aspect('auto') ax.coastlines(lw=0.3) theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) # Plot titles for i in range(3): if title[i] is None: title[i] = '' t = ax.set_title('%s\n%s' % (title[0], title[2]), loc='left', fontdict=plotSideTitle) t = ax.set_title(title[1], fontdict=plotTitle) if title[0] != '' or title[2] != '': t.set_position([.5, 1.06]) # Color bar cbax = fig.add_axes( (panel[n][0] + 0.35, panel[n][1] + 0.0354, 0.0326, 0.1792)) cbar = fig.colorbar(p1, cax=cbax) w, h = get_ax_size(fig, cbax) if levels is None: cbar.ax.tick_params(labelsize=9.0, length=0) else: maxval = np.amax(np.absolute(levels[1:-1])) if maxval < 10.0: fmt = "%5.2f" pad = 25 elif maxval < 100.0: fmt = "%5.1f" pad = 25 else: fmt = "%6.1f" pad = 30 cbar.set_ticks(levels[1:-1]) labels = [fmt % l for l in levels[1:-1]] cbar.ax.set_yticklabels(labels, ha='right') cbar.ax.tick_params(labelsize=9.0, pad=pad, length=0) # Min, Mean, Max fig.text(panel[n][0] + 0.35, panel[n][1] + 0.225, "Max\nMean\nMin", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.45, panel[n][1] + 0.225, "%.2f\n%.2f\n%.2f" % stats[0:3], ha='right', fontdict=plotSideTitle) # RMSE, CORR if len(stats) == 5: fig.text(panel[n][0] + 0.35, panel[n][1] + 0., "RMSE\nCORR", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.45, panel[n][1] + 0., "%.2f\n%.2f" % stats[3:5], ha='right', fontdict=plotSideTitle)
def plot_panel_map(n, fig, proj, var, clevels, cmap, title, parameter, conf=None, stats={}): var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # Contour plot ax = fig.add_axes(panel[n], projection=proj) region_str = parameter.regions[0] region = regions_specs[region_str] if "domain" in region.keys(): # type: ignore # Get domain to plot domain = region["domain"] # type: ignore else: # Assume global domain domain = cdutil.region.domain(latitude=(-90.0, 90, "ccb")) kargs = domain.components()[0].kargs lon_west, lon_east, lat_south, lat_north = (0, 360, -90, 90) if "longitude" in kargs: lon_west, lon_east, _ = kargs["longitude"] if "latitude" in kargs: lat_south, lat_north, _ = kargs["latitude"] lon_covered = lon_east - lon_west lon_step = determine_tick_step(lon_covered) xticks = np.arange(lon_west, lon_east, lon_step) # Subtract 0.50 to get 0 W to show up on the right side of the plot. # If less than 0.50 is subtracted, then 0 W will overlap 0 E on the left side of the plot. # If a number is added, then the value won't show up at all. xticks = np.append(xticks, lon_east - 0.50) lat_covered = lat_north - lat_south lat_step = determine_tick_step(lat_covered) yticks = np.arange(lat_south, lat_north, lat_step) yticks = np.append(yticks, lat_north) ax.set_extent([lon_west, lon_east, lat_south, lat_north], crs=proj) cmap = get_colormap(cmap, parameter) contours = ax.contourf( lon, lat, var, transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend="both", ) if conf is not None: conf = add_cyclic(conf) conf = ma.squeeze(conf.asma()) # Values in conf will be either 0 or 1. Thus, there are only two levels - # represented by the no-hatching and hatching levels. ax.contourf( lon, lat, conf, 2, transform=ccrs.PlateCarree(), norm=norm, colors="none", extend="both", hatches=[None, "//"], ) # Full world would be aspect 360/(2*180) = 1 ax.set_aspect((lon_east - lon_west) / (2 * (lat_north - lat_south))) ax.coastlines(lw=0.3) if title[0] is not None: ax.set_title(title[0], loc="left", fontdict=plotSideTitle) if title[1] is not None: ax.set_title(title[1], fontdict=plotTitle) if title[2] is not None: ax.set_title(title[2], loc="right", fontdict=plotSideTitle) ax.set_xticks(xticks, crs=ccrs.PlateCarree()) ax.set_yticks(yticks, crs=ccrs.PlateCarree()) lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format=".0f") lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction="out", width=1) ax.xaxis.set_ticks_position("bottom") ax.yaxis.set_ticks_position("left") # Place a vertical line in the middle of the plot - i.e. 180 degrees ax.axvline(x=0.5, color="k", linewidth=0.5) # Color bar cbax = fig.add_axes( (panel[n][0] + 0.6635, panel[n][1] + 0.0115, 0.0326, 0.1792)) cbar = fig.colorbar(contours, cax=cbax) w, h = get_ax_size(fig, cbax) if levels is None: cbar.ax.tick_params(labelsize=9.0, length=0) else: maxval = np.amax(np.absolute(levels[1:-1])) if maxval < 1.0: fmt = "%5.3f" pad = 30 elif maxval < 10.0: fmt = "%5.2f" pad = 25 elif maxval < 100.0: fmt = "%5.1f" pad = 25 else: fmt = "%6.1f" pad = 30 cbar.set_ticks(levels[1:-1]) labels = [fmt % level for level in levels[1:-1]] cbar.ax.set_yticklabels(labels, ha="right") cbar.ax.tick_params(labelsize=9.0, pad=pad, length=0) # Display stats if stats: top_stats = (stats["max"], stats["min"], stats["mean"], stats["std"]) top_text = "Max\nMin\nMean\nSTD" fig.text( panel[n][0] + 0.6635, panel[n][1] + 0.2107, top_text, ha="left", fontdict=plotSideTitle, ) fig.text( panel[n][0] + 0.7635, panel[n][1] + 0.2107, "%.2f\n%.2f\n%.2f\n%.2f" % top_stats, ha="right", fontdict=plotSideTitle, ) if "rmse" in stats.keys(): bottom_stats = (stats["rmse"], stats["corr"]) bottom_text = "RMSE\nCORR" fig.text( panel[n][0] + 0.6635, panel[n][1] - 0.0205, bottom_text, ha="left", fontdict=plotSideTitle, ) fig.text( panel[n][0] + 0.7635, panel[n][1] - 0.0205, "%.2f\n%.2f" % bottom_stats, ha="right", fontdict=plotSideTitle, ) # Hatch text if conf is not None: hatch_text = "Hatched when pvalue < 0.05" fig.text( panel[n][0] + 0.25, panel[n][1] - 0.0355, hatch_text, ha="right", fontdict=plotSideTitle, )
# currentTime=refDate + datetime.timedelta(hours=int(time[t])) # #print currentTime # if currentTime.year in wantedYears and currentTime.month in wantedMonths: # print "Current time inspecting: %s"%(currentTime) # myIndex=t; dateString="%s"%(currentTime) # find subset not to import all the data res = findSubsetIndices(41,43,2,4,lats,lons) lon,lat=meshgrid(lons[res[0]:res[1]],lats[res[2]:res[3]]) sst = cdf.variables["sst"][:,:,int(res[2]):int(res[3]),int(res[0]):int(res[1])] ice = cdf.variables["ice"][:,:,int(res[2]):int(res[3]),int(res[0]):int(res[1])] print "Extracted data for area: (%s,%s) to (%s,%s)"%(lon.min(),lat.min(),lon.max(),lat.max()) # Squeezing sst = ma.squeeze(sst) ice = ma.squeeze(ice) print "Preparing Basemap" # setup nsper basemap # Lat/Lon coords of image corners ll_lat = lat.min() ur_lat = lat.max() ll_lon = lon.min() ur_lon = lon.max() cent_lat = lat.mean() cent_lon = lon.mean() m = Basemap(llcrnrlat=ll_lat, urcrnrlat=ur_lat,\ llcrnrlon=ll_lon, urcrnrlon=ur_lon, \ resolution='h', projection='nsper', \
def plot(reference, test, diff, metrics_dict, parameter): # Create figure fig = plt.figure(figsize=parameter.figsize, dpi=parameter.dpi) # Top panel ax1 = fig.add_axes(panel[0]) ax1.plot(test.getLatitude()[:], ma.squeeze(test.asma()), 'k', linewidth=2) ax1.plot(reference.getLatitude()[:], ma.squeeze(reference.asma()), 'r', linewidth=2) ax1.set_xticks([-90, -60, -30, 0, 30, 60, 90]) ax1.set_xlim(-90, 90) ax1.tick_params(labelsize=11.0, direction='out', width=1) ax1.xaxis.set_ticks_position('bottom') ax1.set_ylabel(test.long_name + ' (' + test.units + ')') test_title = "Test" if parameter.test_title == '' else parameter.test_title test_title += ' : {}'.format(parameter.test_name_yrs) ref_title = "Reference" if parameter.reference_title == '' else parameter.reference_title ref_title += ' : {}'.format(parameter.reference_name) fig.text(panel[0][0], panel[0][1] + panel[0][3] + 0.03, test_title, ha='left', fontdict=plotSideTitle, color='black') fig.text(panel[0][0], panel[0][1] + panel[0][3] + 0.01, ref_title, ha='left', fontdict=plotSideTitle, color='red') # Bottom panel ax2 = fig.add_axes(panel[1]) ax2.plot(diff.getLatitude()[:], ma.squeeze(diff.asma()), 'k', linewidth=2) ax2.axhline(y=0, color='0.5') ax2.set_title(parameter.diff_title, fontdict=plotSideTitle, loc='center') ax2.set_xticks([-90, -60, -30, 0, 30, 60, 90]) ax2.set_xlim(-90, 90) ax2.tick_params(labelsize=11.0, direction='out', width=1) ax2.xaxis.set_ticks_position('bottom') ax2.set_ylabel(test.long_name + ' (' + test.units + ')') # Figure title fig.suptitle(parameter.main_title, x=0.5, y=0.95, fontsize=18) # Save figure for f in parameter.output_format: f = f.lower().split('.')[-1] fnm = os.path.join(get_output_dir(parameter.current_set, parameter), parameter.output_file + '.' + f) plt.savefig(fnm) # Get the filename that the user has passed in and display that. # When running in a container, the paths are modified. fnm = os.path.join( get_output_dir(parameter.current_set, parameter, ignore_container=True), parameter.output_file + '.' + f) print('Plot saved in: ' + fnm) # Save individual subplots for f in parameter.output_format_subplot: fnm = os.path.join(get_output_dir(parameter.current_set, parameter), parameter.output_file) page = fig.get_size_inches() i = 0 for p in panel: # Extent of subplot subpage = np.array(p).reshape(2, 2) subpage[1, :] = subpage[0, :] + subpage[1, :] subpage = subpage + np.array(border).reshape(2, 2) subpage = list(((subpage) * page).flatten()) extent = matplotlib.transforms.Bbox.from_extents(*subpage) # Save subplot fname = fnm + '.%i.' % (i) + f plt.savefig(fname, bbox_inches=extent) orig_fnm = os.path.join( get_output_dir(parameter.current_set, parameter, ignore_container=True), parameter.output_file) fname = orig_fnm + '.%i.' % (i) + f print('Sub-plot saved in: ' + fname) i += 1 plt.close()
# cm.gist_heat.set_bad('black', 0.8) # Set masked values to dark gray cb = plt.colorbar(img, orientation=orientation, extend='both', shrink=shrink, label=unitlabel) cm.bwr.set_bad('black', 0.8) # Set masked values to dark gray cb.locator = ticker.MaxNLocator(nbins=8) cb.update_ticks() return img # In[164]: #This loop goes through each variable, pulls out the mean and standard deviation for the #projected and hindcasts models, and plots the differences between the two time periods. for i in range(len(metadata['name'])): #Get a variable, just to see how many dimensions it has. hind_avg = ma.squeeze(ma.masked_array(avg.variables[metadata.orgName[i]][:])) surface_flag = 0 if len(hind_avg.shape) > 2: # this takes just the surface value hind_avg = ma.squeeze(ma.masked_array(avg.variables[metadata.orgName[i]][:])[0,0]) hind_stddev = ma.squeeze(ma.masked_array(stddev.variables[metadata.orgName[i]][:])[0,0]) proj_avg = ma.squeeze(ma.masked_array(pavg.variables[metadata.orgName[i]][:])[0,0]) proj_stddev = ma.squeeze(ma.masked_array(pstddev.variables[metadata.orgName[i]][:])[0,0]) surface_flag = 1 else: hind_stddev = ma.squeeze(ma.masked_array(stddev.variables[metadata.orgName[i]][:])[0]) proj_avg = ma.squeeze(ma.masked_array(pavg.variables[metadata.orgName[i]][:])[0]) proj_stddev = ma.squeeze(ma.masked_array(pstddev.variables[metadata.orgName[i]][:])[0]) #Create subplots fig = plt.figure(figsize=(16,5)) if surface_flag == 1: plt.suptitle(metadata.name[i] + ', Surface Only', fontsize=20)
def plot_panel(n, fig, proj, var, clevels, cmap, title, parameters, stats=None): var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # ax.set_global() region_str = parameters.regions[0] region = regions_specs[region_str] global_domain = True full_lon = True if "domain" in region.keys(): # type: ignore # Get domain to plot domain = region["domain"] # type: ignore global_domain = False else: # Assume global domain domain = cdutil.region.domain(latitude=(-90.0, 90, "ccb")) kargs = domain.components()[0].kargs lon_west, lon_east, lat_south, lat_north = (0, 360, -90, 90) if "longitude" in kargs: full_lon = False lon_west, lon_east, _ = kargs["longitude"] # Note cartopy Problem with gridlines across the dateline:https://github.com/SciTools/cartopy/issues/821. Region cross dateline is not supported yet. if lon_west > 180 and lon_east > 180: lon_west = lon_west - 360 lon_east = lon_east - 360 if "latitude" in kargs: lat_south, lat_north, _ = kargs["latitude"] lon_covered = lon_east - lon_west lon_step = determine_tick_step(lon_covered) xticks = np.arange(lon_west, lon_east, lon_step) # Subtract 0.50 to get 0 W to show up on the right side of the plot. # If less than 0.50 is subtracted, then 0 W will overlap 0 E on the left side of the plot. # If a number is added, then the value won't show up at all. if global_domain or full_lon: xticks = np.append(xticks, lon_east - 0.50) proj = ccrs.PlateCarree(central_longitude=180) else: xticks = np.append(xticks, lon_east) lat_covered = lat_north - lat_south lat_step = determine_tick_step(lat_covered) yticks = np.arange(lat_south, lat_north, lat_step) yticks = np.append(yticks, lat_north) # Contour plot ax = fig.add_axes(panel[n], projection=proj) ax.set_extent([lon_west, lon_east, lat_south, lat_north], crs=proj) cmap = get_colormap(cmap, parameters) p1 = ax.contourf( lon, lat, var, transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend="both", ) # ax.set_aspect('auto') # Full world would be aspect 360/(2*180) = 1 ax.set_aspect((lon_east - lon_west) / (2 * (lat_north - lat_south))) ax.coastlines(lw=0.3) if not global_domain and "RRM" in region_str: ax.coastlines(resolution="50m", color="black", linewidth=1) state_borders = cfeature.NaturalEarthFeature( category="cultural", name="admin_1_states_provinces_lakes", scale="50m", facecolor="none", ) ax.add_feature(state_borders, edgecolor="black") if title[0] is not None: ax.set_title(title[0], loc="left", fontdict=plotSideTitle) if title[1] is not None: ax.set_title(title[1], fontdict=plotTitle) if title[2] is not None: ax.set_title(title[2], loc="right", fontdict=plotSideTitle) ax.set_xticks(xticks, crs=ccrs.PlateCarree()) ax.set_yticks(yticks, crs=ccrs.PlateCarree()) lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format=".0f") lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction="out", width=1) ax.xaxis.set_ticks_position("bottom") ax.yaxis.set_ticks_position("left") # Color bar cbax = fig.add_axes( (panel[n][0] + 0.6635, panel[n][1] + 0.0215, 0.0326, 0.1792)) cbar = fig.colorbar(p1, cax=cbax) w, h = get_ax_size(fig, cbax) if levels is None: cbar.ax.tick_params(labelsize=9.0, length=0) else: maxval = np.amax(np.absolute(levels[1:-1])) if maxval < 10.0: fmt = "%5.2f" pad = 25 elif maxval < 100.0: fmt = "%5.1f" pad = 25 else: fmt = "%6.1f" pad = 30 cbar.set_ticks(levels[1:-1]) labels = [fmt % level for level in levels[1:-1]] cbar.ax.set_yticklabels(labels, ha="right") cbar.ax.tick_params(labelsize=9.0, pad=pad, length=0) # Min, Mean, Max fig.text( panel[n][0] + 0.6635, panel[n][1] + 0.2107, "Max\nMean\nMin", ha="left", fontdict=plotSideTitle, ) fig.text( panel[n][0] + 0.7635, panel[n][1] + 0.2107, "%.2f\n%.2f\n%.2f" % stats[0:3], ha="right", fontdict=plotSideTitle, ) # RMSE, CORR if len(stats) == 5: fig.text( panel[n][0] + 0.6635, panel[n][1] - 0.0105, "RMSE\nCORR", ha="left", fontdict=plotSideTitle, ) fig.text( panel[n][0] + 0.7635, panel[n][1] - 0.0105, "%.2f\n%.2f" % stats[3:5], ha="right", fontdict=plotSideTitle, ) # grid resolution info: if n == 2 and "RRM" in region_str: dlat = lat[2] - lat[1] dlon = lon[2] - lon[1] fig.text( panel[n][0] + 0.4635, panel[n][1] - 0.04, "Resolution: {:.2f}x{:.2f}".format(dlat, dlon), ha="left", fontdict=plotSideTitle, )
def _sel(binname, name, varid, loop_block, endian, nx, ny, nz, nt, undef, do_squeese, zidx=None, tidx=None): # check for index exceeding if tidx == None: tloop = range(nt) elif isinstance(tidx, int): if tidx >= nt: raise ExceedTidxError(name, (nt, nz, ny, nx), tidx) tloop = [tidx] elif isinstance(tidx, (list, tuple, np.ndarray)): for _itidx in tidx: if _itidx >= nt: raise ExceedTidxError(name, (nt, nz, ny, nx), _itidx) tloop = tidx else: raise InvalidTidxError(type(tidx)) if zidx == None: zloop = range(nz) elif isinstance(zidx, int): if zidx >= nz: raise ExceedZidxError(name, (nt, nz, ny, nx), zidx) zloop = [zidx] elif isinstance(zidx, (list, tuple, np.ndarray)): for _izidx in zidx: if _izidx >= nz: raise ExceedZidxError(name, (nt, nz, ny, nx), _izidx) zloop = zidx else: raise InvalidZidxError(type(zidx)) with open(binname, "rb") as f: _data = [] for _it in tloop: if zloop == range(nz): f.seek((_it * loop_block + varid) * nx * ny * 4, os.SEEK_SET) _data.append(ma.masked_equal(ma.masked_array(np.fromfile(f,\ dtype=_endian2simbole(endian)+"f4", count=nx*ny*nz)), value=undef).reshape(nz, ny, nx)) else: _idata = [] for _iz in zloop: f.seek((varid + _it * loop_block + _iz) * nx * ny * 4, os.SEEK_SET) _idata.append( ma.masked_equal(ma.masked_array( np.fromfile(f, dtype=_endian2simbole(endian) + "f4", count=nx * ny)), value=undef).reshape(ny, nx)) _data.append(_idata) _data = ma.masked_array(_data) if do_squeese: return ma.squeeze(_data) else: return _data