Skip to content

Commit 6ab76a7

Browse files
committed
fix noise tapering and error handling
1 parent 006fb49 commit 6ab76a7

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

pysteps/blending/steps.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ def __check_inputs(self):
777777
self.__params.filter_kwargs = deepcopy(self.__config.filter_kwargs)
778778

779779
if self.__config.noise_kwargs is None:
780-
self.__params.noise_kwargs = dict()
780+
self.__params.noise_kwargs = {"win_fun": "tukey"}
781781
else:
782782
self.__params.noise_kwargs = deepcopy(self.__config.noise_kwargs)
783783

@@ -1092,16 +1092,31 @@ def transform_to_lagrangian(precip, i):
10921092

10931093
self.__precip_models = np.stack(temp_precip_models)
10941094

1095+
if self.__params.noise_kwargs["win_fun"] is not None:
1096+
tapering = utils.tapering.compute_window_function(
1097+
self.__precip.shape[1],
1098+
self.__precip.shape[2],
1099+
self.__params.noise_kwargs["win_fun"],
1100+
)
1101+
else:
1102+
tapering = np.ones((self.__precip.shape[1], self.__precip.shape[2]))
1103+
1104+
tapering_mask = tapering == 0.0
1105+
masked_precip = self.__precip.copy()
1106+
masked_precip[:, tapering_mask] = np.nanmin(self.__precip)
1107+
masked_precip_models = self.__precip_models.copy()
1108+
masked_precip_models[:, :, tapering_mask] = np.nanmin(self.__precip_models)
1109+
10951110
# Check for zero input fields in the radar and NWP data.
10961111
self.__params.zero_precip_radar = blending.utils.check_norain(
1097-
self.__precip,
1112+
masked_precip,
10981113
self.__config.precip_threshold,
10991114
self.__config.norain_threshold,
11001115
)
11011116
# The norain fraction threshold used for nwp is the default value of 0.0,
11021117
# since nwp does not suffer from clutter.
11031118
self.__params.zero_precip_model_fields = blending.utils.check_norain(
1104-
self.__precip_models,
1119+
masked_precip_models,
11051120
self.__config.precip_threshold,
11061121
self.__config.norain_threshold,
11071122
)

pysteps/utils/tapering.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def compute_window_function(m, n, func, **kwargs):
8282
Array of shape (m, n) containing the tapering weights.
8383
"""
8484
X, Y = np.meshgrid(np.arange(n), np.arange(m))
85-
R = np.sqrt((X - int(n / 2)) ** 2 + (Y - int(m / 2)) ** 2)
85+
R = np.sqrt(((X / n) - 0.5) ** 2 + ((Y / m) - 0.5) ** 2)
8686

8787
if func == "hann":
8888
return _hann(R)
@@ -108,26 +108,24 @@ def _compute_mask_distances(mask):
108108

109109
def _hann(R):
110110
W = np.ones_like(R)
111-
N = min(R.shape[0], R.shape[1])
112-
mask = R > int(N / 2)
111+
mask = R > 0.5
113112

114113
W[mask] = 0.0
115-
W[~mask] = 0.5 * (1.0 - np.cos(2.0 * np.pi * (R[~mask] + int(N / 2)) / N))
114+
W[~mask] = 0.5 * (1.0 - np.cos(2.0 * np.pi * (R[~mask] + 0.5)))
116115

117116
return W
118117

119118

120119
def _tukey(R, alpha):
121120
W = np.ones_like(R)
122-
N = min(R.shape[0], R.shape[1])
123121

124-
mask1 = R < int(N / 2)
125-
mask2 = R > int(N / 2) * (1.0 - alpha)
122+
mask1 = R < 0.5
123+
mask2 = R > 0.5 * (1.0 - alpha)
126124
mask = np.logical_and(mask1, mask2)
127125
W[mask] = 0.5 * (
128-
1.0 + np.cos(np.pi * (R[mask] / (alpha * 0.5 * N) - 1.0 / alpha + 1.0))
126+
1.0 + np.cos(np.pi * (R[mask] / (alpha * 0.5) - 1.0 / alpha + 1.0))
129127
)
130-
mask = R >= int(N / 2)
128+
mask = R >= 0.5
131129
W[mask] = 0.0
132130

133131
return W

0 commit comments

Comments
 (0)