|
21 | 21 | """ |
22 | 22 |
|
23 | 23 | import numpy as np |
| 24 | +import xarray as xr |
24 | 25 | from pysteps import nowcasts |
25 | 26 | from pysteps.utils import conversion |
26 | 27 | from scipy.stats import rankdata |
27 | 28 |
|
| 29 | +from pysteps.xarray_helpers import convert_output_to_xarray_dataset |
| 30 | + |
28 | 31 |
|
29 | 32 | def forecast( |
30 | | - precip, |
31 | | - precip_metadata, |
32 | | - velocity, |
| 33 | + radar_dataset: xr.Dataset, |
33 | 34 | timesteps, |
34 | 35 | timestep, |
35 | 36 | nowcast_method, |
36 | | - precip_nwp=None, |
37 | | - precip_nwp_metadata=None, |
| 37 | + model_dataset: xr.Dataset = None, |
38 | 38 | start_blending=120, |
39 | 39 | end_blending=240, |
40 | 40 | fill_nwp=True, |
41 | 41 | saliency=False, |
42 | 42 | nowcast_kwargs=None, |
43 | 43 | ): |
44 | 44 | """Generate a forecast by linearly or saliency-based blending of nowcasts with NWP data |
| 45 | + # XR: Update docstring |
45 | 46 |
|
46 | 47 | Parameters |
47 | 48 | ---------- |
@@ -105,31 +106,27 @@ def forecast( |
105 | 106 | if nowcast_kwargs is None: |
106 | 107 | nowcast_kwargs = dict() |
107 | 108 |
|
108 | | - # Ensure that only the most recent precip timestep is used |
109 | | - if len(precip.shape) == 3: |
110 | | - precip = precip[-1, :, :] |
111 | | - |
112 | 109 | # First calculate the number of needed timesteps (up to end_blending) for the nowcast |
113 | 110 | # to ensure that the nowcast calculation time is limited. |
114 | 111 | timesteps_nowcast = int(end_blending / timestep) |
115 | 112 |
|
116 | 113 | nowcast_method_func = nowcasts.get_method(nowcast_method) |
117 | 114 |
|
118 | 115 | # Check if NWP data is given as input |
119 | | - if precip_nwp is not None: |
| 116 | + if model_dataset is not None: |
120 | 117 | # Calculate the nowcast |
121 | | - precip_nowcast = nowcast_method_func( |
122 | | - precip, |
123 | | - velocity, |
124 | | - timesteps_nowcast, |
125 | | - **nowcast_kwargs, |
| 118 | + nowcast_dataset = nowcast_method_func( |
| 119 | + radar_dataset, timesteps_nowcast, **nowcast_kwargs |
126 | 120 | ) |
127 | 121 |
|
128 | 122 | # Make sure that precip_nowcast and precip_nwp are in mm/h |
129 | | - precip_nowcast, _ = conversion.to_rainrate( |
130 | | - precip_nowcast, metadata=precip_metadata |
131 | | - ) |
132 | | - precip_nwp, _ = conversion.to_rainrate(precip_nwp, metadata=precip_nwp_metadata) |
| 123 | + nowcast_dataset = conversion.to_rainrate(nowcast_dataset) |
| 124 | + nowcast_precip_var = nowcast_dataset.attrs["precip_var"] |
| 125 | + precip_nowcast = nowcast_dataset[nowcast_precip_var].values |
| 126 | + |
| 127 | + model_dataset = conversion.to_rainrate(model_dataset) |
| 128 | + model_precip_var = model_dataset.attrs["precip_var"] |
| 129 | + precip_nwp = model_dataset[model_precip_var].values |
133 | 130 |
|
134 | 131 | if len(precip_nowcast.shape) == 4: |
135 | 132 | n_ens_members_nowcast = precip_nowcast.shape[0] |
@@ -261,22 +258,19 @@ def forecast( |
261 | 258 |
|
262 | 259 | else: |
263 | 260 | # Calculate the nowcast |
264 | | - precip_nowcast = nowcast_method_func( |
265 | | - precip, |
266 | | - velocity, |
267 | | - timesteps, |
268 | | - **nowcast_kwargs, |
| 261 | + nowcast_dataset = nowcast_method_func( |
| 262 | + radar_dataset, timesteps, **nowcast_kwargs |
269 | 263 | ) |
270 | 264 |
|
271 | 265 | # Make sure that precip_nowcast and precip_nwp are in mm/h |
272 | | - precip_nowcast, _ = conversion.to_rainrate( |
273 | | - precip_nowcast, metadata=precip_metadata |
274 | | - ) |
| 266 | + nowcast_dataset = conversion.to_rainrate(nowcast_dataset) |
| 267 | + nowcast_precip_var = nowcast_dataset.attrs["precip_var"] |
| 268 | + precip_nowcast = nowcast_dataset[nowcast_precip_var].values |
275 | 269 |
|
276 | 270 | # If no NWP data is given, the blended field is simply equal to the nowcast field |
277 | 271 | precip_blended = precip_nowcast |
278 | 272 |
|
279 | | - return precip_blended |
| 273 | + return convert_output_to_xarray_dataset(radar_dataset, timesteps, precip_blended) |
280 | 274 |
|
281 | 275 |
|
282 | 276 | def _get_slice(n_dims, ref_dim, ref_id): |
|
0 commit comments