# Author: An-Chi Ho
# Date: 13th July 2021
#---------------------------------------------------------------------
# This script shows how to use a value array as the inner dimension selector to express
# dependency on a file dimension. By this means, we don't need to specify the *_across
# parameter and Start() can recognize this dependecy relationship.
# In the first case, 'time' is dependent on 'sdate'. We create the actual time values
# for each sdate beforehand. The time array is two-dimensional with the names 'time'
# and 'sdate'.
# In the second case, 'region' is dependent on 'sdate'. The two files have different 
# index for Nino3. sdate 1993 has 'Nino3' at index 9 while sdate 2013 has 'Nino3' at 
# index 11. Create a value array for region selector so Start() can look for 'Nino3' in 
# each file.
#---------------------------------------------------------------------

library(startR)
library(lubridate)

# Case 1: 'time' depends on 'sdate'
repos <- '/esarchive/exp/ecmwf/system4_m1/daily_mean/$var$_f24h/$var$_$sdate$.nc'

sdates <- ymd("20010501") + rep(years(0:2), each = 1)
times <- array(ymd("20010501") + days(0:30) + rep(years(0:2), each = 31),
               dim = c(time = 31, sdate = 3))
times <- as.POSIXct(times * 86400, tz = 'UTC', origin = '1970-01-01')

exp <- Start(dat = repos,
             var = 'tos',
             sdate = format(sdates, "%Y%m%d"),
             time = times,  #dim: [time = 31, sdate = 3]. time is corresponding to each sdate
             ensemble = indices(1:5),
             lat = 'all', 
             lon = 'all',
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lon = NULL, lat = NULL, time = 'sdate'),
             retrieve = T) 

dim(exp)
#     dat      var    sdate     time ensemble      lat      lon 
#       1        1        3       31        5      256      512 

exp[1, 1, 2, 1:10, 1, 100, 100]
# [1] 302.1276 302.1346 302.2003 302.2121 302.2552 302.3312 302.3184 302.3507
# [9] 302.3665 302.3865

summary(exp)
#    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
#     271      274      287      287      299      305 19757385 

#=============================================================================

# Case 2: 'region' depends on 'sdate'
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')

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)

dim(data)
#   dat    var  sdate region   time 
#     1      1      2      1      2 

data[1, 1, , 1, ]
#         [,1]     [,2]
#[1,] 24.98788 24.46488  # --> region index 9 in original file
#[2,] 24.47482 24.75953  # --> region index 11 in orginal file








