From b618bd26e247bae026b2d73f146a4f40a4c007c1 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 13 Aug 2020 17:08:03 +0200 Subject: [PATCH] Bugfix for metadata_dims when more than 1 dat. Unit test is created. --- R/Start.R | 15 ++++ tests/testthat/test-Start-metadata_dims.R | 100 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/testthat/test-Start-metadata_dims.R diff --git a/R/Start.R b/R/Start.R index 62d7e8c..7a9e27a 100644 --- a/R/Start.R +++ b/R/Start.R @@ -4109,7 +4109,22 @@ Start <- function(..., # dim = indices/selectors, if (!silent) { .message("Successfully retrieved data.") } + +# NOTE: The original var_backup saves only the first of the list (i.e., 1st dat). +# If there is more than one dat, it should be put under each dat rather than $common. +# Question: If there is only one dat, should it be under $common or $dat1? +# It is under $common now. + if (length(attr(data_array, 'Variables')) > 1) { # more than 1 dat. Put under each dat + var_backup <- attr(data_array, 'Variables') + for (kk in 1:length(var_backup)) { + picked_vars[[kk]][[names(var_backup[[kk]])]] <- var_backup[[kk]][[1]] + } + var_backup <- NULL + + } else { #old code var_backup <- attr(data_array, 'Variables')[[1]] + } + 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-metadata_dims.R b/tests/testthat/test-Start-metadata_dims.R new file mode 100644 index 0000000..a771f98 --- /dev/null +++ b/tests/testthat/test-Start-metadata_dims.R @@ -0,0 +1,100 @@ +context("Start() metadata_dims check") + +test_that("1. One data set", { + repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc" + data <- Start(dat = list(list(name = 'system5_m1', path = repos)), + var = 'tas', + sdate = '20170101', + ensemble = indices(1), + time = indices(1), + lat = indices(1:10), + lon = indices(1:10), + synonims = list(lat = c('lat', 'latitude'), + lon = c('lon', 'longitude')), + return_vars = list(time = 'sdate', + longitude = 'dat', + latitude = 'dat'), + metadata_dims = 'dat', # it can be omitted since it is automatically specified as 'dat' + retrieve = T + ) + + expect_equal( + length(attr(data, 'Variables')), + 2 + ) + expect_equal( + names(attr(data, 'Variables')), + c("common", "system5_m1") + ) + expect_equal( + names(attr(data, 'Variables')$common), + c('time', 'tas') + ) + expect_equal( + names(attr(data, 'Variables')$system5_m1), + c("longitude", "latitude") + ) + expect_equal( + length(attr(data, 'Variables')$common$tas), + 12 + ) + +} + + +test_that("2. Two data sets", { + repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc" + repos2 <- "/esarchive/exp/ecmwf/system4_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc" + + data <- Start(dat = list(list(name = 'system4_m1', path = repos2), + list(name = 'system5_m1', path = repos)), + var = 'tas', + sdate = '20170101', + ensemble = indices(1), + time = indices(1), + lat = indices(1:10), + lon = indices(1:10), + synonims = list(lat = c('lat', 'latitude'), + lon = c('lon', 'longitude')), + return_vars = list(time = 'sdate', + longitude = 'dat', + latitude = 'dat'), + metadata_dims = 'dat', # it can be omitted since it is automatically specified as 'dat' + retrieve = T + ) + + expect_equal( + length(attr(data, 'Variables')), + 3 + ) + expect_equal( + names(attr(data, 'Variables')), + c("common", "system4_m1", "system5_m1") + ) + expect_equal( + names(attr(data, 'Variables')$common), + 'time' + ) + expect_equal( + names(attr(data, 'Variables')$system4_m1), + c("longitude", "latitude", "tas") + ) + expect_equal( + names(attr(data, 'Variables')$system5_m1), + c("longitude", "latitude", "tas") + ) + expect_equal( + length(attr(data, 'Variables')$system5_m1$tas), + 12 + ) + expect_equal( + length(attr(data, 'Variables')$system4_m1$tas), + 11 + ) + +} + +test_that("3. Specify metadata_dims with another file dimension", { + + +} -- GitLab