GMTB Workflow Documentation
get_spd_field.py
Go to the documentation of this file.
1 import pygrib
2 import os, math
3 import numpy as np
4 import matplotlib.pyplot as plt
5 from mpl_toolkits.basemap import Basemap
6 
7 ##\ingroup graphics
8 def get_spd_field(fn,lvl,typeOfLevel,outdir,outmap,fhr,var):
9  print "GET ARGS: ", locals()
10  tmp = locals()
11  alist = {}
12  for i in tmp:
13  if tmp[i] is not None and i != 'fn':
14  print i , tmp[i]
15  if i == 'level':
16  alist[i] = int(tmp[i])
17  else:
18  alist[i] = tmp[i]
19  print "ALIST : ", alist
20  grbs=pygrib.open(fn)
21  plt.figure(figsize=(15,12))
22  lev = int(lvl)
23  if var == 'wind':
24  grbu=grbs.select(typeOfLevel='isobaricInhPa', shortName='u', level=lev)[0]
25  grbv=grbs.select(typeOfLevel='isobaricInhPa', shortName='v', level=lev)[0]
26  clevs=np.arange(30,120,5)
27  elif var == '10wind':
28  grbu=grbs.select(typeOfLevel='heightAboveGround', shortName='10u', level=lev)[0]
29  grbv=grbs.select(typeOfLevel='heightAboveGround', shortName='10v', level=lev)[0]
30  clevs=np.arange(5,35,1)
31 
32  datau=grbu.values
33  datav=grbv.values
34  atime=grbu.analDate.strftime('Analysis: %Y%m%d %H UTC')
35  altime=grbu.analDate.strftime('%Y%m%d%H')
36  vtime=grbu.validDate.strftime('Valid: %Y%m%d %H UTC')
37  date=os.getenv('START_TIME') # @Y@m@d@H
38  grid = outmap
39 
40  lat,lon = grbu.latlons()
41  data=np.sqrt(datau**2+datav**2)
42 
43  if grid == 'glob' or grid == 'G3':
44  m =Basemap(projection='cyl',lat_ts=10,llcrnrlon=lon.min(), \
45  urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), \
46  resolution='c')
47  elif grid == 'nh_mill':
48  m =Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), \
49  urcrnrlon=lon.max(),llcrnrlat=0,urcrnrlat=90, \
50  resolution='c')
51  elif grid == 'sh_mill':
52  m =Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), \
53  urcrnrlon=lon.max(),llcrnrlat=-90,urcrnrlat=0, \
54  resolution='c')
55  elif grid == 'trop_mill':
56  m =Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), \
57  urcrnrlon=lon.max(),llcrnrlat=-23,urcrnrlat=23, \
58  resolution='c')
59  elif grid == 'nps':
60  m =Basemap(projection='npstere',boundinglat=-0.268, \
61  lon_0=-105,resolution='c')
62  elif grid == 'sps':
63  m =Basemap(projection='npstere',boundinglat=-60, \
64  lon_0=105,resolution='c')
65  elif grid == 'G218':
66  m=Basemap(projection='lcc',lat_ts=10,llcrnrlon=226.514, \
67  urcrnrlon=-49.420,llcrnrlat=12.190,urcrnrlat=57.328, \
68  resolution='c',lat_1=25,lat_2=25,lon_0=265,rsphere=6371200)
69  elif grid == 'G104':
70  lon_w,lon_e,lat_s,lat_n =[-147, -64, 10.8, 89.9]
71  lon_0 = lon_w + (lon_e - lon_w) / 2.
72  ref = lat_s if abs(lat_s) > abs(lat_n) else lat_n
73  lat_0 = math.copysign(90., ref)
74  proj = 'npstere' if lat_0 > 0 else 'spstere'
75  prj = Basemap(projection=proj, lon_0=lon_0, lat_0=lat_0,
76  boundinglat=0, resolution='c')
77  lons = [lon_w, lon_e, lon_w, lon_e, lon_0, lon_0]
78  lats = [lat_s, lat_s, lat_n, lat_n, lat_s, lat_n]
79  x, y = prj(lons, lats)
80  ll_lon, ll_lat = prj(min(x), min(y), inverse=True)
81  ur_lon, ur_lat = prj(max(x), max(y), inverse=True)
82  m=Basemap(projection='stere', lat_0=lat_0, lon_0=lon_0,
83  llcrnrlon=ll_lon, llcrnrlat=ll_lat,
84  urcrnrlon=ur_lon, urcrnrlat=ur_lat, resolution='c')
85 
86  x, y = m(lon,lat)
87 
88  # Draw political boundaries
89  m.drawcoastlines()
90  m.drawstates()
91  m.drawcountries()
92  m.drawmapboundary()
93 # m.drawlsmask(ocean_color='0.8',land_color='white')
94  m.drawlsmask(ocean_color='white',land_color='white')
95 
96  # Draw lat/lon grid
97  parallels = np.arange(-90.,90,10.)
98  m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
99  meridians = np.arange(0.,360.,20.)
100  m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)
101 
102 # clevs=np.arange(30,100,5)
103  cs = m.contourf(x,y,data,clevs,cmap=plt.cm.rainbow)
104 
105  # Determine how to label the vertical coordinate based on the contents of the grib file, or other
106  # information.'''
107  if typeOfLevel == 'isobaricInhPa':
108  vert_unit = 'hPa'
109  elif typeOfLevel == 'hybrid':
110  vert_unit = 'sigma'
111  elif typeOfLevel == 'heightAboveGround':
112  vert_unit = 'm'
113  else:
114  vert_unit ='m'
115 
116  # Plot the wind field in barbs. Masks are applied here to
117  # thin the quantity of barbs plotted for calrity.
118  maskarray = np.ones(datau.shape)
119  if grid == 'G3':
120  maskarray[1::7,1::14] = 0
121  elif grid == 'G104':
122  maskarray[1::3,1::6] = 0
123  else:
124  maskarray[1::20,1::40] = 0
125 
126  mu = np.ma.masked_array(datau, mask=maskarray)
127  mv = np.ma.masked_array(datav, mask=maskarray)
128 
129  barb_control = dict(height=0.6, width=0.3, emptybarb=0.07)
130  m.barbs(x, y, mu, mv, barbcolor='k',pivot='middle',sizes=barb_control, linewidth=0.4, length=5)
131 
132 
133 
134  #Add a colorbar and title, and then show the plot.
135  plt.colorbar(cs,orientation='horizontal',shrink=0.8)
136  plt.title('%s \nFcst Hr: %s' % (atime, fhr) , loc='left')
137 
138  plt.title('wind magnitude (m s**-1,shaded)', loc = 'right')
139  plt.title('%s %s' % (lvl,vert_unit))
140  plt.xlabel('%s' % (vtime), fontsize=18, labelpad=40)
141 
142  # Output file name definition
143  filename=''.join(['wind','%s' % (lvl),vert_unit,'_',grid,'_',altime,'_f',fhr,'.png'])
144 
145 
146  # Check to see if output location exists, if not, make it.
147  if not os.path.exists(outdir):
148  os.makedirs(outdir)
149 
150  plt.savefig('/'.join([outdir, filename]))
151  plt.close()
152 
153 
154 
155 
def get_spd_field(fn, lvl, typeOfLevel, outdir, outmap, fhr, var)
Definition: get_spd_field.py:8