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
16 if os.path.exists(mu.expStats+
'.nc'):
17 os.remove(mu.expStats+
'.nc')
19 fcdirs = [d
for d
in os.listdir(
'.')
if os.path.isdir(d)]
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):
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")
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)
35 tmpbin = deepcopy(tmp)
36 tmpbin[np.logical_or(lats < mu.latBandsBounds [latBand+1], lats > mu.latBandsBounds [latBand])] = np.NaN
39 alltmp = np.append(alltmp, tmpbin)
40 if varName
in mu.varNames3d:
42 alltmp = alltmp.reshape(mu.ncells,mu.nlevelsP1)
44 alltmp = alltmp.reshape(mu.ncells,mu.nlevels)
46 alltmp = alltmp.reshape(mu.ncells,mu.nlevelSurface)
49 if varName
in mu.varNames3d:
50 alltmp = np.append(alltmp, tmpbin, axis=0)
52 alltmp = np.append(alltmp, tmpbin)
53 if (fcdir == len(fcdirs)-1):
54 newfile =
write_stats(alltmp,varName,mu.latBands[latBand],str(fcTDelta))
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)
64 if os.path.exists(mu.expStats+
'.nc'):
65 w_nc_fid = Dataset(mu.expStats+
'.nc',
'a', format=
'NETCDF4')
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:
75 bias2exp = w_nc_fid.createVariable(mu.expStats+
"_day"+fcTDelta+
"_"+band+
"_"+varNames+
"_"+statName,
'f4',
"levelP1")
77 bias2exp = w_nc_fid.createVariable(mu.expStats+
"_day"+fcTDelta+
"_"+band+
"_"+varNames+
"_"+statName,
'f4',
"level")
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]
88 if __name__ ==
'__main__':
main()
def write_stats(array_f, varNames, band, fcTDelta)