Skip to content

Commit d9648c4

Browse files
authored
Extrapolation fixes (#290)
* Fix incorrect replacement of nan values with zeros when interp_order>1 * Remove unnecessary encoding line * Set allow_nonfinite_values automatically based on the input data
1 parent 49cc4f5 commit d9648c4

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

pysteps/extrapolation/semilagrangian.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
pysteps.extrapolation.semilagrangian
43
====================================
@@ -152,6 +151,8 @@ def extrapolate(
152151
precip = precip.copy()
153152
precip[~mask_finite] = 0.0
154153
mask_finite = mask_finite.astype(float)
154+
else:
155+
mask_finite = np.ones(precip.shape)
155156

156157
prefilter = True if interp_order > 1 else False
157158

@@ -241,16 +242,15 @@ def interpolate_motion(displacement, velocity_inc, td):
241242
)
242243
precip_warped[mask_warped < 0.5] = minval
243244

244-
if allow_nonfinite_values:
245-
mask_warped = ip.map_coordinates(
246-
mask_finite,
247-
coords_warped,
248-
mode=map_coordinates_mode,
249-
cval=0,
250-
order=1,
251-
prefilter=False,
252-
)
253-
precip_warped[mask_warped < 0.5] = np.nan
245+
mask_warped = ip.map_coordinates(
246+
mask_finite,
247+
coords_warped,
248+
mode=map_coordinates_mode,
249+
cval=0,
250+
order=1,
251+
prefilter=False,
252+
)
253+
precip_warped[mask_warped < 0.5] = np.nan
254254

255255
precip_extrap.append(np.reshape(precip_warped, precip.shape))
256256

pysteps/nowcasts/anvil.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ def forecast(
196196

197197
# transform the input fields to Lagrangian coordinates by extrapolation
198198
extrapolator = extrapolation.get_method(extrap_method)
199+
extrap_kwargs["allow_nonfinite_values"] = (
200+
True if np.any(~np.isfinite(vil)) else False
201+
)
202+
199203
res = list()
200204

201205
def worker(vil, i):
@@ -205,7 +209,6 @@ def worker(vil, i):
205209
vil[i, :],
206210
velocity,
207211
vil.shape[0] - 1 - i,
208-
allow_nonfinite_values=True,
209212
**extrap_kwargs,
210213
)[-1],
211214
)
@@ -364,12 +367,16 @@ def worker(vil, i):
364367
r_f_ip = r_f_prev
365368

366369
t_diff_prev = t_sub - t_prev
370+
367371
extrap_kwargs["displacement_prev"] = dp
372+
extrap_kwargs["allow_nonfinite_values"] = (
373+
True if np.any(~np.isfinite(r_f_ip)) else False
374+
)
375+
368376
r_f_ep, dp = extrapolator(
369377
r_f_ip,
370378
velocity,
371379
[t_diff_prev],
372-
allow_nonfinite_values=True,
373380
**extrap_kwargs,
374381
)
375382
r_f.append(r_f_ep[0])
@@ -384,7 +391,6 @@ def worker(vil, i):
384391
None,
385392
velocity,
386393
[t_diff_prev],
387-
allow_nonfinite_values=True,
388394
**extrap_kwargs,
389395
)
390396
t_prev = t + 1

pysteps/nowcasts/extrapolation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import time
15+
import numpy as np
1516

1617
from pysteps import extrapolation
1718

@@ -73,6 +74,10 @@ def forecast(
7374
else:
7475
extrap_kwargs = extrap_kwargs.copy()
7576

77+
extrap_kwargs["allow_nonfinite_values"] = (
78+
True if np.any(~np.isfinite(precip)) else False
79+
)
80+
7681
if measure_time:
7782
print(
7883
"Computing extrapolation nowcast from a "

pysteps/nowcasts/linda.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ def forecast(
207207
feature_kwargs = dict()
208208
if extrap_kwargs is None:
209209
extrap_kwargs = dict()
210+
else:
211+
extrap_kwargs = extrap_kwargs.copy()
210212

211213
if localization_window_radius is None:
212214
localization_window_radius = 0.2 * np.min(precip_fields.shape[1:])
@@ -276,6 +278,10 @@ def forecast(
276278
vel_pert_kwargs["vp_par"] = vp_par
277279
vel_pert_kwargs["vp_perp"] = vp_perp
278280

281+
extrap_kwargs["allow_nonfinite_values"] = (
282+
True if np.any(~np.isfinite(precip_fields)) else False
283+
)
284+
279285
fct_gen = _linda_deterministic_init(
280286
precip_fields,
281287
advection_field,

pysteps/nowcasts/sprog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def forecast(
214214

215215
extrap_kwargs = extrap_kwargs.copy()
216216
extrap_kwargs["xy_coords"] = xy_coords
217-
extrap_kwargs["allow_nonfinite_values"] = True
217+
extrap_kwargs["allow_nonfinite_values"] = True if np.any(~np.isfinite(R)) else False
218218

219219
# advect the previous precipitation fields to the same position with the
220220
# most recent one (i.e. transform them into the Lagrangian coordinates)

pysteps/nowcasts/sseps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ def forecast(
316316
R = R[-(ar_order + 1) :, :, :].copy()
317317
extrap_kwargs = extrap_kwargs.copy()
318318
extrap_kwargs["xy_coords"] = xy_coords
319+
extrap_kwargs["allow_nonfinite_values"] = True if np.any(~np.isfinite(R)) else False
320+
319321
res = []
320322
f = lambda R, i: extrapolator_method(
321323
R[i, :, :], V, ar_order - i, "min", **extrap_kwargs

pysteps/nowcasts/steps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def forecast(
402402
# most recent one (i.e. transform them into the Lagrangian coordinates)
403403
extrap_kwargs = extrap_kwargs.copy()
404404
extrap_kwargs["xy_coords"] = xy_coords
405-
extrap_kwargs["allow_nonfinite_values"] = True
405+
extrap_kwargs["allow_nonfinite_values"] = True if np.any(~np.isfinite(R)) else False
406+
406407
res = list()
407408

408409
def f(R, i):

0 commit comments

Comments
 (0)