From af58cfa65f83eb253efd95ef542c95fe6a99ceca Mon Sep 17 00:00:00 2001
From: Delaigue Olivier <olivier.delaigue@irstea.fr>
Date: Thu, 28 Jul 2022 10:54:25 +0200
Subject: [PATCH] feat: allow 'WupPer' to be equal to 0L in 'SimGR' and 'CalGR'
 Refs: #52

---
 R/CalGR.R | 24 ++++++++++++++----------
 R/SimGR.R | 24 ++++++++++++++----------
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/R/CalGR.R b/R/CalGR.R
index 2ab26dc..f9ac28b 100644
--- a/R/CalGR.R
+++ b/R/CalGR.R
@@ -23,18 +23,22 @@ CalGR <- function(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"),
 
   WupInd <- NULL
   if (!is.null(WupPer)) {
-    WupPer <- as.POSIXct(WupPer, tz = "UTC")
-    if (length(WupPer) != 2) {
-      stop("Warm-up period \"WupPer\" must be of length 2")
-    }
-    if (any(is.na(WupPer))) {
-      stop("Non convenient date format for the warm-up period \"WupPer\"")
-    } else {
-      if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
-        stop("Non convenient date for the warm-up period \"WupPer\"")
+    if (!identical(WupPer, 0L)){
+      WupPer <- as.POSIXct(WupPer, tz = "UTC")
+      if (length(WupPer) != 2) {
+        stop("Warm-up period \"WupPer\" must be of length 2")
+      }
+      if (any(is.na(WupPer))) {
+        stop("Non convenient date format for the warm-up period \"WupPer\"")
       } else {
-        WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
+        if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
+          stop("Non convenient date for the warm-up period \"WupPer\"")
+        } else {
+          WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
+        }
       }
+    } else {
+      WupInd <- 0L
     }
   }
 
diff --git a/R/SimGR.R b/R/SimGR.R
index fe9183a..f5d2fc7 100644
--- a/R/SimGR.R
+++ b/R/SimGR.R
@@ -39,18 +39,22 @@ SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2",
 
   WupInd <- NULL
   if (!is.null(WupPer)) {
-    WupPer <- as.POSIXct(WupPer, tz = "UTC")
-    if (length(WupPer) != 2) {
-      stop("Warm-up period \"WupPer\" must be of length 2")
-    }
-    if (any(is.na(WupPer))) {
-      stop("Non convenient date format for the warm-up period \"WupPer\"")
-    } else {
-      if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
-        stop("Non convenient date for the warm-up period \"WupPer\"")
+    if (!identical(WupPer, 0L)){
+      WupPer <- as.POSIXct(WupPer, tz = "UTC")
+      if (length(WupPer) != 2) {
+        stop("Warm-up period \"WupPer\" must be of length 2")
+      }
+      if (any(is.na(WupPer))) {
+        stop("Non convenient date format for the warm-up period \"WupPer\"")
       } else {
-        WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
+        if (!any(PrepGR$InputsModel$DatesR == WupPer[1]) | !any(PrepGR$InputsModel$DatesR == WupPer[2])) {
+          stop("Non convenient date for the warm-up period \"WupPer\"")
+        } else {
+          WupInd <- which(PrepGR$InputsModel$DatesR == WupPer[1]):which(PrepGR$InputsModel$DatesR == WupPer[2])
+        }
       }
+    } else {
+      WupInd <- 0L
     }
   }
 
-- 
GitLab