-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlier_plot2.py
executable file
·368 lines (338 loc) · 14.6 KB
/
outlier_plot2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
import netCDF4 as nc4
from mg2 import wv_sat_methods as wsm
from mg2 import micro_mg2_0 as mg
from mg2_constants import *
HIST_FILE_NAME = "/global/homes/s/santos/project/MG2_data_collection/run/MG2_data_collection.cam.h1.0001-01-06-00000.nc"
file = nc4.Dataset(HIST_FILE_NAME, 'r')
ncol = len(file.dimensions['ncol'])
lev = len(file.dimensions['lev'])
ilev = len(file.dimensions['ilev'])
errstring = wsm.wv_sat_methods_init(kind, tmelt, h2otrip, tboil, ttrice, epsilo)
if str(errstring).strip() != '':
print("wv_sat_methods initialization error: ", errstring)
errstring = mg.micro_mg_init(kind, gravit, rair, rh2o, cpair, tmelt, latvap,
latice, rhmini, dcs, dcs_tdep, uniform, do_cldice,
use_hetfrz_classnuc, precip_frac_method,
berg_eff_factor, allow_sed_supersat, ice_sed_ai,
prc_coef1, prc_exp, prc_exp1, cld_sed,
mg_prc_coeff_fix, alpha_grad, beta_grad)
if str(errstring).strip() != '':
print("MG2 initialization error: ", errstring)
mgncol = 1
t = file.variables["MG2IN_T"]
q = file.variables["MG2IN_Q"]
qc = file.variables["MG2IN_QC"]
qi = file.variables["MG2IN_QI"]
nc = file.variables["MG2IN_NC"]
ni = file.variables["MG2IN_NI"]
qr = file.variables["MG2IN_QR"]
qs = file.variables["MG2IN_QS"]
nr = file.variables["MG2IN_NR"]
ns = file.variables["MG2IN_NS"]
relvar = file.variables["MG2IN_RELVAR"]
accre_enhan = file.variables["MG2IN_ACCRE_ENHAN"]
p = file.variables["MG2IN_P"]
pdel = file.variables["MG2IN_PDEL"]
precipf = file.variables["MG2IN_PRECIP"]
liqcldf = file.variables["MG2IN_LIQCLDF"]
icecldf = file.variables["MG2IN_ICECLDF"]
naai = file.variables["MG2IN_NAAI"]
npccn = file.variables["MG2IN_NPCCN"]
rndst = np.empty((t.shape[0], t.shape[1], t.shape[2], 4))
rndst[:,:,:,0] = file.variables["MG2IN_RNDST1"][:]
rndst[:,:,:,1] = file.variables["MG2IN_RNDST2"][:]
rndst[:,:,:,2] = file.variables["MG2IN_RNDST3"][:]
rndst[:,:,:,3] = file.variables["MG2IN_RNDST4"][:]
nacon = np.empty((t.shape[0], t.shape[1], t.shape[2], 4))
nacon[:,:,:,0] = file.variables["MG2IN_NACON1"][:]
nacon[:,:,:,1] = file.variables["MG2IN_NACON2"][:]
nacon[:,:,:,2] = file.variables["MG2IN_NACON3"][:]
nacon[:,:,:,3] = file.variables["MG2IN_NACON4"][:]
frzimm = file.variables["MG2IN_FRZIMM"]
frzcnt = file.variables["MG2IN_FRZCNT"]
frzdep = file.variables["MG2IN_FRZDEP"]
t_loc = np.empty((mgncol, t.shape[1]), order='F')
q_loc = np.empty((mgncol, q.shape[1]), order='F')
qc_loc = np.empty((mgncol, qc.shape[1]), order='F')
qi_loc = np.empty((mgncol, qi.shape[1]), order='F')
nc_loc = np.empty((mgncol, nc.shape[1]), order='F')
ni_loc = np.empty((mgncol, ni.shape[1]), order='F')
qr_loc = np.empty((mgncol, qr.shape[1]), order='F')
qs_loc = np.empty((mgncol, qs.shape[1]), order='F')
nr_loc = np.empty((mgncol, nr.shape[1]), order='F')
ns_loc = np.empty((mgncol, ns.shape[1]), order='F')
relvar_loc = np.empty((mgncol, relvar.shape[1]), order='F')
accre_enhan_loc = np.empty((mgncol, accre_enhan.shape[1]), order='F')
p_loc = np.empty((mgncol, p.shape[1]), order='F')
pdel_loc = np.empty((mgncol, pdel.shape[1]), order='F')
precipf_loc = np.empty((mgncol, precipf.shape[1]), order='F')
liqcldf_loc = np.empty((mgncol, liqcldf.shape[1]), order='F')
icecldf_loc = np.empty((mgncol, icecldf.shape[1]), order='F')
naai_loc = np.empty((mgncol, naai.shape[1]), order='F')
npccn_loc = np.empty((mgncol, npccn.shape[1]), order='F')
rndst_loc = np.empty((mgncol, rndst.shape[1], 4), order='F')
nacon_loc = np.empty((mgncol, nacon.shape[1], 4), order='F')
frzimm_loc = np.empty((mgncol, frzimm.shape[1]), order='F')
frzcnt_loc = np.empty((mgncol, frzcnt.shape[1]), order='F')
frzdep_loc = np.empty((mgncol, frzdep.shape[1]), order='F')
final_time = 1800
timesteps = np.array([60, 120])
loc_arrays = {
'T': t_loc,
'Q': q_loc,
'QC': qc_loc,
'QI': qi_loc,
'QR': qr_loc,
'QS': qs_loc,
'NR': nr_loc,
}
var_names = sorted(list(loc_arrays.keys()))
finals = {}
roughs = {}
for name in var_names:
finals[name] = np.zeros((1 + final_time / timesteps[0], lev))
roughs[name] = np.zeros((1 + final_time / timesteps[1], lev))
rain_evap_fine = np.zeros((final_time / timesteps[0], lev))
rain_evap_coarse = np.zeros((final_time / timesteps[1], lev))
rain_sed_fine = np.zeros((final_time / timesteps[0], lev))
rain_sed_coarse = np.zeros((final_time / timesteps[1], lev))
rain_auto_fine = np.zeros((final_time / timesteps[0], lev))
rain_auto_coarse = np.zeros((final_time / timesteps[1], lev))
rain_accr_fine = np.zeros((final_time / timesteps[0], lev))
rain_accr_coarse = np.zeros((final_time / timesteps[1], lev))
max_ind = 1532
for it in range(timesteps.size):
print("Starting timestep=", timesteps[it])
assert final_time % timesteps[it] == 0
nsteps = final_time / timesteps[it]
deltat = float(timesteps[it])
t_loc[:,:] = t[0,:,max_ind].transpose()
q_loc[:,:] = q[0,:,max_ind].transpose()
qc_loc[:,:] = qc[0,:,max_ind].transpose()
qi_loc[:,:] = qi[0,:,max_ind].transpose()
nc_loc[:,:] = nc[0,:,max_ind].transpose()
ni_loc[:,:] = ni[0,:,max_ind].transpose()
qr_loc[:,:] = qr[0,:,max_ind].transpose()
qs_loc[:,:] = qs[0,:,max_ind].transpose()
nr_loc[:,:] = nr[0,:,max_ind].transpose()
ns_loc[:,:] = ns[0,:,max_ind].transpose()
relvar_loc[:,:] = relvar[0,:,max_ind].transpose()
accre_enhan_loc[:,:] = accre_enhan[0,:,max_ind].transpose()
p_loc[:,:] = p[0,:,max_ind].transpose()
pdel_loc[:,:] = pdel[0,:,max_ind].transpose()
precipf_loc[:,:] = precipf[0,:,max_ind].transpose()
liqcldf_loc[:,:] = liqcldf[0,:,max_ind].transpose()
icecldf_loc[:,:] = icecldf[0,:,max_ind].transpose()
naai_loc[:,:] = naai[0,:,max_ind].transpose()
npccn_loc[:,:] = npccn[0,:,max_ind].transpose()
rndst_loc[:,:,:] = rndst[0,:,max_ind:max_ind+1,:].transpose([1, 0, 2])
nacon_loc[:,:,:] = nacon[0,:,max_ind:max_ind+1,:].transpose([1, 0, 2])
frzimm_loc[:,:] = frzimm[0,:,max_ind].transpose()
frzcnt_loc[:,:] = frzcnt[0,:,max_ind].transpose()
frzdep_loc[:,:] = frzdep[0,:,max_ind].transpose()
if it == 0:
for name in var_names:
finals[name][0,:] = loc_arrays[name]
else:
for name in var_names:
roughs[name][0,:] = loc_arrays[name]
for n in range(nsteps):
qcsinksum_rate1ord, tlat, qvlat, qctend, qitend, nctend, nitend, qrtend, \
qstend, nrtend, nstend, effc, effc_fn, effi, prect, preci, nevapr, \
evapsnow, prain, prodsnow, cmeout, deffi, pgamrad, lamcrad, qsout, dsout, \
rflx, sflx, qrout, reff_rain, reff_snow, qcsevap, qisevap, qvres, cmeitot, \
vtrmc, vtrmi, umr, ums, qcsedten, qisedten, qrsedten, qssedten, pratot, \
prctot, mnuccctot, mnuccttot, msacwitot, psacwstot, bergstot, bergtot, \
melttot, homotot, qcrestot, prcitot, praitot, qirestot, mnuccrtot, \
pracstot, meltsdttot, frzrdttot, mnuccdtot, nrout, nsout, refl, arefl, \
areflz, frefl, csrfl, acsrfl, fcsrfl, rercld, ncai, ncal, qrout2, qsout2, \
nrout2, nsout2, drout2, dsout2, freqs, freqr, nfice, qcrat, errstring, \
prer_evap \
= mg.micro_mg_tend(deltat, t_loc, q_loc, qc_loc, qi_loc, nc_loc,
ni_loc, qr_loc, qs_loc, nr_loc, ns_loc,
relvar_loc, accre_enhan_loc, p_loc, pdel_loc,
precipf_loc, liqcldf_loc, icecldf_loc, naai_loc,
npccn_loc, rndst_loc, nacon_loc,
frzimm=frzimm_loc, frzcnt=frzcnt_loc,
frzdep=frzdep_loc, mgncol=mgncol, nlev=lev)
# Should use geopotential!
t_loc += tlat * deltat / cpair
q_loc += qvlat * deltat
q_loc[:,:] = np.where(q_loc < 1.e-12, 1.e-12, q_loc)
qc_loc += qctend * deltat
qc_loc[:,:] = np.where(qc_loc < 0., 0., qc_loc)
qi_loc += qitend * deltat
qi_loc[:,:] = np.where(qi_loc < 0., 0., qi_loc)
qr_loc += qrtend * deltat
qr_loc[:,:] = np.where(qr_loc < 0., 0., qr_loc)
qs_loc += qstend * deltat
qs_loc[:,:] = np.where(qs_loc < 0., 0., qs_loc)
nc_loc += nctend * deltat
nc_loc[:,:] = np.where(nc_loc > 1.e10, 1.e10, np.where(nc_loc < 1.e-12, 1.e-12, nc_loc))
ni_loc += nitend * deltat
ni_loc[:,:] = np.where(nc_loc > 1.e10, 1.e10, np.where(ni_loc < 1.e-12, 1.e-12, ni_loc))
nr_loc += nrtend * deltat
nr_loc[:,:] = np.where(nc_loc > 1.e10, 1.e10, np.where(nr_loc < 1.e-12, 1.e-12, nr_loc))
ns_loc += nstend * deltat
ns_loc[:,:] = np.where(nc_loc > 1.e10, 1.e10, np.where(ns_loc < 1.e-12, 1.e-12, ns_loc))
if it == 0:
for name in var_names:
finals[name][n+1,:] = loc_arrays[name]
rain_evap_fine[n,:] = prer_evap
rain_sed_fine[n,:] = qrsedten
rain_auto_fine[n,:] = prctot
rain_accr_fine[n,:] = pratot
else:
for name in var_names:
roughs[name][n+1,:] = loc_arrays[name]
rain_evap_coarse[n,:] = prer_evap
rain_sed_coarse[n,:] = qrsedten
rain_auto_coarse[n,:] = prctot
rain_accr_coarse[n,:] = pratot
# Do something with final columns.
levels = file.variables["ilev"]
times_fine = np.linspace(0., final_time, finals['Q'].shape[0])
times_coarse = np.linspace(0., final_time, roughs['Q'].shape[0])
times_fine, lev_fine = np.meshgrid(times_fine, levels)
times_coarse, lev_coarse = np.meshgrid(times_coarse, levels)
assert timesteps[1] % timesteps[0] == 0
stride = timesteps[1] / timesteps[0]
for name in var_names:
plt.pcolormesh(times_fine, lev_fine, finals[name].transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_{}.eps'.format(timesteps[0], name))
plt.close()
plt.pcolormesh(times_coarse, lev_coarse, roughs[name].transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_{}.eps'.format(timesteps[1], name))
plt.close()
plt.pcolormesh(times_coarse, lev_coarse, roughs[name].transpose() - finals[name][::stride,:].transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_{}s-{}s_{}.eps'.format(timesteps[1], timesteps[0], name))
plt.close()
# Store coarse tendency averages from the fine tendency.
mean_fine = np.empty(rain_evap_coarse.transpose().shape)
plt.pcolormesh(times_fine, lev_fine, rain_evap_fine.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_evap.eps'.format(timesteps[0], name))
plt.close()
plt.pcolormesh(times_coarse, lev_coarse, rain_evap_coarse.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_evap.eps'.format(timesteps[1], name))
plt.close()
for k in range(mean_fine.shape[0]):
for n in range(mean_fine.shape[1]):
mean_fine[k,n] = rain_evap_fine[stride*n:stride*(n+1),k].mean()
plt.pcolormesh(times_coarse, lev_coarse, rain_evap_coarse.transpose() - mean_fine)
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_{}s-{}s_rain_evap.eps'.format(timesteps[1], timesteps[0], name))
plt.close()
plt.pcolormesh(times_fine[:,1:], lev_fine[:,1:], rain_sed_fine.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_sed.eps'.format(timesteps[0], name))
plt.close()
plt.pcolormesh(times_coarse[:,1:], lev_coarse[:,1:], rain_sed_coarse.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_sed.eps'.format(timesteps[1], name))
plt.close()
for k in range(mean_fine.shape[0]):
for n in range(mean_fine.shape[1]):
mean_fine[k,n] = rain_sed_fine[stride*n:stride*(n+1),k].mean()
plt.pcolormesh(times_coarse, lev_coarse, rain_sed_coarse.transpose() - mean_fine)
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_{}s-{}s_rain_sed.eps'.format(timesteps[1], timesteps[0], name))
plt.close()
plt.pcolormesh(times_fine[:,1:], lev_fine[:,1:], rain_auto_fine.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_auto.eps'.format(timesteps[0], name))
plt.close()
plt.pcolormesh(times_coarse[:,1:], lev_coarse[:,1:], rain_auto_coarse.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_auto.eps'.format(timesteps[1], name))
plt.close()
for k in range(mean_fine.shape[0]):
for n in range(mean_fine.shape[1]):
mean_fine[k,n] = rain_auto_fine[stride*n:stride*(n+1),k].mean()
plt.pcolormesh(times_coarse, lev_coarse, rain_auto_coarse.transpose() - mean_fine)
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_{}s-{}s_rain_auto.eps'.format(timesteps[1], timesteps[0], name))
plt.close()
plt.pcolormesh(times_fine[:,1:], lev_fine[:,1:], rain_accr_fine.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_accr.eps'.format(timesteps[0], name))
plt.close()
plt.pcolormesh(times_coarse[:,1:], lev_coarse[:,1:], rain_accr_coarse.transpose())
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_time{}s_rain_accr.eps'.format(timesteps[1], name))
plt.close()
for k in range(mean_fine.shape[0]):
for n in range(mean_fine.shape[1]):
mean_fine[k,n] = rain_accr_fine[stride*n:stride*(n+1),k].mean()
plt.pcolormesh(times_coarse, lev_coarse, rain_accr_coarse.transpose() - mean_fine)
plt.xlabel("Time (s)")
plt.ylabel("Reference pressure (mb)")
plt.axis('tight')
plt.gca().invert_yaxis()
plt.colorbar()
plt.savefig('./outlier_{}s-{}s_rain_accr.eps'.format(timesteps[1], timesteps[0], name))
plt.close()