diff --git a/.Rbuildignore b/.Rbuildignore index aa7059a3b2102a246821ef37d6ddd1d831deb5f0..39771b3df13ec8782f7cfd434821880d1c37ec1a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,7 +9,7 @@ ^inst/doc$ ^\.gitlab-ci\.yml$ ## unit tests should be ignored when building the package for CRAN -^tests$ +# ^tests$ ^inst/PlotProfiling\.R$ ^.gitlab$ # Suggested by http://r-pkgs.had.co.nz/package.html diff --git a/R/Start.R b/R/Start.R index 2be297b1cf0df03679544b751ef45ca2b6eabc30..42f72b6ff21519f53409f4269cf345296f78f780 100644 --- a/R/Start.R +++ b/R/Start.R @@ -3279,8 +3279,11 @@ Start <- function(..., # dim = indices/selectors, indices_chunk <- c(indices_chunk, rep(item, length(tmp) - length(indices_chunk))) } sub_array_of_indices_by_file <- split(sub_array_of_indices, indices_chunk) - for (item in 2:length(sub_array_of_indices_by_file)) { - sub_array_of_indices_by_file[[item]] <- sub_array_of_indices_by_file[[item]] - cumsum(inner_dim_lengths)[item - 1] + for (item in names((sub_array_of_indices_by_file))) { + # If item is 1, cumsum(inner_dim_lengths)[item - 1] returns numeric(0) + if (as.numeric(item) > 1) { + sub_array_of_indices_by_file[[item]] <- sub_array_of_indices_by_file[[item]] - cumsum(inner_dim_lengths)[as.numeric(item) - 1] + } } transformed_indices <- unlist(sub_array_of_indices_by_file, use.names = FALSE) } diff --git a/tests/testthat/test-Start-DCPP-across-depends-largest_dims_length.R b/tests/testthat/test-Start-DCPP-across-depends-largest_dims_length.R new file mode 100644 index 0000000000000000000000000000000000000000..3e10c55392f16d30af0c00792f2ee0ad908b7d8f --- /dev/null +++ b/tests/testthat/test-Start-DCPP-across-depends-largest_dims_length.R @@ -0,0 +1,63 @@ +suppressMessages({ +test_that("Chunks of DCPP files with largest_dims_length = TRUE - Local execution", { + path <- '/esarchive/exp/CMIP6/dcppA-hindcast/HadGEM3-GC31-MM/DCPP/MOHC/HadGEM3-GC31-MM/dcppA-hindcast/r1i1p1f2/Omon/tos/gn/v20200417/$var$_Omon_HadGEM3-GC31-MM_dcppA-hindcast_s$sdate$-r1i1p1f2_gn_$chunk$.nc' + path <- paste0('/esarchive/scratch/aho/startR_unittest_files/', path) + + sdates <- c('2017', '2018') +suppressWarnings( + dat1 <- Start(dat = path, + var = 'tos', + sdate = sdates, + chunk = 'all', + chunk_depends = 'sdate', + time = indices(1:15), + i = indices(1:10), + j = indices(1:10), + time_across = 'chunk', + merge_across_dims = TRUE, + largest_dims_length = TRUE, + retrieve = TRUE, + return_vars = list(time = 'sdate')) +) + +# Start at chunk 2 (skip time steps in the first file) +suppressWarnings( + dat2 <- Start(dat = path, + var = 'tos', + sdate = sdates, + chunk = 'all', + chunk_depends = 'sdate', + time = indices(3:15), + i = indices(1:10), + j = indices(1:10), + time_across = 'chunk', + merge_across_dims = TRUE, + largest_dims_length = TRUE, + retrieve = TRUE, + return_vars = list(time = 'sdate')) +) + +expect_equal(dat1[1, 1, 1:2, 3:15, 10, 10], dat2[1, 1, 1:2, , 10, 10]) + +# Start at chunk 3 (skip time steps in the first and second files) +suppressWarnings( + dat3 <- Start(dat = path, + var = 'tos', + sdate = sdates, + chunk = 'all', + chunk_depends = 'sdate', + time = indices(15), + i = indices(1:10), + j = indices(1:10), + time_across = 'chunk', + merge_across_dims = TRUE, + largest_dims_length = TRUE, + retrieve = TRUE, + return_vars = list(time = 'sdate')) +) + +expect_equal(dat1[1, 1, 1:2, 15, 10, 10], dat3[1, 1, 1:2, , 10, 10]) + +}) + +}) #suppressMessages