#---------------------------------------------------
# If assign a selector with an array that has file dim as dimension, Start() read 
# the values depending on the the file dim. 
#---------------------------------------------------
context("Start() implicit inner dimension")


test_that("1. region with different index between files", {

path <- paste0('/esarchive/exp/ecearth/a35b/diags/DCPP/EC-Earth-Consortium/',
               'EC-Earth3-HR/dcppA-hindcast/r1i1p1f1/Omon/$var$_mixed/gn/v20201107/',
               '$var$_Omon_EC-Earth3-HR_dcppA-hindcast_s$sdate$-r1i1p1f1_gn_$chunk$.nc')

# two sdates have different index for Nino3. 
region <- array('Nino3', dim = c(sdate = 2, region = 1))

data <- Start(dat = path,
              var = 'tosmean',
              sdate = c('1993', '2013'),
              chunk = indices(1:2),
              chunk_depends = 'sdate',
              region = region,
              time = 'all', 
              time_across = 'chunk',
              merge_across_dims = TRUE,
              return_vars = list(time = c('sdate', 'chunk'),
                                 region = 'sdate'),
               retrieve = T)

data1 <- Start(dat = path,
              var = 'tosmean',
              sdate = c('1993'),
              chunk = indices(1:2),
              chunk_depends = 'sdate',
              region = 'Nino3',
              time = 'all', #c(1:length(forecast_month)),
              time_across = 'chunk',
              merge_across_dims = TRUE,
              return_vars = list(time = c('sdate', 'chunk'),
                                 region = NULL),
               retrieve = T)

data2 <- Start(dat = path,
              var = 'tosmean',
              sdate = c('2013'),
              chunk = indices(1:2),
              chunk_depends = 'sdate',
              region = 'Nino3',
              time = 'all', #c(1:length(forecast_month)),
              time_across = 'chunk',
              merge_across_dims = TRUE,
              return_vars = list(time = c('sdate', 'chunk'),
                                 region = NULL),
               retrieve = T)

expect_equal(
dim(data),
c(dat = 1, var = 1, sdate = 2, region = 1, time = 2)
)
expect_equal(
data[1, 1, 1, 1, ],
data1[1, 1, 1, 1, ]
)
expect_equal(
data[1, 1, 2, 1, ],
data2[1, 1, 1, 1, ]
)


})
