MPAS-JEDI
writediag_modelspace_aggr.py
Go to the documentation of this file.
1 import datetime as dt
2 import glob
3 import os
4 import sys
5 import numpy
6 import numpy as np
7 from netCDF4 import Dataset
8 import matplotlib.cm as cm
9 import matplotlib.pyplot as plt
10 from mpl_toolkits.basemap import Basemap
11 from copy import deepcopy
12 from datetime import datetime, timedelta
13 import modelsp_utils as mu
14 
16  if os.path.exists(mu.expStats+'.nc'):
17  os.remove(mu.expStats+'.nc')
18  path = os.getcwd()
19  fcdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
20  allfiledate = []
21  lats, lons = mu.readGrid()
22  for varName in mu.varNames:
23  for latBand in range(0, len(mu.latBands)):
24  for fcTDelta in np.arange(0,mu.fcRange+mu.interval,mu.interval):
25  alltmp = [[]]
26  tmp = []
27  for fcdir in range(0,len(fcdirs)):
28  d = datetime.strptime(fcdirs[fcdir], "%Y%m%d%H") + timedelta(days=fcTDelta)
29  fileDate= d.strftime("%Y-%m-%d_%H.%M.%S")
30 
31  ncFile1 = mu.GFSANA_DIR+'/x1.'+str(mu.ncells)+'.init.'+fileDate+'.nc'
32  ncFile2 = './'+fcdirs[fcdir]+'/restart.'+fileDate+'.nc'
33  tmp = mu.varDiff(varName,ncFile1,ncFile2)
34 
35  tmpbin = deepcopy(tmp)
36  tmpbin[np.logical_or(lats < mu.latBandsBounds [latBand+1], lats > mu.latBandsBounds [latBand])] = np.NaN
37 
38  if (fcdir == 0):
39  alltmp = np.append(alltmp, tmpbin)
40  if varName in mu.varNames3d:
41  if (varName == 'w'):
42  alltmp = alltmp.reshape(mu.ncells,mu.nlevelsP1)
43  else:
44  alltmp = alltmp.reshape(mu.ncells,mu.nlevels)
45  else:
46  alltmp = alltmp.reshape(mu.ncells,mu.nlevelSurface)
47 
48  if (fcdir != 0):
49  if varName in mu.varNames3d:
50  alltmp = np.append(alltmp, tmpbin, axis=0)
51  else:
52  alltmp = np.append(alltmp, tmpbin)
53  if (fcdir == len(fcdirs)-1):
54  newfile = write_stats(alltmp,varName,mu.latBands[latBand],str(fcTDelta))
55 #TODO: move the following functions to plot_utils.py or basic_plot_functions.py
56 def write_stats(array_f,varNames,band,fcTDelta):
57  STATS = {}
58  STATS['Mean'] = np.nanmean(array_f,axis=0)
59  STATS['RMS'] = np.sqrt(np.nanmean(array_f**2,axis=0))
60  STATS['STD'] = np.nanstd(array_f,axis=0)
61  STATS['Min'] = np.nanmin(array_f,axis=0)
62  STATS['Max'] = np.nanmax(array_f,axis=0)
63 
64  if os.path.exists(mu.expStats+'.nc'):
65  w_nc_fid = Dataset(mu.expStats+'.nc', 'a', format='NETCDF4')
66  else:
67  w_nc_fid = Dataset(mu.expStats+'.nc', 'w', format='NETCDF4')
68  w_nc_fid.description = "MPAS diagnostics/statistics"
69  w_nc_fid.createDimension('level', mu.nlevels)
70  w_nc_fid.createDimension('levelP1',mu.nlevelsP1)
71  w_nc_fid.createDimension('levelSurface', mu.nlevelSurface)
72  for statName in mu.allFileStats:
73  if varNames in mu.varNames3d:
74  if (varNames == 'w'):
75  bias2exp = w_nc_fid.createVariable(mu.expStats+"_day"+fcTDelta+"_"+band+"_"+varNames+"_"+statName,'f4', "levelP1")
76  else:
77  bias2exp = w_nc_fid.createVariable(mu.expStats+"_day"+fcTDelta+"_"+band+"_"+varNames+"_"+statName,'f4', "level")
78  else:
79  bias2exp = w_nc_fid.createVariable(mu.expStats+"_day"+fcTDelta+"_"+band+"_"+varNames+"_"+statName,'f4', "levelSurface")
80  w_nc_fid.variables[mu.expStats+"_day"+fcTDelta+'_'+band+'_'+varNames+'_'+statName][:] = STATS[statName]
81 
82 
83  w_nc_fid.close()
84 
85 def main():
87 
88 if __name__ == '__main__': main()
def write_stats(array_f, varNames, band, fcTDelta)