2 import predefined_configs
as pconf
3 import binning_utils
as bu
10 from netCDF4
import Dataset
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
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')
30 args = ap.parse_args()
32 initDate = datetime.strptime(date,
'%Y%m%d%H')
33 fileDate= initDate.strftime(
'%Y-%m-%d_%H.%M.%S')
36 ReferenceAnalysis = str(args.referenceAnalysis)+
'.'+fileDate+
'.nc'
37 MPAS = str(args.mpasState)+
'.'+fileDate+
'.nc'
39 lats, lons = mu.readGrid(gridFile=ReferenceAnalysis)
43 for attribName
in su.fileStatAttributes:
44 statsDict[attribName] = []
45 for statName
in su.allFileStats:
46 statsDict[statName] = []
49 DiagSpaceGrp = conf.DiagSpaceConfig[dsKey][
'DiagSpaceGrp']
51 for varName
in vu.modVarNames2d+vu.modVarNames3d:
52 print(
'Working on '+varName)
53 varShort, varUnits = vu.modelVarAttributes(varName)
55 field = np.asarray(mu.varDiff(varName, ReferenceAnalysis, MPAS))
61 if nDims == 2: nLevels = dims[1]
67 binMethod = bu.identityBinMethod
69 binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
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)
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]],
95 for (binVal, minBound, maxBound)
in list(zip(
96 latitudeBins[
'values'],
97 latitudeBins[
'minBounds'],
98 latitudeBins[
'maxBounds'],
101 binnedDiagField = deepcopy(field)
103 binnedDiagField[lats < minBound] = np.NaN
104 binnedDiagField[lats > maxBound] = np.NaN
106 binnedDiagField[lats < minBound,:] = np.NaN
107 binnedDiagField[lats > maxBound,:] = np.NaN
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)
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'],
134 binnedDiagField = deepcopy(field)
136 binnedDiagField[lons < minLon] = np.NaN
137 binnedDiagField[lons > maxLon] = np.NaN
138 binnedDiagField[lats < minLat] = np.NaN
139 binnedDiagField[lats > maxLat] = np.NaN
141 binnedDiagField[lons < minLon,:] = np.NaN
142 binnedDiagField[lons > maxLon,:] = np.NaN
143 binnedDiagField[lats < minLat,:] = np.NaN
144 binnedDiagField[lats > maxLat,:] = np.NaN
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)
161 binVar = vu.modVarLev
162 binVarShort, binVarUnits = vu.modelVarAttributes(binVar)
165 binMethod = bu.identityBinMethod
166 binnedDiagField = deepcopy(field)
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
183 latitudeBins = pconf.namedLatBands
184 for (latBand, minBound, maxBound)
in list(zip(
185 latitudeBins[
'values'],
186 latitudeBins[
'minBounds'],
187 latitudeBins[
'maxBounds'],
192 binnedDiagField = deepcopy(field)
193 binnedDiagField[lats < minBound,:] = np.NaN
194 binnedDiagField[lats > maxBound,:] = np.NaN
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
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'],
219 binMethod = geoirInst
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
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
241 su.write_stats_nc(dsKey, statsDict)
246 if __name__ ==
'__main__':
main()