From a7256eaee0bb4a4ff5a91a10ef1816df3f53ff10 Mon Sep 17 00:00:00 2001
From: Thibault Hallouin <thibault.hallouin@inrae.fr>
Date: Wed, 25 May 2022 11:12:38 +0200
Subject: [PATCH] make threshold parameter optional

since it is not needed by metrics such as QS or CRPS
---
 include/evalhyd/probabilist.hpp |  7 +++++--
 include/evalhyd/utils.hpp       | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/include/evalhyd/probabilist.hpp b/include/evalhyd/probabilist.hpp
index 140d1a2..fe59365 100644
--- a/include/evalhyd/probabilist.hpp
+++ b/include/evalhyd/probabilist.hpp
@@ -36,15 +36,18 @@ namespace evalhyd
                 const std::vector<std::string>& metrics,
                 const xt::xtensor<double, 2>& q_obs,
                 const xt::xtensor<double, 2>& q_frc,
-                const xt::xtensor<double, 1>& q_thr
+                const xt::xtensor<double, 1>& q_thr = {}
         )
         {
             // check that the metrics to be computed are valid
-            utils::check_metrics(
+            eh::utils::check_metrics(
                     metrics,
                     {"BS", "BSS", "BS_CRD", "BS_LBD", "QS", "CRPS"}
             );
 
+            // check that optional parameters are given as arguments
+            eh::utils::check_optionals(metrics, q_thr);
+
             // instantiate probabilist evaluator
             eh::probabilist::Evaluator evaluator(q_obs, q_frc, q_thr);
 
diff --git a/include/evalhyd/utils.hpp b/include/evalhyd/utils.hpp
index 4caa7e5..d96b351 100644
--- a/include/evalhyd/utils.hpp
+++ b/include/evalhyd/utils.hpp
@@ -92,6 +92,32 @@ namespace evalhyd
             }
         }
 
+        /// Procedure to check that optional parameters are provided
+        /// as arguments when required metrics need them.
+        ///
+        /// \param [in] metrics:
+        ///     Vector of strings for the metric(s) to be computed.
+        /// \param [in] thresholds:
+        ///     Array of thresholds for metrics based on exceedance events.
+        inline void check_optionals (
+                const std::vector<std::string>& metrics,
+                const xt::xtensor<double, 1>& thresholds
+        )
+        {
+            std::vector<std::string>threshold_metrics =
+                    {"BS, BS_CRD", "BS_LBD", "BSS"};
+
+            for (const auto& metric : metrics)
+            {
+                if (std::find(threshold_metrics.begin(), threshold_metrics.end(),
+                              metric) != threshold_metrics.end())
+                    if (thresholds.size() < 1)
+                        throw std::runtime_error(
+                                "missing parameter 'thresholds' required to "
+                                "compute " + metric
+                        );
+            }
+        }
     }
 }
 
-- 
GitLab