MPAS-JEDI
writediagstats_modelspace.py
Go to the documentation of this file.
1 import argparse
2 import predefined_configs as pconf
3 import binning_utils as bu
4 import config as conf
5 import datetime as dt
6 import os
7 import sys
8 import numpy
9 import numpy as np
10 from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/
11 import matplotlib.cm as cm
12 import matplotlib.pyplot as plt
13 from mpl_toolkits.basemap import Basemap
14 from copy import deepcopy
15 from datetime import datetime, timedelta
16 import modelsp_utils as mu
17 import stat_utils as su
18 import var_utils as vu
19 
21  # Parse command line for date
22  ap = argparse.ArgumentParser()
23  ap.add_argument('date', default=os.getcwd().split('/')[-3], type=str, nargs = '?',
24  help='Valid date (%Y%m%d%H)')
25  ap.add_argument('-r', '--referenceAnalysis', default = mu.GFSANA_DIR+'/x1.40962.init', type = str,
26  help='Path/prefix of reference analysis state')
27  ap.add_argument('-m', '--mpasState', default = '../restart', type = str,
28  help='Path/prefix of arbitrary MPAS state')
29 
30  args = ap.parse_args()
31  date = str(args.date)
32  initDate = datetime.strptime(date,'%Y%m%d%H')
33  fileDate= initDate.strftime('%Y-%m-%d_%H.%M.%S')
34 
35  #TODO: allow for entire ReferenceAnalysis and MPAS as args instead of date
36  ReferenceAnalysis = str(args.referenceAnalysis)+'.'+fileDate+'.nc'
37  MPAS = str(args.mpasState)+'.'+fileDate+'.nc'
38 
39  lats, lons = mu.readGrid(gridFile=ReferenceAnalysis)
40 
41  # Initialize a dictionary to contain all statistical info for this osKey
42  statsDict = {}
43  for attribName in su.fileStatAttributes:
44  statsDict[attribName] = []
45  for statName in su.allFileStats:
46  statsDict[statName] = []
47 
48  dsKey = 'mpas'
49  DiagSpaceGrp = conf.DiagSpaceConfig[dsKey]['DiagSpaceGrp']
50 
51  for varName in vu.modVarNames2d+vu.modVarNames3d:
52  print('Working on '+varName)
53  varShort, varUnits = vu.modelVarAttributes(varName)
54 
55  field = np.asarray(mu.varDiff(varName, ReferenceAnalysis, MPAS))
56  diagName = 'mmgfsan'
57 
58  dims = field.shape
59  nLevels = 1
60  nDims = len(dims)
61  if nDims == 2: nLevels = dims[1]
62 
63 # TODO(JJG): generalize binning and diagnostics similar to DiagnoseObsStatistics
64 # TODO(JJG): extend to ACC diagnostic, 500mb geopotential height variable
65 
66  # no binVar/binMethod (all data)
67  binMethod = bu.identityBinMethod
68  binVar = vu.noBinVar
69  binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
70  binVal = vu.noBinVar
71  binnedDiag = deepcopy(field).flatten()
72  statsVal = su.calcStats(binnedDiag)
73  for statName in su.allFileStats:
74  statsDict[statName].append(statsVal[statName])
75  statsDict['DiagSpaceGrp'].append(DiagSpaceGrp)
76  statsDict['varName'].append(varShort)
77  statsDict['varUnits'].append(varUnits)
78  statsDict['diagName'].append(diagName)
79  statsDict['binMethod'].append(binMethod)
80  statsDict['binVar'].append(binVarShort)
81  statsDict['binUnits'].append(binVarUnits)
82  statsDict['binVal'].append(binVal)
83 
84  # binVar == vu.modVarLat (all levels)
85  ## binMethods:
86  ## (1) named latitude bands (str)
87  ## (2) equidistant latitude ranges (float)
88  binVar = vu.modVarLat
89  binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
90  for (binMethod, latitudeBins) in list(zip(
91  [bu.latbandsMethod, bu.identityBinMethod],
92  [pconf.namedLatBands, pconf.binLims[vu.modVarLat]],
93  )):
94 
95  for (binVal, minBound, maxBound) in list(zip(
96  latitudeBins['values'],
97  latitudeBins['minBounds'],
98  latitudeBins['maxBounds'],
99  )):
100 
101  binnedDiagField = deepcopy(field)
102  if nDims == 1:
103  binnedDiagField[lats < minBound] = np.NaN
104  binnedDiagField[lats > maxBound] = np.NaN
105  elif nDims == 2:
106  binnedDiagField[lats < minBound,:] = np.NaN
107  binnedDiagField[lats > maxBound,:] = np.NaN
108 
109  binnedDiag = binnedDiagField.flatten()
110  statsVal = su.calcStats(binnedDiag)
111  for statName in su.allFileStats:
112  statsDict[statName].append(statsVal[statName])
113  statsDict['DiagSpaceGrp'].append(DiagSpaceGrp)
114  statsDict['varName'].append(varShort)
115  statsDict['varUnits'].append(varUnits)
116  statsDict['diagName'].append(diagName)
117  statsDict['binMethod'].append(binMethod)
118  statsDict['binVar'].append(binVarShort)
119  statsDict['binUnits'].append(binVarUnits)
120  statsDict['binVal'].append(binVal)
121 
122  # GEO IR instrument lat/lon box binMethod, Region binVar
123  binMethod = bu.geoirlatlonboxMethod
124  binVar = vu.modelRegionBinVar
125  binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
126  for (binVal, minLon, maxLon, minLat, maxLat) in list(zip(
127  pconf.geoirLonBands['values'],
128  pconf.geoirLonBands['minBounds'],
129  pconf.geoirLonBands['maxBounds'],
130  pconf.geoirLatBands['minBounds'],
131  pconf.geoirLatBands['maxBounds'],
132  )):
133 
134  binnedDiagField = deepcopy(field)
135  if nDims == 1:
136  binnedDiagField[lons < minLon] = np.NaN
137  binnedDiagField[lons > maxLon] = np.NaN
138  binnedDiagField[lats < minLat] = np.NaN
139  binnedDiagField[lats > maxLat] = np.NaN
140  elif nDims == 2:
141  binnedDiagField[lons < minLon,:] = np.NaN
142  binnedDiagField[lons > maxLon,:] = np.NaN
143  binnedDiagField[lats < minLat,:] = np.NaN
144  binnedDiagField[lats > maxLat,:] = np.NaN
145 
146  binnedDiag = binnedDiagField.flatten()
147  statsVal = su.calcStats(binnedDiag)
148  for statName in su.allFileStats:
149  statsDict[statName].append(statsVal[statName])
150  statsDict['DiagSpaceGrp'].append(DiagSpaceGrp)
151  statsDict['varName'].append(varShort)
152  statsDict['varUnits'].append(varUnits)
153  statsDict['diagName'].append(diagName)
154  statsDict['binMethod'].append(binMethod)
155  statsDict['binVar'].append(binVarShort)
156  statsDict['binUnits'].append(binVarUnits)
157  statsDict['binVal'].append(binVal)
158 
159  # binVar == vu.modVarLev
160  if nDims > 1:
161  binVar = vu.modVarLev
162  binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
163 
164  ## identity binMethod
165  binMethod = bu.identityBinMethod
166  binnedDiagField = deepcopy(field)
167 
168  for ilev in list(range(0,nLevels)):
169  binnedDiag = binnedDiagField[:,ilev]
170  statsVal = su.calcStats(binnedDiag)
171  for statName in su.allFileStats:
172  statsDict[statName].append(statsVal[statName])
173  statsDict['binVal'].append(str(ilev))
174  statsDict['DiagSpaceGrp'] += [DiagSpaceGrp]*nLevels
175  statsDict['varName'] += [varShort]*nLevels
176  statsDict['varUnits'] += [varUnits]*nLevels
177  statsDict['diagName'] += [diagName]*nLevels
178  statsDict['binMethod'] += [binMethod]*nLevels
179  statsDict['binVar'] += [binVarShort]*nLevels
180  statsDict['binUnits'] += [binVarUnits]*nLevels
181 
182  ## named latitude band binMethods
183  latitudeBins = pconf.namedLatBands
184  for (latBand, minBound, maxBound) in list(zip(
185  latitudeBins['values'],
186  latitudeBins['minBounds'],
187  latitudeBins['maxBounds'],
188  )):
189 
190  binMethod = latBand
191 
192  binnedDiagField = deepcopy(field)
193  binnedDiagField[lats < minBound,:] = np.NaN
194  binnedDiagField[lats > maxBound,:] = np.NaN
195 
196  for ilev in list(range(0,nLevels)):
197  binnedDiag = binnedDiagField[:,ilev]
198  statsVal = su.calcStats(binnedDiag)
199  for statName in su.allFileStats:
200  statsDict[statName].append(statsVal[statName])
201  statsDict['binVal'].append(str(ilev))
202  statsDict['DiagSpaceGrp'] += [DiagSpaceGrp]*nLevels
203  statsDict['varName'] += [varShort]*nLevels
204  statsDict['varUnits'] += [varUnits]*nLevels
205  statsDict['diagName'] += [diagName]*nLevels
206  statsDict['binMethod'] += [binMethod]*nLevels
207  statsDict['binVar'] += [binVarShort]*nLevels
208  statsDict['binUnits'] += [binVarUnits]*nLevels
209 
210  ## GEO IR instrument lat/lon box binMethod
211  for (geoirInst, minLon, maxLon, minLat, maxLat) in list(zip(
212  pconf.geoirLonBands['values'],
213  pconf.geoirLonBands['minBounds'],
214  pconf.geoirLonBands['maxBounds'],
215  pconf.geoirLatBands['minBounds'],
216  pconf.geoirLatBands['maxBounds'],
217  )):
218 
219  binMethod = geoirInst
220 
221  binnedDiagField = deepcopy(field)
222  binnedDiagField[lons < minLon,:] = np.NaN
223  binnedDiagField[lons > maxLon,:] = np.NaN
224  binnedDiagField[lats < minLat,:] = np.NaN
225  binnedDiagField[lats > maxLat,:] = np.NaN
226 
227  for ilev in list(range(0,nLevels)):
228  binnedDiag = binnedDiagField[:,ilev]
229  statsVal = su.calcStats(binnedDiag)
230  for statName in su.allFileStats:
231  statsDict[statName].append(statsVal[statName])
232  statsDict['binVal'].append(str(ilev))
233  statsDict['DiagSpaceGrp'] += [DiagSpaceGrp]*nLevels
234  statsDict['varName'] += [varShort]*nLevels
235  statsDict['varUnits'] += [varUnits]*nLevels
236  statsDict['diagName'] += [diagName]*nLevels
237  statsDict['binMethod'] += [binMethod]*nLevels
238  statsDict['binVar'] += [binVarShort]*nLevels
239  statsDict['binUnits'] += [binVarUnits]*nLevels
240 
241  su.write_stats_nc(dsKey, statsDict)
242 
243 def main():
245 
246 if __name__ == '__main__': main()