diff --git a/DESCRIPTION b/DESCRIPTION index ad645118a7df4d145f5ceafd86e6f5d5b6eb4c8a..04cabb554a0dd48ccb41c9175ccb9b1f4b25cd43 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: airGRteaching Type: Package Title: Teaching Hydrological Modelling with the GR Rainfall-Runoff Models ('Shiny' Interface Included) Version: 0.2.11 -Date: 2021-06-04 +Date: 2021-06-07 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")), diff --git a/NAMESPACE b/NAMESPACE index fc9b636f333d0323aa1ea7a2744ef0bb69a79787..f8de05b04beeab2ea9bd049111483083dcb019c7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,9 @@ ##################################### ## S3 methods ## ##################################### -S3method(as.data.frame, airGRt) +S3method(as.data.frame, PrepGR) +S3method(as.data.frame, CalGR) +S3method(as.data.frame, SimGR) S3method(dyplot, default) S3method(dyplot, PrepGR) S3method(dyplot, CalGR) @@ -46,4 +48,4 @@ importFrom(shinyjs, useShinyjs) importFrom(shinyjs, enable) importFrom(shinyjs, disable) import(markdown) -importFrom(plotrix, boxed.labels) \ No newline at end of file +importFrom(plotrix, boxed.labels) diff --git a/NEWS.md b/NEWS.md index fd34ea356c9018827ca5807a95b627f4b0e37f19..e39804bbf2390907cbda4e8bd9ce8bfce7434fa6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,12 +4,18 @@ -### 0.2.11 Release Notes (2021-06-04) +### 0.2.11 Release Notes (2021-06-07) #### Bug fixes - Fix `ShinyGR()` when the date column of `ObsDF` has another name as 'DatesR'. ([#38](https://gitlab.irstea.fr/HYCAR-Hydro/airgrteaching/-/issues/38)) + +#### User-visible changes + +- The `as.data.frame` S3 methods have been set for classes `PrepGR`, `CalGR` and `SimGR`. ([#39](https://gitlab.irstea.fr/HYCAR-Hydro/airgrteaching/-/issues/39)) +- The useless `airGRt` class and the `as.data.frame.airGRt` S3 method no longer exist ([#39](https://gitlab.irstea.fr/HYCAR-Hydro/airgrteaching/-/issues/39)) + ____________________________________________________________________________________ @@ -23,7 +29,7 @@ ________________________________________________________________________________ #### Bug fixes -- Fix `plot.PrepGR()` when all `Qobs` are missing. The function now displays an empty plot for the observed discharges. ([#35](https://gitlab.irstea.fr/HYCAR-Hydro/airgrteaching/-/issues/35)) +- Fix `plot.PrepGR()` when all `Qobs` are missing. The function now displays an empty plot for the observed discharges ([#35](https://gitlab.irstea.fr/HYCAR-Hydro/airgrteaching/-/issues/35)) #### User-visible changes diff --git a/R/CalGR.R b/R/CalGR.R index 7814f20479f3333cf4f2f2c1c33a0b29698a661a..89ec11bd213b74540a8ee857f9896b7c75e5dd4f 100644 --- a/R/CalGR.R +++ b/R/CalGR.R @@ -80,7 +80,7 @@ CalGR <- function(PrepGR, CalCrit = c("NSE", "KGE", "KGE2", "RMSE"), TypeModel = PrepGR$TypeModel, CalCrit = CalCrit, PeriodModel = list(WarmUp = as.POSIXct(PrepGR$InputsModel$DatesR[range(MOD_opt$IndPeriod_WarmUp)], tz = "UTC"), Run = CalPer)) - class(CalGR) <- c("CalGR", "GR", "airGRt") + class(CalGR) <- c("CalGR", "GR") return(CalGR) } diff --git a/R/PrepGR.R b/R/PrepGR.R index 215c2f6c24328ed7e17c733046acfa1a2d6402bd..8947ae1b1ee4077906b183e09d67bc5ae281fef2 100644 --- a/R/PrepGR.R +++ b/R/PrepGR.R @@ -67,7 +67,7 @@ PrepGR <- function(ObsDF = NULL, DatesR = NULL, Precip = NULL, PotEvap = NULL, Q PrepGR <- list(InputsModel = MOD_obs, Qobs = ObsDF$Qobs, TypeModel = TypeModel) - class(PrepGR) <- c("PrepGR", "GR", "airGRt") + class(PrepGR) <- c("PrepGR", "GR") return(PrepGR) } diff --git a/R/SimGR.R b/R/SimGR.R index 60f20844c9c58f51fced8d23bf6fdfae498ac1e1..966183c5ad686a73fbaea65ca43cad00c49c432d 100644 --- a/R/SimGR.R +++ b/R/SimGR.R @@ -103,7 +103,7 @@ SimGR <- function(PrepGR, CalGR = NULL, Param, EffCrit = c("NSE", "KGE", "KGE2", CalCrit = CalGR$CalCrit, EffCrit = CRT, PeriodModel = list(WarmUp = as.POSIXct(PrepGR$InputsModel$DatesR[range(MOD_opt$IndPeriod_WarmUp)], tz = "UTC"), Run = SimPer)) - class(SimGR) <- c("SimGR", "GR", "airGRt") + class(SimGR) <- c("SimGR", "GR") return(SimGR) } diff --git a/R/as.data.frame.R b/R/as.data.frame.R new file mode 100644 index 0000000000000000000000000000000000000000..7a25ccd1e0f69c3c09dd161a4d2f1f3d9b04285e --- /dev/null +++ b/R/as.data.frame.R @@ -0,0 +1,81 @@ +as.data.frame.PrepGR <- function(x, row.names = NULL, ...) { + + # if (!(inherits(x, "PrepGR"))) { + # stop("'InputsCrit' must be of class 'PrepGR'") + # } + + TMGR <- .TypeModelGR(x) + myGR <- list() + + myGR$FracSolid <- NA + myGR$TempMean <- NA + + if (TMGR$CemaNeige) { + PrecipSol <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip) * as.data.frame(x$InputsModel$LayerFracSolidPrecip), na.rm = TRUE) + PrecipSim <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip), na.rm = TRUE) + FracSolid <- PrecipSol / PrecipSim + FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid) + myGR$FracSolid <- FracSolid + TempMean <- rowMeans(as.data.frame(x$InputsModel$LayerTempMean), na.rm = TRUE) + myGR$TempMean <- TempMean + } + + myGR$DatesR <- x$InputsModel$DatesR + myGR$PotEvap <- x$InputsModel$PotEvap + myGR$Precip <- x$InputsModel$Precip + myGR$Qobs <- x$Qobs + myGR$Qsim <- NA + + TabSim <- data.frame(Dates = myGR$DatesR, + PotEvap = myGR$PotEvap, + PrecipObs = myGR$Precip, + PrecipFracSolid_CemaNeige = myGR$FracSolid, + TempMeanSim_CemaNeige = myGR$TempMean, + Qobs = myGR$Qobs, + Qsim = myGR$Qsim) + as.data.frame(x = TabSim, row.names = row.names, ...) +} + + +as.data.frame.CalGR <- function(x, row.names = NULL, ...) { + + # if (inherits(x, "CalGR") | inherits(x, "SimGR"))) { + # stop("'InputsCrit' must be of class 'CalGR', 'SimGR'") + # } + + TMGR <- .TypeModelGR(x) + myGR <- list() + + myGR$FracSolid <- NA + myGR$TempMean <- NA + + if (TMGR$CemaNeige) { + PrecipSol <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Psol"), na.rm = TRUE) + PrecipSim <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Pliq"), na.rm = TRUE) + PrecipSol + FracSolid <- PrecipSol / PrecipSim + FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid) + myGR$FracSolid <- FracSolid + TempMean <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Temp"), na.rm = TRUE) + myGR$TempMean <- TempMean + } + + myGR$DatesR <- x$OutputsModel$DatesR + myGR$PotEvap <- x$OutputsModel$PotEvap + myGR$Precip <- x$OutputsModel$Precip + myGR$Qobs <- x$Qobs + myGR$Qsim <- x$OutputsModel$Qsim + + TabSim <- data.frame(Dates = myGR$DatesR, + PotEvap = myGR$PotEvap, + PrecipObs = myGR$Precip, + PrecipFracSolid_CemaNeige = myGR$FracSolid, + TempMeanSim_CemaNeige = myGR$TempMean, + Qobs = myGR$Qobs, + Qsim = myGR$Qsim) + as.data.frame(x = TabSim, row.names = row.names, ...) +} + + +as.data.frame.SimGR <- as.data.frame.CalGR + + diff --git a/R/as.data.frame.airGRt.R b/R/as.data.frame.airGRt.R deleted file mode 100644 index 26b19aee5a472450c427b69bde1a2cd5c38a91a5..0000000000000000000000000000000000000000 --- a/R/as.data.frame.airGRt.R +++ /dev/null @@ -1,55 +0,0 @@ -as.data.frame.airGRt <- function(x, row.names = NULL, ...) { - - if (!(inherits(x, "PrepGR") | inherits(x, "CalGR") | inherits(x, "SimGR"))) { - stop("'InputsCrit' must be of class 'PrepGR', 'CalGR', 'SimGR'") - } - - TMGR <- .TypeModelGR(x) - myGR <- list() - - myGR$FracSolid <- NA - myGR$TempMean <- NA - - if (inherits(x, "PrepGR")) { - if (TMGR$CemaNeige) { - PrecipSol <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip) * as.data.frame(x$InputsModel$LayerFracSolidPrecip), na.rm = TRUE) - PrecipSim <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip), na.rm = TRUE) - FracSolid <- PrecipSol / PrecipSim - FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid) - myGR$FracSolid <- FracSolid - TempMean <- rowMeans(as.data.frame(x$InputsModel$LayerTempMean), na.rm = TRUE) - myGR$TempMean <- TempMean - } - myGR$DatesR <- x$InputsModel$DatesR - myGR$PotEvap <- x$InputsModel$PotEvap - myGR$Precip <- x$InputsModel$Precip - myGR$Qobs <- x$Qobs - myGR$Qsim <- NA - } - if (inherits(x, "CalGR") | inherits(x, "SimGR")) { - if (TMGR$CemaNeige) { - PrecipSol <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Psol"), na.rm = TRUE) - PrecipSim <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Pliq"), na.rm = TRUE) + PrecipSol - FracSolid <- PrecipSol / PrecipSim - FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid) - myGR$FracSolid <- FracSolid - TempMean <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Temp"), na.rm = TRUE) - myGR$TempMean <- TempMean - } - myGR$DatesR <- x$OutputsModel$DatesR - myGR$PotEvap <- x$OutputsModel$PotEvap - myGR$Precip <- x$OutputsModel$Precip - myGR$Qobs <- x$Qobs - myGR$Qsim <- x$OutputsModel$Qsim - } -TabSim <- data.frame(Dates = myGR$DatesR, - PotEvap = myGR$PotEvap, - PrecipObs = myGR$Precip, - PrecipFracSolid_CemaNeige = myGR$FracSolid, - TempMeanSim_CemaNeige = myGR$TempMean, - Qobs = myGR$Qobs, - Qsim = myGR$Qsim) -as.data.frame(x = TabSim, row.names = row.names, ...) -} - - diff --git a/man/as.data.frame.airGRt.Rd b/man/as.data.frame.Rd similarity index 86% rename from man/as.data.frame.airGRt.Rd rename to man/as.data.frame.Rd index b147bd5103cef324628a4747b1851e1cccb81bb1..b50eeede7707ecc88b8524ae2fd7adfa6e671d44 100644 --- a/man/as.data.frame.airGRt.Rd +++ b/man/as.data.frame.Rd @@ -1,6 +1,8 @@ \encoding{UTF-8} \name{as.data.frame} -\alias{as.data.frame.airGRt} +\alias{as.data.frame.PrepGR} +\alias{as.data.frame.CalGR} +\alias{as.data.frame.SimGR} \alias{as.data.frame} @@ -11,12 +13,16 @@ \usage{ -\method{as.data.frame}{airGRt}(x, row.names = NULL, ...) +\method{as.data.frame}{PrepGR}(x, row.names = NULL, ...) + +\method{as.data.frame}{CalGR}(x, row.names = NULL, ...) + +\method{as.data.frame}{SimGR}(x, row.names = NULL, ...) } \arguments{ - \item{x}{[object of class \emph{airGRt}] typically an object of class \code{\link{PrepGR}}, \code{\link{CalGR}} or \code{\link{SimGR}}} + \item{x}{[\code{\link{PrepGR}}], [\code{\link{CalGR}}] or [\code{\link{SimGR}}] objects} \item{row.names}{\code{NULL} or a character vector giving the row names for the data.frame. Missing values are not allowed} \item{...}{additional arguments to be passed to or from methods} }