diff --git a/DESCRIPTION b/DESCRIPTION index a6ceb5eb4245b0f6fcbb6b627bb43c3e27ff7d73..bb56081c41492df3752ffdbfd7ff6cf2a51a4e27 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: airGR Type: Package Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling -Version: 1.6.8.19 +Version: 1.6.8.20 Date: 2020-12-01 Authors@R: c( person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")), diff --git a/NAMESPACE b/NAMESPACE index 7119024b0009bfe35e663a2b7907c5cf1d112a5a..698087fb5d45f0f0e19edf8f7c31168af3373ba7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -65,6 +65,7 @@ export(TransfoParam_Lag) export(plot) export(plot.OutputsModel) export(.ErrorCrit) +export(.AggregConvertFunTable) diff --git a/NEWS.md b/NEWS.md index 83b55d04b2bd5444030795f48012588db2eced46..84a2383dae2cea6b48911a22aae1e975de65f2ee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,12 +2,13 @@ -### 1.6.8.10 Release Notes (2020-11-21) +### 1.6.8.20 Release Notes (2020-12-01) #### New features - Added <code>SeriesAggreg</code> S3 method with functions for `InputsModel`, `OutputsModel`, `list`, `data.frame` class objects. This new version of the <code>SeriesAggreg()</code> function allows to compute regimes. -- Added<code>.AggregConvertFun()</code> private function in order to choose automatically the <code>ConvertFun</code> to apply on each element of <code>InputsModel</code> and <code>OutputsModel</code> objects. +- Added <code>getAggregConvertFun()</code> private function in order to choose automatically the <code>ConvertFun</code> to apply on each element of objects used in <code>SeriesAggreg</code>. +- Added <code>.AggregConvertFunTable</code> data.frame that allows the user to see what names of list items or data.frame column names are guessed and eventually customise this correspondence table. #### Bug fixes diff --git a/R/SeriesAggreg.data.frame.R b/R/SeriesAggreg.data.frame.R index 502a2ba7a7b736f70b93021885d58148a64e4bd9..1b4c102035a213f18dc24f8c4b19d71fd4869192 100644 --- a/R/SeriesAggreg.data.frame.R +++ b/R/SeriesAggreg.data.frame.R @@ -1,6 +1,6 @@ SeriesAggreg.data.frame <- function(TabSeries, Format, - ConvertFun = .AggregConvertFun(names(TabSeries)[-1]), + ConvertFun = getAggregConvertFun(names(TabSeries)[-1]), TimeFormat = NULL, NewTimeFormat = NULL, YearFirstMonth = 1, diff --git a/R/SeriesAggreg.list.R b/R/SeriesAggreg.list.R index 932840e73cc09912dc8bd9e84e6f5e4cd25bfa9e..806736634f1206c95a14370eb3ed034eb8ad7f71 100644 --- a/R/SeriesAggreg.list.R +++ b/R/SeriesAggreg.list.R @@ -68,7 +68,7 @@ SeriesAggreg.list <- function(TabSeries, } dfOut <- NULL if (length(cols)) { - ConvertFun2 <- .AggregConvertFun(names(cols)) + ConvertFun2 <- getAggregConvertFun(names(cols)) if (is.null(recursive)) { if (missing(ConvertFun)) { stop("'ConvertFun' argument should provided if 'recursive = NULL'") @@ -104,7 +104,7 @@ SeriesAggreg.list <- function(TabSeries, listCols <- listCols[setdiff(names(listCols), names(dfCols))] if (length(listCols) > 0) { # Check for predefined ConvertFun for all sub-elements - ConvertFun <- .AggregConvertFun(names(listCols)) + ConvertFun <- getAggregConvertFun(names(listCols)) # Run SeriesAggreg for each embedded list listRes <- lapply(names(listCols), @@ -143,7 +143,7 @@ SeriesAggreg.list <- function(TabSeries, "), it will be ignored in the aggregation" ) } else { - ConvertFun <- rep(.AggregConvertFun(key), ncol(m)) + ConvertFun <- rep(getAggregConvertFun(key), ncol(m)) res[[key]] <- SeriesAggreg(data.frame(DatesR, m), Format = Format, ConvertFun = ConvertFun) diff --git a/R/Utils.R b/R/Utils.R index 23eae05ef30aeb65e19164ce8ca7b3c9f62d475d..d1cde5eb0dc10d9f3f686ddf03dab9c24671d850 100644 --- a/R/Utils.R +++ b/R/Utils.R @@ -11,32 +11,6 @@ # } # } - - -## ================================================================================= -## function to manage Fortran outputs -## ================================================================================= - -.AggregConvertFun <- function(Outputs) { - Table <- rbind( - data.frame(ConvertFun = "mean", - Outputs = c("Prod","Rout","Exp","SnowPack","ThermalState", - "Gratio","Temp","Gthreshold","Glocalmax","LayerTempMean", "T")), - data.frame(ConvertFun = "sum", - Outputs = c("zzz","PotEvap","Precip","Pn","Ps","AE","Perc","PR","Q9", - "Q1","Exch","AExch1","AExch2","AExch","QR","QRExp", - "QD","Qsim","Pliq","Psol","PotMelt","Melt","PliqAndMelt", - "LayerPrecip","LayerFracSolidPrecip", "Qmm", "Qls", "E", "P", "Qupstream")) - ) - res <- sapply(Outputs, function(iOutputs) { - iRes <- Table$ConvertFun[Table$Outputs == iOutputs] - iRes <- ifelse(any(is.na(iRes)), NA, iRes) - }) - return(res) -} - - - ## ================================================================================= ## function to manage Fortran outputs ## ================================================================================= diff --git a/R/UtilsSeriesAggreg.R b/R/UtilsSeriesAggreg.R index 397cfd076df5bdf0df5e218c18230938c4582b94..de72c97487b10ba15adfb74aaf36e0e08c15a64f 100644 --- a/R/UtilsSeriesAggreg.R +++ b/R/UtilsSeriesAggreg.R @@ -40,4 +40,24 @@ getSeriesAggregClass <- function(Format) { m = "monthly", Y = "yearly" ) -} \ No newline at end of file +} + +.AggregConvertFunTable <- rbind( + data.frame(ConvertFun = "mean", + Outputs = c("Prod","Rout","Exp","SnowPack","ThermalState", + "Gratio","Temp","Gthreshold","Glocalmax","LayerTempMean", "T")), + data.frame(ConvertFun = "sum", + Outputs = c("zzz","PotEvap","Precip","Pn","Ps","AE","Perc","PR","Q9", + "Q1","Exch","AExch1","AExch2","AExch","QR","QRExp", + "QD","Qsim","Pliq","Psol","PotMelt","Melt","PliqAndMelt", + "LayerPrecip","LayerFracSolidPrecip", "Qmm", "Qls", "E", "P", "Qupstream")) + ) + +getAggregConvertFun <- function(Outputs) { + + res <- sapply(Outputs, function(iOutputs) { + iRes <- .AggregConvertFunTable$ConvertFun[.AggregConvertFunTable$Outputs == iOutputs] + iRes <- ifelse(any(is.na(iRes)), NA, iRes) + }) + return(res) +} diff --git a/man/SeriesAggreg.Rd b/man/SeriesAggreg.Rd index 156677816f5aece3523554bffe631d1ac7b20b7e..ff8f33da4fe35d00a6d1b4305e705981c4aa0184 100644 --- a/man/SeriesAggreg.Rd +++ b/man/SeriesAggreg.Rd @@ -24,13 +24,16 @@ Warning: on the aggregated outputs, the dates correspond to the beginning of the \code{\link{SeriesAggreg.InputsModel}} and \code{\link{SeriesAggreg.OutputsModel}} call \code{\link{SeriesAggreg.list}} which itself calls \code{\link{SeriesAggreg.data.frame}}. So, all arguments passed to any \code{\link{SeriesAggreg}} method will be passed to \code{\link{SeriesAggreg.data.frame}}. + + If \code{ConvertFun} is not provided, the operation to perform is guessed from the name of the list items or column + name dataframe in respect with the matching table \code{.AggregConvertFunTable}. } \usage{ \method{SeriesAggreg}{data.frame}(TabSeries, Format, - ConvertFun = .AggregConvertFun(names(TabSeries)[-1]), + ConvertFun = getAggregConvertFun(names(TabSeries)[-1]), TimeFormat = NULL, NewTimeFormat = NULL, YearFirstMonth = 1, @@ -61,7 +64,7 @@ Warning: on the aggregated outputs, the dates correspond to the beginning of the \item{NewTimeFormat}{(deprecated) [character] output time step format (i.e. \code{"hourly"}, \code{"daily"}, \code{"monthly"} or \code{"yearly"}). Use the \code{TabSeries} argument instead} -\item{ConvertFun}{[character] names of aggregation functions (e.g. for P[mm], T[degC], Q[mm]: \code{ConvertFun = c("sum", "mean", "sum"})) (Default: use the name of the column or "mean" for regime calculation)} +\item{ConvertFun}{[character] names of aggregation functions (e.g. for P[mm], T[degC], Q[mm]: \code{ConvertFun = c("sum", "mean", "sum"})) (Default: use the name of the column (See details) or "mean" for regime calculation)} \item{YearFirstMonth}{(optional) [numeric] integer used when \code{Format = "\%Y"} to set when the starting month of the year (e.g. 01 for calendar year or 09 for hydrological year starting in September)}