diff --git a/DESCRIPTION b/DESCRIPTION index cf417719d8fb14f5d03bf55239ba8e09750246b3..f5abe9b20009f94e6e8608815c1f8aa7fae8da3e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,7 +7,7 @@ Authors@R: c( person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"), person("Laurent", "Coron", role = c("aut"), comment = c(ORCID = "0000-0002-1503-6204")), person("Pierre", "Brigode", role = c("aut"), comment = c(ORCID = "0000-0001-8257-0741")), - person("Guillaume", "Thirel", role = c("ctb"), comment = c(ORCID = "0000-0002-1444-1830")) + person("Guillaume", "Thirel", role = c("aut"), comment = c(ORCID = "0000-0002-1444-1830")) ) Depends: R (>= 3.6.0), diff --git a/R/CalGR.R b/R/CalGR.R index 2ab26dc98869be0a9995d0c7a3b6cec8f9c6018b..04e8e0a5eb865aee84192428aa12ea9a4252f941 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 } } @@ -55,7 +59,7 @@ CalGR <- function(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"), MOD_opt <- CreateRunOptions(FUN_MOD = get(PrepGR$TypeModel), InputsModel = PrepGR$InputsModel, - IndPeriod_WarmUp = WupInd, IndPeriod_Run = CalInd, verbose = FALSE) + IndPeriod_WarmUp = WupInd, IndPeriod_Run = CalInd, verbose = TRUE) MOD_crt <- CreateInputsCrit(FUN_CRIT = FUN_CRIT, InputsModel = PrepGR$InputsModel, diff --git a/R/SimGR.R b/R/SimGR.R index fe9183a042722167ac9736a64ad3d796cb59192a..f5d2fc7ffd860f660eb144ee08c7166fb27a6b6e 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 } } diff --git a/man/CalGR.Rd b/man/CalGR.Rd index 953086d31fde277104ffe31ac35b89fce0fea54c..d511b14d766b1e441d8313b23a1474b456447997 100644 --- a/man/CalGR.Rd +++ b/man/CalGR.Rd @@ -21,7 +21,7 @@ CalGR(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"), \item{CalCrit}{[character] name of the objective function (must be one of \code{"NSE"}, \code{"KGE"}, \code{"KGE2"} or \code{"RMSE"})} - \item{WupPer}{(optional) [character] vector of 2 values to define the beginning and end of the warm-up period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]. See details} + \item{WupPer}{(optional) [character] vector of 2 values to define the beginning and end of the warm-up period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]; [0L] to disable the warm-up period. See details} \item{CalPer}{[character] vector of 2 values to define the beginning and end of the calibration period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]} @@ -53,11 +53,13 @@ airGR functions } + \details{ -\code{WupPer = NULL} indicates that, if available, a period of one year immediately present before the \code{CalPer} period is used. In order to disable the warm up of the model, users have to make sure not to provide data before \code{CalPer} when preparing the data with \code{\link{PrepGR}}. +\code{WupPer = NULL} indicates that, if available, a period of one year immediately present before the CalPer period is used. \code{WupPer = 0L} allows to disable the warm up of the model. } -\author{Olivier Delaigue} + +\author{Olivier Delaigue, Guillaume Thirel} \examples{ diff --git a/man/SimGR.Rd b/man/SimGR.Rd index 03a5e48d6aee07f96b9d0b52acf2ec196f254bbb..c4a40a6516fc042606226c89d31a03295de6251d 100644 --- a/man/SimGR.Rd +++ b/man/SimGR.Rd @@ -25,7 +25,7 @@ SimGR(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2", "RMSE"), \item{EffCrit}{[character] name of the efficiency criterion (must be one of \code{"NSE"}, \code{"KGE"}, \code{"KGE2"} or \code{"RMSE"})} - \item{WupPer}{(optional) [character] vector of 2 values to define the beginning and end of the warm-up period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]} + \item{WupPer}{(optional) [character] vector of 2 values to define the beginning and end of the warm-up period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]; [0L] to disable the warm-up period. See details} \item{SimPer}{[character] vector of 2 values to define the beginning and end of the simulation period [\code{"YYYY-mm-dd"} or \code{"YYYY-mm-dd HH:MM:SS"}]} @@ -63,9 +63,12 @@ functions \details{ The user can customize the parameters with the \code{Param} argument. The user can also use the parameters resulting from a calibration. In this case, it is necessary to use the \code{\link{CalGR}} function. +\cr\cr +\code{WupPer = NULL} indicates that, if available, a period of one year immediately present before the CalPer period is used. \code{WupPer = 0L} allows to disable the warm up of the model. } -\author{Olivier Delaigue} + +\author{Olivier Delaigue, Guillaume Thirel} \examples{ diff --git a/tests/testthat/test-SimGR.R b/tests/testthat/test-SimGR.R index 9f0ab70b7d338168a13404bad68b9979a5a4aacd..7bf71ba4c819809a1e40cde8116138d6361f71eb 100644 --- a/tests/testthat/test-SimGR.R +++ b/tests/testthat/test-SimGR.R @@ -4,10 +4,12 @@ context("SimGR") data(L0123001, package = "airGR") -BasinObsSubNA <- BasinObs[, c("DatesR", "P", "E", "Qmm", "T")] +BasinObsUsual <- BasinObs[, c("DatesR", "P", "E", "Qmm", "T")] +BasinObsSubNA <- BasinObsUsual BasinObsSubNA[BasinObsSubNA$DatesR >= "1994-01-01", ]$Qmm <- NA BasinObsTotNA <- BasinObsSubNA[BasinObsSubNA$DatesR >= "1994-01-01", ] +PrepUsual <- PrepGR(ObsDF = BasinObsUsual, HydroModel = "GR4J", CemaNeige = FALSE) PrepSubNA <- PrepGR(ObsDF = BasinObsSubNA, HydroModel = "GR4J", CemaNeige = FALSE) PrepTotNA <- PrepGR(ObsDF = BasinObsTotNA, HydroModel = "GR4J", CemaNeige = FALSE) @@ -18,7 +20,7 @@ test_that("NA on a sub-period", { object = SimGR(PrepGR = PrepSubNA, Param = c(270.426, 0.984, 108.853, 2.149), EffCrit = "KGE2", WupPer = c("1994-01-01", "1994-12-31"), SimPer = c("1995-01-01", "1998-12-31")) , - expected = "\"PrepGR\" does not contain any Qobs values on \"SimPer\". The efficiency criterion is not computed", + regexp = "\"PrepGR\" does not contain any Qobs values on \"SimPer\". The efficiency criterion is not computed", ) }) @@ -29,6 +31,17 @@ test_that("NA on the total period", { object = SimGR(PrepGR = PrepTotNA, Param = c(270.426, 0.984, 108.853, 2.149), EffCrit = "KGE2", WupPer = c("1994-01-01", "1994-12-31"), SimPer = c("1995-01-01", "1998-12-31")) , - expected = "\"PrepGR\" does not contain any Qobs values. The efficiency criterion is not computed", + regexp = "\"PrepGR\" does not contain any Qobs values. The efficiency criterion is not computed", + ) +}) + +# ---- + +test_that("Diasle warm-up period", { + expect_message( + object = SimGR(PrepGR = PrepUsual, Param = c(270.426, 0.984, 108.853, 2.149), EffCrit = "KGE2", + WupPer = 0L, SimPer = c("1995-01-01", "1998-12-31")) + , + regexp = "No warm up period is used", ) })