Commit 8ef0f0de authored by Dorchies David's avatar Dorchies David
Browse files

fix: Single ConvertFun argument for list

- ConvertFun for InputsModel and OutputsModel are entirely defined into SeriesAggreg.list
- otherwise SeriesAggreg.list only accept a single ConvertFun to apply to all elements and sub-elements of the list

Refs #82
Showing with 47 additions and 22 deletions
+47 -22
SeriesAggreg.InputsModel <- function(x, Format, ...) { SeriesAggreg.InputsModel <- function(x, Format, ...) {
SeriesAggreg.list(x, SeriesAggreg.list(x,
Format, Format,
ConvertFun = .GetAggregConvertFun(names(x), Format), ConvertFun = NA,
except = c("ZLayers", "LengthHydro", "BasinAreas"), except = c("ZLayers", "LengthHydro", "BasinAreas"),
...) ...)
} }
SeriesAggreg.OutputsModel <- function(x, Format, ...) { SeriesAggreg.OutputsModel <- function(x, Format, ...) {
SeriesAggreg.list(x, SeriesAggreg.list(x,
Format, Format,
ConvertFun = .GetAggregConvertFun(names(x), Format), ConvertFun = NA,
except = "StateEnd", except = "StateEnd",
...) ...)
} }
...@@ -12,6 +12,16 @@ SeriesAggreg.list <- function(x, ...@@ -12,6 +12,16 @@ SeriesAggreg.list <- function(x,
warning("deprecated 'NewTimeFormat' argument: 'Format' argument is used instead", warning("deprecated 'NewTimeFormat' argument: 'Format' argument is used instead",
call. = FALSE) call. = FALSE)
} }
# Check ConvertFun
if (any(class(x) %in% c("InputsModel", "OutputsModel"))) {
if (!all(is.na(ConvertFun))) {
warning("Argument 'ConvertFun' is ignored on 'InputsModel' and 'OutputsModel' objects")
}
} else if (length(ConvertFun)!=1) {
stop("Argument 'ConvertFun' must be of length 1 with 'list' object")
} else if (!is.character(ConvertFun)) {
stop("Argument 'ConvertFun' must be a character")
}
# Determination of DatesR # Determination of DatesR
if (!is.null(x$DatesR)) { if (!is.null(x$DatesR)) {
...@@ -59,13 +69,11 @@ SeriesAggreg.list <- function(x, ...@@ -59,13 +69,11 @@ SeriesAggreg.list <- function(x,
} }
dfOut <- NULL dfOut <- NULL
if (length(cols)) { if (length(cols)) {
ConvertFun2 <- .GetAggregConvertFun(names(cols), Format) # Treating aggregation at root level
if (is.null(recursive)) { if (is.na(ConvertFun)) {
if (missing(ConvertFun)) { ConvertFun2 <- .GetAggregConvertFun(names(cols), Format)
stop("'ConvertFun' argument should provided if 'recursive = NULL'") } else {
} else if (!is.na(ConvertFun)) { ConvertFun2 <- rep(ConvertFun, length(cols))
ConvertFun2 <- rep(ConvertFun, length(cols))
}
} }
dfOut <- SeriesAggreg(cbind(DatesR, as.data.frame(cols)), dfOut <- SeriesAggreg(cbind(DatesR, as.data.frame(cols)),
Format, Format,
...@@ -93,17 +101,27 @@ SeriesAggreg.list <- function(x, ...@@ -93,17 +101,27 @@ SeriesAggreg.list <- function(x,
dfCols <- c(dfCols, x[sapply(x, inherits, "matrix")]) dfCols <- c(dfCols, x[sapply(x, inherits, "matrix")])
listCols <- listCols[setdiff(names(listCols), names(dfCols))] listCols <- listCols[setdiff(names(listCols), names(dfCols))]
if (length(listCols) > 0) { if (length(listCols) > 0) {
# Check for predefined ConvertFun for all sub-elements if (is.na(ConvertFun)) {
ConvertFun <- .GetAggregConvertFun(names(listCols), Format) # Check for predefined ConvertFun for all sub-elements
listConvertFun <- .GetAggregConvertFun(names(listCols), Format)
} else {
listConvert
}
# Run SeriesAggreg for each embedded list # Run SeriesAggreg for each embedded list
listRes <- lapply(names(listCols), function(x) { listRes <- lapply(names(listCols), function(y) {
listCols[[x]]$DatesR <- DatesR listCols[[y]]$DatesR <- DatesR
SeriesAggreg(listCols[[x]], if (is.na(ConvertFun)) {
SeriesAggreg.list(listCols[[y]],
Format = Format, Format = Format,
except = except,
ConvertFun = ConvertFun[x],
recursive = NULL, recursive = NULL,
...) ...,
ConvertFun = listConvertFun[y])
} else {
SeriesAggreg.list(listCols[[y]],
Format = Format,
recursive = NULL,
...)
}
}) })
names(listRes) <- names(listCols) names(listRes) <- names(listCols)
if (is.null(res$DatesR)) { if (is.null(res$DatesR)) {
...@@ -129,10 +147,14 @@ SeriesAggreg.list <- function(x, ...@@ -129,10 +147,14 @@ SeriesAggreg.list <- function(x,
"), it will be ignored in the aggregation" "), it will be ignored in the aggregation"
) )
} else { } else {
ConvertFun <- rep(.GetAggregConvertFun(key, Format), ncol(m)) if (is.na(ConvertFun)) {
res[[key]] <- SeriesAggreg(data.frame(DatesR, m), ConvertFun2 <- rep(.GetAggregConvertFun(key, Format), ncol(m))
} else {
ConvertFun2 <- rep(ConvertFun, ncol(m))
}
res[[key]] <- SeriesAggreg.data.frame(data.frame(DatesR, m),
Format = Format, Format = Format,
ConvertFun = ConvertFun) ConvertFun = ConvertFun2)
} }
} }
} }
......
...@@ -62,7 +62,7 @@ Warning: on the aggregated outputs, the dates correspond to the beginning of the ...@@ -62,7 +62,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{Format} argument instead} \item{NewTimeFormat}{(deprecated) [character] output time step format (i.e. \code{"hourly"}, \code{"daily"}, \code{"monthly"} or \code{"yearly"}). Use the \code{Format} 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 (see details) or \code{"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"})) or name of aggregation function to apply to all elements if the parameter 'x' is a [list]}
\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)} \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)}
......
...@@ -90,7 +90,10 @@ test_that("No DatesR should warning", { ...@@ -90,7 +90,10 @@ test_that("No DatesR should warning", {
E = BasinObs$E, E = BasinObs$E,
Qmm = BasinObs$Qmm Qmm = BasinObs$Qmm
) )
expect_warning(SeriesAggreg(TabSeries, "%Y%m"), regexp = "has been automatically chosen") expect_warning(
SeriesAggreg(TabSeries, "%Y%m", ConvertFun = "sum"),
regexp = "has been automatically chosen"
)
}) })
test_that("Check SeriesAggreg.list 'DatesR' argument", { test_that("Check SeriesAggreg.list 'DatesR' argument", {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment