1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ### A. Spiga and A. Colaitis -- LMD -- 30/05/2011 |
---|
4 | |
---|
5 | def winds (namefile,\ |
---|
6 | nvert,\ |
---|
7 | proj="cyl",\ |
---|
8 | back=None,\ |
---|
9 | target=None, |
---|
10 | stride=5,\ |
---|
11 | var='HGT'): |
---|
12 | from netCDF4 import Dataset |
---|
13 | from myplot import getcoord2d,define_proj,makeplotpng,simplinterv,vectorfield,ptitle |
---|
14 | from matplotlib.pyplot import contourf, subplot, figure |
---|
15 | import numpy as np |
---|
16 | nc = Dataset(namefile) |
---|
17 | [lon2d,lat2d] = getcoord2d(nc) |
---|
18 | [wlon,wlat] = simplinterv(lon2d,lat2d) |
---|
19 | [u,v] = getwinds(nc) |
---|
20 | nt = len(u[:,0,0,0]) |
---|
21 | if var not in nc.variables: |
---|
22 | print "not found in file:",var |
---|
23 | exit() |
---|
24 | else: |
---|
25 | dimension = np.array(nc.variables[var]).ndim |
---|
26 | if dimension == 3: what_I_plot = nc.variables[var][:,:,:] |
---|
27 | elif dimension == 4: what_I_plot = nc.variables[var][:,nvert,:,:] |
---|
28 | fig = figure() |
---|
29 | fig.subplots_adjust(wspace = 0.0, hspace = 0.3) |
---|
30 | sub = 221 |
---|
31 | for i in range(0,nt-1,int(nt/4.)): |
---|
32 | subplot(sub) |
---|
33 | ptitle("Winds time"+str(i)+" level"+str(nvert)) |
---|
34 | m = define_proj(proj,wlon,wlat,back=back) |
---|
35 | x, y = m(lon2d, lat2d) |
---|
36 | contourf(x, y, what_I_plot[i,:,:], 30) |
---|
37 | vectorfield(u[i,nvert,:,:], v[i,nvert,:,:],\ |
---|
38 | x, y, stride=stride, csmooth=2,\ |
---|
39 | scale=20., factor=300., color='k') |
---|
40 | sub += 1 |
---|
41 | if not target: zeplot = namefile+".winds"+var+str(nvert) |
---|
42 | else: zeplot = target+"/winds"+var+str(nvert) |
---|
43 | makeplotpng(zeplot,pad_inches_value=0.35) |
---|
44 | |
---|
45 | def getwinds (nc,charu='U',charv='V'): |
---|
46 | import numpy as np |
---|
47 | u = nc.variables[charu] |
---|
48 | v = nc.variables[charv] |
---|
49 | if charu == 'U': u = u[:, :, :, 0:len(u[0,0,0,:])-1] |
---|
50 | if charv == 'V': v = v[:, :, 0:len(v[0,0,:,0])-1, :] |
---|
51 | #print np.array(u).shape, np.array(v).shape |
---|
52 | return u,v |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | import sys |
---|
56 | ### to be replaced by argparse |
---|
57 | from optparse import OptionParser |
---|
58 | parser = OptionParser() |
---|
59 | parser.add_option('-f', action='store', dest='namefile', type="string", default=None, help='name of WRF file [NEEDED]') |
---|
60 | parser.add_option('-l', action='store', dest='nvert', type="int", default=0, help='subscript for vertical level') |
---|
61 | parser.add_option('-p', action='store', dest='proj', type="string", default='cyl', help='projection') |
---|
62 | parser.add_option('-b', action='store', dest='back', type="string", default=None, help='background') |
---|
63 | parser.add_option('-t', action='store', dest='target', type="string", default=None, help='destination folder') |
---|
64 | parser.add_option('-s', action='store', dest='stride', type="int", default=5, help='stride vectors') |
---|
65 | parser.add_option('-v', action='store', dest='var', type="string", default='HGT', help='variable contoured') |
---|
66 | (opt,args) = parser.parse_args() |
---|
67 | if opt.namefile is None: |
---|
68 | print "I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h" |
---|
69 | exit() |
---|
70 | print "Options:", opt |
---|
71 | winds (opt.namefile,opt.nvert,proj=opt.proj,back=opt.back,target=opt.target,stride=opt.stride,var=opt.var) |
---|