-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_monthly_means.py
More file actions
executable file
·352 lines (320 loc) · 12.1 KB
/
plot_monthly_means.py
File metadata and controls
executable file
·352 lines (320 loc) · 12.1 KB
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
#!/usr/bin/env python3
from functools import partial
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import netCDF4 as nc4
from e3sm_case_output import E3SMCaseOutput, day_str
START_YEAR = 1
START_MONTH = 1
END_YEAR = 4
END_MONTH = 2
START_AVG_YEAR = 1
START_AVG_MONTH = 3
END_AVG_YEAR = 4
END_AVG_MONTH = 2
MONTHLY_FILE_LOC="/p/lscratchh/santos36/timestep_monthly_avgs/"
USE_PRESAER=False
LAND_TROPICS = True
TROPICS_ONLY=False
MIDLATITUDES_ONLY=False
if LAND_TROPICS:
TROPICS_ONLY = True
assert not (TROPICS_ONLY and MIDLATITUDES_ONLY), \
"can't do only tropics and only midlatitudes"
nmonths = (END_YEAR - START_YEAR) * 12 - (START_MONTH - 1) + END_MONTH
imonths = list(range(nmonths))
curr_month = START_MONTH
curr_year = START_YEAR
months = []
years = []
for i in range(nmonths):
months.append(curr_month)
years.append(curr_year)
curr_month += 1
if curr_month > 12:
curr_month = 1
curr_year += 1
navgmonths = (END_AVG_YEAR - START_AVG_YEAR) * 12 \
- (START_AVG_MONTH - 1) + END_AVG_MONTH
suffix = '_y{}m{}-y{}m{}'.format(day_str(START_YEAR),
day_str(START_MONTH),
day_str(END_YEAR),
day_str(END_MONTH))
if USE_PRESAER:
suffix += '_presaer'
if TROPICS_ONLY:
if LAND_TROPICS:
suffix += '_lndtropics'
else:
suffix += '_tropics'
if MIDLATITUDES_ONLY:
suffix += '_midlats'
log_file = open("plot_monthly_log{}.txt".format(suffix), 'w')
if USE_PRESAER:
REF_CASE = E3SMCaseOutput("timestep_presaer_ctrl", "CTRLPA", MONTHLY_FILE_LOC, START_MONTH, END_MONTH)
TEST_CASES = [
]
else:
REF_CASE = E3SMCaseOutput("timestep_ctrl", "CTRL", MONTHLY_FILE_LOC, START_MONTH, END_MONTH)
TEST_CASES = [
E3SMCaseOutput("timestep_all_10s", "ALL10", MONTHLY_FILE_LOC, START_MONTH, END_MONTH),
]
case_num = len(TEST_CASES)
rfile0 = nc4.Dataset(REF_CASE.get_monthly_file_name(START_MONTH, START_YEAR), 'r')
nlev = len(rfile0.dimensions['lev'])
ncol = len(rfile0.dimensions['ncol'])
area = rfile0['area'][:]
# For tropics_only cases, just use a weight of 0 for all other cases.
if TROPICS_ONLY:
lat = rfile0['lat'][:]
if LAND_TROPICS:
landfrac = rfile0['LANDFRAC'][0,:]
for i in range(ncol):
if np.abs(lat[i]) > 30.:
area[i] = 0.
else:
area[i] *= landfrac[i]
else:
for i in range(ncol):
if np.abs(lat[i]) > 30.:
area[i] = 0.
# Same for midlatitudes.
elif MIDLATITUDES_ONLY:
lat = rfile0['lat'][:]
for i in range(ncol):
if np.abs(lat[i]) < 30. or np.abs(lat[i]) > 60.:
area[i] = 0.
area_sum = area.sum()
weights = area/area_sum
rfile0.close()
def calc_var_stats(ref_case, test_cases, month, year, varnames):
varnames_read = [name for name in varnames if name != "PRECT" and name != "TAU"]
if "PRECT" in varnames:
if "PRECL" not in varnames:
varnames_read.append("PRECL")
if "PRECC" not in varnames:
varnames_read.append("PRECC")
if "TAU" in varnames:
if "TAUX" not in varnames:
varnames_read.append("TAUX")
if "TAUY" not in varnames:
varnames_read.append("TAUY")
ref_time_avg, test_time_avgs, diff_time_avgs = ref_case.compare_monthly_averages(test_cases, month, year, varnames_read)
if "PRECT" in varnames:
ref_time_avg["PRECT"] = ref_time_avg["PRECL"] + ref_time_avg["PRECC"]
for icase in range(case_num):
test_time_avgs[icase]["PRECT"] = test_time_avgs[icase]["PRECL"] + test_time_avgs[icase]["PRECC"]
diff_time_avgs[icase]["PRECT"] = diff_time_avgs[icase]["PRECL"] + diff_time_avgs[icase]["PRECC"]
if "TAU" in varnames:
ref_time_avg["TAU"] = np.sqrt(ref_time_avg["TAUX"]**2 + ref_time_avg["TAUY"]**2)
for icase in range(case_num):
test_time_avgs[icase]["TAU"] = np.sqrt(test_time_avgs[icase]["TAUX"]**2 + test_time_avgs[icase]["TAUY"]**2)
diff_time_avgs[icase]["TAU"] = test_time_avgs[icase]["TAU"] - ref_time_avg["TAU"]
ref_avg = dict()
test_avgs = dict()
diff_avgs = dict()
rmses = dict()
for varname in varnames:
if varname in vars_3D:
ref_avg[varname] = np.zeros((nlev,))
for jlev in range(nlev):
ref_avg[varname][jlev] = (ref_time_avg[varname][jlev,:] * weights).sum()
else:
ref_avg[varname] = (ref_time_avg[varname] * weights).sum()
test_avgs[varname] = []
diff_avgs[varname] = []
rmses[varname] = []
for i in range(len(test_cases)):
if varname in vars_3D:
test_avgs[varname].append(np.zeros((nlev,)))
diff_avgs[varname].append(np.zeros((nlev,)))
rmses[varname].append(np.zeros((nlev,)))
for jlev in range(nlev):
test_avgs[varname][-1][jlev] = (test_time_avgs[i][varname][jlev,:] * weights).sum()
diff_avgs[varname][-1][jlev] = (diff_time_avgs[i][varname][jlev,:] * weights).sum()
rmses[varname][-1][jlev] = np.sqrt((diff_time_avgs[i][varname][jlev,:]**2 * weights).sum())
else:
test_avgs[varname].append((test_time_avgs[i][varname] * weights).sum())
diff_avgs[varname].append((diff_time_avgs[i][varname] * weights).sum())
rmses[varname].append(np.sqrt((diff_time_avgs[i][varname]**2 * weights).sum()))
assert np.isclose(diff_avgs[varname][i], test_avgs[varname][i] - ref_avg[varname]).all(), \
"Problem with diff of variable {} from case {}".format(varname, TEST_CASES[i].short_name)
return (ref_avg, test_avgs, diff_avgs, rmses)
# Possible ways to extract a 2D section start here:
def identity(x):
return x
def slice_at(level, x):
return x[:,level]
def plot_vars_over_time(names, units, scales, log_plot_names):
ref_means = dict()
test_means = dict()
diff_means = dict()
rmses = dict()
for name in names:
if name in vars_3D:
ref_means[name] = np.zeros((nmonths, nlev))
test_means[name] = np.zeros((case_num, nmonths, nlev))
diff_means[name] = np.zeros((case_num, nmonths, nlev))
rmses[name] = np.zeros((case_num, nmonths, nlev))
else:
ref_means[name] = np.zeros((nmonths,))
test_means[name] = np.zeros((case_num, nmonths))
diff_means[name] = np.zeros((case_num, nmonths))
rmses[name] = np.zeros((case_num, nmonths))
for imonth in range(nmonths):
month = months[imonth]
year = years[imonth]
print("On month: ", month, ", year: ", year, file=log_file, flush=True)
ref_mean, test_case_means, diff_case_means, case_rmses = calc_var_stats(REF_CASE, TEST_CASES, month, year, names)
for name in names:
ref_means[name][imonth] = ref_mean[name]*scales[name]
for i in range(case_num):
test_means[name][i,imonth] = test_case_means[name][i]*scales[name]
diff_means[name][i,imonth] = diff_case_means[name][i]*scales[name]
rmses[name][i,imonth] = case_rmses[name][i]*scales[name]
for name in names:
plot_name = name
if name in plot_names:
plot_name = plot_names[name]
get_2D = identity
if name == "RELHUM" or name == "Q" or name == "T":
get_2D = partial(slice_at, nlev-1)
if name in log_plot_names:
plot_var = plt.semilogy
else:
plot_var = plt.plot
ref_plot_var = get_2D(ref_means[name])
plot_var(imonths, ref_plot_var, label=REF_CASE.short_name)
for i in range(case_num):
test_plot_var = get_2D(test_means[name][i])
plot_var(imonths,
test_plot_var,
label=TEST_CASES[i].short_name)
plt.axis('tight')
plt.xlabel("month")
plt.ylabel("Mean {} ({})".format(plot_name, units[name]))
plt.savefig('{}_time{}.png'.format(name, suffix))
plt.close()
for i in range(case_num):
diff_plot_var = get_2D(diff_means[name][i])
plot_var(imonths,
diff_plot_var,
label=TEST_CASES[i].short_name)
plt.axis('tight')
plt.xlabel("month")
plt.ylabel("Mean {} difference ({})".format(plot_name, units[name]))
plt.savefig('{}_diff_time{}.png'.format(name, suffix))
plt.close()
for i in range(case_num):
rmse_plot_var = get_2D(rmses[name][i])
plot_var(imonths,
rmse_plot_var,
label=TEST_CASES[i].short_name)
plt.axis('tight')
plt.xlabel("month")
plt.ylabel("{} RMSE ({})".format(plot_name, units[name]))
plt.savefig('{}_rmse_time{}.png'.format(name, suffix))
plt.close()
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_weights = [month_days[months[imonth] - 1] for imonth in imonths]
weight_norm = sum(month_weights)
month_weights = [weight / weight_norm for weight in month_weights]
print(name, " has reference mean: ", sum([ref_plot_var[imonth] * month_weights[imonth] for imonth in imonths]),
file=log_file)
for i in range(case_num):
test_plot_var = get_2D(test_means[name][i])
diff_plot_var = get_2D(diff_means[name][i])
print(name, " has case ", TEST_CASES[i].short_name, " mean: ", sum([test_plot_var[imonth] * month_weights[imonth] for imonth in imonths]),
file=log_file)
print(name, " has difference mean: ", sum([diff_plot_var[imonth] * month_weights[imonth] for imonth in imonths]),
file=log_file)
plot_names = {
'LWCF': "long-wave cloud forcing",
'SWCF': "short wave cloud forcing",
'PRECC': "convective precipitation",
'PRECL': "large scale precipitation",
'PRECT': "total precipitation",
'TGCLDIWP': "ice water path",
'TGCLDLWP': "liquid water path",
'CLDTOT': "cloud area fraction",
'CLDLOW': "low cloud area fraction",
'CLDMED': "mid-level cloud area fraction",
'CLDHGH': "high cloud area fraction",
'LHFLX': "latent heat flux",
'SHFLX': "sensible heat flux",
'TAU': "surface wind stress",
'TS': "surface temperature",
'PSL': "sea level pressure",
'OMEGA500': "vertical velocity at 500 mb",
'U10': "10 meter wind speed",
'RELHUM': "surface relative humidity",
'Q': "surface specific humidity",
'TMQ': "precipitable water",
'T': "lowest level temperature",
}
units = {
'LWCF': r'$W/m^2$',
'SWCF': r'$W/m^2$',
'PRECC': r'$mm/day$',
'PRECL': r'$mm/day$',
'PRECT': r'$mm/day$',
'TGCLDIWP': r'$g/m^2$',
'TGCLDLWP': r'$g/m^2$',
'AODABS': r'units?',
'AODUV': r'units?',
'AODVIS': r'units?',
'FLDS': r'$W/m^2$',
'FLNS': r'$W/m^2$',
'FLNSC': r'$W/m^2$',
'FLNT': r'$W/m^2$',
'FLNTC': r'$W/m^2$',
'FLUT': r'$W/m^2$',
'FLUTC': r'$W/m^2$',
'FSDS': r'$W/m^2$',
'FSDSC': r'$W/m^2$',
'FSNS': r'$W/m^2$',
'FSNSC': r'$W/m^2$',
'FSNT': r'$W/m^2$',
'FSNTC': r'$W/m^2$',
'FSNTOA': r'$W/m^2$',
'FSNTOAC': r'$W/m^2$',
'FSUTOA': r'$W/m^2$',
'FSUTOAC': r'$W/m^2$',
'CLDTOT': r'fraction',
'CLDLOW': r'fraction',
'CLDMED': r'fraction',
'CLDHGH': r'fraction',
'OMEGA500': r'Pa/s',
'LHFLX': r'$W/m^2$',
'SHFLX': r'$W/m^2$',
'TAU': r'$N/m^2$',
'TAUX': r'$N/m^2$',
'TAUY': r'$N/m^2$',
'TS': r'$K$',
'PSL': r'$Pa$',
'U10': r'$m/s$',
'RELHUM': r'%',
'Q': r'$g/kg$',
'TMQ': r'$kg/m^2$',
'T': r'$K$',
}
names = list(units.keys())
scales = dict()
for name in names:
scales[name] = 1.
scales['TGCLDIWP'] = 1000.
scales['TGCLDLWP'] = 1000.
scales['PRECC'] = 1000.*86400.
scales['PRECL'] = 1000.*86400.
scales['PRECT'] = 1000.*86400.
scales['Q'] = 1000.
vars_3D = [
'RELHUM',
'Q',
'T',
]
log_plot_names = []#'AODABS', 'AODVIS', 'AODUV']
plot_vars_over_time(names, units, scales, log_plot_names)
log_file.close()