library(startR)

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')
# They three all work (with the newest development)
region <- array(c('Nino3', 'Nino3'), #'Nino3.4', 'Nino3.4'),
                dim = c(sdate = 2, region = 1))
test1 <- Start(dat = path,
             var = 'tosmean',
             sdate = c('1993', '2013'),
             chunk = indices(1:2),
             chunk_depends = 'sdate',
             region = region, #indices(c(9, 13)),
             time = 'all',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             return_vars = list(time = c('sdate', 'chunk'),
                                region = 'dat'),
              retrieve = T)
test1[1,1,,1,]
attributes(test1)$Variables$common$region

test2 <- Start(dat = path,
             var = 'tosmean',
             sdate = c('1993', '2013'),
             chunk = indices(1:2),
             chunk_depends = 'sdate',
             region = region, #indices(c(9, 13)),
             region_across = 'sdate',
#             region_var = 'region',
             time = 'all',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             return_vars = list(time = c('sdate', 'chunk'),
                                region = 'sdate'),
              retrieve = T)
test2[1,1,,1,]
attributes(test2)$Variables$common$region
region <- array(c(7, 11), dim = c(sdate = 1, region = 2))
test3 <- Start(dat = path,
             var = 'tosmean',
             sdate = c('1993', '2013'),
             chunk = indices(1:2),
             chunk_depends = 'sdate',
             region = 'Nino3', #region, #indices(c(9, 13)),
#             region_across = 'sdate',
             region_var = 'region',
             time = 'all',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             return_vars = list(time = c('sdate', 'chunk')),
                                #region = 'sdate'),
              retrieve = T)
test3[1,1,,1,]
attributes(test2)$Variables$common$region

# Original An-Chi's code:
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')
# They three all work (with the newest development)
region <- array('Nino3', dim = c(sdate = 2, region = 1))
#region <- array(c(indices(9), indices(11)), dim = c(sdate = 2, region = 1))
#region <- array(c('Nino3', 'Nino3', 'Nino3.4', 'Nino3.4'), dim = c(sdate = 2, region = 2))
exp <- Start(dat = path,
             var = 'tosmean',
             sdate = c('1993', '2013'),
             chunk = indices(1:2),
             chunk_depends = 'sdate',
             region = region, #indices(c(9, 13)),
#region_across = 'sdate',
#             region_var = 'region',
             time = 'all',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             return_vars = list(time = c('sdate', 'chunk'),
                                region = 'sdate'),
              retrieve = T)
# The current Start() version
exp[1,1,,1,]
          [,1]     [,2]
[1,] 24.987885 24.46488  # CORRECT. It is indices(9) of 1st sdate
[2,]  4.167362  3.17115  # WRONG. It is indices(9) of 2nd sdate
# This is the value we want. The indices(11) of 2nd sdate
[1] 24.47482 24.75953
