MPAS-JEDI
writediag_modelspace.py
Go to the documentation of this file.
1 import datetime as dt
2 import os
3 import sys
4 import numpy
5 import numpy as np
6 from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/
7 import matplotlib.cm as cm
8 import matplotlib.pyplot as plt
9 from mpl_toolkits.basemap import Basemap
10 from copy import deepcopy
11 from datetime import datetime, timedelta
12 import modelsp_utils as mu
13 
15  if os.path.exists(mu.expStats+'.nc'):
16  os.remove(mu.expStats+'.nc')
17  path = os.getcwd()
18  date = path.split('/')[-2]
19  initDate = datetime.strptime(date,"%Y%m%d%H")
20  lats, lons = mu.readGrid()
21 
22  for varName in mu.varNames:
23  for latBand in range(0, len(mu.latBands)):
24  tmp = []
25  meanncs = []
26  rmsncs = []
27  msncs = []
28 
29  for fcTDelta in np.arange(0, mu.fcRange+mu.interval,mu.interval):
30  fcDate = initDate + timedelta(days=fcTDelta)
31  fileDate= fcDate.strftime("%Y-%m-%d_%H.%M.%S")
32  ncFile1 = mu.GFSANA_DIR+'/x1.40962.init.'+fileDate+'.nc'
33  ncFile2 = '../restart.'+fileDate+'.nc'
34  tmp = mu.varDiff(varName,ncFile1,ncFile2)
35 
36  #bin for regions
37  tmpbin = []
38  tmpbin = deepcopy(tmp)
39  tmpbin[np.logical_or(lats < mu.latBandsBounds [latBand+1], lats > mu.latBandsBounds [latBand])] = np.NaN
40  #save every level stat
41  newfile = write_stats(tmpbin,varName,mu.latBands[latBand],str(fcTDelta))
42 
43  meannc = np.nanmean(tmpbin.flatten(),axis=0)
44  rmsnc = np.sqrt(np.nanmean(tmpbin.flatten()**2,axis=0))
45  msnc = np.nanmean(tmpbin.flatten()**2,axis=0)
46 
47  meanncs = np.append(meanncs,meannc)
48  rmsncs = np.append(rmsncs,rmsnc)
49  msncs = np.append(msncs,msnc)
50  #save all levels stat
51  newfile = write_stats_levall(meanncs,varName,mu.latBands[latBand],'Mean')
52  newfile = write_stats_levall(rmsncs,varName,mu.latBands[latBand],'RMS')
53  newfile = write_stats_levall(msncs,varName,mu.latBands[latBand],'MS')
54 #TODO: move the following functions to plot_utils.py or basic_plot_functions.py
55 def write_stats(array_f,varNames,band,fcTDelta):
56  STATS = {}
57  STATS['Mean'] = np.nanmean(array_f,axis=0)
58  STATS['MS'] = np.nanmean(array_f**2,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  if os.path.exists(mu.expStats+'.nc'):
64  w_nc_fid = Dataset(mu.expStats+'.nc', 'a', format='NETCDF4')
65  else:
66  w_nc_fid = Dataset(mu.expStats+'.nc', 'w', format='NETCDF4')
67  w_nc_fid.description = "MPAS diagnostics/statistics"
68  w_nc_fid.createDimension('level', 55)
69  w_nc_fid.createDimension('fcnums', int(mu.fcNums))
70  w_nc_fid.createDimension('levelP1', 56)
71  w_nc_fid.createDimension('levelSurface', 1)
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  w_nc_fid.close()
83 
84 def write_stats_levall(array_f,varNames,band,statName):
85  if os.path.exists(mu.expStats+'.nc'):
86  w_nc_fid = Dataset(mu.expStats+'.nc', 'a', format='NETCDF4')
87  else:
88  w_nc_fid = Dataset(mu.expStats+'.nc', 'w', format='NETCDF4')
89  w_nc_fid.description = "MPAS diagnostics/statistics"
90 
91  bias2exp = w_nc_fid.createVariable(mu.expStats+"_"+band+"_"+varNames+"_"+statName,'f4', "fcnums")
92  w_nc_fid.variables[mu.expStats+'_'+band+'_'+varNames+'_'+statName][:] = array_f
93 
94  w_nc_fid.close()
95 
96 def main():
98 
99 if __name__ == '__main__': main()
def write_stats_levall(array_f, varNames, band, statName)
def write_stats(array_f, varNames, band, fcTDelta)