Skip to content

Patterns

1 2 3 4 5 6 7

Code

Importing

import sys
import os
import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as P
from mpl_toolkits.basemap import Basemap,shiftgrid
from netCDF4 import Dataset
import numpy as N
from netCDF4 import Dataset, num2date,date2num
from datetime import datetime
from matplotlib.offsetbox import AnchoredText
from skimage.metrics import structural_similarity as ssim

Setting

P.style.use('seaborn-talk')

params = {      'legend.fontsize': 8,\
                'font.family': 'serif',\
                }
P.rcParams.update(params)

Reading

file = '../../data/ncep/z500_day_ncep_19810101-20211231_eu_rm3.cdf'

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['phi'][:])
tim = nc.variables['T']

tim = num2date(tim[:], units=tim.units,calendar=tim.calendar) 

jj = []
mm = []
dd = []

for it in tim:

    jj.append(it.year)
    mm.append(it.month)
    dd.append(it.day)

jj = N.array(jj)
mm = N.array(mm)
dd = N.array(dd)

nc.close()

nd = len(dd)

x0 = N.min(lon)
x1 = N.max(lon)
y0 = N.min(lat)
y1 = N.max(lat)

print (dat.shape)

Mapping

for d in range(10900,nd):

    date = '%4i-%02i-%02i'%(jj[d],mm[d],dd[d])

    print (date)

    P.figure(figsize=(6,4))
    ax = P.subplot(111)

    m = Basemap(projection='cyl',llcrnrlat=y0,urcrnrlat=y1,llcrnrlon=x0,urcrnrlon=x1,resolution='l')
    m.drawcoastlines(color='gray',linewidth=0.5,zorder=5)

    xx,yy = m(*N.meshgrid(lon,lat))

    tmp = (dat[d,0,:,:]-N.min(dat[d,0,:,:]))/(N.max(dat[d,0,:,:])-N.min(dat[d,0,:,:]))

    m.contourf(xx,yy,tmp,levels=N.linspace(0.0,1.1,15),cmap=P.get_cmap('terrain'),extend='both')
    m.contour(xx,yy,tmp,levels=N.linspace(0.0,1.1,15),colors='k',linewidths=1.5,zorder=8)

    at = AnchoredText(date,prop=dict(size=10),frameon=True,loc='lower left')
    at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
    at.zorder = 20    
    ax.add_artist(at)

    P.tight_layout()
    P.savefig('./includes/ncep/%s.png'%date,dpi=40,transparent=False,bbox_inches='tight',pad_inches=0.0)