GMTB Workflow Documentation
REG_graphics.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 
5 import matplotlib as mpl
6 mpl.use('Agg')
7 import pygrib
8 from mpl_toolkits.basemap import Basemap
9 import numpy as np
10 import matplotlib.pyplot as plt
11 import datetime
12 import os,sys
13 import maps, regrib
14 import get_grib_field as ggf
15 
16 def set_def_maps(preset_flag):
17  '''Defines the standard set of figures produced by the graphics package.
18  Top key is user's choice of a short descriptor for the figure generated.
19  Subdictionary for each individual figure with following params:
20  lev: 'lev' for desired variable in grib file
21  sname: 'shortName' value of desired variable from grib file
22  pname: 'parameterName' value of desired variable from grib file. Used when shortName is unavailable
23  step: 'stepType' value of desired variable from grib file. e.g., avg or instant
24  barbs: flag for plotting winds over variable
25  hgt: flag for plotting height over variable
26  vc: 'typeOfLevel' value in grib to set vertical coordinate
27 
28  Both lev and vc are required to determine the appropriate level of the given variable.
29  sname and pname are alternative methods to define the variable name. sname is preferred.
30  If pname is used, the title and file names will reflect the top-level dictionary entry. A corresponding
31  variable entry to match the figure describer must be made in the maps.py dictionary of variables.
32 
33  '''
34  if preset_flag == 'testbed':
35  map_confs = {
36  '250_wind': {'lev': '250', 'sname': 'wind', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
37  '250_temp': {'lev': '250', 'sname': 't', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
38  '500_hgt': {'lev': '500', 'sname': 'gh', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
39  '500_temp': {'lev': '500', 'sname': 't', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
40  '500_vort': {'lev': '500', 'sname': 'absv', 'barbs': False, 'hgt': True, 'vc': 'isobaricInhPa'},
41  '700_temp': {'lev': '700', 'sname': 't', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
42  '700_vvel': {'lev': '700', 'sname': 'w', 'barbs': False, 'hgt': True, 'vc': 'isobaricInhPa'},
43  '850_hgt': {'lev': '850', 'sname': 'gh', 'barbs': True, 'hgt': False, 'vc': 'isobaricInhPa'},
44  '850_temp': {'lev': '850', 'sname': 't', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
45  '850_rh': {'lev': '850', 'sname': 'r', 'barbs': True, 'hgt': True, 'vc': 'isobaricInhPa'},
46  '2m_tmp': {'lev': '2', 'sname': '2t', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
47  '2m_dpt': {'lev': '2', 'sname': '2d', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
48  '6h_acpcp': {'lev': '0', 'sname': 'acpcp', 'barbs': False, 'hgt': False, 'vc': 'surface'},
49  '6h_apcp': {'lev': '0', 'sname': 'tp', 'barbs': False, 'hgt': False, 'vc': 'surface'}
50  }
51  elif preset_flag == 'reg_test_flx':
52  # Winds and height aren't included in flx file, so turn all to False.
53  map_confs = {
54  '2m_tmp': {'lev': '2', 'sname': '2t', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
55  '2m_spfh': {'lev': '2', 'sname': 'q', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
56  'pwat': {'lev': '0', 'sname': 'pwat', 'barbs': False, 'hgt': False, 'vc': 'entireAtmosphere'},
57  'prate': {'lev': '0', 'sname': 'prate', 'barbs': False, 'hgt': False, 'vc': 'surface'},
58  '10m_u': {'lev': '10', 'sname': '10u', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
59  '10m_v': {'lev': '10', 'sname': '10v', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
60  }
61  elif preset_flag == 'reg_test_unknown':
62  map_confs = {
63  'ULWRF': {'lev': '0', 'pname': '212', 'barbs': False, 'hgt': False, 'vc': 'surface', 'step': 'avg'},
64  '2m_tmp': {'lev': '2', 'sname': '2t', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
65  '2m_spfh': {'lev': '2', 'sname': 'q', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
66  'pwat': {'lev': '0', 'sname': 'pwat', 'barbs': False, 'hgt': False, 'vc': 'entireAtmosphere'},
67  'prate': {'lev': '0', 'sname': 'prate', 'barbs': False, 'hgt': False, 'vc': 'surface'},
68  'CPRAT': {'lev': '0', 'pname': '214', 'barbs': False, 'hgt': False, 'vc': 'surface', 'step': 'avg'},
69  '10m_u': {'lev': '10', 'sname': '10u', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
70  '10m_v': {'lev': '10', 'sname': '10v', 'barbs': False, 'hgt': False, 'vc': 'heightAboveGround'},
71  }
72  return map_confs
73 
74 
75 def main():
76  # Set the input and output directories in config_REG
77  indir=os.getenv('IDIR')
78  outdir=os.getenv('ODIR')
79 
80  # Loop over all fhrs: range(start, stop, interval)
81  for fhr in range(24,48,120):
82  # Set the file name with the appropriate prefix: sigf or flxf
83  fnbase=os.getenv('FBASE')
84  filename=''.join([fnbase,'{:02d}'.format(fhr)])
85  #filename=''.join(['flxf','{:02d}'.format(fhr)])
86  # Change preset_flag based on which types of files you want to plot
87  # Options:
88  # reg_test_flx - flux files
89  # reg_test_sig - sig files
90  preset_flag='reg_test_unknown'
91  grib='/'.join([indir, filename])
92  print grib
93  # Set user_def = True if you want to plot multiple levels of a given variable
94  # Suggest "True" for sigf files and "False" for flxf files.
95  user_def = False
96  date='2012010100'
97  plot_wind=True
98  print grib, outdir, fhr
99 
100  # Set forecast hour
101  sname=['wind', 'q', 't']
102  # Sets up the plotting for several different levels: range(min, max, int)
103  plevels=range(1,64,10)
104  coord='hybrid'
105  if user_def:
106  for var in sname:
107  for level in plevels:
108 
109  if plot_wind or var == 'wind':
110  u = ggf.get_ua_field(grib,level,coord,shortName='u')
111  v = ggf.get_ua_field(grib,level,coord,shortName='v')
112  winds=[u,v]
113  else:
114  winds=None
115 
116  # Get the variable from file by passing key,value pairs from the grib file. If you do not want to
117  # use a particular field to search for the grib record, "None" is also acceptable.
118  if var != 'wind':
119  field=ggf.get_ua_field(grib,level,coord,shortname=var)
120  else:
121  field=u
122 
123 
124  mymap=maps.make_basemap(field,date,var,level,
125  winds=winds, fhr=fhr)
126  mymap.set_bm()
127  mymap.run()
128  mymap.save_figure(outdir)
129  else:
130  def_maps=set_def_maps(preset_flag)
131  for fig in def_maps:
132  # If statement here is to plot a single figure from the full list of presets
133  #if fig == '10m_u':
134  try:
135  level = def_maps[fig].get('lev')
136 
137  sname = def_maps[fig].get('sname', None)
138  pname = def_maps[fig].get('pname', None)
139  step = def_maps[fig].get('step',None)
140  hgt = def_maps[fig].get('hgt')
141  plot_wind = def_maps[fig].get('barbs',False)
142  coord = def_maps[fig].get('vc')
143 
144 
145  # var controls the figure title and file output name. Use a descriptive one
146  # by choosing either the shortName from grib, or the label in the dictionary (fig)
147  if sname is not None:
148  var = sname
149  else:
150  var = fig
151  print level,var, hgt,plot_wind, hgt
152 
153 
154  # Grab winds from file if plotting the barbs
155  if plot_wind or var == 'wind':
156  u = ggf.get_ua_field(grib,level,coord,shortName='u')
157  v = ggf.get_ua_field(grib,level,coord,shortName='v')
158  winds=[u,v]
159  else:
160  winds=None
161 
162  # Get the variable from file by passing key,value pairs from the grib file. If you do not want to
163  # use a particular field to search for the grib record, "None" is also acceptable.
164  if var != 'wind':
165  field=ggf.get_ua_field(grib,level,coord,shortName=sname,parameterName=pname,stepType=step)
166  else:
167  field=u
168 
169  height = None
170  if hgt:
171  height = ggf.get_ua_field(grib,'gh',level,coord)
172  print "Made it here"
173 
174  mymap=maps.make_basemap(field,date,var,level,
175  winds=winds, plot_height=height,def_maps=def_maps,
176  fhr=fhr)
177  mymap.set_bm()
178  mymap.run()
179  mymap.save_figure(outdir)
180  except(ValueError):
181  print 'Skipping figure'
182  pass
183 
184 main()
def set_def_maps(preset_flag)
Definition: REG_graphics.py:16