-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_precip_freq.py
executable file
·186 lines (155 loc) · 5.97 KB
/
plot_precip_freq.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
#!/usr/bin/env python
from os.path import join
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import netCDF4 as nc4
from e3sm_case_output import day_str
REF_CASE_NAME = "timestep_ctrl"
TEST_CASE_NAME = "timestep_all_10s"
OUTPUT_DIR = "/p/lustre2/santos36/timestep_precip/"
LAND_TROPICS = True
TROPICS_ONLY = False
if LAND_TROPICS:
TROPICS_ONLY = True
START_YEAR = 3
START_MONTH = 3
END_YEAR = 4
END_MONTH = 2
suffix = '_y{}m{}-y{}m{}'.format(day_str(START_YEAR),
day_str(START_MONTH),
day_str(END_YEAR),
day_str(END_MONTH))
if TROPICS_ONLY:
if LAND_TROPICS:
suffix += '_lndtropics'
else:
suffix += '_tropics'
log_file = open("plot_precip_log{}.txt".format(suffix), 'w')
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
out_file_template = "{}.freq.{}-{}.nc"
first_file_name = out_file_template.format(REF_CASE_NAME, "00"+day_str(START_YEAR),
day_str(START_MONTH))
first_file = nc4.Dataset(join(OUTPUT_DIR, first_file_name), 'r')
ncol = len(first_file.dimensions['ncol'])
nbins = len(first_file.dimensions['nbins'])
bin_lower_bounds = first_file['bin_lower_bounds'][:]
bin_width = np.log(bin_lower_bounds[2] / bin_lower_bounds[1])
lat = first_file['lat'][:]
lon = first_file['lon'][:]
area = first_file['area'][:]
# For tropics_only cases, just use a weight of 0 for all other columns.
if TROPICS_ONLY:
if LAND_TROPICS:
# Just pick a random file with the same grid as the run.
landfrac_file_name = '/p/lustre2/santos36/timestep_monthly_avgs/timestep_ctrl.0001-01.nc'
landfrac_file = nc4.Dataset(landfrac_file_name, 'r')
landfrac = landfrac_file['LANDFRAC'][0,:]
for i in range(ncol):
if np.abs(lat[i]) > 30.:
area[i] = 0.
else:
area[i] *= landfrac[i]
landfrac_file.close()
else:
for i in range(ncol):
if np.abs(lat[i]) > 30.:
area[i] = 0.
area_sum = area.sum()
weights = area/area_sum
first_file.close()
ref_sample_num_total = 0
test_sample_num_total = 0
prec_vars = ("PRECC", "PRECL", "PRECT")
ref_num_avgs = {}
ref_amount_avgs = {}
for var in prec_vars:
ref_num_avgs[var] = np.zeros((nbins,))
ref_amount_avgs[var] = np.zeros((nbins,))
test_num_avgs = {}
test_amount_avgs = {}
for var in prec_vars:
test_num_avgs[var] = np.zeros((nbins,))
test_amount_avgs[var] = np.zeros((nbins,))
for i in range(nmonths):
year = years[i]
year_string = "00" + day_str(year)
month = months[i]
month_string = day_str(month)
print("On year {}, month {}.".format(year, month))
out_file_name = out_file_template.format(REF_CASE_NAME, year_string, month_string)
out_file = nc4.Dataset(join(OUTPUT_DIR, out_file_name), 'r')
ref_sample_num_total += out_file.sample_num
for var in prec_vars:
num_name = "{}_num".format(var)
amount_name = "{}_amount".format(var)
for j in range(ncol):
ref_num_avgs[var] += out_file[num_name][j,:] * weights[j]
for j in range(ncol):
ref_amount_avgs[var] += out_file[amount_name][j,:] * weights[j]
out_file_name = out_file_template.format(TEST_CASE_NAME, year_string, month_string)
out_file = nc4.Dataset(join(OUTPUT_DIR, out_file_name), 'r')
test_sample_num_total += out_file.sample_num
for var in prec_vars:
num_name = "{}_num".format(var)
amount_name = "{}_amount".format(var)
for j in range(ncol):
test_num_avgs[var] += out_file[num_name][j,:] * weights[j]
for j in range(ncol):
test_amount_avgs[var] += out_file[amount_name][j,:] * weights[j]
for var in prec_vars:
ref_num_avgs[var] /= ref_sample_num_total
ref_amount_avgs[var] /= ref_sample_num_total
test_num_avgs[var] /= test_sample_num_total
test_amount_avgs[var] /= test_sample_num_total
# Threshold for precipitation to be considered "extreme", in mm/day.
PRECE_THRESHOLD = 97.
ibinthresh = -1
for i in range(nbins):
if bin_lower_bounds[i] > PRECE_THRESHOLD:
ibinthresh = i
break
if ibinthresh == -1:
print("Warning: extreme precip threshold greater than largest bin bound.")
for var in prec_vars:
# Leave out zero bin from loglog plot.
plt.loglog(bin_lower_bounds[1:], ref_num_avgs[var][1:], 'k')
plt.loglog(bin_lower_bounds[1:], test_num_avgs[var][1:], 'r')
plt.title("Frequency distribution of precipitation ({}/{}-{}/{})".format(
day_str(START_MONTH), day_str(START_YEAR),
day_str(END_MONTH), day_str(END_YEAR)))
plt.xlabel("Precipitation intensity (mm/day)")
plt.ylabel("fraction")
plt.savefig("{}_freq{}.png".format(var, suffix))
plt.close()
plt.semilogx(bin_lower_bounds[1:], ref_amount_avgs[var][1:] / bin_width, 'k')
plt.semilogx(bin_lower_bounds[1:], test_amount_avgs[var][1:] / bin_width, 'r')
if var == "PRECT":
print("Extreme precipitation rate for reference: ",
ref_amount_avgs[var][ibinthresh:].sum(),
file=log_file)
print("Extreme precipitation rate for test: ",
test_amount_avgs[var][ibinthresh:].sum(), "(Diff = ",
test_amount_avgs[var][ibinthresh:].sum() - ref_amount_avgs[var][ibinthresh:].sum(), ")",
file=log_file)
plt.title("Amounts of precipitation ({}/{}-{}/{})".format(
day_str(START_MONTH), day_str(START_YEAR),
day_str(END_MONTH), day_str(END_YEAR)))
plt.xlabel("Precipitation intensity (mm/day)")
plt.ylabel("Average precipitation amount (mm/day)")
plt.savefig("{}_amount{}.png".format(var, suffix))
plt.close()
log_file.close()