From deb106d75551a1814ad44388b8c1a58c4ebe107a Mon Sep 17 00:00:00 2001
From: Delaigue Olivier <olivier.delaigue@irstea.fr>
Date: Wed, 4 Aug 2021 15:28:33 +0200
Subject: [PATCH] refactor(PE_Oudin): add an error message when TimeStepIn =
 "hourly" and length(Temp) != length(idJD) Refs #134

---
 R/PE_Oudin.R | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/R/PE_Oudin.R b/R/PE_Oudin.R
index 7b748c54..e8f56053 100644
--- a/R/PE_Oudin.R
+++ b/R/PE_Oudin.R
@@ -41,11 +41,10 @@ PE_Oudin <- function(JD, Temp,
   TimeStepIn  <- match.arg(TimeStepIn , choices = TimeStep)
   TimeStepOut <- match.arg(TimeStepOut, choices = TimeStep)
   rleJD <- rle(JD)
+  msgDaliy <- "each day should have only one identical value of julian days. The time series is not sorted, or contains duplicate or missing dates"
+  msgHourly <- "each day must have 24 identical values of julian days (one for each hour). The time series is not sorted, or contains duplicate or missing dates"
   if (TimeStepIn == "daily" & any(rleJD$lengths != 1)) {
-    warning("each day should have only one identical value of julian days. The time series is not sorted, or contains duplicate or missing dates")
-  }
-  if (TimeStepIn == "hourly" & any(rleJD$lengths != 24)) {
-    warning("each day must have 24 identical values of julian days (one for each hour). The time series is not sorted, or contains duplicate or missing dates")
+    warning(msgDaliy)
   }
 
 
@@ -54,7 +53,14 @@ PE_Oudin <- function(JD, Temp,
   if (TimeStepIn == "hourly") {
     JD <- rleJD$values
     idJD <- rep(seq_along(JD), each = rleJD$lengths[1L])
-    Temp <- as.vector(tapply(X = Temp, INDEX = idJD, FUN = mean))
+    if (length(Temp) != length(idJD)) {
+      stop(msgHourly)
+    } else {
+      Temp <- as.vector(tapply(X = Temp, INDEX = idJD, FUN = mean))
+    }
+  }
+  if (TimeStepIn == "hourly" & any(rleJD$lengths != 24)) {
+    warning(msgHourly)
   }
 
 
-- 
GitLab