diff --git a/R/Start.R b/R/Start.R index c741de2226c5184f93eaef261aa4743ae258d53e..7eb43af280d50ad19c03aacf4670872c6663be1f 100644 --- a/R/Start.R +++ b/R/Start.R @@ -893,7 +893,7 @@ Start <- function(..., # dim = indices/selectors, dim_params, dim_reorder_params) # Check if file pattern contains '$var$' substring - if (!grepl("$var$", dim_params[[found_pattern_dim]], fixed = TRUE)) { + if (any(!grepl("$var$", dim_params[[found_pattern_dim]], fixed = TRUE))) { .warning(paste0("The special wildcard '$var$' is not present in the file ", "path. This might cause Start() to fail if it cannot parse", "the inner dimensions in all the files.")) @@ -2503,6 +2503,10 @@ Start <- function(..., # dim = indices/selectors, } # Find the largest length of each time step inner_dim_lengths <- do.call(pmax, inner_dim_lengths_cat) + ## NOTE: NA values can be present if the size of a depending + ## dimension varies along its depended dim. Removing them allows + ## retrieval of the common indices. Could cause other issues? + inner_dim_lengths <- inner_dim_lengths[which(!is.na(inner_dim_lengths))] } fri <- first_round_indices <- NULL diff --git a/tests/testthat/test-Start-multiple_depends.R b/tests/testthat/test-Start-multiple_depends.R new file mode 100644 index 0000000000000000000000000000000000000000..473d0614522458decee0aaae21a4d3bc36f72f20 --- /dev/null +++ b/tests/testthat/test-Start-multiple_depends.R @@ -0,0 +1,65 @@ +suppressMessages({ +# This unit test tests the case where a depended dimension has multiple +# depending dimensions and the 'all' selector is used for a depending dim. + +path <- "/esarchive/exp/CMIP6/$dcpp$/HadGEM3-GC31-MM/DCPP/MOHC/HadGEM3-GC31-MM/$dcpp$/r1i1p1f2/Omon/tos/gn/v20200417/$var$_Omon_HadGEM3-GC31-MM_$dcpp$_s$sdate$-r1i1p1f2_gn_$chunk$.nc" + +path <- paste0('/esarchive/scratch/aho/startR_unittest_files', path) + +sdates <- c('2018', '2019') + +test_that("1. ", { +suppressWarnings( +dat1 <- Start(dat = path, + var = 'tos', + chunk = 'all', + time = indices(1:14), + time_across = 'chunk', + sdate = sdates, + dcpp = list('2018' = "dcppA-hindcast", '2019' = "dcppB-forecast"), + dcpp_depends = 'sdate', + chunk_depends = 'sdate', + merge_across_dims = TRUE, + largest_dims_length = TRUE, + i = indices(450:460), + j = indices(685:700), + return_vars = list(time = c('chunk', 'sdate')), + retrieve = TRUE) +) + +suppressWarnings( +dat2 <- Start(dat = path, + var = 'tos', + chunk = list('2018' = c('201811-201812', '201901-201912'), + '2019' = c('201911-201912', '202001-202012')), + time = 'all', + time_across = 'chunk', + sdate = sdates, + dcpp = list('2018' = "dcppA-hindcast", '2019' = "dcppB-forecast"), + dcpp_depends = 'sdate', + chunk_depends = 'sdate', + merge_across_dims = TRUE, + largest_dims_length = TRUE, + i = indices(450:460), + j = indices(685:700), + return_vars = list(time = c('chunk', 'sdate')), + retrieve = TRUE) +) + +expect_equal( + as.vector(dat1), + as.vector(dat2) +) +expect_equal( + mean(dat2, na.rm = T), + 29.21144, + tolerance = 0.0001 +) +expect_equal( + dat1[1, 1, 2, 2, 1, 1:3, 10], + c(28.84955, 28.84827, 28.84126), + tolerance = 0.0001 + ) +}) + +}) #suppressMessages