From b871d1d492297aafb6f752bc4debdb044ae97489 Mon Sep 17 00:00:00 2001
From: Delaigue Olivier <olivier.delaigue@irstea.priv>
Date: Fri, 7 Feb 2020 10:00:35 +0000
Subject: [PATCH] v0.2.8.15 NEW: PrepGR now runs if all Qobs are NA #8 CalGR:
 stop SimGR: warning about the absence of efficiency criterion

---
 DESCRIPTION |  2 +-
 NEWS.md     |  2 +-
 R/CalGR.R   |  5 +++++
 R/PrepGR.R  |  8 +++++++-
 R/SimGR.R   | 26 +++++++++++++++++++-------
 5 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 00dc32b..14ce1ab 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: airGRteaching
 Type: Package
 Title: Teaching Hydrological Modelling with the GR Rainfall-Runoff Models ('Shiny' Interface Included)
-Version: 0.2.8.14
+Version: 0.2.8.15
 Date: 2020-02-07
 Authors@R: c(
   person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"),
diff --git a/NEWS.md b/NEWS.md
index 7f37b7a..2739272 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -4,7 +4,7 @@
 
 
 
-### 0.2.8.14 Release Notes (2020-02-07)
+### 0.2.8.15 Release Notes (2020-02-07)
 
 
 #### New features
diff --git a/R/CalGR.R b/R/CalGR.R
index aa66785..915492b 100644
--- a/R/CalGR.R
+++ b/R/CalGR.R
@@ -5,6 +5,11 @@ CalGR <- function(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"),
     stop("Non convenient data for argument \"PrepGR\". Must be of class \"PrepGR\"")
   }
   
+  isQobs <- !all(is.na(PrepGR$Qobs))
+  if (!isQobs) {
+    stop("\"PrepGR\" does not contain any Qobs values. It is not possible to calibrate the model")
+  }
+  
   WupInd <- NULL
   if (!is.null(WupPer)) {
     WupPer <- as.POSIXct(WupPer, tz = "UTC")
diff --git a/R/PrepGR.R b/R/PrepGR.R
index 08d9e82..52506ec 100644
--- a/R/PrepGR.R
+++ b/R/PrepGR.R
@@ -3,7 +3,7 @@ PrepGR <- function(ObsDF = NULL, DatesR = NULL, Precip = NULL, PotEvap = NULL, Q
                    HydroModel, CemaNeige = FALSE) {
   
   
-  if (is.null(ObsDF) && (is.null(DatesR) | is.null(Precip) | is.null(PotEvap) | is.null(Qobs))) {
+  if (is.null(ObsDF) && (is.null(DatesR) | is.null(Precip) | is.null(PotEvap))) {
     stop("Missing input data")
   }
 
@@ -13,6 +13,12 @@ PrepGR <- function(ObsDF = NULL, DatesR = NULL, Precip = NULL, PotEvap = NULL, Q
     }
   }
   
+  if (!is.null(Qobs)) {
+    Qobs <- Qobs
+  } else {
+    Qobs <- NA
+  }
+  
   if (!is.null(TempMean)) {
     TempMean <- TempMean
   } else {
diff --git a/R/SimGR.R b/R/SimGR.R
index a4fb3d8..f940b83 100644
--- a/R/SimGR.R
+++ b/R/SimGR.R
@@ -5,10 +5,14 @@ SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2",
     stop("Non convenient data for argument \"PrepGR\". Must be of class \"PrepGR\"")
   }
   
+  isQobs <- !all(is.na(PrepGR$Qobs))
+  if (!isQobs) {
+    warning("\"PrepGR\" does not contain any Qobs values. The efficiency criterion is not computed")
+  }
+  
   if (!missing(CalGR)) {
     warning("Deprecated \"CalGR\" argument. Use \"Param\" instead")
   }
-  
   ### to remove when the CalGR will be removed
   if (missing(Param)) {
     Param <- NULL
@@ -19,12 +23,10 @@ SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2",
   if (is.null(CalGR) & is.null(Param)) {
     stop("Arguments \"CalGR\" and \"Param\" are missing, with no default. You must fill in one of these two arguments")
   }
-
   if (is.null(Param)) {
     Param <- CalGR$OutputsCalib$ParamFinalR
   }
   ###
-  
   if (inherits(Param, "CalGR")) {
     Param <- Param$OutputsCalib$ParamFinalR
   }
@@ -72,22 +74,32 @@ SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2",
     stop("Non convenient transformation \"transfo\"")
   } else {
     transfo <- transfo[1L]
-  }  
+  }
+
+  
   
   MOD_opt <- CreateRunOptions(FUN_MOD = get(PrepGR$TypeModel), InputsModel = PrepGR$InputsModel, 
                               IndPeriod_WarmUp = WupInd, IndPeriod_Run = SimInd, verbose = verbose)
 
   
+  if (isQobs) {
   MOD_crt <- CreateInputsCrit(FUN_CRIT = FUN_CRIT, InputsModel = PrepGR$InputsModel, 
-                              RunOptions = MOD_opt, Obs = PrepGR$Qobs[SimInd], transfo = transfo)  
+                              RunOptions = MOD_opt, Obs = PrepGR$Qobs[SimInd], transfo = transfo)
+  } else {
+    MOD_crt <- NULL
+  }
   
   
   SIM <- RunModel(InputsModel = PrepGR$InputsModel, RunOptions = MOD_opt, 
                   Param = Param, FUN_MOD = get(PrepGR$TypeModel))
   
   
-  CRT <- ErrorCrit(InputsCrit = MOD_crt, OutputsModel = SIM, verbose = verbose)
-
+  if (isQobs) {
+    CRT <- ErrorCrit(InputsCrit = MOD_crt, OutputsModel = SIM, verbose = verbose)
+  } else {
+    CRT <- NULL
+  }
+  
   
   SimGR <- list(OptionsSimul = MOD_opt, OptionsCrit = MOD_crt, OutputsModel = SIM, Qobs = PrepGR$Qobs[SimInd],
                 TypeModel = PrepGR$TypeModel,
-- 
GitLab