From e69a69bbea3f5888dc63ebeddd7e8bdf867fd61c Mon Sep 17 00:00:00 2001 From: vagudets Date: Tue, 30 Jul 2024 16:08:25 +0200 Subject: [PATCH 1/6] Create unit tests for multiple depending dims --- tests/testthat/test-Start-multiple_depends.R | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/testthat/test-Start-multiple_depends.R diff --git a/tests/testthat/test-Start-multiple_depends.R b/tests/testthat/test-Start-multiple_depends.R new file mode 100644 index 0000000..bf600e5 --- /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 -- GitLab From 46634b41f5ec0f5724df544ac75bcb8b1a543498 Mon Sep 17 00:00:00 2001 From: vagudets Date: Mon, 2 Sep 2024 16:11:33 +0200 Subject: [PATCH 2/6] Update unit test --- tests/testthat/test-Start-multiple_depends.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-Start-multiple_depends.R b/tests/testthat/test-Start-multiple_depends.R index bf600e5..473d061 100644 --- a/tests/testthat/test-Start-multiple_depends.R +++ b/tests/testthat/test-Start-multiple_depends.R @@ -4,7 +4,7 @@ suppressMessages({ 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) +path <- paste0('/esarchive/scratch/aho/startR_unittest_files', path) sdates <- c('2018', '2019') -- GitLab From a110112a395d55b5bfadfbe9297a0d418d118644 Mon Sep 17 00:00:00 2001 From: vagudets Date: Mon, 2 Sep 2024 16:15:40 +0200 Subject: [PATCH 3/6] Remove NA values from inner_dim_lengths --- R/Start.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/Start.R b/R/Start.R index c741de2..839c130 100644 --- a/R/Start.R +++ b/R/Start.R @@ -2503,6 +2503,7 @@ Start <- function(..., # dim = indices/selectors, } # Find the largest length of each time step inner_dim_lengths <- do.call(pmax, inner_dim_lengths_cat) + inner_dim_lengths <- inner_dim_lengths[which(!is.na(inner_dim_lengths)] } fri <- first_round_indices <- NULL -- GitLab From c7f35b3e175c76cf8dc5964096fe0b64131115ab Mon Sep 17 00:00:00 2001 From: vagudets Date: Tue, 3 Sep 2024 10:44:35 +0200 Subject: [PATCH 4/6] Fix condition to check $ --- R/Start.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Start.R b/R/Start.R index c741de2..f1a1991 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.")) -- GitLab From 3d4eb815e185a14e267b658ca29ce2449e9c6376 Mon Sep 17 00:00:00 2001 From: vagudets Date: Tue, 3 Sep 2024 12:30:14 +0200 Subject: [PATCH 5/6] Fix bug (missing parenthesis) --- R/Start.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Start.R b/R/Start.R index 4cf76dd..dfa0034 100644 --- a/R/Start.R +++ b/R/Start.R @@ -2503,7 +2503,7 @@ Start <- function(..., # dim = indices/selectors, } # Find the largest length of each time step inner_dim_lengths <- do.call(pmax, inner_dim_lengths_cat) - inner_dim_lengths <- inner_dim_lengths[which(!is.na(inner_dim_lengths)] + inner_dim_lengths <- inner_dim_lengths[which(!is.na(inner_dim_lengths))] } fri <- first_round_indices <- NULL -- GitLab From 46b20dd7ed767de7fe0e02e23e638e549a0d7b5f Mon Sep 17 00:00:00 2001 From: vagudets Date: Fri, 6 Sep 2024 10:29:23 +0200 Subject: [PATCH 6/6] Add comment --- R/Start.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/Start.R b/R/Start.R index dfa0034..7eb43af 100644 --- a/R/Start.R +++ b/R/Start.R @@ -2503,6 +2503,9 @@ 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))] } -- GitLab