Skip to content

Commit 7791b4a

Browse files
committed
make last test discoverable and fix dimension test
1 parent 73bf8b2 commit 7791b4a

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

pysteps/tests/test_blending_utils.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from datetime import datetime, timezone
34
import os
45

56
import numpy as np
@@ -18,6 +19,7 @@
1819
stack_cascades,
1920
)
2021
from pysteps.utils.check_norain import check_norain
22+
from pysteps.xarray_helpers import convert_input_to_xarray_dataset
2123

2224
pytest.importorskip("netCDF4")
2325

@@ -59,6 +61,9 @@
5961
y1=-731900.0,
6062
y2=0.0,
6163
)
64+
precip_nwp_dataset = convert_input_to_xarray_dataset(
65+
precip_nwp, None, nwp_metadata, datetime.now(tz=timezone.utc)
66+
)
6267

6368
# Get the analysis time and valid time
6469
times_nwp = np.array(
@@ -95,16 +100,19 @@
95100
# Prepare input NWP files
96101
# Convert to rain rates [mm/h]
97102
converter = pysteps.utils.get_method("mm/h")
98-
precip_nwp, nwp_metadata = converter(precip_nwp, nwp_metadata)
103+
precip_nwp_dataset = converter(precip_nwp_dataset)
104+
nwp_precip_var = precip_nwp_dataset.attrs["precip_var"]
99105

100106
# Threshold the data
101-
nwp_metadata["threshold"] = 0.1
102-
precip_nwp[precip_nwp < nwp_metadata["threshold"]] = 0.0
107+
precip_nwp_dataset[nwp_precip_var].attrs["threshold"] = 0.1
108+
precip_nwp_dataset[nwp_precip_var].data[
109+
precip_nwp_dataset[nwp_precip_var].values < 0.1
110+
] = 0.0
103111

104112
# Transform the data
105113
transformer = pysteps.utils.get_method("dB")
106-
precip_nwp, nwp_metadata = transformer(
107-
precip_nwp, nwp_metadata, threshold=nwp_metadata["threshold"]
114+
precip_nwp_dataset = transformer(
115+
precip_nwp_dataset, threshold=nwp_metadata["threshold"]
108116
)
109117

110118
# Set two issue times for testing
@@ -117,7 +125,7 @@
117125
# Set the testing arguments
118126
# Test function arguments
119127
utils_arg_names = (
120-
"precip_nwp",
128+
"precip_nwp_dataset",
121129
"nwp_model",
122130
"issue_times",
123131
"timestep",
@@ -130,31 +138,31 @@
130138
# Test function values
131139
utils_arg_values = [
132140
(
133-
precip_nwp,
141+
precip_nwp_dataset,
134142
"test",
135143
[issue_time_first, issue_time_second],
136144
5.0,
137145
3,
138146
times_nwp,
139-
precip_nwp.shape[1:],
147+
precip_nwp_dataset[nwp_precip_var].values.shape[1:],
140148
weights,
141149
)
142150
]
143151

144152
smoothing_arg_names = (
145-
"precip_nwp",
153+
"precip_nwp_dataset",
146154
"max_padding_size_in_px",
147155
"gaussian_kernel_size",
148156
"inverted",
149157
"non_linear_growth_kernel_sizes",
150158
)
151159

152160
smoothing_arg_values = [
153-
(precip_nwp, 80, 9, False, False),
154-
(precip_nwp, 10, 9, False, False),
155-
(precip_nwp, 80, 5, False, False),
156-
(precip_nwp, 80, 9, True, False),
157-
(precip_nwp, 80, 9, False, True),
161+
(precip_nwp_dataset, 80, 9, False, False),
162+
(precip_nwp_dataset, 10, 9, False, False),
163+
(precip_nwp_dataset, 80, 5, False, False),
164+
(precip_nwp_dataset, 80, 9, True, False),
165+
(precip_nwp_dataset, 80, 9, False, True),
158166
]
159167

160168

@@ -164,7 +172,7 @@
164172
@pytest.mark.parametrize(utils_arg_names, utils_arg_values)
165173
# The test function to be used
166174
def test_blending_utils(
167-
precip_nwp,
175+
precip_nwp_dataset,
168176
nwp_model,
169177
issue_times,
170178
timestep,
@@ -186,7 +194,7 @@ def test_blending_utils(
186194
# Compute and store the motion
187195
###
188196
compute_store_nwp_motion(
189-
precip_nwp=precip_nwp,
197+
precip_nwp=precip_nwp_dataset,
190198
oflow_method=oflow_method,
191199
analysis_time=valid_times[0],
192200
nwp_model=nwp_model,
@@ -214,7 +222,7 @@ def test_blending_utils(
214222
# Decompose and store NWP forecast
215223
###
216224
decompose_NWP(
217-
R_NWP=precip_nwp,
225+
R_NWP=precip_nwp_dataset,
218226
NWP_model=nwp_model,
219227
analysis_time=valid_times[0],
220228
timestep=timestep,
@@ -304,13 +312,13 @@ def test_blending_utils(
304312
# Check, for a sample, if the stored motion fields are as expected
305313
assert_array_almost_equal(
306314
v_nwp_first[1],
307-
oflow_method(precip_nwp[0:2, :, :]),
315+
oflow_method(precip_nwp_dataset[0:2, :, :]),
308316
decimal=3,
309317
err_msg="Stored motion field of first forecast not equal to expected motion field",
310318
)
311319
assert_array_almost_equal(
312320
v_nwp_second[1],
313-
oflow_method(precip_nwp[3:5, :, :]),
321+
oflow_method(precip_nwp_dataset[3:5, :, :]),
314322
decimal=3,
315323
err_msg="Stored motion field of second forecast not equal to expected motion field",
316324
)
@@ -364,7 +372,11 @@ def test_blending_utils(
364372
assert v_nwp_blended.shape == v_nwp_first[1].shape
365373
assert_array_almost_equal(
366374
v_nwp_blended,
367-
(oflow_method(precip_nwp[0:2, :, :]) + oflow_method(precip_nwp[3:5, :, :])) / 2,
375+
(
376+
oflow_method(precip_nwp_dataset[0:2, :, :])
377+
+ oflow_method(precip_nwp_dataset[3:5, :, :])
378+
)
379+
/ 2,
368380
decimal=3,
369381
err_msg="Blended motion field does not equal average of the two motion fields",
370382
)
@@ -385,18 +397,18 @@ def test_blending_utils(
385397

386398
assert_array_almost_equal(
387399
precip_recomposed_first,
388-
precip_nwp[0, :, :],
400+
precip_nwp_dataset[0, :, :],
389401
decimal=3,
390402
err_msg="Recomposed field of first forecast does not equal original field",
391403
)
392404
assert_array_almost_equal(
393405
precip_recomposed_second,
394-
precip_nwp[3, :, :],
406+
precip_nwp_dataset[3, :, :],
395407
decimal=3,
396408
err_msg="Recomposed field of second forecast does not equal original field",
397409
)
398410

399-
precip_arr = precip_nwp
411+
precip_arr = precip_nwp_dataset
400412
# rainy fraction is 0.005847
401413
assert not check_norain(precip_arr, win_fun=None)
402414
assert not check_norain(
@@ -427,15 +439,15 @@ def test_blending_utils(
427439
# Finally, also test the compute_smooth_dilated mask functionality
428440
@pytest.mark.parametrize(smoothing_arg_names, smoothing_arg_values)
429441
def test_blending_smoothing_utils(
430-
precip_nwp,
442+
precip_nwp_dataset,
431443
max_padding_size_in_px,
432444
gaussian_kernel_size,
433445
inverted,
434446
non_linear_growth_kernel_sizes,
435447
):
436448
# First add some nans to indicate a mask
437-
precip_nwp[:, 0:100, 0:100] = np.nan
438-
nan_indices = np.isnan(precip_nwp[0])
449+
precip_nwp_dataset[:, 0:100, 0:100] = np.nan
450+
nan_indices = np.isnan(precip_nwp_dataset[0])
439451
new_mask = compute_smooth_dilated_mask(
440452
nan_indices,
441453
max_padding_size_in_px=max_padding_size_in_px,

pysteps/utils/dimension.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,10 @@ def aggregate_fields(
326326
}
327327
)
328328
)
329+
329330
for d, ws in zip(dim, window_size):
330-
dataset[d].attrs["stepsize"] = dataset[d].attrs["stepsize"] * ws
331+
if "stepsize" in dataset[d].attrs:
332+
dataset[d].attrs["stepsize"] = dataset[d].attrs["stepsize"] * ws
331333

332334
return dataset
333335

requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pygrib
2323
pywavelets
2424
geotiff
2525
gdal==3.4.1; "22.04" in platform_version and "Ubuntu" in platform_version
26+
cookiecutter
2627

2728
# Testing
2829
pytest

0 commit comments

Comments
 (0)