Skip to content

Commit fd2e665

Browse files
committed
Fix possible division by zero
1 parent 63cf198 commit fd2e665

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

pysteps/nowcasts/linda.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,15 @@ def worker(i):
793793
inv_acf_mapping = _compute_inverse_acf_mapping(stats.lognorm, distpar)
794794
mask_acf = weights_acf > 1e-4
795795
std = _weighted_std(fct_err[mask_acf], weights_dist[i][mask_acf])
796-
acf = inv_acf_mapping(
797-
_compute_sample_acf(weights_acf * (fct_err - 1.0) / std)
798-
)
799-
acf = _fit_acf(acf)
796+
if np.isfinite(std):
797+
acf = inv_acf_mapping(
798+
_compute_sample_acf(weights_acf * (fct_err - 1.0) / std)
799+
)
800+
acf = _fit_acf(acf)
801+
else:
802+
distpar = None
803+
std = None
804+
acf = None
800805
else:
801806
distpar = None
802807
std = None
@@ -1392,13 +1397,16 @@ def _masked_convolution(field, kernel):
13921397

13931398
def _weighted_std(f, w):
13941399
"""
1395-
Compute standard deviation of forecast errors with spatially varying weights
1396-
values close to zero are omitted.
1400+
Compute standard deviation of forecast errors with spatially varying weights.
1401+
Values close to zero are omitted.
13971402
"""
13981403
mask = np.abs(f - 1.0) > 1e-4
1399-
c = (w[mask].size - 1.0) / w[mask].size
1400-
1401-
return np.sqrt(np.sum(w[mask] * (f[mask] - 1.0) ** 2.0) / (c * np.sum(w[mask])))
1404+
n = np.count_nonzero(mask)
1405+
if n > 0:
1406+
c = (w[mask].size - 1.0) / n
1407+
return np.sqrt(np.sum(w[mask] * (f[mask] - 1.0) ** 2.0) / (c * np.sum(w[mask])))
1408+
else:
1409+
return np.nan
14021410

14031411

14041412
def _window_tukey(m, n, ci, cj, ri, rj, alpha=0.5):

0 commit comments

Comments
 (0)