Skip to content

Commit de057a0

Browse files
committed
fix loose ends and make all relevant tests succeed
1 parent 3436d27 commit de057a0

34 files changed

+220
-409
lines changed

pysteps/decorators.py

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
.. autosummary::
1010
:toctree: ../generated/
1111
12-
postprocess_import
1312
check_input_frames
1413
prepare_interpolator
1514
memoize
@@ -22,8 +21,6 @@
2221

2322
import numpy as np
2423

25-
from pysteps.xarray_helpers import convert_input_to_xarray_dataset
26-
2724

2825
def _add_extra_kwrds_to_docstrings(target_func, extra_kwargs_doc_text):
2926
"""
@@ -44,76 +41,6 @@ def _add_extra_kwrds_to_docstrings(target_func, extra_kwargs_doc_text):
4441
return target_func
4542

4643

47-
def postprocess_import(fillna=np.nan, dtype="double"):
48-
"""
49-
Postprocess the imported precipitation data.
50-
Operations:
51-
- Allow type casting (dtype keyword)
52-
- Set invalid or missing data to predefined value (fillna keyword)
53-
This decorator replaces the text "{extra_kwargs}" in the function's
54-
docstring with the documentation of the keywords used in the postprocessing.
55-
The additional docstrings are added as "Other Parameters" in the importer function.
56-
57-
Parameters
58-
----------
59-
dtype: str
60-
Default data type for precipitation. Double precision by default.
61-
fillna: float or np.nan
62-
Default value used to represent the missing data ("No Coverage").
63-
By default, np.nan is used.
64-
If the importer returns a MaskedArray, all the masked values are set to the
65-
fillna value. If a numpy array is returned, all the invalid values (nan and inf)
66-
are set to the fillna value.
67-
68-
"""
69-
70-
def _postprocess_import(importer):
71-
@wraps(importer)
72-
def _import_with_postprocessing(*args, **kwargs):
73-
precip, quality, metadata = importer(*args, **kwargs)
74-
75-
_dtype = kwargs.get("dtype", dtype)
76-
77-
accepted_precisions = ["float32", "float64", "single", "double"]
78-
if _dtype not in accepted_precisions:
79-
raise ValueError(
80-
"The selected precision does not correspond to a valid value."
81-
"The accepted values are: " + str(accepted_precisions)
82-
)
83-
84-
if isinstance(precip, np.ma.MaskedArray):
85-
invalid_mask = np.ma.getmaskarray(precip)
86-
precip.data[invalid_mask] = fillna
87-
else:
88-
# If plain numpy arrays are used, the importers should indicate
89-
# the invalid values with np.nan.
90-
_fillna = kwargs.get("fillna", fillna)
91-
if _fillna is not np.nan:
92-
mask = ~np.isfinite(precip)
93-
precip[mask] = _fillna
94-
95-
return convert_input_to_xarray_dataset(
96-
precip.astype(_dtype), quality, metadata
97-
)
98-
99-
extra_kwargs_doc = """
100-
Other Parameters
101-
----------------
102-
dtype: str
103-
Data-type to which the array is cast.
104-
Valid values: "float32", "float64", "single", and "double".
105-
fillna: float or np.nan
106-
Value used to represent the missing data ("No Coverage").
107-
By default, np.nan is used.
108-
"""
109-
110-
_add_extra_kwrds_to_docstrings(_import_with_postprocessing, extra_kwargs_doc)
111-
112-
return _import_with_postprocessing
113-
114-
return _postprocess_import
115-
116-
11744
def check_input_frames(
11845
minimum_input_frames=2, maximum_input_frames=np.inf, just_ndim=False
11946
):
Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
11
import numpy as np
2-
import xarray as xr
3-
from pysteps.xarray_helpers import convert_output_to_xarray_dataset
42

53

6-
def extrapolate(precip_dataset: xr.Dataset, timesteps, outval=np.nan, **kwargs):
4+
def extrapolate(precip, velocity, timesteps, outval=np.nan, **kwargs):
75
"""
86
A dummy extrapolation method to apply Eulerian persistence to a
97
two-dimensional precipitation field. The method returns the a sequence
108
of the same initial field with no extrapolation applied (i.e. Eulerian
119
persistence).
12-
1310
Parameters
1411
----------
15-
precip_dataset : xarray.Dataset
16-
xarray dataset containing the input precipitation field. All
12+
precip : array-like
13+
Array of shape (m,n) containing the input precipitation field. All
1714
values are required to be finite.
15+
velocity : array-like
16+
Not used by the method.
1817
timesteps : int or list of floats
1918
Number of time steps or a list of time steps.
2019
outval : float, optional
2120
Not used by the method.
22-
2321
Other Parameters
2422
----------------
2523
return_displacement : bool
2624
If True, return the total advection velocity (displacement) between the
2725
initial input field and the advected one integrated along
2826
the trajectory. Default : False
29-
3027
Returns
3128
-------
3229
out : array or tuple
3330
If return_displacement=False, return a sequence of the same initial field
3431
of shape (num_timesteps,m,n). Otherwise, return a tuple containing the
3532
replicated fields and a (2,m,n) array of zeros.
36-
3733
References
3834
----------
3935
:cite:`GZ2002`
40-
4136
"""
42-
del outval # Unused by _eulerian_persistence
43-
precip_var = precip_dataset.attrs["precip_var"]
44-
precip = precip_dataset[precip_var].values[-1]
37+
del velocity, outval # Unused by _eulerian_persistence
4538

4639
if isinstance(timesteps, int):
4740
num_timesteps = timesteps
@@ -51,11 +44,8 @@ def extrapolate(precip_dataset: xr.Dataset, timesteps, outval=np.nan, **kwargs):
5144
return_displacement = kwargs.get("return_displacement", False)
5245

5346
extrapolated_precip = np.repeat(precip[np.newaxis, :, :], num_timesteps, axis=0)
54-
extrapolated_precip_dataset = convert_output_to_xarray_dataset(
55-
precip_dataset, timesteps, extrapolated_precip
56-
)
5747

5848
if not return_displacement:
59-
return extrapolated_precip_dataset
49+
return extrapolated_precip
6050
else:
61-
return extrapolated_precip_dataset, np.zeros((2,) + extrapolated_precip.shape)
51+
return extrapolated_precip, np.zeros((2,) + extrapolated_precip.shape)

0 commit comments

Comments
 (0)