diff --git a/R/Season.R b/R/Season.R index 8745d26265c383041da59552c9cf4e43f87b4a6c..a1402afc5e952c38f9231c363ac94418a1c492be 100644 --- a/R/Season.R +++ b/R/Season.R @@ -127,16 +127,21 @@ Season <- function(data, time_dim = 'ftime', monini, moninf, monsup, } if (use_apply) { - time_dim_ind <- match(time_dim, names(dim(data))) - res <- apply(data, c(1:length(dim(data)))[-time_dim_ind], .Season, - monini = monini, moninf = moninf, monsup = monsup, - method = method, na.rm = na.rm) - if (length(dim(res)) < length(dim(data))) { - res <- InsertDim(res, posdim = 1, lendim = 1, name = time_dim, ncores = ncores) + if (length(dim(data)) == 1) { + res <- .Season(data, monini = monini, moninf = moninf, monsup = monsup, + method = method, na.rm = na.rm) + names(dim(res)) <- time_dim } else { - names(dim(res))[1] <- time_dim + time_dim_ind <- match(time_dim, names(dim(data))) + res <- apply(data, c(1:length(dim(data)))[-time_dim_ind], .Season, + monini = monini, moninf = moninf, monsup = monsup, + method = method, na.rm = na.rm) + if (length(dim(res)) < length(dim(data))) { + res <- InsertDim(res, posdim = 1, lendim = 1, name = time_dim, ncores = ncores) + } else { + names(dim(res))[1] <- time_dim + } } - } else { res <- Apply(list(data), target_dims = time_dim, diff --git a/tests/testthat/test-Season.R b/tests/testthat/test-Season.R index 36c3027043245125cb1b960802da3b0164788905..0b009a6e8aeffda408123303e13f97977b84ff30 100644 --- a/tests/testthat/test-Season.R +++ b/tests/testthat/test-Season.R @@ -11,6 +11,10 @@ context("s2dv::Season tests") na <- floor(runif(30, min = 1, max = 144*3)) dat2[na] <- NA + # dat3 + set.seed(1) + dat3 <- array(rnorm(12), dim = c(ftime = 12)) + ############################################## test_that("1. Input checks", { @@ -161,4 +165,26 @@ test_that("3. Output checks: dat2", { }) ############################################## +test_that("3. Output checks: dat3", { + expect_equal( + as.numeric(Season(dat3, monini = 10, moninf = 12, monsup = 2)), + 0.3630533, + tolerance = 0.0001 + ) + expect_equal( + dim(Season(dat3, monini = 10, moninf = 12, monsup = 2)), + c(ftime = 1) + ) +# expect_equal( +# Season(dat3, monini = 10, moninf = 12, monsup = 2, ncores = 2), +# 0.3630533, +# tolerance = 0.0001 +# ) +# expect_equal( +# dim(Season(dat3, monini = 10, moninf = 12, monsup = 2, ncores = 2)), +# c(ftime = 1) +# ) + +}) +###############################################