diff --git a/DESCRIPTION b/DESCRIPTION index 98fecac842870eb8bc56bcfb6379228bf67f9314..eddf374650c62b817897c6d75a03a72b1613be6c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,8 @@ Imports: SPEI, lmom, lmomco, - zoo + zoo, + s2dv Suggests: testthat, knitr, @@ -47,5 +48,5 @@ License: GPL-3 URL: https://earth.bsc.es/gitlab/es/csindicators/ BugReports: https://earth.bsc.es/gitlab/es/csindicators/-/issues Encoding: UTF-8 -RoxygenNote: 7.2.0 +RoxygenNote: 7.2.3 Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index bab43edcccecb70f92ef63df66c81e870745ec26..15c3c9a6e194bd8fea84dac36081c0d8033674aa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -54,6 +54,7 @@ importFrom(lmomco,parpe3) importFrom(lmomco,pwm.pp) importFrom(lmomco,pwm.ub) importFrom(lmomco,pwm2lmom) +importFrom(s2dv,Reorder) importFrom(stats,approxfun) importFrom(stats,ecdf) importFrom(stats,qnorm) diff --git a/R/SelectPeriodOnData.R b/R/SelectPeriodOnData.R index a4cc07c1e7493e44ae6c944a740e9bd1eda6de3a..1cc2792ed9a1bb3cdd909fd99a4a1a1db6402559 100644 --- a/R/SelectPeriodOnData.R +++ b/R/SelectPeriodOnData.R @@ -101,6 +101,7 @@ CST_SelectPeriodOnData <- function(data, start, end, time_dim = 'time', #'Period <- SelectPeriodOnData(data, Dates, start = list(21, 6), end = list(21, 9)) #'@import multiApply #'@importFrom ClimProjDiags Subset +#'@importFrom s2dv Reorder #'@export SelectPeriodOnData <- function(data, dates, start, end, time_dim = 'time', ncores = NULL) { @@ -153,7 +154,8 @@ SelectPeriodOnData <- function(data, dates, start, end, indices <- as.list(rep(1, length(dim_remove))) res <- Subset(res, along = dim_remove, indices, drop = 'selected') } - pos <- match(names(dim(data)), names(dim(res))) - res <- aperm(res, pos) + if (any(names(dims) != names(dim(res)))) { + res <- Reorder(res, names(dims)) + } return(res) } diff --git a/R/SelectPeriodOnDates.R b/R/SelectPeriodOnDates.R index 0919c5d0cf216b92e17e69a7028796990317654d..550aad94ff28525af253b13a68b80d785f10725f 100644 --- a/R/SelectPeriodOnDates.R +++ b/R/SelectPeriodOnDates.R @@ -19,8 +19,6 @@ #'@return A multidimensional array with named dimensions containing the subset of #'the vector dates during the period requested from \code{start} to \code{end}. #' -#'@import multiApply -#' #'@examples #'Dates <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), #' as.Date("30-11-2000", format = "%d-%m-%Y"), by = 'day'), @@ -30,6 +28,8 @@ #' as.Date("30-11-2002", format = "%d-%m-%Y"), by = 'day')) #'dim(Dates) <- c(time = 214, sdate = 3) #'Period <- SelectPeriodOnDates(Dates, start = list(21, 6), end = list(21, 9)) +#'@import multiApply +#'@importFrom s2dv Reorder #'@export SelectPeriodOnDates <- function(dates, start, end, time_dim = 'time', ncores = NULL) { @@ -45,6 +45,12 @@ SelectPeriodOnDates <- function(dates, start, end, ini_day = start[[1]], ini_month = start[[2]], end_day = end[[1]], end_month = end[[2]], ncores = ncores)$output1 + reorder <- FALSE + if (which(names(dim(dates)) == time_dim) != 1) { + dimdates <- names(dim(dates)) + dates <- Reorder(dates, c(time_dim, names(dim(dates))[which(names(dim(dates)) != time_dim)])) + reorder <- TRUE + } # when 29Feb is included the length of the output changes: regular <- Apply(list(res), target_dims = time_dim, fun = sum, ncores = ncores)$output1 @@ -64,12 +70,11 @@ 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)))) { - pos <- match(names(dim(dates)), names(dim(res))) - res <- aperm(res, pos) - } res <- dates[res] dim(res) <- dims + if (reorder) { + res <- Reorder(res, dimdates) + } } return(res) } diff --git a/tests/testthat/test-SelectPeriod.R b/tests/testthat/test-SelectPeriod.R index 93fc6eb52dd94714db6be8dc3fc67dcbb505d3aa..03b40722a1737d7941e880732d1dd8036e0d3c14 100644 --- a/tests/testthat/test-SelectPeriod.R +++ b/tests/testthat/test-SelectPeriod.R @@ -56,7 +56,6 @@ test_that("2. Output checks", { dim(res), c(time = 52) ) - }) ############################################## @@ -250,3 +249,49 @@ test_that("4. Seasonal", { ) }) +############################################## + +test_that("5. Test sample data", { + dates <- c(seq(as.POSIXct("10-03-2011", format = "%d-%m-%Y", tz = "UTC"), + as.POSIXct("20-03-2011", format = "%d-%m-%Y", tz = "UTC"), by = 'day'), + seq(as.POSIXct("10-03-2012", format = "%d-%m-%Y", tz = "UTC"), + as.POSIXct("20-03-2012", format = "%d-%m-%Y", tz = "UTC"), by = 'day'), + seq(as.POSIXct("10-03-2013", format = "%d-%m-%Y", tz = "UTC"), + as.POSIXct("20-03-2013", format = "%d-%m-%Y", tz = "UTC"), by = 'day')) + dim(dates) <- c(ftime = 11, sdate = 3) + expect_equal( + SelectPeriodOnDates(dates = lonlat_prec$attrs$Dates, start = list(10, 03), + end = list(20, 03), time_dim = 'ftime'), + dates + ) + dates_lonlat <- lonlat_prec_st$attrs$Dates + dates_lonlat <- Reorder(dates_lonlat-12*3600, c(2,1)) + expect_equal( + SelectPeriodOnDates(dates = dates_lonlat, start = list(10, 03), + end = list(20, 03), time_dim = 'ftime'), + dates + ) + # test error + dates <- CSTools::lonlat_prec_st$attrs$Dates + out_sdates <- c(as.POSIXct("10-03-2011", format = "%d-%m-%Y", tz = "UTC"), + as.POSIXct("10-03-2012", format = "%d-%m-%Y", tz = "UTC"), + as.POSIXct("10-03-2013", format = "%d-%m-%Y", tz = "UTC")) + dim(dates) <- c(a = 3, len = 31) + expect_error( + SelectPeriodOnDates(dates = dates, start = list(10, 03), + end = list(20, 03), time_dim = 'ftime'), + "Could not find dimension 'ftime' in 1th object provided in 'data'." + ) + dim(dates) <- c(sdate = 3, ftime = 31) + expect_equal( + SelectPeriodOnDates(dates = dates-12*3600, start = list(10, 03), + end = list(20, 03), time_dim = 'ftime')[, 1], + out_sdates + ) + dates <- Reorder(dates, c(2,1)) + expect_equal( + SelectPeriodOnDates(dates = dates-12*3600, start = list(10, 03), + end = list(20, 03), time_dim = 'ftime')[1, ], + out_sdates + ) +})