From 805e4a6af9271dec04e55417d7539b7c09841f40 Mon Sep 17 00:00:00 2001
From: Thibault Hallouin <thibault.hallouin@inrae.fr>
Date: Thu, 15 Jun 2023 16:03:35 +0200
Subject: [PATCH] fix bug in bootstrap sampling when period starts on Jan 1st

resolves https://gitlab.irstea.fr/HYCAR-Hydro/evalhyd/evalhyd-cpp/-/issues/8
---
 changelog.rst                          |  4 +++-
 include/evalhyd/detail/uncertainty.hpp | 13 +++++++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/changelog.rst b/changelog.rst
index 0ba34c1..77a0200 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -9,7 +9,7 @@ Yet to be versioned and released. Only available from *dev* branch until then.
 
 * remove `"WSS"` and `"AWI"` as probabilistic evaluation metric because of the
   arbitrary nature of using the sample climatology as reference/benchmark
-  (`#7-CPP <https://gitlab.irstea.fr/HYCAR-Hydro/evalhyd/evalhyd-cpp/-/issues/7>`_)
+  (`CPP#7 <https://gitlab.irstea.fr/HYCAR-Hydro/evalhyd/evalhyd-cpp/-/issues/7>`_)
 
 .. rubric:: Bug fixes
 
@@ -21,6 +21,8 @@ Yet to be versioned and released. Only available from *dev* branch until then.
   to the presence of square brackets in the regular expressions that do not seem
   supported
   (`R#6 <https://gitlab.irstea.fr/HYCAR-Hydro/evalhyd/evalhyd-r/-/issues/6>`_)
+* fix bug when using calendar year as block for the bootstrapping functionality
+  (`CPP#8 <https://gitlab.irstea.fr/HYCAR-Hydro/evalhyd/evalhyd-cpp/-/issues/8>`_)
 
 v0.1.0
 ------
diff --git a/include/evalhyd/detail/uncertainty.hpp b/include/evalhyd/detail/uncertainty.hpp
index 25b13af..af55fc2 100644
--- a/include/evalhyd/detail/uncertainty.hpp
+++ b/include/evalhyd/detail/uncertainty.hpp
@@ -76,7 +76,16 @@ namespace evalhyd
             int start_yr = v_tm.front().tm_year + 1900;
             int end_yr = v_tm.back().tm_year + 1900;
 
-            // assume start of year block as start of time series
+            // deal with special case with a start on 1st of January
+            // (note: use string rather than *tm_yday* member of time_point
+            //  because *tm_yday* is not set when using `std::get_time`)
+            if (datetimes[0].substr(5, 5) == "01-01")
+            {
+                // add one year to make sure last year is included in loop
+                end_yr += 1;
+            }
+
+            // take start of year block as start of time series
             std::tm start_hy = v_tm.front();
 
             xt::xtensor<int, 1> year_blocks = xt::zeros<int>({v_tm.size()});
@@ -100,7 +109,7 @@ namespace evalhyd
                 if ((n_days != 365) && (n_days != 366))
                 {
                     throw std::runtime_error(
-                            "year starting in " + std::to_string(y)
+                            "year starting in " + std::to_string(y - 400)
                             + " is incomplete"
                     );
                 }
-- 
GitLab