From 636e006b829231b5ea9788342f8ca91b9843ecbc Mon Sep 17 00:00:00 2001 From: Jorge Date: Wed, 20 Nov 2019 17:37:17 +0100 Subject: [PATCH 1/2] aggregate season --- R/aggregateGrid.R | 15 +++++++++++++++ R/bindGrid.R | 4 ++-- man/aggregateGrid.Rd | 9 ++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/R/aggregateGrid.R b/R/aggregateGrid.R index c02eaeb..849b44f 100644 --- a/R/aggregateGrid.R +++ b/R/aggregateGrid.R @@ -23,6 +23,8 @@ #' aggregation function in first place, and other optional arguments to be passed to the aggregation function. See the examples. #' @param aggr.m Same as \code{aggr.d}, but indicating the monthly aggregation function. #' @param aggr.y Same as \code{aggr.d}, but indicating the annual aggregation function. +#' @param aggr.s Same as \code{aggr.d}, but indicating the seasonal aggregation function. The season can be indicated +#' as shown in this example: aggr.s = list(FUN = list("mean", na.rm = TRUE), season = c(12,1,2)) #' @param aggr.mem Same as \code{aggr.d}, but indicating the function for computing the member aggregation. #' @param aggr.lat Same as \code{aggr.d}, indicating the aggregation function to be applied along latitude. #' @param weight.by.lat Logical. Should latitudinal averages be weighted by the cosine of latitude?. @@ -88,6 +90,7 @@ aggregateGrid <- function(grid, aggr.d = list(FUN = NULL), aggr.m = list(FUN = NULL), aggr.y = list(FUN = NULL), + aggr.s = list(FUN = NULL, season = NULL), aggr.lat = list(FUN = NULL), weight.by.lat = TRUE, aggr.lon = list(FUN = NULL), @@ -104,6 +107,18 @@ aggregateGrid <- function(grid, if (!is.null(aggr.y$FUN)) { grid <- timeAggregation(grid, "YY", aggr.y, parallel, max.ncores, ncores) } + if (!is.null(aggr.s$FUN)) { + if (is.null(aggr.s$season)) stop("Please indicate the desired season using the aggr.s argument") + grid <- subsetGrid(grid,season = aggr.s$season) + months <- sapply(getRefDates(grid),FUN = function(z) substr(z,start = 6,stop = 7)) %>% as.numeric() + indLastMonth <- which(months == aggr.s$season[length(aggr.s$season)]) + indCut <- c(0,indLastMonth[which(diff(indLastMonth) > 1)],length(months)) + + grid <- lapply(1:(length(indCut)-1), FUN = function(z) { + subsetDimension(grid,dimension = "time", indices = (indCut[z]+1):(indCut[z+1])) %>% + climatology(clim.fun = aggr.s$FUN) + }) %>% bindGrid(dimension = "time") + } if (!is.null(aggr.lat$FUN)) { grid <- latAggregation(grid, aggr.lat, weight.by.lat, parallel, max.ncores, ncores) } diff --git a/R/bindGrid.R b/R/bindGrid.R index b39cafa..48a3bd6 100644 --- a/R/bindGrid.R +++ b/R/bindGrid.R @@ -279,12 +279,12 @@ bindGrid.spatial <- function(..., dimn, tol) { lat <- unname(lat) grid.list <- NULL lats <- do.call(coordfun, lat) - if (class(lats) == "list") lats <- unlist(lats) + if (class(lats) == "list") lats <- unlist(lats) %>% unname() attr(ref[["Data"]], "dimensions") <- dimNames # n.vars <- getShape(ref, "var") #if (n.vars > 1) lats <- rep(list(lats), n.vars) if (dimn == "loc") { - ref[["xyCoords"]] <- lats + ref[["xyCoords"]] <- lats ref[["Metadata"]][["station_id"]] <- station_id ref[["Metadata"]][["name"]] <- station_name } else { diff --git a/man/aggregateGrid.Rd b/man/aggregateGrid.Rd index 2db98a5..bcf160a 100644 --- a/man/aggregateGrid.Rd +++ b/man/aggregateGrid.Rd @@ -6,9 +6,9 @@ \usage{ aggregateGrid(grid, aggr.mem = list(FUN = NULL), aggr.d = list(FUN = NULL), aggr.m = list(FUN = NULL), aggr.y = list(FUN = NULL), - aggr.lat = list(FUN = NULL), weight.by.lat = TRUE, - aggr.lon = list(FUN = NULL), parallel = FALSE, max.ncores = 16, - ncores = NULL) + aggr.s = list(FUN = NULL, season = NULL), aggr.lat = list(FUN = + NULL), weight.by.lat = TRUE, aggr.lon = list(FUN = NULL), + parallel = FALSE, max.ncores = 16, ncores = NULL) } \arguments{ \item{grid}{a grid or multigrid to be aggregated.} @@ -22,6 +22,9 @@ aggregation function in first place, and other optional arguments to be passed t \item{aggr.y}{Same as \code{aggr.d}, but indicating the annual aggregation function.} +\item{aggr.s}{Same as \code{aggr.d}, but indicating the seasonal aggregation function. The season can be indicated +as shown in this example: aggr.s = list(FUN = list("mean", na.rm = TRUE), season = c(12,1,2))} + \item{aggr.lat}{Same as \code{aggr.d}, indicating the aggregation function to be applied along latitude.} \item{weight.by.lat}{Logical. Should latitudinal averages be weighted by the cosine of latitude?. From 717a0a1271ccaf89b035e618f0dd1e525b37d1dd Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 7 Feb 2020 10:12:51 +0100 Subject: [PATCH 2/2] . --- man/aggregateGrid.Rd | 4 ---- 1 file changed, 4 deletions(-) diff --git a/man/aggregateGrid.Rd b/man/aggregateGrid.Rd index 4b8c407..b2c9856 100644 --- a/man/aggregateGrid.Rd +++ b/man/aggregateGrid.Rd @@ -8,12 +8,8 @@ aggregateGrid(grid, aggr.mem = list(FUN = NULL), aggr.d = list(FUN = NULL), aggr.m = list(FUN = NULL), aggr.y = list(FUN = NULL), aggr.s = list(FUN = NULL, season = NULL), aggr.lat = list(FUN = NULL), weight.by.lat = TRUE, aggr.lon = list(FUN = NULL), -<<<<<<< HEAD - parallel = FALSE, max.ncores = 16, ncores = NULL) -======= aggr.loc = list(FUN = NULL), parallel = FALSE, max.ncores = 16, ncores = NULL) ->>>>>>> 3b2929bb8f2ebaf513b884aa78b2b1488ad3f665 } \arguments{ \item{grid}{a grid or multigrid to be aggregated.}