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')
)

})
