5 from netCDF4
import Dataset
6 import matplotlib.cm
as cm
7 import matplotlib.pyplot
as plt
8 from mpl_toolkits.basemap
import Basemap
9 from copy
import deepcopy
11 from datetime
import datetime, timedelta
12 import numpy.random
as npr
13 import modelsp_utils
as mu
15 expStats =
'expmgfsci'
20 if os.path.exists(expStats+
'.nc'):
21 os.remove(expStats+
'.nc')
24 for metrics
in allmetrics:
26 for varName
in mu.varNames3d:
27 for latBand
in mu.latBands:
28 for fcTDelta
in np.arange(0, mu.fcRange+mu.interval,mu.interval):
29 varNamesList = [
'expmgfs_day'+str(fcTDelta)+
'_'+ latBand +
'_'+ varName +
'_' + metrics]
30 varNamesListAll.append(varNamesList)
31 for metrics
in allmetrics:
33 for varName
in mu.varNames:
34 for latBand
in mu.latBands:
35 varNamesList2 = [
'expmgfs_'+ latBand +
'_'+ varName +
'_' + metrics]
36 varNamesListAll.append(varNamesList2)
37 for i
in range(0,len(varNamesListAll)):
41 alldiffdata_rmsdiv = []
44 while TDATE <= mu.EDATE:
48 date = TDATE.strftime(
'%Y%m%d%H')
49 xlabeltime = np.append(xlabeltime,date[4:][:-2])
50 nc_file1 = mu.EXP_DIR1+
'/'+date+
'/diagnostic_stats/expmgfs.nc'
51 nc_fid1 = Dataset(nc_file1,
"r", format=
"NETCDF4")
53 data1 = np.array( nc_fid1.variables[
''.join(varNamesListAll[i])][:] )
54 alldata = np.append(alldata, data1)
56 nc_file2 = mu.EXP_DIR2+
'/'+date+
'/diagnostic_stats/expmgfs.nc'
57 nc_fid2 = Dataset(nc_file2,
"r", format=
"NETCDF4")
59 data2 = np.array( nc_fid2.variables[
''.join(varNamesListAll[i])][:] )
61 diffdata = data2 - data1
62 alldiffdata = np.append(alldiffdata, diffdata)
66 alldiffdata = alldiffdata.reshape(len(xlabeltime),len(diffdata)).T
67 newfile =
write_stats(alldiffdata,
''.join(varNamesListAll[i]))
82 STATS[
'Mean'] = np.nanmean(array_f,axis=1)
84 low, high =
bootstrap(array_f[i,:], 10000, np.mean, 0.05)
85 lows = np.append(lows, low)
86 highs = np.append(highs,high)
87 stat = np.column_stack((STATS[
'Mean'], lows, highs))
89 if os.path.exists(expStats+
'.nc'):
90 w_nc_fid = Dataset(expStats+
'.nc',
'a', format=
'NETCDF4')
92 w_nc_fid = Dataset(expStats+
'.nc',
'w', format=
'NETCDF4')
93 w_nc_fid.description =
"MPAS diagnostics/statistics"
94 w_nc_fid.createDimension(
'level',int(mu.nlevels))
95 w_nc_fid.createDimension(
'levelP1',int(mu.nlevelsP1))
96 w_nc_fid.createDimension(
'fcnums',int(mu.fcNums))
97 w_nc_fid.createDimension(
'stat',3)
99 w_nc_var = w_nc_fid.createVariable(varNames,
'f4', (
'level',
'stat'))
101 w_nc_var = w_nc_fid.createVariable(varNames,
'f4', (
'levelP1',
'stat'))
103 w_nc_var = w_nc_fid.createVariable(varNames,
'f4', (
'fcnums',
'stat'))
104 w_nc_fid.variables[varNames][:] = stat
110 idx = npr.randint(0, n, (num_samples, n))
111 samples_with_replacement = data[idx]
112 stat = np.sort(statistic(samples_with_replacement, 1))
113 return (stat[int((alpha/2.0)*num_samples)], stat[int((1-alpha/2.0)*num_samples)])
118 if __name__ ==
'__main__':
main()
def write_stats(array_f, varNames)
def bootstrap(data, num_samples, statistic, alpha)