Commit f68e23a6 authored by aho's avatar aho
Browse files

Modify time unit judgement to consider the time variable that doesn't have name 'time'.

parent 8f7482c6
Pipeline #7056 passed with stage
in 60 minutes and 27 seconds
......@@ -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']]
......
......@@ -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 = ', '),
......
......@@ -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", {
......
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
#})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment