diff --git a/modules/Loading/R/get_timeidx.R b/modules/Loading/R/get_forecast_times.R similarity index 87% rename from modules/Loading/R/get_timeidx.R rename to modules/Loading/R/get_forecast_times.R index 2796b2021d3ce341508a6ec11ab4799cb2a53188..621c7d2778596255a3dc7b5f883dcddf20fbccd3 100644 --- a/modules/Loading/R/get_timeidx.R +++ b/modules/Loading/R/get_forecast_times.R @@ -2,17 +2,19 @@ #'to the start dates, min. and max. leadtimes and #'time frequency. # +#'The leadtimes are returned as dates in POSIXct format, +#'so that they can be read by Start(). #'The leadtimes are defined by months -#'Ex. 20201101 with leadtimes 1-4 corresponds to -#'the forecasting times covering November to february #' #'@param sdates vector containind the start dates #'@param ltmin first leadtime #'@param ltmax last leadtime #'@param time_freq time frequency ("monthly_mean" or "daily_mean" or "daily") +#'@param ncores number of cores to use for parallel computation -get_timeidx <- function(sdates, ltmin, ltmax, - time_freq="monthly_mean") { +get_forecast_times <- function(sdates, ltmin, ltmax, + time_freq="monthly_mean", + ncores = 1) { if (time_freq %in% c("daily_mean", "daily")) { @@ -55,7 +57,7 @@ get_timeidx <- function(sdates, ltmin, ltmax, output_dims = c("time"), idx_min = ltmin - 1, idx_max = ltmax - 1, - ncores = recipe$Analysis$ncores + ncores = ncores )[[1]] indxs_dims <- dim(indxs) indxs <- as.POSIXct(indxs, origin = "1970-01-01", tz = "UTC") diff --git a/modules/Loading/R/load_decadal.R b/modules/Loading/R/load_decadal.R index c55688e01dc480bd6440fae78d4fc15ebea87b2d..cdb3d339ab1c4abe05727ba92baf6cb4a82f9119 100644 --- a/modules/Loading/R/load_decadal.R +++ b/modules/Loading/R/load_decadal.R @@ -3,7 +3,7 @@ source("modules/Loading/R/get_regrid_params.R") source("modules/Loading/R/helper_loading_decadal.R") source("modules/Loading/R/dates2load.R") source("modules/Loading/R/check_latlon.R") -source("modules/Loading/R/get_timeidx.R") +source("modules/Loading/R/get_forecast_times.R") source("modules/Loading/R/compare_exp_obs_grids.R") #==================================================================== diff --git a/modules/Loading/R/load_seasonal.R b/modules/Loading/R/load_seasonal.R index 1e6c9367be006160210168efb9d524ccacccd874..1824b7d5b9408bca1be3b2374e702ca6a3cf55ba 100644 --- a/modules/Loading/R/load_seasonal.R +++ b/modules/Loading/R/load_seasonal.R @@ -1,7 +1,7 @@ # Load required libraries/funs source("modules/Loading/R/get_regrid_params.R") source("modules/Loading/R/dates2load.R") -source("modules/Loading/R/get_timeidx.R") +source("modules/Loading/R/get_forecast_times.R") source("modules/Loading/R/check_latlon.R") source("modules/Loading/R/compare_exp_obs_grids.R") @@ -27,16 +27,16 @@ load_seasonal <- function(recipe) { sdates <- dates2load(recipe, recipe$Run$logger) idxs <- NULL - idxs$hcst <- get_timeidx(sdates$hcst, - recipe$Analysis$Time$ftime_min, - recipe$Analysis$Time$ftime_max, - time_freq=store.freq) + idxs$hcst <- get_forecast_times(sdates$hcst, + recipe$Analysis$Time$ftime_min, + recipe$Analysis$Time$ftime_max, + time_freq=store.freq) if (!(is.null(sdates$fcst))) { - idxs$fcst <- get_timeidx(sdates$fcst, - recipe$Analysis$Time$ftime_min, - recipe$Analysis$Time$ftime_max, - time_freq=store.freq) + idxs$fcst <- get_forecast_times(sdates$fcst, + recipe$Analysis$Time$ftime_min, + recipe$Analysis$Time$ftime_max, + time_freq=store.freq) } # get datasets dict: diff --git a/modules/Loading/R/load_subseasonal.R b/modules/Loading/R/load_subseasonal.R index da316a61b8a447fc8ad6e4ba4a2160e17ef15fba..dba21ee1e868fab210e4158377b31c32a216ba2d 100644 --- a/modules/Loading/R/load_subseasonal.R +++ b/modules/Loading/R/load_subseasonal.R @@ -33,7 +33,7 @@ load_subseasonal <- function(recipe) { ##fcst.name <- recipe$Analysis$Datasets$System[[sys]]$name # get datasets dict: - archive <-get_archive(recipe) # read_yaml("conf/archive_subseasonal.yml")[[recipe$Run$filesystem]] + archive <- get_archive(recipe) # read_yaml("conf/archive_subseasonal.yml")[[recipe$Run$filesystem]] exp_descrip <- archive$System[[exp.name]] freq.hcst <- unlist(exp_descrip[[store.freq]][variable[1]]) diff --git a/modules/Loading/R/load_tas_tos.R b/modules/Loading/R/load_tas_tos.R index d035f05d04a89afbf6f950a98559227d7293ff9f..c19b79923bdc5a676b5b071bcfbd8cb095bb5a25 100644 --- a/modules/Loading/R/load_tas_tos.R +++ b/modules/Loading/R/load_tas_tos.R @@ -1,7 +1,7 @@ # Load required libraries/funs source("modules/Loading/R/get_regrid_params.R") source("modules/Loading/R/dates2load.R") -source("modules/Loading/R/get_timeidx.R") +source("modules/Loading/R/get_forecast_times.R") source("modules/Loading/R/check_latlon.R") source('modules/Loading/R/mask_tas_tos.R') source("modules/Loading/R/compare_exp_obs_grids.R") @@ -37,16 +37,16 @@ load_tas_tos <- function(recipe) { sdates <- dates2load(recipe, recipe$Run$logger) idxs <- NULL - idxs$hcst <- get_timeidx(sdates$hcst, - recipe$Analysis$Time$ftime_min, - recipe$Analysis$Time$ftime_max, - time_freq = store.freq) + idxs$hcst <- get_forecast_times(sdates$hcst, + recipe$Analysis$Time$ftime_min, + recipe$Analysis$Time$ftime_max, + time_freq = store.freq) if (!(is.null(sdates$fcst))) { - idxs$fcst <- get_timeidx(sdates$fcst, - recipe$Analysis$Time$ftime_min, - recipe$Analysis$Time$ftime_max, - time_freq = store.freq) + idxs$fcst <- get_forecast_times(sdates$fcst, + recipe$Analysis$Time$ftime_min, + recipe$Analysis$Time$ftime_max, + time_freq = store.freq) } # get esarchive datasets dict: diff --git a/tests/testthat/test-utils-get_forecast_times.R b/tests/testthat/test-utils-get_forecast_times.R new file mode 100644 index 0000000000000000000000000000000000000000..89e6dbc6dc6eb4994c1fb43ee2dc2bfbccbc7590 --- /dev/null +++ b/tests/testthat/test-utils-get_forecast_times.R @@ -0,0 +1,113 @@ +context("get_forecast_times.R") + +source("tools/libs.R") +source("modules/Loading/R/get_forecast_times.R") +source("modules/Loading/R/dates2load.R") +########################################### + +sdates <- list() +sdates$fcst <- "20250501" +dim(sdates$fcst) <- c(sday = 1, sweek = 1, syear = 1) +sdates$hcst <- paste0(1993:2016, "0501") +dim(sdates$hcst) <- c(sday = 1, sweek = 1, syear = length(1993:2016)) + +test_that("1. Seasonal daily, 6 months", { + ltmin <- 1 + ltmax <- 6 + time_freq <- "daily_mean" + + idxs <- list() + idxs$hcst <- get_forecast_times(sdates = sdates$hcst, + ltmin = ltmin, + ltmax = ltmax, + time_freq = time_freq) + + idxs$fcst <- get_forecast_times(sdates = sdates$fcst, + ltmin = ltmin, + ltmax = ltmax, + time_freq = time_freq) + # Results + expect_equal( + dim(idxs$hcst), + c(file_date = 24, time = 184) + ) + expect_equal( + as.character(idxs$hcst[1]), + "1993-05-01 12:01:00" + ) + expect_equal( + as.character(idxs$hcst[24, 2]), + "2016-05-02 12:01:00" + ) + expect_equal( + as.character(idxs$hcst[4, 91]), + "1996-07-30 12:01:00" + ) + expect_equal( + dim(idxs$fcst), + c(file_date = 1, time = 184) + ) + expect_equal( + as.character(idxs$fcst[1]), + "2025-05-01 12:01:00" + ) + expect_equal( + as.character(idxs$fcst[1, 2]), + "2025-05-02 12:01:00" + ) + expect_equal( + as.character(idxs$fcst[1, 91]), + "2025-07-30 12:01:00" + ) +}) + + +sdates <- list() +sdates$fcst <- "20231201" +dim(sdates$fcst) <- c(sday = 1, sweek = 1, syear = 1) +sdates$hcst <- paste0(1993:1996, "1201") +dim(sdates$hcst) <- c(sday = 1, sweek = 1, syear = length(1993:1996)) + +test_that("2. Seasonal monthly, 2 months, end of year", { + ltmin <- 1 + ltmax <- 2 + time_freq <- "monthly_mean" + + idxs <- list() + idxs$hcst <- get_forecast_times(sdates = sdates$hcst, + ltmin = ltmin, + ltmax = ltmax, + time_freq = time_freq) + + idxs$fcst <- get_forecast_times(sdates = sdates$fcst, + ltmin = ltmin, + ltmax = ltmax, + time_freq = time_freq) + # Results + expect_equal( + dim(idxs$hcst), + c(time = 2, file_date = 4) + ) + expect_equal( + as.character(idxs$hcst[1, ]), + c("1993-12-16 12:01:00", "1994-12-16 12:01:00", + "1995-12-16 12:01:00", "1996-12-16 12:01:00") + ) + expect_equal( + as.character(idxs$hcst[2, ]), + c("1994-01-16 12:01:00", "1995-01-16 12:01:00", + "1996-01-16 12:01:00", "1997-01-16 12:01:00") + ) + expect_equal( + dim(idxs$fcst), + c(time = 2, file_date = 1) + ) + expect_equal( + as.character(idxs$fcst[1, ]), + "2023-12-16 12:01:00" + ) + expect_equal( + as.character(idxs$fcst[2, ]), + "2024-01-16 12:01:00" + ) +})