diff --git a/R/NcDataReader.R b/R/NcDataReader.R index 95fd1f1f46d5e069006cc2308ddeef7992ffecba..9c85e331b04d1ce237f7269b2841c225541cc9e6 100644 --- a/R/NcDataReader.R +++ b/R/NcDataReader.R @@ -184,8 +184,17 @@ NcDataReader <- function(file_path = NULL, file_object = NULL, }) if (length(names(attr(result, 'variables'))) == 1) { - # The 1st condition is for implicit time dim (if time length = 1, it is allowed to not be defined in Start call. Therefore, it is not in the list of synonims) - if (names(attr(result, 'variables')) == 'time' | 'time' %in% synonims[[names(attr(result, 'variables'))]]) { + # The 1st condition is for implicit time dim (if time length = 1, it is + # allowed to not be defined in Start call. Therefore, it is not in the list + # of synonims); + # the 2nd condition is for the normal case; the 3rd one is that if return_vars + # has a variable that is not 'time'. The only way to know if it should be time + # is to check calendar. + # All these conditions are to prevent the variables with time-like units but + # actually not a time variable, e.g., drought period [days]. + if (names(attr(result, 'variables')) == 'time' | + 'time' %in% synonims[[names(attr(result, 'variables'))]] | + 'calendar' %in% names(attr(result, 'variables')[[1]])) { var_name <- names(attr(result, 'variables')) units <- attr(result, 'variables')[[var_name]][['units']] diff --git a/R/zzz.R b/R/zzz.R index d946ebf538f5986985acff7f7bc81961489cc870..6b6189b3c0697892ba35a3f78eecb0db42832343 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1430,7 +1430,7 @@ generate_picked_var_of_read <- function(var_to_read, var_to_check, array_of_file if (any(names(array_var_dims) %in% names(var_file_dims))) { array_var_dims <- array_var_dims[-which(names(array_var_dims) %in% names(var_file_dims))] } - if (names(array_var_dims) != names(var_dims)) { + if (any(names(array_var_dims) != names(var_dims))) { stop("Error while reading the variable '", var_to_read, "' from ", "the file. Dimensions do not match.\nExpected ", paste(paste0("'", names(array_var_dims), "'"), collapse = ', '), diff --git a/tests/testthat/test-Start-implicit_inner_dim.R b/tests/testthat/test-Start-implicit_inner_dim.R index 6a3262a6fe17373fb0d7df8469f593b545e76744..3788af0f48c41729cf56bcc2a00049bda3561c7e 100644 --- a/tests/testthat/test-Start-implicit_inner_dim.R +++ b/tests/testthat/test-Start-implicit_inner_dim.R @@ -3,6 +3,7 @@ context("Start() implicit inner dimension") # startR allows it not to be specified in the call. Users can still define it in # 'return_vars'. #--------------------------------------------------------------- +#NOTE: Also useful for test-Start-time_unit.R test3 test_that("1. time = 1", { diff --git a/tests/testthat/test-Start-time_unit.R b/tests/testthat/test-Start-time_unit.R new file mode 100644 index 0000000000000000000000000000000000000000..3aa193042caec24d0065c1eb60d987a3fb96aa0a --- /dev/null +++ b/tests/testthat/test-Start-time_unit.R @@ -0,0 +1,91 @@ +context("To detect the variable with time format and adjust the units") + +test_that("1. The data has units like time", { + + +suppressWarnings( +FD <- Start(dat = '/esarchive/obs/ukmo/hadex3/original_files/1961-90/HadEX3_$var$_MON.nc', + var = 'FD', # units: days + time = indices(1), + longitude = indices(1), + latitude = indices(1), + num_procs = 1, + return_vars = list(time = NULL), + retrieve = TRUE) +) +suppressWarnings( +FD2 <- Start(dat = '/esarchive/obs/ukmo/hadex3/original_files/1961-90/HadEX3_$var$_MON.nc', + var = 'FD', # units: days + time = indices(1), + longitude = indices(1), + latitude = indices(1), + num_procs = 1, +# return_vars = list(time = NULL), + retrieve = TRUE) +) +expect_equal( +attr(FD, 'Variables')$common$FD$units, +'days' +) +expect_equal( +attr(FD2, 'Variables')$common$FD$units, +'days' +) + + +}) + +test_that("2. The metadata variable name is not time", { +# VITIGEOOS + +vari <- "rsds" +anlgs <- paste0("/esarchive/oper/VITIGEOSS","/output/cfsv2/weekly_mean/", + "$var$/$var$-vitigeoss-cat","_1999-2018_", "$file_date$.nc") + +file_date_array <- array(dim = c(sweek = 2, sday = 3)) +file_date_array[, 1] <- c(paste0('04', c('04', '07'))) +file_date_array[, 2] <- c(paste0('04', c('07', '11'))) +file_date_array[, 3] <- c(paste0('04', c('11', '14'))) + +suppressWarnings( + hcst <- Start(dat = anlgs, + var = vari, + latitude = indices(1), #'all', + longitude= indices(1), #'all', + member= indices(1), #'all', + time = 'all', # inner dim!! + syear = 'all', #inner dim!! + file_date = file_date_array, + split_multiselected_dims = TRUE, + retrieve = T, + return_vars = list(leadtimes = 'file_date'), + synonims = list(longitude = c('lon', 'longitude'), + latitude = c('lat', 'latitude'), + syear = c('sdate','syear'), + member = c('ensemble','member'))) +) +time_attr <- attr(hcst, 'Variables')$common$leadtimes + +expect_equal( +dim(time_attr), +c(sweek = 2, sday = 3, syear = 20, time = 4) +) +expect_equal( +time_attr[,1,1,1], +as.POSIXct(c("1999-04-08","1999-04-11"), tz = "UTC") +) +expect_equal( +time_attr[2,,1,1], +as.POSIXct(c("1999-04-11","1999-04-15", "1999-04-18"), tz = "UTC") +) +expect_equal( +time_attr[1,1,20,], +as.POSIXct(c("2018-04-08", "2018-04-15", "2018-04-22 UTC", "2018-04-29 UTC"), tz = "UTC") +) + +}) + + +#test_that("3. Time dimension is implicit", { +# See test-Start-implicit_inner_dim.R +#})