11# -*- coding: utf-8 -*-
22
3+ from datetime import datetime , timezone
34import os
45
56import numpy as np
1819 stack_cascades ,
1920)
2021from pysteps .utils .check_norain import check_norain
22+ from pysteps .xarray_helpers import convert_input_to_xarray_dataset
2123
2224pytest .importorskip ("netCDF4" )
2325
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
6469times_nwp = np .array (
95100# Prepare input NWP files
96101# Convert to rain rates [mm/h]
97102converter = 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
105113transformer = 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
117125# Set the testing arguments
118126# Test function arguments
119127utils_arg_names = (
120- "precip_nwp " ,
128+ "precip_nwp_dataset " ,
121129 "nwp_model" ,
122130 "issue_times" ,
123131 "timestep" ,
130138# Test function values
131139utils_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
144152smoothing_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
152160smoothing_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
164172@pytest .mark .parametrize (utils_arg_names , utils_arg_values )
165173# The test function to be used
166174def 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 )
429441def 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 ,
0 commit comments