def setUp(self): self.clt = cdms2.open(cdat_info.get_sampledata_path() + '/clt.nc')('clt')[0, ...] self.tas = cdms2.open(cdat_info.get_sampledata_path() + \ '/tas_ecm_1979.nc')('tas')[0, ...] if PLOT: lllat = self.clt.getLatitude()[:].min() urlat = self.clt.getLatitude()[:].max() lllon = self.clt.getLongitude()[:].min() urlon = self.clt.getLongitude()[:].max() self.cmap = bm(llcrnrlat=lllat, llcrnrlon=lllon, urcrnrlat=urlat, urcrnrlon=urlon, resolution='i', projection='cyl') lllat = self.tas.getLatitude()[:].min() urlat = self.tas.getLatitude()[:].max() lllon = self.tas.getLongitude()[:].min() urlon = self.tas.getLongitude()[:].max() self.tmap = bm(llcrnrlat=lllat, llcrnrlon=lllon, urcrnrlat=urlat, urcrnrlon=urlon, resolution='i', projection='cyl')
def setUp(self): self.rootPe = 0 self.pe = MPI.COMM_WORLD.Get_rank() self.clt = cdms2.open(sys.prefix + '/sample_data/clt.nc')('clt')[0, ...] # self.tas = cdms2.open(sys.prefix + \ # 'tas_ccsr-95a_1979.01-1979.12.nc')('tas')[0, 0,...] self.tas = cdms2.open(sys.prefix + \ '/sample_data/tas_ecm_1979.nc')('tas')[0, ...] if PLOT: lllat = self.clt.getLatitude()[:].min() urlat = self.clt.getLatitude()[:].max() lllon = self.clt.getLongitude()[:].min() urlon = self.clt.getLongitude()[:].max() self.cmap = bm(llcrnrlat = lllat, llcrnrlon = lllon, urcrnrlat = urlat, urcrnrlon = urlon, resolution = 'i', projection = 'cyl') lllat = self.tas.getLatitude()[:].min() urlat = self.tas.getLatitude()[:].max() lllon = self.tas.getLongitude()[:].min() urlon = self.tas.getLongitude()[:].max() self.tmap = bm(llcrnrlat = lllat, llcrnrlon = lllon, urcrnrlat = urlat, urcrnrlon = urlon, resolution = 'i', projection = 'cyl')
def plotmap(self, domain = [0., 360., -90., 90.], res='c', stepp=2, scale=20): latitudes = self.windspeed.latitudes.data longitudes = self.windspeed.longitudes.data m = bm(projection='cyl',llcrnrlat=latitudes.min(),urcrnrlat=latitudes.max(),\ llcrnrlon=longitudes.min(),urcrnrlon=longitudes.max(),\ lat_ts=0, resolution=res) lons, lats = np.meshgrid(longitudes, latitudes) cmap = palettable.colorbrewer.sequential.Oranges_9.mpl_colormap f, ax = plt.subplots(figsize=(10,6)) m.ax = ax x, y = m(lons, lats) im = m.pcolormesh(lons, lats, self.windspeed.data, cmap=cmap) cb = m.colorbar(im) cb.set_label('wind speed (m/s)', fontsize=14) Q = m.quiver(x[::stepp,::stepp], y[::stepp,::stepp], \ self.uanoms.data[::stepp,::stepp], self.vanoms.data[::stepp,::stepp], \ pivot='middle', scale=scale) l,b,w,h = ax.get_position().bounds qk = plt.quiverkey(Q, l+w-0.1, b-0.03, 5, "5 m/s", labelpos='E', fontproperties={'size':14}, coordinates='figure') m.drawcoastlines() return f
def _get_basemap(self): """ given the projection and the domain, returns the dataset as well as the Basemap instance """ if self.domain: self._get_domain(self.domain) else: self.dset_domain = self.analogs.dset # get the lat and lons latitudes = self.dset_domain['latitudes'].data longitudes = self.dset_domain['longitudes'].data if not(self.proj): self.proj = 'cyl' if self.proj in ['cyl', 'merc']: """ NOTE: using a Mercator projection won't work if either / both the northermost / southermost latitudes are 90 / -90 if one wants (but why would you do that ?) to plot a global domain in merc, one needs to pass: domain = [0., 360., -89.9, 89.9] to the `scalar_plot` class Any sub-domain of a global domain will work fine """ self.m = bm(projection=self.proj,llcrnrlat=latitudes.min(),urcrnrlat=latitudes.max(),\ llcrnrlon=longitudes.min(),urcrnrlon=longitudes.max(),\ lat_ts=0, resolution=self.res) if self.proj == 'moll': self.m = bm(projection='moll',lon_0=180, resolution=self.res) if self.proj in ['npstere','spstere']: self.m = bm(projection=self.proj,boundinglat=0,lon_0=0, resolution=self.res, round=True) return self
def setUp(self): self.clt = cdms2.open(sys.prefix + '/sample_data/clt.nc')('clt')[0, ...] self.tas = cdms2.open(sys.prefix + \ '/sample_data/tas_ecm_1979.nc')('tas')[0, ...] if PLOT: lllat = self.clt.getLatitude()[:].min() urlat = self.clt.getLatitude()[:].max() lllon = self.clt.getLongitude()[:].min() urlon = self.clt.getLongitude()[:].max() self.cmap = bm(llcrnrlat = lllat, llcrnrlon = lllon, urcrnrlat = urlat, urcrnrlon = urlon, resolution = 'i', projection = 'cyl') lllat = self.tas.getLatitude()[:].min() urlat = self.tas.getLatitude()[:].max() lllon = self.tas.getLongitude()[:].min() urlon = self.tas.getLongitude()[:].max() self.tmap = bm(llcrnrlat = lllat, llcrnrlon = lllon, urcrnrlat = urlat, urcrnrlon = urlon, resolution = 'i', projection = 'cyl')
def plotmap(self, domain=[0., 360., -90., 90.], res='c', stepp=2, scale=20): latitudes = self.windspeed.latitudes.data longitudes = self.windspeed.longitudes.data m = bm(projection='cyl',llcrnrlat=latitudes.min(),urcrnrlat=latitudes.max(),\ llcrnrlon=longitudes.min(),urcrnrlon=longitudes.max(),\ lat_ts=0, resolution=res) lons, lats = np.meshgrid(longitudes, latitudes) cmap = palettable.colorbrewer.sequential.Oranges_9.mpl_colormap f, ax = plt.subplots(figsize=(10, 6)) m.ax = ax x, y = m(lons, lats) im = m.pcolormesh(lons, lats, self.windspeed.data, cmap=cmap) cb = m.colorbar(im) cb.set_label('wind speed (m/s)', fontsize=14) Q = m.quiver(x[::stepp,::stepp], y[::stepp,::stepp], \ self.uanoms.data[::stepp,::stepp], self.vanoms.data[::stepp,::stepp], \ pivot='middle', scale=scale) l, b, w, h = ax.get_position().bounds qk = plt.quiverkey(Q, l + w - 0.1, b - 0.03, 5, "5 m/s", labelpos='E', fontproperties={'size': 14}, coordinates='figure') m.drawcoastlines() return f
left_lon = boxlon[0] right_lon = boxlon[-1] if 0 in boxlon[1:-2]: # if we cross the gml left_lon = boxlon[0] - 360 num_subplots = 2 * num_eofs for subplot in range(num_subplots): if subplot <= num_eofs - 1: #the F maps # Generate the clevs x = round(np.max(np.abs(eof_grid[subplot, :, :])), 2) clev = np.linspace(-x, x, 21) plt.subplot(2, num_eofs, subplot + 1) plt.title(round(varfrac[subplot], 3)) m = bm(projection='cea', llcrnrlat=southern_lat, urcrnrlat=northern_lat, llcrnrlon=left_lon, urcrnrlon=right_lon, resolution='c') m.drawcoastlines() m.drawmapboundary(fill_color='0.3') cs = m.contourf(bmlon, bmlat, eof_grid[subplot, :, :], clev, shading='flat', latlon=True) cbar = m.colorbar(cs, location='right', pad="5%") else: # the amplitude time series atx = np.arange(0, len(dates), 3) labx = np.array(dates)[atx] plt.subplot(2, num_eofs, subplot + 1)
corr_array[i,j,0] = corr(hold[i,j,:,0,0], hold[i,j,:,1,0]) corr_array[i,j,1] = corr(hold[i,j,:,0,1], hold[i,j,:,1,1]) #r, pval = regress(diffv1_master[:,i,j], diffv2_master[:,i,j]) corr_array[:,:,2] = corr_array[:,:,1] - corr_array[:,:,0] llcrnlat, urcrnlat, llcrnlon, urcrnrlon = [lats[0], lats[-1], lons[0], lons[-1]] if 0 in lons[1:-2]: # if we cross the gml llcrnlon = lons[0]-360 # Make the map of the corr array fig = plt.figure() plt.subplot(3,1,1) bmlon, bmlat = np.meshgrid(lons, lats) m = bm(projection = 'cea', llcrnrlat=llcrnlat,urcrnrlat=urcrnlat, llcrnrlon=llcrnlon,urcrnrlon=urcrnrlon,resolution='c') m.drawcoastlines() m.drawmapboundary(fill_color='0.3') clevs = np.linspace(-1, 1, 11) cs = m.contourf(bmlon, bmlat, corr_array[:,:,1], clevs, shading = 'flat', latlon = True, cmap=plt.cm.RdBu_r) cbar = m.colorbar(cs, location='right', pad="5%") cbar.set_label("correlation-coefficient", fontsize = 8) plt.title("test") plt.subplot(3,1,2) m = bm(projection = 'cea', llcrnrlat=llcrnlat,urcrnrlat=urcrnlat, llcrnrlon=llcrnlon,urcrnrlon=urcrnrlon,resolution='c') m.drawcoastlines() m.drawmapboundary(fill_color='0.3') clevs = np.linspace(-1, 1, 11) cs = m.contourf(bmlon, bmlat, corr_array[:,:,0], clevs, shading = 'flat', latlon = True, cmap=plt.cm.RdBu_r) cbar = m.colorbar(cs, location='right', pad="5%")
pass dset = xr.concat(l, dim='time') if ndays != 1: dset = dset.mean('time') dseta = dseta.mean('time') lat = dset.lat lon = dset.lon if os.path.exists( "/Users/nicolasf/operational/Vanuatu/code/basemap_pickle_SST.pickle" ): m = pickle.load( open( "/Users/nicolasf/operational/Vanuatu/code/basemap_pickle_SST.pickle", "rb" ) ) else: m = bm(projection='cyl',llcrnrlat=lat.data.min(),urcrnrlat=lat.data.max(), llcrnrlon=lon.data.min(),urcrnrlon=lon.data.max(), lat_ts=0,resolution='f') pickle.dump( m, open( "/Users/nicolasf/operational/Vanuatu/code/basemap_pickle_SST.pickle", "wb" ) ) lons, lats = np.meshgrid(lon, lat) jet = plt.get_cmap('jet') f = plot_field_contourf(m, dset['CRW_SST'], lats, lons, 22.5, 30, 0.25, grid=True, title='CRW SST, last {} days to {:%Y-%m-%d}\nData made available by NOAA Coral Reef Watch'.format(ndays, last_available), cmap=jet) f.savefig('/Users/nicolasf/operational/Vanuatu/figures/CRW_SST_last_{}days.png'.format(ndays), dpi=200) f = plot_field_contourf(m, dseta['CRW_SSTANOMALY'], lats, lons, -1.5, 1.5, 0.05, grid=True, title='CRW SST anomaly, last {} days to {:%Y-%m-%d}\nData made available by NOAA Coral Reef Watch'.format(ndays, last_available), cmap=cm.balance) f.savefig('/Users/nicolasf/operational/Vanuatu/figures/CRW_SST_anomaly_last_{}days.png'.format(ndays), dpi=200)
cities['Lae'] = (-6.73333, 147) cities['Alotau'] = (-10.316667, 150.433333) cities['Daru'] = (-9.083333, 143.2) cities['Madang'] = (-5.216667, 145.8) cities['Wewak'] = (-3.55, 143.633333) # ### original resolution of the dataset res = 0.25 # ### factor by which the resolution will be increased interp_factor = 10 # ### set up the map m = bm(projection='cyl', llcrnrlon=domain['lonmin']+0.25,\ llcrnrlat=domain['latmin']+0.25,\ urcrnrlon=domain['lonmax']-0.25,\ urcrnrlat=domain['latmax']-0.25, resolution='f') # get the date of today (UTC) today = datetime.utcnow() lag = 2 trmm_date = today - timedelta(days=lag) print("processing up to {:%Y-%m-%d}\n".format(trmm_date)) # loads the virtual stations file Virtual_Stations = pd.read_excel('../data/PNG_stations_subset.xls') for ndays in [30, 60, 90]:
# ## Apply the PCM to the dataset: # In[72]: get_ipython().run_cell_magic(u'time', u'', u'# Normalize data:\nXn = scaler.transform(X) \n\n# Reduce the dataset (compute the y):\nXr = reducer.transform(Xn) # Here we compute: np.dot(Xn - reducer.mean_, np.transpose(reducer.components_))\n\n# Classify the dataset:\nLABELS = gmm.predict(Xr) # [Np,1]\nPOST = gmm.predict_proba(Xr) # [Np,Nc]') # ## Time for figures # In[73]: # Plot LABELS map: lon = dsub['LONGITUDE'].values lat = dsub['LATITUDE'].values mp0 = bm(projection='cyl', llcrnrlat=-1,urcrnrlat=71, llcrnrlon=-91,urcrnrlon=1, lat_ts=0,resolution='c') fig, axes = plt.subplots(nrows=1,ncols=1,figsize=(5,5), dpi=160, facecolor='w', edgecolor='k') axes = np.reshape(axes,(1,1)) for ie in range(1): mp = mp0 mp.ax = axes[0,ie] plt.jet() sc = mp.scatter(lon,lat,1,LABELS) mp.drawmeridians(np.arange(0, 360, 10), labels=[0,0,0,1], color='0.8', linewidth=0.5, fontsize=8) mp.drawparallels(np.arange(-80, 80, 5), labels=[1,0,0,0], color='0.8', linewidth=0.5, fontsize=8) mp.drawlsmask(land_color='0.8', ocean_color='w') # mp.drawcoastlines() mp.colorbar(sc,ax=axes[0,ie]) # ax.set_title(tit) axes[0,ie].set_title("LABELS (K=%i)"%(K)) plt.tight_layout()
def plot(self): """ basemap plot of a scalar quantity """ if self.domain: self.get_domain(self.domain) else: self.dset_domain = self.compos.dset # get the lat and lons latitudes = self.dset_domain['latitudes'].data longitudes = self.dset_domain['longitudes'].data # get the data (composite anomalies) mat = self.dset_domain['composite_anomalies'].data pvalues = self.dset_domain['pvalues'].data if not(self.proj): self.proj = 'cyl' if self.proj in ['merc', 'cyl']: m = bm(projection=self.proj,llcrnrlat=latitudes.min(),urcrnrlat=latitudes.max(),\ llcrnrlon=longitudes.min(),urcrnrlon=longitudes.max(),\ lat_ts=0, resolution=self.res) if self.proj == 'moll': m = bm(projection='moll',lon_0=180, resolution=res) if self.proj in ['npstere','spstere']: m = bm(projection=self.proj,boundinglat=0,lon_0=0, resolution=self.res, round=True) # we add cyclic longitude mat, lon = addcyclic(mat, longitudes) pvalues, lon = addcyclic(pvalues, longitudes) longitudes = lon # m = pickle.load( open( "basemap.pickle", "rb" ) ) # meshgrid lon and lat for plotting with basemap lons, lats = np.meshgrid(longitudes, latitudes) # ravel and removes nans for calculation of intervals etc calc_data = self.compos.dset['composite_anomalies'].data calc_data = np.ravel(calc_data[np.isfinite(calc_data)]) # the following is borrowed from xray # see: plot.py in xray/xray/plot if self.vmin is None: self.vmin = np.percentile(calc_data, self.robust_percentile) if self.robust else calc_data.min() if self.vmax is None: self.vmax = np.percentile(calc_data, 100 - self.robust_percentile) if self.robust else calc_data.max() del(calc_data) # Simple heuristics for whether these data should have a divergent map divergent = ((self.vmin < 0) and (self.vmax > 0)) or self.center is not None # Now set center to 0 so math below makes sense if self.center is None: self.center = 0 # A divergent map should be symmetric around the center value if divergent: vlim = max(abs(self.vmin - self.center), abs(self.vmax - self.center)) self.vmin, self.vmax = -vlim, vlim # Now add in the centering value and set the limits self.vmin += self.center self.vmax += self.center # Choose default colormaps if not provided if self.cmap is None: if divergent: # get the colormap defined in the dset_dict self.cmap = eval(self.compos.dset_dict['plot']['cmap']) else: # get the default matplotlib colormap self.cmap = plt.get_cmap() # =============================================================== # plots r, c = mat.shape f, ax = plt.subplots(figsize=(8,8*(c/r)), dpi=200) # the basemap instance gets attached to the created axes m.ax = ax # pcolormesh im = m.pcolormesh(lons, lats, ma.masked_invalid(mat), \ vmin=self.vmin, vmax=self.vmax, cmap=self.cmap, latlon=True) # if test is defined, one contours the p-values for that level if self.test: #P = ma.where(self.dset['pvalues'].data <0.1 , 1, np.nan) #m.contourf(lon, lat, P, colors='0.99', \ # hatches=['...'], latlon=True, zorder=2, alpha=0.2) m.contour(lons, lats, pvalues, [self.test], latlon=True, \ colors='#8C001A', linewidths=1.5) # sets the colorbar cb = m.colorbar(im) [l.set_fontsize(14) for l in cb.ax.yaxis.get_ticklabels()] cb.set_label(self.compos.dset_dict['units'],fontsize=14) # draw the coastlines m.drawcoastlines() # if one is plotting SST data, fill the continents if self.compos.variable in ['sst','SST']: m.fillcontinents('0.8', lake_color='0.8') # if grid, plots the lat / lon lines on the map if self.grid: # if it's hires ('f' or 'h'), every 10 degrees if self.res in ['h','f']: m.drawmeridians(np.arange(0, 360, 5), labels=[0,0,0,1], fontsize=14) m.drawparallels(np.arange(-80, 80+5, 5), labels=[1,0,0,0], fontsize=14) # if not hires, plots lines every 40 degrees else: m.drawmeridians(np.arange(0, 360, 60), labels=[0,0,0,1], fontsize=14) m.drawparallels(np.arange(-80, 80+10, 40), labels=[1,0,0,0], fontsize=14) if not(self.border): ax.axis('off') # set the title from the description in the dataset + variable JSON entry ax.set_title(self.compos.dset_dict['description'], fontsize=14) return f self.dset_domain.close() plt.close(f)
http://nbviewer.jupyter.org/github/nicolasfauchereau/metocean/blob/master/notebooks/xray.ipynb Author: Mrugen Anil Deshmukh (mad5js) Date Created: 1 April 2016 """ import xray import numpy as np from mpl_toolkits.basemap import Basemap as bm from matplotlib import pyplot as plt import math # This piece of code has been used directly from the tutorial at - # http://nbviewer.jupyter.org/github/nicolasfauchereau/metocean/blob/master/notebooks/xray.ipynb m = bm(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\ llcrnrlon=0,urcrnrlon=360,\ lat_ts=0,resolution='c') # The following function has been borrowed from the given tutorial as well def plot_field(X, lat, lon, vmin, vmax, step, cmap=plt.get_cmap('jet'), ax=False, title=False, grid=False): if not ax: f, ax = plt.subplots(figsize=(10, 10)) m.ax = ax im = m.contourf(lons, lats, X, np.arange(vmin, vmax+step, step), latlon=True, cmap=cmap, extend='both', ax=ax) m.drawcoastlines() if grid: m.drawmeridians(np.arange(0, 360, 60), labels=[0,0,0,1]) m.drawparallels([-90, 0, 90], labels=[1,0,0,0]) m.colorbar(im) if title: ax.set_title(title)