import sys
import os
import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt
import matplotlib.pyplot as P
from mpl_toolkits.basemap import Basemap,shiftgrid,maskoceans
from matplotlib.path import Path
from matplotlib.patches import PathPatch
#from osgeo import gdal
import numpy
import shapefile
from netCDF4 import Dataset
import numpy as N
from netCDF4 import Dataset, num2date,date2num
from scipy import signal,stats
import matplotlib as mpl
import matplotlib.colors as colors
from datetime import datetime
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.offsetbox import AnchoredText
from skimage.metrics import structural_similarity as ssim
plt.style.use('seaborn-talk')
params = { 'legend.fontsize': 8,\
'font.family': 'serif',\
}
plt.rcParams.update(params)
def ma(a,n=3):
ret=N.cumsum(a,dtype=float)
ret[n:]=ret[n:]-ret[:-n]
return ret[n-1:]/n
for date in ['1992-02-04','1991-03-22']:
for rcm in ['era-rca','era-clm','era-wrf']:
file = '../data/rcms/%s_%s.cdf'%(rcm,date)
nc = Dataset(file,'r')
lon = N.array(nc.variables['lon'][:]);nx = len(lon)
lat = N.array(nc.variables['lat'][:]);ny = len(lat)
dat = N.array(nc.variables['pr'][:])*24*3600
nc.close()
print (dat.shape)
P.figure()
ax = P.subplot(111)
m = Basemap(projection='cyl',llcrnrlat=29,urcrnrlat=38,llcrnrlon=22,urcrnrlon=38.5,resolution='h')
m.drawcoastlines(color='k')
m.drawcountries(linewidth=1,color='k')
#m.drawlsmask(land_color = "gray",ocean_color="gray",resolution = 'h')
m.shadedrelief()
xx,yy = m(lon,lat)
dats = maskoceans(xx,yy,dat[0,:,:])
lev = N.arange(20,140,20)
CM = ax.contourf(xx,yy,dats,levels=lev,cmap=plt.get_cmap('YlGnBu'),extend='max')
ax.contour(xx,yy,dats,levels=lev,colors='k',linewidths=0.1)
cbar = m.colorbar(CM,location='right',extend='max',pad="1%",drawedges=True,ticks=lev)
cbar.set_label('pr',fontsize=12,weight='bold')
cbar.ax.tick_params(labelsize=8)
at = AnchoredText('%s: %s'%(rcm,date),prop=dict(size=14),frameon=True,loc='lower left')
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)
xx,yy = m(35.930359,31.963158)
m.scatter(xx,yy,s=20,c='r',ec='k')
P.text(xx,yy,'Amman',fontsize=10)
xx,yy = m(35.444832,30.328960)
m.scatter(xx,yy,s=20,c='r',ec='k')
P.text(xx,yy,'Petra',fontsize=10)
plt.savefig('./img/%s_%s.png'%(rcm,date),dpi=240,transparent=False,bbox_inches='tight',pad_inches=0.0)