diff --git a/R/CST_Subset.R b/R/CST_Subset.R index bc3915e880f271062dd3b53b02d8dbf3f3c734ba..2e69c1f9fe2d7de0d2d87170f1806d20d0b9737a 100644 --- a/R/CST_Subset.R +++ b/R/CST_Subset.R @@ -151,7 +151,7 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, .subset_with_attrs <- function(x, ...) { args_subset <- list(...) if (is.null(dim(x)) | length(dim(x)) == 1) { - l <- x[args_subset[['indices']]] + l <- x[args_subset[['indices']][[1]]] } else { l <- ClimProjDiags::Subset(x, along = args_subset[['along']], indices = args_subset[['indices']], diff --git a/tests/testthat/test-CST_Analogs.R b/tests/testthat/test-CST_Analogs.R index b94e993b854b03753442a4454fb6f96e8a8afb89..808c6317dc43e5fbaf8b46531fe1eeefccbac180 100644 --- a/tests/testthat/test-CST_Analogs.R +++ b/tests/testthat/test-CST_Analogs.R @@ -32,18 +32,15 @@ test_that("1. Input checks: CST_Analogs", { # Check 's2dv_cube' expect_error( CST_Analogs(expL = 1, obsL = 1), - paste0("Parameter 'expL' and 'obsL' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'expL' and 'obsL' must be of the class 's2dv_cube'.") ) expect_error( CST_Analogs(expL = exp, obsL = obs, expVar = 1), - paste0("Parameter 'expVar' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'expVar' must be of the class 's2dv_cube'.") ) expect_error( CST_Analogs(expL = exp, obsL = obs, obsVar = 1), - paste0("Parameter 'obsVar' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'obsVar' must be of the class 's2dv_cube'.") ) # Check 'obsL' object structure diff --git a/tests/testthat/test-CST_BiasCorrection.R b/tests/testthat/test-CST_BiasCorrection.R index ec9b596e889a56798bfab20e55df2e467eece00a..61b40c7401a425ca735b4ea4f52dd15213c263be 100644 --- a/tests/testthat/test-CST_BiasCorrection.R +++ b/tests/testthat/test-CST_BiasCorrection.R @@ -108,9 +108,8 @@ test_that("1. Input checks", { ) expect_warning( CST_BiasCorrection(exp = exp2, obs = obs2), - "Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values." - ) + ) # exp_cor expect_error( CST_BiasCorrection(exp = exp1, obs = obs1, exp_cor = exp_cor1, sdate_dim = 'time'), diff --git a/tests/testthat/test-CST_Calibration.R b/tests/testthat/test-CST_Calibration.R index da50d810ad78e72f92214de8f641b087c2eb5d70..491aff29556fc0a67f4abc9fb5ad11500aca272d 100644 --- a/tests/testthat/test-CST_Calibration.R +++ b/tests/testthat/test-CST_Calibration.R @@ -91,9 +91,7 @@ test_that("1. Input checks", { ) expect_warning( CST_Calibration(exp = exp2, obs = obs2, exp_cor = exp2), - "Parameter 'obs' contains NA values", - "Parameter 'exp' contains NA values.", - "Parameter 'exp_cor' contains NA values." + "Parameter 'exp' contains NA values." ) # exp_cor expect_error( diff --git a/tests/testthat/test-CST_CategoricalEnsCombination.R b/tests/testthat/test-CST_CategoricalEnsCombination.R index a52f822b9a4a28ff8545caaa067e3587b3ea79dd..0a13866385283f08d7d08763825b373590685428 100644 --- a/tests/testthat/test-CST_CategoricalEnsCombination.R +++ b/tests/testthat/test-CST_CategoricalEnsCombination.R @@ -26,7 +26,8 @@ obs2$data[1, 1, 2, 1, 1, 1] <- NA test_that("Sanity checks", { expect_error( CST_CategoricalEnsCombination(exp = 1), - "Parameter 'exp' and 'obs' must be of the class 's2dv_cube', " + paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", + "as output by CSTools::CST_Load.") ) expect_error( CST_CategoricalEnsCombination(obs = 1), @@ -83,6 +84,6 @@ test_that("Sanity checks", { ) expect_warning( CST_CategoricalEnsCombination(exp = exp2, obs = obs2), - "Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values." + "Parameter 'exp' contains NA values." ) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-CST_MultivarRMSE.R b/tests/testthat/test-CST_MultivarRMSE.R index f7bc347c844773cf85c9c05e261bf73c528613cd..1cc87593cedb3095c9f2f914e0a80ddcfd8d56e4 100644 --- a/tests/testthat/test-CST_MultivarRMSE.R +++ b/tests/testthat/test-CST_MultivarRMSE.R @@ -156,7 +156,7 @@ test_that("2. Output checks", { memb_dim = 'members', sdate_dim = 'sdates') expect_equal( names(res1), - c('data', 'coords', 'attrs') + c('data', 'coords', 'attrs', 'dims') ) expect_equal( dim(res1$data), diff --git a/tests/testthat/test-CST_QuantileMapping.R b/tests/testthat/test-CST_QuantileMapping.R index e12f1d9a87353abf17019441dfc87c6b199efd3a..1f58ad4be4dd1861b9bb831cd9b5c2a38c4798f0 100644 --- a/tests/testthat/test-CST_QuantileMapping.R +++ b/tests/testthat/test-CST_QuantileMapping.R @@ -113,8 +113,7 @@ test_that("1. Sanity checks", { # s2dv_cube expect_error( CST_QuantileMapping(exp = 1), - paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube'.") ) expect_error( CST_QuantileMapping(exp = exp1), @@ -122,13 +121,11 @@ test_that("1. Sanity checks", { ) expect_error( CST_QuantileMapping(exp = exp1, obs = 1), - paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube'.") ) expect_error( CST_QuantileMapping(exp = exp1, obs = obs1, exp_cor = 1), - paste0("Parameter 'exp_cor' must be of the class 's2dv_cube', as output ", - "by CSTools::CST_Load.") + paste0("Parameter 'exp_cor' must be of the class 's2dv_cube'.") ) # exp and obs expect_error( diff --git a/tests/testthat/test-CST_SaveExp.R b/tests/testthat/test-CST_SaveExp.R index 0ed7923caf8a7377f30b7703dd880a927b27cbb4..f39dffe9e147739b101725af556378572cd8db71 100644 --- a/tests/testthat/test-CST_SaveExp.R +++ b/tests/testthat/test-CST_SaveExp.R @@ -49,8 +49,7 @@ test_that("1. Input checks: CST_SaveExp", { # s2dv_cube expect_error( CST_SaveExp(data = 1), - paste0("Parameter 'data' must be of the class 's2dv_cube', ", - "as output by CSTools::CST_Load.") + paste0("Parameter 'data' must be of the class 's2dv_cube'.") ) # structure expect_error( diff --git a/tests/testthat/test-CST_SplitDim.R b/tests/testthat/test-CST_SplitDim.R index 0c8f2e013e815c94c8d88157aca12e60b3f2b0cc..45e2b1a89cf9895241f2181ea3ea324a2700fc23 100644 --- a/tests/testthat/test-CST_SplitDim.R +++ b/tests/testthat/test-CST_SplitDim.R @@ -51,8 +51,7 @@ class(data4) <- 's2dv_cube' test_that("1. Input checks", { expect_error( CST_SplitDim(data = 1), - paste0("Parameter 'data' must be of the class 's2dv_cube', as output by ", - "CSTools::CST_Load.") + paste0("Parameter 'data' must be of the class 's2dv_cube'.") ) expect_error( CST_SplitDim(data = data1), diff --git a/tests/testthat/test-CST_Subset.R b/tests/testthat/test-CST_Subset.R index 2d270e38a1eb315f78f31ac191e9a0f5c7fcf29a..9fc04b48408c654e40c13f3537a5daff387261f9 100644 --- a/tests/testthat/test-CST_Subset.R +++ b/tests/testthat/test-CST_Subset.R @@ -117,8 +117,8 @@ suppressWarnings( sdate = c('20170101'), ensemble = indices(1), time = indices(1:3), - lat = indices(1:10), - lon = indices(1:10), + lat = indices(1:2), + lon = indices(1:2), synonims = list(lat = c('lat', 'latitude'), lon = c('lon', 'longitude')), return_vars = list(time = 'sdate', @@ -146,7 +146,7 @@ test_that("3. Output checks with Start", { # Check dimensions expect_equal( dim(res8$data), - c(dat = 1, var = 1, sdate = 1, ensemble = 1, time = 3, lat = 10, lon = 2) + c(dat = 1, var = 1, sdate = 1, ensemble = 1, time = 3, lat = 2, lon = 2) ) expect_equal( dim(res8$data), @@ -154,7 +154,7 @@ test_that("3. Output checks with Start", { ) expect_equal( dim(res10$data), - c(time = 3, lat = 10, lon = 2) + c(time = 3, lat = 2, lon = 2) ) # Check coordinates expect_equal( @@ -227,7 +227,7 @@ test_that("3. Output checks with Start", { var_dim = 'var', drop = 'non-selected') expect_equal( dim(res11$data), - c(dat = 1, var = 1, time = 2, lat = 10, lon = 2) + c(dat = 1, var = 1, time = 2, lat = 2, lon = 2) ) expect_equal( names(res11$coords), diff --git a/tests/testthat/test-CST_WeatherRegimes.R b/tests/testthat/test-CST_WeatherRegimes.R index 796c551923623732e28b6ba0c00ebc8879d4ec59..59eeb38800bad55d3e637c999776509ade58a6a9 100644 --- a/tests/testthat/test-CST_WeatherRegimes.R +++ b/tests/testthat/test-CST_WeatherRegimes.R @@ -27,8 +27,7 @@ class(data1) <- 's2dv_cube' test_that("1. Input checks", { expect_error( CST_WeatherRegimes(data = 1), - paste0("Parameter 'data' must be of the class 's2dv_cube', as output by ", - "CSTools::CST_Load.") + paste0("Parameter 'data' must be of the class 's2dv_cube'.") ) # Check 'exp' object structure expect_error( @@ -84,9 +83,11 @@ test_that("1. Input checks", { ############################################## test_that("2. Output checks", { - expect_equal( - names(dim(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), - c('lat', 'lon', 'cluster') + suppressWarnings( + expect_equal( + names(dim(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), + c('lat', 'lon', 'cluster') + ) ) data1 <- 1 : 400 dim(data1) <- c(sdate = 2, ftime = 10, lat = 5, lon = 4) @@ -137,13 +138,17 @@ test_that("2. Output checks", { data1[, 2, 3] <- NA data1 <- list(data = data1, coords = list(lat = 1:5, lon = 1:4)) class(data1) <- 's2dv_cube' - expect_equal( - any(is.na(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), - TRUE + suppressWarnings( + expect_equal( + any(is.na(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), + TRUE + ) ) - expect_equal( - names(dim(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), - c('lat', 'lon', 'cluster') + suppressWarnings( + expect_equal( + names(dim(CST_WeatherRegimes(data = data1, ncenters = 3, EOFs = FALSE)$data)), + c('lat', 'lon', 'cluster') + ) ) }) diff --git a/tests/testthat/test-as.s2dv_cube.R b/tests/testthat/test-as.s2dv_cube.R index f2343080560a11e72ab0f643b7c8df2d9dd33f63..8ff195827853e4e974a147ea75d2617786c9e22e 100644 --- a/tests/testthat/test-as.s2dv_cube.R +++ b/tests/testthat/test-as.s2dv_cube.R @@ -17,145 +17,145 @@ test_that("1. Input checks", { ############################################## -test_that("2. Tests from Load()", { - startDates <- c('20001101', '20011101') - suppressWarnings( - ob1 <- Load(var = 'tas', exp = 'system5c3s', - nmember = 2, sdates = startDates, - leadtimemax = 3, latmin = 30, latmax = 35, - lonmin = 10, lonmax = 20, output = 'lonlat') - ) - res1 <- as.s2dv_cube(ob1) +# test_that("2. Tests from Load()", { +# startDates <- c('20001101', '20011101') +# suppressWarnings( +# ob1 <- Load(var = 'tas', exp = 'system5c3s', +# nmember = 2, sdates = startDates, +# leadtimemax = 3, latmin = 30, latmax = 35, +# lonmin = 10, lonmax = 20, output = 'lonlat') +# ) +# res1 <- as.s2dv_cube(ob1) - # dimensions - expect_equal( - dim(res1$data), - c(dataset = 1, member = 2, sdate = 2, ftime = 3, lat = 6, lon = 11) - ) - # elements - expect_equal( - names(res1), - c("data", "dims", "coords", "attrs") - ) - expect_equal( - names(res1$attrs), - c("Variable", "Datasets", "Dates", "when", "source_files", - "not_found_files", "load_parameters") - ) - # coordinates - expect_equal( - attributes(res1$coords$sdate), - list(indices = FALSE) - ) - expect_equal( - attributes(res1$coords$ftime), - list(indices = TRUE) - ) - expect_equal( - dim(res1$coords$lat), - NULL - ) - expect_equal( - dim(res1$coords$lon), - NULL - ) - expect_equal( - length(res1$coords$lat), - 6 - ) - # Dates - expect_equal( - dim(res1$attrs$Dates), - c(ftime = 3, sdate = 2) - ) -}) +# # dimensions +# expect_equal( +# dim(res1$data), +# c(dataset = 1, member = 2, sdate = 2, ftime = 3, lat = 6, lon = 11) +# ) +# # elements +# expect_equal( +# names(res1), +# c("data", "dims", "coords", "attrs") +# ) +# expect_equal( +# names(res1$attrs), +# c("Variable", "Datasets", "Dates", "when", "source_files", +# "not_found_files", "load_parameters") +# ) +# # coordinates +# expect_equal( +# attributes(res1$coords$sdate), +# list(indices = FALSE) +# ) +# expect_equal( +# attributes(res1$coords$ftime), +# list(indices = TRUE) +# ) +# expect_equal( +# dim(res1$coords$lat), +# NULL +# ) +# expect_equal( +# dim(res1$coords$lon), +# NULL +# ) +# expect_equal( +# length(res1$coords$lat), +# 6 +# ) +# # Dates +# expect_equal( +# dim(res1$attrs$Dates), +# c(ftime = 3, sdate = 2) +# ) +# }) ############################################## -test_that("3. Tests from Load()", { - obs_path <- list(name = "ERA5", - path = "/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc") - ob2 <- Load(var = 'windagl100', obs = list(obs_path), - sdates = '20180301', nmember = 1, - leadtimemin = 1, leadtimemax = 1, - storefreq = "monthly", sampleperiod = 1, - latmin = 36, latmax = 38, lonmin = 0, lonmax = 4, - output = 'lonlat', nprocs = 1, grid = 'r360x181') +# test_that("3. Tests from Load()", { +# obs_path <- list(name = "ERA5", +# path = "/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc") +# ob2 <- Load(var = 'windagl100', obs = list(obs_path), +# sdates = '20180301', nmember = 1, +# leadtimemin = 1, leadtimemax = 1, +# storefreq = "monthly", sampleperiod = 1, +# latmin = 36, latmax = 38, lonmin = 0, lonmax = 4, +# output = 'lonlat', nprocs = 1, grid = 'r360x181') - res2 <- as.s2dv_cube(ob2) +# res2 <- as.s2dv_cube(ob2) - # dimensions - expect_equal( - dim(res2$data), - c(dataset = 1, member = 1, sdate = 1, ftime = 1, lat = 3, lon = 5) - ) - # elements - expect_equal( - names(res2$attrs), - c("Variable", "Datasets", "Dates", "when", "source_files", - "not_found_files", "load_parameters") - ) - # coordinates - expect_equal( - attributes(res2$coords$sdate), - list(indices = FALSE) - ) - expect_equal( - unlist(res2$coords)[1:4], - c(dataset = "ERA5", member = "1", sdate = "20180301", ftime = "1") - ) - expect_equal( - dim(res2$coords$ftime), - NULL - ) - expect_equal( - length(res2$coords$lat), - 3 - ) - # Dates - expect_equal( - dim(res2$attrs$Dates), - c(ftime = 1, sdate = 1) - ) -}) +# # dimensions +# expect_equal( +# dim(res2$data), +# c(dataset = 1, member = 1, sdate = 1, ftime = 1, lat = 3, lon = 5) +# ) +# # elements +# expect_equal( +# names(res2$attrs), +# c("Variable", "Datasets", "Dates", "when", "source_files", +# "not_found_files", "load_parameters") +# ) +# # coordinates +# expect_equal( +# attributes(res2$coords$sdate), +# list(indices = FALSE) +# ) +# expect_equal( +# unlist(res2$coords)[1:4], +# c(dataset = "ERA5", member = "1", sdate = "20180301", ftime = "1") +# ) +# expect_equal( +# dim(res2$coords$ftime), +# NULL +# ) +# expect_equal( +# length(res2$coords$lat), +# 3 +# ) +# # Dates +# expect_equal( +# dim(res2$attrs$Dates), +# c(ftime = 1, sdate = 1) +# ) +# }) ############################################## -test_that("4. Tests from Load()", { - exp <- list(name = 'ecmwfS5', - path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc") - obs <- list(name = 'era5', - path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc') - suppressWarnings( - ob3 <- Load(var = 'prlr', exp = list(exp), obs = list(obs), - sdates = paste0(1993:1995, '1101'), nmember = 1, - storefreq = "monthly", sampleperiod = 1, - latmin = 42, latmax = 45, lonmin = 4, lonmax = 6, - output = 'lonlat', nprocs = 1) - ) - expect_warning( - as.s2dv_cube(ob3), - "The output is a list of two 's2dv_cube' objects corresponding to 'exp' and 'obs'." - ) - suppressWarnings( - res3 <- as.s2dv_cube(ob3) - ) +# test_that("4. Tests from Load()", { +# exp <- list(name = 'ecmwfS5', +# path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc") +# obs <- list(name = 'era5', +# path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc') +# suppressWarnings( +# ob3 <- Load(var = 'prlr', exp = list(exp), obs = list(obs), +# sdates = paste0(1993:1995, '1101'), nmember = 1, +# storefreq = "monthly", sampleperiod = 1, +# latmin = 42, latmax = 45, lonmin = 4, lonmax = 6, +# output = 'lonlat', nprocs = 1) +# ) +# expect_warning( +# as.s2dv_cube(ob3), +# "The output is a list of two 's2dv_cube' objects corresponding to 'exp' and 'obs'." +# ) +# suppressWarnings( +# res3 <- as.s2dv_cube(ob3) +# ) - # dimensions - expect_equal( - dim(res3[[1]]$data), - c(dataset = 1, member = 1, sdate = 3, ftime = 8, lat = 4, lon = 3) - ) - expect_equal( - unlist(res3[[1]]$coords)[1:4], - c(dataset = "ecmwfS5", member = "1", sdate1 = "19931101", sdate2 = "19941101") - ) - # Dates - expect_equal( - dim(res3[[1]]$attrs$Dates), - dim(res3[[2]]$attrs$Dates) - ) -}) +# # dimensions +# expect_equal( +# dim(res3[[1]]$data), +# c(dataset = 1, member = 1, sdate = 3, ftime = 8, lat = 4, lon = 3) +# ) +# expect_equal( +# unlist(res3[[1]]$coords)[1:4], +# c(dataset = "ecmwfS5", member = "1", sdate1 = "19931101", sdate2 = "19941101") +# ) +# # Dates +# expect_equal( +# dim(res3[[1]]$attrs$Dates), +# dim(res3[[2]]$attrs$Dates) +# ) +# }) ############################################## @@ -166,9 +166,9 @@ test_that("5. Tests from Start()", { var = 'tas', sdate = c('20170101', '20180101'), ensemble = indices(1:3), - time = 'all', - latitude = indices(1:10), - longitude = indices(1:10), + time = indices(1:3), + latitude = indices(1:2), + longitude = indices(1:2), return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'), retrieve = TRUE) ) @@ -178,7 +178,7 @@ test_that("5. Tests from Start()", { # dimensions expect_equal( dim(res4$data), - c(dat = 1, var = 1, sdate = 2, ensemble = 3, time = 7, latitude = 10, longitude = 10) + c(dat = 1, var = 1, sdate = 2, ensemble = 3, time = 3, latitude = 2, longitude = 2) ) # elements expect_equal( @@ -204,12 +204,12 @@ test_that("5. Tests from Start()", { ) expect_equal( length(res4$coords$latitude), - 10 + 2 ) # Dates expect_equal( dim(res4$attrs$Dates), - c(sdate = 2, time = 7) + c(sdate = 2, time = 3) ) }) @@ -228,10 +228,10 @@ test_that("6. Tests from Start()", { suppressWarnings( hcst <- Start(dat = anlgs, var = vari, - latitude = indices(1:4), #'all', - longitude= indices(1:4), #'all', - member= indices(1), #'all', - time = 'all', + latitude = indices(1:2), #'all', + longitude = indices(1:2), #'all', + member = indices(1), #'all', + time = indices(1:3), syear = indices(1:4), file_date = file_date_array, split_multiselected_dims = TRUE, @@ -248,7 +248,7 @@ test_that("6. Tests from Start()", { # dimensions expect_equal( dim(res5$data), - c(dat = 1, var = 1, latitude = 4, longitude = 4, member = 1, time = 4, + c(dat = 1, var = 1, latitude = 2, longitude = 2, member = 1, time = 3, syear = 4, sweek = 2, sday = 3) ) # elements