From 0ca9b41a0e664776744dd6cb0b6c7b01d4cd252b Mon Sep 17 00:00:00 2001
From: Thibault Hallouin <thibault.hallouin@inrae.fr>
Date: Wed, 8 Feb 2023 11:29:48 +0100
Subject: [PATCH] catch empty observations after NaN filtering

when observations only contain NaNs, the resulting data subset is
empty, which makes the quantile calculation fail

this can happen when evalhyd is used on a period with no observations
(which may rest on the user responsibility), but this can also happen
when subperiods (i.e. via temporal masking) have no observations even
if there are observations in the overall period (which is less easy
for the user to anticipate)
---
 .../evalhyd/detail/probabilist/intervals.hpp  | 29 ++++++++++++-------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/include/evalhyd/detail/probabilist/intervals.hpp b/include/evalhyd/detail/probabilist/intervals.hpp
index 8d30246..05ee880 100644
--- a/include/evalhyd/detail/probabilist/intervals.hpp
+++ b/include/evalhyd/detail/probabilist/intervals.hpp
@@ -209,16 +209,25 @@ namespace evalhyd
                                     auto obs_filtered =
                                             xt::filter(obs, !xt::isnan(obs));
 
-                                    // lower bound
-                                    xt::view(clim_bnds, s, l, m, e, i, 0) =
-                                            xt::quantile(
-                                                    obs_filtered, {quantiles(i, 0)}
-                                            )();
-                                    // upper bound
-                                    xt::view(clim_bnds, s, l, m, e, i, 1) =
-                                            xt::quantile(
-                                                    obs_filtered, {quantiles(i, 1)}
-                                            )();
+                                    if (obs_filtered.size() > 0)
+                                    {
+                                        // lower bound
+                                        xt::view(clim_bnds, s, l, m, e, i, 0) =
+                                                xt::quantile(
+                                                        obs_filtered, {quantiles(i, 0)}
+                                                )();
+                                        // upper bound
+                                        xt::view(clim_bnds, s, l, m, e, i, 1) =
+                                                xt::quantile(
+                                                        obs_filtered, {quantiles(i, 1)}
+                                                )();
+                                    }
+                                    else
+                                    {
+                                        xt::view(clim_bnds, s, l, m, e, i, xt::all()) =
+                                                NAN;
+                                    }
+
                                 }
                             }
                         }
-- 
GitLab