From 9e8c32967cc91271d8c5464eeb6ff3f86baf9c38 Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 1 Mar 2021 19:57:16 +0100 Subject: [PATCH] Consider implicit inner dim in return_vars name check. Unit test is created. --- R/Start.R | 6 +- .../testthat/test-Start-implicit_inner_dim.R | 89 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-Start-implicit_inner_dim.R diff --git a/R/Start.R b/R/Start.R index 33d5809..80e1c83 100644 --- a/R/Start.R +++ b/R/Start.R @@ -1403,8 +1403,12 @@ Start <- function(..., # dim = indices/selectors, #///////////////////////////////////////////////////// # Check if return_vars name is inner dim name. If it is synonim, change back to # inner dim name and return a warning. + dim_params_found_file_dims <- dim_params[found_file_dims[[i]]] + file_dims_array_dim_names <- unlist(lapply(lapply(dim_params_found_file_dims, dim), + names)) if (any(!names(return_vars) %in% expected_inner_dims[[i]] & - !names(return_vars) %in% unlist(var_params))) { + !names(return_vars) %in% unlist(var_params) & + !names(return_vars) %in% file_dims_array_dim_names)) { not_inner_dimname_ind <- which(!names(return_vars) %in% expected_inner_dims[[i]]) for (not_inner_dim in 1:length(not_inner_dimname_ind)) { wrong_name_return_vars <- names(return_vars)[not_inner_dimname_ind[not_inner_dim]] diff --git a/tests/testthat/test-Start-implicit_inner_dim.R b/tests/testthat/test-Start-implicit_inner_dim.R new file mode 100644 index 0000000..621fe5b --- /dev/null +++ b/tests/testthat/test-Start-implicit_inner_dim.R @@ -0,0 +1,89 @@ +context("Start() implicit inner dimension") +# The unit test is for the implicitly defined inner dimension. If a file dimension selector +# is an array with named dimensions, and 'split_multiselected_dims' is used, then the file +# dim can be split into multiple dimensions that may contain inner dimensions. + +# merge_across_dims + split_multiselected_dims + implicit inner dim???? + +#--------------------------------------------------------------- + +test_that("1. Split into inner dimension", { + +obs.path <- "/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$file_date$.nc" +variable <- "prlr" +dates_file <- c("201311","201312","201411","201412") +dim(dates_file) <- c(time = 2, syear = 2) + +# (1) +suppressWarnings( +obs <- Start(dat = obs.path, + var = variable, + file_date = dates_file, + latitude = values(list(35.6, 40)), + latitude_reorder = Sort(decreasing = TRUE), + longitude = values(list(-10, 10)), + longitude_reorder = CircularSort(-180, 180), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('lon', 'longitude')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'file_date'), + split_multiselected_dims = TRUE, + retrieve = FALSE) +) + +expect_equal( +attr(obs, 'Dimensions'), +c(dat = 1, var = 1, time = 2, syear = 2, latitude = 18, longitude = 81) +) +expect_equal( +dim(attr(obs, 'Variables')$common$time), +c(file_date = 4, time = 1) +) +expect_equal( +attr(obs, 'Variables')$common$time[1, 1], +as.POSIXct('2013-11-15 23:30:00', tz = 'UTC') +) + +}) + + +test_that("2. Split into file dimension", { + +obs.path <- "/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h-r1440x721cds/$var$_$file_date$.nc" +variable <- "prlr" +dates_file <- c("201311","201312","201411","201412") +dim(dates_file) <- c(smonth = 2, syear = 2) + +suppressWarnings( +obs <- Start(dat = obs.path, + var = variable, + file_date = dates_file, + time = 'all', + latitude = values(list(35.6, 40)), + latitude_reorder = Sort(decreasing = TRUE), + longitude = values(list(-10, 10)), + longitude_reorder = CircularSort(-180, 180), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('lon', 'longitude')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'file_date'), + split_multiselected_dims = TRUE, + retrieve = FALSE) +) + +expect_equal( +attr(obs, 'Dimensions'), +c(dat = 1, var = 1, smonth = 2, syear = 2, time = 1, latitude = 18, longitude = 81) +) +expect_equal( +dim(attr(obs, 'Variables')$common$time), +c(file_date = 4, time = 1) +) +expect_equal( +attr(obs, 'Variables')$common$time[1, 1], +as.POSIXct('2013-11-15 23:30:00', tz = 'UTC') +) + +}) -- GitLab