From c02b4fa27913534d4f2cafc59fcc819bb2b9140c Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 30 Jan 2020 16:13:54 +0100 Subject: [PATCH 1/2] Season() + Apply. Unit test added. --- DESCRIPTION | 3 +- NAMESPACE | 1 + R/Season.R | 230 ++++++++++++++++++++++------------- man/Season.Rd | 77 ++++++------ tests/testthat/test-Season.R | 122 +++++++++++++++++++ 5 files changed, 307 insertions(+), 126 deletions(-) create mode 100644 tests/testthat/test-Season.R diff --git a/DESCRIPTION b/DESCRIPTION index 0045265c..6780c83d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,7 +50,8 @@ Imports: ncdf4, parallel, plyr, - SpecsVerification (>= 0.5.0) + SpecsVerification (>= 0.5.0), + multiApply (>= 2.0.0) Suggests: easyVerification, testthat diff --git a/NAMESPACE b/NAMESPACE index 4e855789..00c59bfa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -90,6 +90,7 @@ import(graphics) import(mapproj) import(maps) import(methods) +import(multiApply) import(ncdf4) import(parallel) import(plyr) diff --git a/R/Season.R b/R/Season.R index baa19bbc..22b3578c 100644 --- a/R/Season.R +++ b/R/Season.R @@ -1,98 +1,154 @@ -#'Computes Seasonal Means +#'Compute seasonal mean #' -#'Computes seasonal means on timeseries organized in a array of any number of -#'dimensions up to 10 dimensions where the time dimension is one of those 10 -#'dimensions. +#'Compute the seasonal mean (or other methods) on monthly time series along +#'one dimension of a named multi-dimensional arrays. Partial season is not +#'accounted. #' -#'@param var Array containing the timeseries along one of its dimensions. -#'@param posdim Dimension along which to compute seasonal means = Time -#' dimension. -#'@param monini an integer indicating the first month of the time series: 1 to -#' 12. -#'@param moninf an integer indicating the month when to start the seasonal -#' means: 1 to 12. -#'@param monsup an integer indicating the month when to stop the seasonal -#' means: 1 to 12. +#'@param data A named numeric array with at least one dimension 'time_dim'. +#'@param time_dim A character string indicating the name of dimension along +#' which the seasonal means are computed. The default value is 'sdate'. +#'@param monini An integer indicating what the first month of the time series is. +#' It can be from 1 to 12. +#'@param moninf An integer indicating the starting month of the seasonal mean. +#' It can be from 1 to 12. +#'@param monsup An integer indicating the end month of the seasonal mean. It +#' can be from 1 to 12. +#'@param method An R function to be applied for seasonal calculation. For +#' example, 'sum' can be used for total precipitation. The default value is mean. +#'@param na.rm A logical value indicating whether to remove NA values along +#' 'time_dim' when calculating climatology (TRUE) or return NA if there is NA +#' along 'time_dim' (FALSE). The default value is TRUE. +#'@param ncores An integer indicating the number of cores to use for parallel +#' computation. The default value is NULL. #' -#'@return Array with the same dimensions as var except along the posdim -#' dimension whose length corresponds to the number of seasons. Partial -#' seasons are not accounted for. -#'@keywords datagen -#'@author History:\cr -#'0.1 - 2011-03 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr -#'1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to CRAN +#'@return An array with the same dimensions as data except along the 'time_dim' +#' dimension, of which the length changes to the number of seasons. +#' +#'@import multiApply #'@examples -#'# Load sample data as in Load() example: -#'example(Load) -#'leadtimes_dimension <- 4 -#'initial_month <- 11 -#'mean_start_month <- 12 -#'mean_stop_month <- 2 -#'season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month, -#' mean_start_month, mean_stop_month) -#'season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month, -#' mean_start_month, mean_stop_month) -#' \donttest{ -#'PlotAno(season_means_mod, season_means_obs, startDates, -#' toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'), -#' legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps') -#' } +#'dat <- 1 : (12 * 5 * 2 * 3 * 2) +#'dim(dat) <- c(dat = 1, memb = 3, time = 12 * 5, lon = 2, lat = 2) +#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +#'dat <- 1 : (12 * 2 * 3) +#'dim(dat) <- c(dat = 2, time = 12, memb = 3) +#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +#'dat <- 1 : (24 * 2 * 3) +#'dim(dat) <- c(dat = 2, time = 24, memb = 3) +#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +#'dat[c(1, 20, 50)] <- NA +#'res1 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = FALSE) +#'res2 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = TRUE) +#'@import multiApply #'@export -Season <- function(var, posdim = 4, monini, moninf, monsup) { - while (monsup < moninf) { - monsup <- monsup + 12 +Season <- function(data, time_dim = 'sdate', monini, moninf, monsup, + method = mean, na.rm = TRUE, ncores = NULL) { + + # Check inputs + ## data + if (is.null(data)) { + stop("Parameter 'data' cannot be NULL.") } - # - # Enlarge the size of var to 10 - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # - dimsvar <- dim(var) - if (is.null(dimsvar)) { - dimsvar <- length(var) + if (!is.numeric(data)) { + stop("Parameter 'data' must be a numeric array.") } - ntime <- dimsvar[posdim] - enlvar <- Enlarge(var, 10) - outdim <- c(dimsvar, array(1, dim = (10 - length(dimsvar)))) - u <- IniListDims(outdim, 10) - v <- IniListDims(outdim, 10) - # - # Compute the seasonal means - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # - ind <- 1:ntime - months <- ((ind - 1) + monini - 1) %% 12 + 1 - years <- ((ind - 1) + monini - 1) %/% 12 - - for (jmon in moninf:monsup) { - u[[posdim]] <- ind[which(months == ((jmon - 1) %% 12 + 1))] - ind0 <- u[[posdim]][1] - indf <- u[[posdim]][length(u[[posdim]])] - if (indf > (ntime - (monsup - jmon))) { - u[[posdim]] <- u[[posdim]][-which(u[[posdim]] == indf)] + if (is.null(dim(data))) { #is vector + dim(data) <- c(length(data)) + names(dim(data)) <- time_dim + } + if(any(is.null(names(dim(data))))| any(nchar(names(dim(data))) == 0)) { + stop("Parameter 'data' must have dimension names.") + } + ## time_dim + if (!is.character(time_dim) | length(time_dim) > 1) { + stop("Parameter 'time_dim' must be a character string.") + } + if (!time_dim %in% names(dim(data))) { + stop("Parameter 'time_dim' is not found in 'data' dimension.") + } + ## monini + if (!is.numeric(monini)) { + stop("Parameter 'monini' must be a positive integer between 1 and 12.") + } else { + if (monini %% 1 != 0 | monini < 1 | monini > 12 | length(monini) > 1) { + stop("Parameter 'monini' must be a positive integer between 1 and 12.") } - if (ind0 < (jmon - moninf + 1)) { - u[[posdim]] <- u[[posdim]][-which(u[[posdim]] == ind0)] - } - if (jmon == moninf) { - nseas <- length(u[[posdim]]) - dimsvar[posdim] <- nseas - outdim[posdim] <- nseas - enlvarout <- array(0, dim = outdim) + } + ## moninf + if (!is.numeric(moninf)) { + stop("Parameter 'moninf' must be a positive integer between 1 and 12.") + } else { + if (moninf %% 1 != 0 | moninf < 1 | moninf > 12 | length(moninf) > 1) { + stop("Parameter 'moninf' must be a positive integer between 1 and 12.") + } + } + ## monsup + if (!is.numeric(monsup)) { + stop("Parameter 'monsup' must be a positive integer between 1 and 12.") + } else { + if (monsup %% 1 != 0 | monsup < 1 | monsup > 12 | length(monsup) > 1) { + stop("Parameter 'monsup' must be a positive integer between 1 and 12.") } - v[[posdim]] <- 1:nseas - enlvarout[v[[1]], v[[2]], v[[3]], v[[4]], v[[5]], v[[6]], v[[7]], v[[8]], - v[[9]], v[[10]]] <- enlvarout[v[[1]], v[[2]], v[[3]], v[[4]], - v[[5]], v[[6]], v[[7]], v[[8]], - v[[9]], v[[10]]] + enlvar[u[[1]], - u[[2]], u[[3]], u[[4]], u[[5]], u[[6]], - u[[7]], u[[8]], u[[9]], u[[10]]] } - varout <- array(dim = dimsvar) - varout[] <- enlvarout - varout <- varout / (monsup - moninf + 1) - # - # Outputs - # ~~~~~~~~~ - # - varout + ## method + if (!is.function(method)) { + stop("Parameter 'method' should be an existing R function, e.g., mean or sum.") + } + ## na.rm + if (!is.logical(na.rm) | length(na.rm) > 1) { + stop("Parameter 'na.rm' must be one logical value.") + } + ## ncores + if (!is.null(ncores)) { + if (!is.numeric(ncores) | ncores %% 1 != 0 | ncores < 0 | + length(ncores) > 1) { + stop("Parameter 'ncores' must be a positive integer.") + } + } + + ############################### + # Calculate Season + + # Correction need if monini is not January: + moninf <- moninf - monini + 1 + monsup <- monsup - monini + 1 + moninf <- ifelse(moninf <= 0, moninf + 12, moninf) + monsup <- ifelse(monsup <= 0, monsup + 12, monsup) + + while (monsup < moninf) { + monsup <- monsup + 12 + } + + res <- Apply(list(data), + target_dims = time_dim, + output_dims = time_dim, + fun = .Season, + monini = monini, moninf = moninf, monsup = monsup, + method = method, na.rm = na.rm, ncores = ncores)$output1 + + return(res) } + +.Season <- function(x, monini, moninf, monsup, method = mean, na.rm = TRUE) { + + #### Create position index: + # Basic index: + pos <- moninf : monsup + # Extended index for all period: + if (length(x) > pos[length(pos)]) { + pos2 <- lapply(pos, function(y) {seq(y, length(x), 12)}) + } else { + pos2 <- pos + } + # Correct if the final season is not complete: + maxyear <- min(unlist(lapply(pos2, length))) + pos2 <- lapply(pos2, function(y) {y[1 : maxyear]}) + # Convert to array: + pos2 <- unlist(pos2) + dim(pos2) <- c(year = maxyear, month = length(pos2)/maxyear) + + timeseries <- apply(pos2, 1, function(y) {method(x[y], na.rm = na.rm)}) + timeseries <- as.array(timeseries) + + return(timeseries) +} + diff --git a/man/Season.Rd b/man/Season.Rd index cc97941c..f1e74d6a 100644 --- a/man/Season.Rd +++ b/man/Season.Rd @@ -2,56 +2,57 @@ % Please edit documentation in R/Season.R \name{Season} \alias{Season} -\title{Computes Seasonal Means} +\title{Compute seasonal mean} \usage{ -Season(var, posdim = 4, monini, moninf, monsup) +Season(data, time_dim = "sdate", monini, moninf, monsup, method = mean, + na.rm = TRUE, ncores = NULL) } \arguments{ -\item{var}{Array containing the timeseries along one of its dimensions.} +\item{data}{A named numeric array with at least one dimension 'time_dim'.} -\item{posdim}{Dimension along which to compute seasonal means = Time -dimension.} +\item{time_dim}{A character string indicating the name of dimension along +which the seasonal means are computed. The default value is 'sdate'.} -\item{monini}{an integer indicating the first month of the time series: 1 to -12.} +\item{monini}{An integer indicating what the first month of the time series is. +It can be from 1 to 12.} -\item{moninf}{an integer indicating the month when to start the seasonal -means: 1 to 12.} +\item{moninf}{An integer indicating the starting month of the seasonal mean. +It can be from 1 to 12.} -\item{monsup}{an integer indicating the month when to stop the seasonal -means: 1 to 12.} +\item{monsup}{An integer indicating the end month of the seasonal mean. It +can be from 1 to 12.} + +\item{method}{An R function to be applied for seasonal calculation. For +example, 'sum' can be used for total precipitation. The default value is mean.} + +\item{na.rm}{A logical value indicating whether to remove NA values along +'time_dim' when calculating climatology (TRUE) or return NA if there is NA +along 'time_dim' (FALSE). The default value is TRUE.} + +\item{ncores}{An integer indicating the number of cores to use for parallel +computation. The default value is NULL.} } \value{ -Array with the same dimensions as var except along the posdim - dimension whose length corresponds to the number of seasons. Partial - seasons are not accounted for. +An array with the same dimensions as data except along the 'time_dim' + dimension, of which the length changes to the number of seasons. } \description{ -Computes seasonal means on timeseries organized in a array of any number of -dimensions up to 10 dimensions where the time dimension is one of those 10 -dimensions. +Compute the seasonal mean (or other methods) on monthly time series along +one dimension of a named multi-dimensional arrays. Partial season is not +accounted. } \examples{ -# Load sample data as in Load() example: -example(Load) -leadtimes_dimension <- 4 -initial_month <- 11 -mean_start_month <- 12 -mean_stop_month <- 2 -season_means_mod <- Season(sampleData$mod, leadtimes_dimension, initial_month, - mean_start_month, mean_stop_month) -season_means_obs <- Season(sampleData$obs, leadtimes_dimension, initial_month, - mean_start_month, mean_stop_month) - \donttest{ -PlotAno(season_means_mod, season_means_obs, startDates, - toptitle = paste('winter (DJF) temperatures'), ytitle = c('K'), - legends = 'ERSST', biglab = FALSE, fileout = 'tos_season_means.eps') - } -} -\author{ -History:\cr -0.1 - 2011-03 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr -1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to CRAN +dat <- 1 : (12 * 5 * 2 * 3 * 2) +dim(dat) <- c(dat = 1, memb = 3, time = 12 * 5, lon = 2, lat = 2) +res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +dat <- 1 : (12 * 2 * 3) +dim(dat) <- c(dat = 2, time = 12, memb = 3) +res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +dat <- 1 : (24 * 2 * 3) +dim(dat) <- c(dat = 2, time = 24, memb = 3) +res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) +dat[c(1, 20, 50)] <- NA +res1 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = FALSE) +res2 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = TRUE) } -\keyword{datagen} diff --git a/tests/testthat/test-Season.R b/tests/testthat/test-Season.R new file mode 100644 index 00000000..0f1cba62 --- /dev/null +++ b/tests/testthat/test-Season.R @@ -0,0 +1,122 @@ +context("s2dverification::Season tests") + +############################################## + # dat1 + set.seed(1) + data1 <- array(rnorm(144*3), dim = c(member = 2, sdate = 12*3, ftime = 2, lon = 3)) + + # dat2 + data2 <- data1 + set.seed(2) + na <- floor(runif(30, min = 1, max = 144*3)) + data2[na] <- NA + +############################################## +test_that("1. Input checks", { + + expect_error( + Season(c()), + "Parameter 'data' cannot be NULL." + ) + expect_error( + Season(c(NA, NA)), + "Parameter 'data' must be a numeric array." + ) + expect_error( + Season(list(a = array(rnorm(50), dim = c(dat = 5, sdate = 10)), b = c(1:4))), + "Parameter 'data' must be a numeric array." + ) + expect_error( + Season(array(1:10, dim = c(2, 5))), + "Parameter 'data' must have dimension names." + ) + expect_error( + Season(dat1, time_dim = 2), + "Parameter 'time_dim' must be a character string." + ) + expect_error( + Season(dat1, time_dim = 'a'), + "Parameter 'time_dim' is not found in 'data' dimension." + ) + expect_error( + Season(dat1, monini = 0, moninf = 1, monsup = 2), + "Parameter 'monini' must be a positive integer between 1 and 12." + ) + expect_error( + Season(dat1, monini = 1, moninf = 'jan', monsup = 2), + "Parameter 'moninf' must be a positive integer between 1 and 12." + ) + expect_error( + Season(dat1, monini = 1, moninf = 1, monsup = 'Jan'), + "Parameter 'monsup' must be a positive integer between 1 and 12." + ) + expect_error( + Season(dat1, monini = 1, moninf = 1, monsup = 2, method = 'mean'), + "Parameter 'method' should be an existing R function, e.g., mean or sum." + ) + expect_error( + Season(dat1, monini = 1, moninf = 1, monsup = 2, na.rm = na.omit), + "Parameter 'na.rm' must be one logical value." + ) + expect_error( + Season(dat1, monini = 1, moninf = 1, monsup = 2, ncores = T), + "Parameter 'ncores' must be a positive integer." + ) + +}) + +############################################## +test_that("2. Output checks: dat1", { + + expect_equal( + dim(Season(data1, monini = 1, moninf = 1, monsup = 2)), + c(sdate = 3, member = 2, ftime = 2, lon = 3) + ) + expect_equal( + dim(Season(data1, time_dim = 'lon', monini = 1, moninf = 1, monsup = 2)), + c(lon = 1, member = 2, sdate = 36, ftime = 2) + ) + expect_equal( + dim(Season(data1, monini = 10, moninf = 12, monsup = 2)), + c(sdate = 3, member = 2, ftime = 2, lon = 3) + ) + expect_equal( + summary(Season(data1, monini = 10, moninf = 12, monsup = 2))[3], + c(Median = 0.007925), + tolerance = 0.0001 + ) + expect_equal( + summary(Season(data1, monini = 10, moninf = 2, monsup = 5, method = sum))[3], + c(Median = 0.2732), + tolerance = 0.0001 + ) + +}) + +############################################## +test_that("3. Output checks: dat2", { + + expect_equal( + summary(Season(data2, monini = 10, moninf = 12, monsup = 2))[3], + c(Median = -0.01987), + tolerance = 0.0001 + ) + expect_equal( + summary(Season(data2, monini = 10, moninf = 12, monsup = 2, na.rm = F))[3], + c(Median = 0.06207), + tolerance = 0.0001 + ) + res <- Season(data2, monini = 10, moninf = 12, monsup = 2, na.rm = F) + expect_equal( + length(res[which(is.na(as.vector(res)))]), + 10 + ) + res <- Season(data2, monini = 10, moninf = 12, monsup = 2) + expect_equal( + length(res[which(is.na(as.vector(res)))]), + 0 + ) +}) + +############################################## + -- GitLab From 2581b87125ac677d2be8673fdec8d6e1f96af71f Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 30 Jan 2020 16:30:55 +0100 Subject: [PATCH 2/2] Correct Season() examples and unit tests. --- R/Season.R | 22 ++++++++++------------ man/Season.Rd | 22 ++++++++++------------ tests/testthat/test-Season.R | 24 ++++++++++++------------ 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/R/Season.R b/R/Season.R index 22b3578c..5ba97869 100644 --- a/R/Season.R +++ b/R/Season.R @@ -26,18 +26,16 @@ #' #'@import multiApply #'@examples -#'dat <- 1 : (12 * 5 * 2 * 3 * 2) -#'dim(dat) <- c(dat = 1, memb = 3, time = 12 * 5, lon = 2, lat = 2) -#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -#'dat <- 1 : (12 * 2 * 3) -#'dim(dat) <- c(dat = 2, time = 12, memb = 3) -#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -#'dat <- 1 : (24 * 2 * 3) -#'dim(dat) <- c(dat = 2, time = 24, memb = 3) -#'res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -#'dat[c(1, 20, 50)] <- NA -#'res1 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = FALSE) -#'res2 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = TRUE) +#'set.seed(1) +#'dat1 <- array(rnorm(144*3), dim = c(member = 2, sdate = 12*3, ftime = 2, lon = 3)) +#'res <- Season(data = dat1, monini = 1, moninf = 1, monsup = 2) +#'res <- Season(data = dat1, monini = 10, moninf = 12, monsup = 2) +#'dat2 <- dat1 +#'set.seed(2) +#'na <- floor(runif(30, min = 1, max = 144*3)) +#'dat2[na] <- NA +#'res <- Season(data = dat2, monini = 3, moninf = 1, monsup = 2) +#'res <- Season(data = dat2, monini = 3, moninf = 1, monsup = 2, na.rm = FALSE) #'@import multiApply #'@export Season <- function(data, time_dim = 'sdate', monini, moninf, monsup, diff --git a/man/Season.Rd b/man/Season.Rd index f1e74d6a..fad6f22c 100644 --- a/man/Season.Rd +++ b/man/Season.Rd @@ -42,17 +42,15 @@ one dimension of a named multi-dimensional arrays. Partial season is not accounted. } \examples{ -dat <- 1 : (12 * 5 * 2 * 3 * 2) -dim(dat) <- c(dat = 1, memb = 3, time = 12 * 5, lon = 2, lat = 2) -res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -dat <- 1 : (12 * 2 * 3) -dim(dat) <- c(dat = 2, time = 12, memb = 3) -res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -dat <- 1 : (24 * 2 * 3) -dim(dat) <- c(dat = 2, time = 24, memb = 3) -res <- Season(data = dat, monini = 1, moninf = 1, monsup = 2) -dat[c(1, 20, 50)] <- NA -res1 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = FALSE) -res2 <- Season(data = dat, monini = 1, moninf = 1, monsup = 2, na.rm = TRUE) +set.seed(1) +dat1 <- array(rnorm(144*3), dim = c(member = 2, sdate = 12*3, ftime = 2, lon = 3)) +res <- Season(data = dat1, monini = 1, moninf = 1, monsup = 2) +res <- Season(data = dat1, monini = 10, moninf = 12, monsup = 2) +dat2 <- dat1 +set.seed(2) +na <- floor(runif(30, min = 1, max = 144*3)) +dat2[na] <- NA +res <- Season(data = dat2, monini = 3, moninf = 1, monsup = 2) +res <- Season(data = dat2, monini = 3, moninf = 1, monsup = 2, na.rm = FALSE) } diff --git a/tests/testthat/test-Season.R b/tests/testthat/test-Season.R index 0f1cba62..b01b242b 100644 --- a/tests/testthat/test-Season.R +++ b/tests/testthat/test-Season.R @@ -3,13 +3,13 @@ context("s2dverification::Season tests") ############################################## # dat1 set.seed(1) - data1 <- array(rnorm(144*3), dim = c(member = 2, sdate = 12*3, ftime = 2, lon = 3)) + dat1 <- array(rnorm(144*3), dim = c(member = 2, sdate = 12*3, ftime = 2, lon = 3)) # dat2 - data2 <- data1 + dat2 <- dat1 set.seed(2) na <- floor(runif(30, min = 1, max = 144*3)) - data2[na] <- NA + dat2[na] <- NA ############################################## test_that("1. Input checks", { @@ -69,24 +69,24 @@ test_that("1. Input checks", { test_that("2. Output checks: dat1", { expect_equal( - dim(Season(data1, monini = 1, moninf = 1, monsup = 2)), + dim(Season(dat1, monini = 1, moninf = 1, monsup = 2)), c(sdate = 3, member = 2, ftime = 2, lon = 3) ) expect_equal( - dim(Season(data1, time_dim = 'lon', monini = 1, moninf = 1, monsup = 2)), + dim(Season(dat1, time_dim = 'lon', monini = 1, moninf = 1, monsup = 2)), c(lon = 1, member = 2, sdate = 36, ftime = 2) ) expect_equal( - dim(Season(data1, monini = 10, moninf = 12, monsup = 2)), + dim(Season(dat1, monini = 10, moninf = 12, monsup = 2)), c(sdate = 3, member = 2, ftime = 2, lon = 3) ) expect_equal( - summary(Season(data1, monini = 10, moninf = 12, monsup = 2))[3], + summary(Season(dat1, monini = 10, moninf = 12, monsup = 2))[3], c(Median = 0.007925), tolerance = 0.0001 ) expect_equal( - summary(Season(data1, monini = 10, moninf = 2, monsup = 5, method = sum))[3], + summary(Season(dat1, monini = 10, moninf = 2, monsup = 5, method = sum))[3], c(Median = 0.2732), tolerance = 0.0001 ) @@ -97,21 +97,21 @@ test_that("2. Output checks: dat1", { test_that("3. Output checks: dat2", { expect_equal( - summary(Season(data2, monini = 10, moninf = 12, monsup = 2))[3], + summary(Season(dat2, monini = 10, moninf = 12, monsup = 2))[3], c(Median = -0.01987), tolerance = 0.0001 ) expect_equal( - summary(Season(data2, monini = 10, moninf = 12, monsup = 2, na.rm = F))[3], + summary(Season(dat2, monini = 10, moninf = 12, monsup = 2, na.rm = F))[3], c(Median = 0.06207), tolerance = 0.0001 ) - res <- Season(data2, monini = 10, moninf = 12, monsup = 2, na.rm = F) + res <- Season(dat2, monini = 10, moninf = 12, monsup = 2, na.rm = F) expect_equal( length(res[which(is.na(as.vector(res)))]), 10 ) - res <- Season(data2, monini = 10, moninf = 12, monsup = 2) + res <- Season(dat2, monini = 10, moninf = 12, monsup = 2) expect_equal( length(res[which(is.na(as.vector(res)))]), 0 -- GitLab