-
Notifications
You must be signed in to change notification settings - Fork 37
/
test_extremes.py
291 lines (250 loc) · 8.24 KB
/
test_extremes.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
import numpy as np
import xarray as xr
from pcmdi_metrics.extremes.lib import compute_metrics
def create_random_precip(years, max_val=None, min_val=None):
# Returns array of precip along with covariate and sftlf
times = xr.cftime_range(
start="{0}-01-01".format(years[0]),
end="{0}-12-31".format(years[1]),
freq="D",
calendar="noleap",
name="time",
)
latd = 2
lond = 2
nyears = int(len(times) / 365)
total_inc = 3
n = np.arange(0, total_inc, total_inc / nyears)
fake_cov = np.arange(0, 0 + total_inc, total_inc / nyears)[0:nyears]
co2arr = np.zeros((len(times), latd, lond))
n0 = 0
for n in fake_cov:
n1 = n0 + 365
co2arr[n0:n1, :, :] = n
n0 += 365
values = (
np.ones((len(times), latd, lond)) * 20
+ np.random.randint(-10, high=10, size=(len(times), latd, lond))
+ co2arr * np.random.random()
)
values = values / 86400 # convert to kg m-2 s-1
lat = np.arange(0, latd)
lon = np.arange(0, lond)
fake_ds = xr.Dataset(
{
"pr": xr.DataArray(
data=values, # enter data here
dims=["time", "lat", "lon"],
coords={"time": times, "lat": lat, "lon": lon},
attrs={"_FillValue": -999.9, "units": "kg m-2 s-1"},
)
}
)
fake_ds["time"].encoding["calendar"] = "noleap"
fake_ds["time"].encoding["units"] = "days since 0000-01-01"
fake_ds = fake_ds.bounds.add_missing_bounds()
if max_val is not None:
fake_ds["pr"] = fake_ds.pr.where(fake_ds.pr <= max_val, max_val)
if min_val is not None:
fake_ds["pr"] = fake_ds.pr.where(fake_ds.pr >= min_val, min_val)
sftlf_arr = np.ones((latd, lond)) * 100
sftlf_arr[0, 0] = 0
sftlf = xr.Dataset(
{
"sftlf": xr.DataArray(
data=sftlf_arr,
dims=["lat", "lon"],
coords={"lat": lat, "lon": lon},
attrs={"_FillValue": -999.9},
)
}
)
sftlf = sftlf.bounds.add_missing_bounds(["X", "Y"])
return fake_ds, fake_cov, sftlf
def create_seasonal_precip(season):
# Returns array of precip along with covariate and sftlf
sd = {"DJF": [1, 2, 12], "MAM": [3, 4, 5], "JJA": [6, 7, 8], "SON": [9, 10, 11]}
mos = sd[season]
years = [1980, 1981]
times = xr.cftime_range(
start="{0}-01-01".format(years[0]),
end="{0}-12-31".format(years[1]),
freq="D",
calendar="noleap",
name="time",
)
latd = 2
lond = 2
values = np.ones((len(times), latd, lond))
lat = np.arange(0, latd)
lon = np.arange(0, lond)
fake_ds = xr.Dataset(
{
"pr": xr.DataArray(
data=values, # enter data here
dims=["time", "lat", "lon"],
coords={"time": times, "lat": lat, "lon": lon},
attrs={"_FillValue": -999.9, "units": "kg m-2 s-1"},
)
}
)
fake_ds = fake_ds.where(
(
(fake_ds["time.month"] == mos[0])
| (fake_ds["time.month"] == mos[1])
| (fake_ds["time.month"] == mos[2])
),
0.0,
)
fake_ds["time"].encoding["calendar"] = "noleap"
fake_ds["time"].encoding["units"] = "days since 0000-01-01"
fake_ds = fake_ds.bounds.add_missing_bounds()
sftlf_arr = np.ones((latd, lond)) * 100
sftlf_arr[0, 0] = 0
sftlf = xr.Dataset(
{
"sftlf": xr.DataArray(
data=sftlf_arr,
dims=["lat", "lon"],
coords={"lat": lat, "lon": lon},
attrs={"_FillValue": -999.9},
)
}
)
sftlf = sftlf.bounds.add_missing_bounds(["X", "Y"])
return fake_ds, sftlf
def test_seasonal_averager_settings():
# Testing that the defaults and mask are set
ds, _, sftlf = create_random_precip([1980, 1981])
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(PR, sftlf)
assert S.dec_mode == "DJF"
assert S.drop_incomplete_djf
assert S.annual_strict
assert S.sftlf.equals(sftlf["sftlf"])
def test_seasonal_averager_ann_max():
drop_incomplete_djf = True
dec_mode = "DJF"
annual_strict = True
ds, _, sftlf = create_random_precip([1980, 1981])
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(
PR,
sftlf,
dec_mode=dec_mode,
drop_incomplete_djf=drop_incomplete_djf,
annual_strict=annual_strict,
)
ann_max = S.annual_stats("max", pentad=False)
assert np.mean(ann_max) == np.mean(ds.groupby("time.year").max(dim="time"))
def test_seasonal_averager_ann_min():
drop_incomplete_djf = True
dec_mode = "DJF"
annual_strict = True
ds, _, sftlf = create_random_precip([1980, 1981])
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(
PR,
sftlf,
dec_mode=dec_mode,
drop_incomplete_djf=drop_incomplete_djf,
annual_strict=annual_strict,
)
ann_min = S.annual_stats("min", pentad=False)
assert np.mean(ann_min) == np.mean(ds.groupby("time.year").min(dim="time"))
# Test that drop_incomplete_djf puts nans in correct places
# Test that rolling averages for say a month is matching manual version
def test_seasonal_averager_ann_djf():
drop_incomplete_djf = True
dec_mode = "DJF"
annual_strict = True
ds, sftlf = create_seasonal_precip("DJF")
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(
PR,
sftlf,
dec_mode=dec_mode,
drop_incomplete_djf=drop_incomplete_djf,
annual_strict=annual_strict,
)
djf = S.seasonal_stats("DJF", "max", pentad=False)
assert djf.max() == 1.0
assert djf.mean() == 1.0
def test_seasonal_averager_rolling_mam():
ds, sftlf = create_seasonal_precip("MAM")
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(PR, sftlf)
S.calc_5day_mean()
# Get the MAM mean value of the rolling mean calculated by the seasonal averager
rolling_mean = float(
S.pentad.where(((ds["time.month"] >= 3) & (ds["time.month"] <= 5))).mean()
)
# This is what the mean value of the 5-day rolling means should be, if
# MAM are 1 and all other times are 0
true_mean = ((1 / 5) + (2 / 5) + (3 / 5) + (4 / 5) + 1 * (31 - 4) + 30 + 31) / (
31 + 30 + 31
)
assert rolling_mean == true_mean
def test_seasonal_averager_rolling_djf():
drop_incomplete_djf = False
dec_mode = "DJF"
annual_strict = True
ds, sftlf = create_seasonal_precip("DJF")
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(
PR,
sftlf,
dec_mode=dec_mode,
drop_incomplete_djf=drop_incomplete_djf,
annual_strict=annual_strict,
)
S.calc_5day_mean()
# Get the DJF mean value of the rolling mean calculated by the seasonal averager
rolling_mean = float(
S.pentad.where(
(
(ds["time.month"] == 1)
| (ds["time.month"] == 2)
| (ds["time.month"] == 12)
)
).mean()
)
# This is what the mean value of the 5-day rolling means should be, if
# DJF are 1 and all other times are 0. Have to slice off 4 days from the first January
# because that is where the time series starts
D = 31
J = 31
F = 28
total_days = D + J + F
true_mean = (
(J - 4)
+ F
+ (1 / 5)
+ (2 / 5)
+ (3 / 5)
+ (4 / 5)
+ (D - 4)
+ J
+ F
+ (1 / 5)
+ (2 / 5)
+ (3 / 5)
+ (4 / 5)
+ (D - 4)
) / (2 * total_days - 4)
assert rolling_mean == true_mean
"""def test_seasonal_averager_drop_djf():
drop_incomplete_djf = True
dec_mode = "DJF"
annual_strict = True
ds, sftlf = create_seasonal_precip("DJF")
PR = compute_metrics.TimeSeriesData(ds, "pr")
S = compute_metrics.SeasonalAverager(
PR,
sftlf,
dec_mode=dec_mode,
drop_incomplete_djf=drop_incomplete_djf,
annual_strict=annual_strict,
)
djf = S.seasonal_stats("DJF", "max", pentad=False)
"""