GMTB Workflow Documentation
GMTB_graphics.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ##\file GMTB_graphics.py
3 
4 import matplotlib as mpl
5 mpl.use('Agg')
6 import pygrib
7 from mpl_toolkits.basemap import Basemap
8 import numpy as np
9 import matplotlib.pyplot as plt
10 import datetime
11 import os,sys,math
12 import maps, regrib
13 import get_grib_field as ggf
14 import get_spd_field as gsf
15 
16 ##\ingroup graphics
17 # This function sets up a variable table to plot (map_confs) using key/value pairs.
18 #\param lev The value of \e level
19 #\param pvar The value of \e shortName
20 #\param barbs Flag for overlaying wind barbs
21 #\param hgt Flag for overlaying height contour
22 #\param vc The value for \e typeOfLevel
23 #\param acc_field Flag for if \e stepTypeInternal is accum
25  map_confs = {
26  '200_wind': {'lev': '200', 'pvar': 'wind', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
27  '200_temp': {'lev': '200', 'pvar': 't', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
28  '500_hgt': {'lev': '500', 'pvar': 'gh', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
29  '500_temp': {'lev': '500', 'pvar': 't', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
30  '500_vort': {'lev': '500', 'pvar': 'absv', 'barbs': False, 'hgt': False, 'vc': 'isobaricInhPa'},
31  '700_temp': {'lev': '700', 'pvar': 't', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
32  '700_vvel': {'lev': '700', 'pvar': 'w', 'barbs': False, 'hgt': False, 'vc': 'isobaricInhPa'},
33  '850_hgt': {'lev': '850', 'pvar': 'gh', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
34  '850_temp': {'lev': '850', 'pvar': 't', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
35  '850_rh': {'lev': '850', 'pvar': 'r', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
36  '2m_tmp': {'lev': '2', 'pvar': '2t', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
37  '2m_dpt': {'lev': '2', 'pvar': '2d', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
38  '10m_u': {'lev': '10', 'pvar': '10u', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
39  '10m_v': {'lev': '10', 'pvar': '10v', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
40  '10m_wind': {'lev': '10', 'pvar':'10wind', 'barbs': True, 'hgt': False, 'vc': 'heightAboveGround'},
41  'prmsl': {'lev': '0', 'pvar': 'prmsl', 'pline': True, 'barbs': True, 'hgt': False, 'vc': 'meanSea'},
42  'pwat': {'lev': '0', 'pvar': 'pwat', 'pline': False, 'barbs': False, 'hgt': False, 'vc': 'unknown'},
43  'prate': {'lev': '0', 'pvar': 'prate','pline': False, 'barbs': False, 'hgt': False, 'vc': 'surface', 'acc_field': True},
44  'cprat': {'lev': '0', 'pvar': 'cprat','pline': False, 'barbs': False, 'hgt': False, 'vc': 'surface', 'acc_field': True},
45  '6h_acpcp': {'lev': '0', 'pvar': 'acpcp', 'barbs': False, 'hgt': False, 'vc': 'surface', 'acc_field': True},
46  '6h_apcp': {'lev': '0', 'pvar': 'tp', 'barbs': False, 'hgt': False, 'vc': 'surface', 'acc_field': True}
47  }
48 
49  return map_confs
50 
51 
52 ##\defgroup graphics graphics
53 def main():
54 
55  ## Get the cycle information from env vars
56  moad_dataroot=os.getenv('MOAD_DATAROOT')
57  rotdir=os.getenv('ROTDIR')
58  date=os.getenv('START_TIME') # @Y@m@d@H
59  fhr=os.getenv('FCST_TIME') # 3 digit fcst hour
60  grid_list=os.getenv('GRID_VX_LIST')
61  vx_grid=grid_list.split(" ")
62  filename=''.join(['pgrbq', fhr, '.gfs.', date, '.grib2'])
63  gribin='/'.join([rotdir, filename])
64  user_def = os.getenv('USER_MAPS',False)
65 
66  ## Set the output file location (template set in maps.py).
67  outdir='/'.join([moad_dataroot, 'figprd'])
68 
69 
70  # Change projection if needed.
71  for grid in vx_grid:
72  outmap = grid
73  print grid
74 
75  # Wind rotation is a user choice, not necessarily a predefined choice, so is included here.
76  wind_rot = {'G104': 'grid', 'G218': 'grid', 'G3': 'grid'}
77 
78  # Assumes that NCEP pre-defined grids will be defined with a leading "G"
79  if grid[0] == 'G':
80  newgrib = ''.join(['pgrbq', fhr, '.gfs.', date,'_', grid, '.grib2'])
81  newgrib = '/'.join([outdir,newgrib])
82  if not os.path.exists(outdir):
83  os.makedirs(outdir)
84  vx = regrib.new_grid(grid,gribin,newgrib,wind_rot.get(grid,None))
85  vx.regrid()
86  grib = newgrib
87  else:
88  grib = gribin
89 
90  plot_wind=True
91 #z print gribin, outdir
92 
93 
94  # Set forecast hour
95  pvars=['gh', 't']
96  plevels=[ 850, 700, 500, 200 ]
97  coord = [ "isobaricInhPa", "isobaricInhPa", "isobaricInhPa", "isobaricInhPa" ]
98  # Variables include: t, r, u, v, w, gh, absv, clwmr,soilw, 2t, 2d, pwat,
99  # cwat,press,q,pt,tp, acpcp
100  if user_def:
101  for var in pvars:
102  for level in plevels:
103 
104  if plot_wind and var == 'wind':
105  u = ggf.get_ua_field(grib,level,coord,shortName='u')
106  v = ggf.get_ua_field(grib,level,coord,shortName='v')
107  winds=[u,v]
108  elif plot_wind and var == '10wind':
109  u = ggf.get_ua_field(grib,level,coord,shortName='10u')
110  v = ggf.get_ua_field(grib,level,coord,shortName='10v')
111  winds=[u,v]
112  else:
113  winds=None
114 
115  # Get the variable from file by passing key,value pairs from the grib file. If you do not want to
116  # use a particular field to search for the grib record, "None" is also acceptable.
117  if var != 'wind':
118  field=ggf.get_ua_field(grib,level,coord,shortname=var)
119  else:
120  field=u
121 
122 
123  mymap=maps.make_basemap(field,date,var,level,
124  winds=winds, fhr=fhr)
125  mymap.set_bm()
126  mymap.run()
127  mymap.save_figure(outdir)
128  else:
129  def_maps=set_def_maps()
130  for fig in def_maps:
131  # Optional if statement here can control evaluating one type of figure at a time.
132  #if fig == '6h_apcp' or fig == '6h_acpcp':
133  acc_field=def_maps[fig].get('acc_field',False)
134  if (acc_field and fhr != '000') or (not acc_field):
135  level = def_maps[fig].get('lev')
136  var = def_maps[fig].get('pvar')
137  hgt = def_maps[fig].get('hgt')
138  plot_wind = def_maps[fig].get('barbs',False)
139  plot_line = def_maps[fig].get('pline',False)
140  coord = def_maps[fig].get('vc')
141 #z print level,var, hgt,plot_wind, hgt
142 
143  # Grab winds from file if plotting the barbs
144  if plot_wind and var == 'wind':
145  u = ggf.get_ua_field(grib,level,coord,shortName='u')
146  v = ggf.get_ua_field(grib,level,coord,shortName='v')
147  winds=[u,v]
148  elif plot_wind and var == '10wind':
149  u = ggf.get_ua_field(grib,level,coord,shortName='10u')
150  v = ggf.get_ua_field(grib,level,coord,shortName='10v')
151  winds=[u,v]
152  else:
153  winds=None
154 
155  height = None
156  if hgt:
157  height = ggf.get_ua_field(grib,level,coord,shortName='gh')
158 
159  # Get the variable from file
160  if var != 'wind' and var != '10wind':
161  field=ggf.get_ua_field(grib,level,coord,shortName=var)
162  mymap=maps.make_basemap(field,date,var,level,area_flag=outmap, line=plot_line,
163  winds=winds, plot_height=height,def_maps=def_maps)
164  mymap.set_bm()
165  mymap.run()
166  mymap.save_figure(outdir)
167 
168  else:
169  # Calculate and plot wind speed on level
170  spd=gsf.get_spd_field(grib,level,coord,outdir,outmap,fhr,var)
171 
172 
173 main()
def set_def_maps()
This function sets up a variable table to plot (map_confs) using key/value pairs. ...