diff --git a/R/Start.R b/R/Start.R index 8243fda0d5826a1da7ba09e65b0524dab2ecac4e..be2e10230a3a4577e6318dee419bbb2d38091c9c 100644 --- a/R/Start.R +++ b/R/Start.R @@ -4218,19 +4218,27 @@ Start <- function(..., # dim = indices/selectors, } else { #(1) var_backup <- attr(data_array, 'Variables') - var_backup_names <- unlist(lapply(var_backup, names)) - new_list <- vector('list', length = length(var_backup_names)) + len <- unlist(lapply(var_backup, length)) + len <- sum(len) + length(which(len == 0)) #0 means NULL + name_list <- lapply(var_backup, names) + new_list <- vector('list', length = len) count <- 1 + for (kk in 1:length(var_backup)) { - for (jj in 1:length(var_backup[[kk]])) { - new_list[[count]] <- var_backup[[kk]][[jj]] + if (length(var_backup[[kk]]) == 0) { #NULL count <- count + 1 + } else { + for (jj in 1:length(var_backup[[kk]])) { + new_list[[count]] <- var_backup[[kk]][[jj]] + names(new_list)[count] <- name_list[[kk]][jj] + count <- count + 1 + } } } - names(new_list) <- var_backup_names var_backup <- new_list } } + attr(data_array, 'Variables') <- NULL attributes(data_array) <- c(attributes(data_array), list(Variables = c(list(common = c(picked_common_vars, var_backup)), diff --git a/tests/testthat/test-Start-first_file_missing.R b/tests/testthat/test-Start-first_file_missing.R index 95319aee359cc3fc462ad72aaeaef50a4a5d397f..25f4d0241cea4b25d642db5863f79fd8df08e0f6 100644 --- a/tests/testthat/test-Start-first_file_missing.R +++ b/tests/testthat/test-Start-first_file_missing.R @@ -12,7 +12,7 @@ sdates1 <- c('20130611') #exists sdates2 <- c('20130618') #does not exist sdates3 <- c("20130611", "20130618") #1st exists, 2nd missing sdates4 <- c("20130618", "20130611") #1st missing, 2nd exists - +sdates5 <- c("20130611", "20130612") #both exist test_that("1. first file missing, no assign parameter 'metadata_dims'", { @@ -69,7 +69,7 @@ data <- Start(dat = file, return_vars = list(latitude = 'dat', longitude = 'dat', time = 'file_date'), -# metadata_dims = c('file_date'), + metadata_dims = c('file_date'), retrieve = T) expect_equal( @@ -87,11 +87,11 @@ data <- Start(dat = file, ) expect_equal( names(attr(data, 'Variables')$common), - c('time') + c('time', NA, 'tas') ) }) -test_that("3. Use parameter 'metadata_dims', all common attributes", { +test_that("3. Use parameter 'metadata_dims', all common attributes, 1st file missing", { data <- Start(dat = file, var = var, @@ -112,7 +112,7 @@ data <- Start(dat = file, expect_equal( names(attr(data, 'Variables')$common), - c('latitude', 'longitude', 'time', 'tas', NA) + c('latitude', 'longitude', 'time', NA, 'tas') ) expect_equal( as.vector(attr(data, 'NotFoundFiles')), @@ -121,3 +121,62 @@ data <- Start(dat = file, }) +test_that("4. Use parameter 'metadata_dims', all common attributes, 2nd file missing", { + +data <- Start(dat = file, + var = var, + file_date = sdates3, + time = indices(1:4), + latitude = values(list(20, 30)), + latitude_reorder = Sort(decreasing = TRUE), + longitude = values(list(-20, -10)), + longitude_reorder = CircularSort(-180, 180), + ensemble = indices(1), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('lon', 'longitude')), + return_vars = list(latitude = NULL, + longitude = NULL, + time = 'file_date'), + metadata_dims = c('file_date'), + retrieve = T) + + expect_equal( + names(attr(data, 'Variables')$common), + c('latitude', 'longitude', 'time', 'tas', NA) + ) + expect_equal( + as.vector(attr(data, 'NotFoundFiles')), + c(NA, "/esarchive/exp/ncep/cfs-v2/weekly_mean/s2s/tas_f24h/tas_20130618.nc") + ) + +}) + +test_that("5. Use parameter 'metadata_dims', all common attributes, no file missing", { + +data <- Start(dat = file, + var = var, + file_date = sdates5, + time = indices(1:4), + latitude = values(list(20, 30)), + latitude_reorder = Sort(decreasing = TRUE), + longitude = values(list(-20, -10)), + longitude_reorder = CircularSort(-180, 180), + ensemble = indices(1), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('lon', 'longitude')), + return_vars = list(latitude = NULL, + longitude = NULL, + time = 'file_date'), + metadata_dims = c('file_date'), + retrieve = T) + + expect_equal( + names(attr(data, 'Variables')$common), + c('latitude', 'longitude', 'time', 'tas', 'tas') + ) + expect_equal( + as.vector(attr(data, 'NotFoundFiles')), + NULL + ) + +}) diff --git a/tests/testthat/test-Start-path_glob_permissive.R b/tests/testthat/test-Start-path_glob_permissive.R index 9e14c0252df03d7a1b9503a12fc2b219cc2baeac..fecb69d39878f5a11334770062a78cf07fe788ec 100644 --- a/tests/testthat/test-Start-path_glob_permissive.R +++ b/tests/testthat/test-Start-path_glob_permissive.R @@ -38,7 +38,7 @@ data <- Start(dat = repos, ) expect_equal( data[1, 1, , , 1, 1], - matrix(c(18.60422, 17.13862, 18.52348, 17.21780), 2, 2), + array(c(18.60422, 17.13862, 18.52348, 17.21780), dim = c(expid = 2, year = 2)), tolerance = 0.0001 ) @@ -79,7 +79,7 @@ data <- Start(dat = repos, ) expect_equal( data[1, 1, , , 1, 1, 1], - matrix(c(18.60422, 17.13862, 18.52348, 17.21780), 2, 2), + array(c(18.60422, 17.13862, 18.52348, 17.21780), dim = c(expid = 2, year = 2)), tolerance = 0.0001 ) expect_equal(