diff --git a/DESCRIPTION b/DESCRIPTION index 7fb435496cb31a9242356224aa619f170b4d50b2..27a3ae3d7bca4425cc74ac6e74de20c36ca776e5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,11 +8,11 @@ Depends: R (>= 3.6.1) Imports: multiApply (>= 2.1.1), + s2dv, stats, ClimProjDiags Suggests: testthat, - s2dv, CSTools License: Apache License 2.0 URL: https://earth.bsc.es/gitlab/es/csindicators/ diff --git a/NAMESPACE b/NAMESPACE index 6ae2d5f5e438221b3e35f4f75adc761bae2cc0a6..09b570971b6851d4262b72debe1af64a9c728ced 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,5 +21,6 @@ export(TotalSpellTimeExceedingThreshold) export(TotalTimeExceedingThreshold) import(multiApply) importFrom(ClimProjDiags,Subset) +importFrom(s2dv,Reorder) importFrom(stats,ecdf) importFrom(stats,quantile) diff --git a/R/SelectPeriodOnDates.R b/R/SelectPeriodOnDates.R index 46ae7ff2b05d8547989dfe9d91867a28d0b266ae..c8444c03fa28d9712e0cde44f45ad0aa11b5a2a8 100644 --- a/R/SelectPeriodOnDates.R +++ b/R/SelectPeriodOnDates.R @@ -11,6 +11,7 @@ #'@return A multidimensional array with named dimensions. #' #'@import multiApply +#'@importFrom s2dv Reorder #' #'@examples #'Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), @@ -52,6 +53,9 @@ SelectPeriodOnDates <- function(dates, start, end, }, ncores = ncores)$output1 res <- as.POSIXct(res, origin = '1970-01-01', tz = 'UTC') } else { + if (!all(names(dim(res)) == names(dim(dates)))) { + res <- s2dv::Reorder(res, names(dim(dates))) + } res <- dates[res] dim(res) <- dims } diff --git a/tests/testthat/test-SelectPeriod.R b/tests/testthat/test-SelectPeriod.R index 5716b1a23f87e970126dc0846b18d268cfb7305b..3db72d862f8900141d25209015a37b6da29477f6 100644 --- a/tests/testthat/test-SelectPeriod.R +++ b/tests/testthat/test-SelectPeriod.R @@ -3,6 +3,30 @@ context("Generic tests") #source("R/SelectPeriodOnDates.R") #source("R/SelectPeriodOnData.R") library(s2dv) +test_that("Sanity checks", { + #source("csindicators/R/AbsToProbs.R") + expect_error(SelectPeriodOnDates('x', start = list(1,1), end = list(1,1)), "invalid 'trim' argument") + # Lluis issue #8: + dates <- c(seq(as.Date("02-05-1993", "%d-%m-%Y", tz = 'UTC'), + as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day"), + seq(as.Date("02-05-1994", "%d-%m-%Y", tz = 'UTC'), + as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day"), + seq(as.Date("02-05-1995", "%d-%m-%Y", tz = 'UTC'), + as.Date("01-12-1995","%d-%m-%Y", tz = 'UTC'), "day")) + dim(dates) <- c(time = 214, file_date = 3) + output <- c(seq(as.Date("21-06-1993", "%d-%m-%Y", tz = 'UTC'), + as.Date("21-09-1993","%d-%m-%Y", tz = 'UTC'), "day"), + seq(as.Date("21-06-1994", "%d-%m-%Y", tz = 'UTC'), + as.Date("21-09-1994","%d-%m-%Y", tz = 'UTC'), "day"), + seq(as.Date("21-06-1995", "%d-%m-%Y", tz = 'UTC'), + as.Date("21-09-1995","%d-%m-%Y", tz = 'UTC'), "day")) + dim(output) <- c(time = 93, file_date = 3) + expect_equal(SelectPeriodOnDates(dates, list(21,6),list(21,9), time_dim = 'time'), output) + dates <- s2dv::Reorder(dates, c('file_date', 'time')) + output <- s2dv::Reorder(output, c('file_date', 'time')) + expect_equal(SelectPeriodOnDates(dates, list(21,6),list(21,9), time_dim = 'time'), output) +}) + test_that("Decadal", { # -------- DECADAL ----------# # decadal: 1 sdate several consequtive years: