#--------------------------------------------------------
# Author: An-Chi Ho
# Date: 7th April 2021
#---------------------------------------------
# This use case shows how to use multiStart() to read two datasets with different 
# prediction length per file together. The two datasets, EC-Earth3 and MPI-ESM1.2-HR,
# have the same start date and calendar, but the prediction length per file is different.
# EC-Earth3 has seveal files for "sdate = 2000" and each file has 12 time steps (i.e., 
# 12 months);  MPI-ESM1.2 has only one file for "sdate = 2000" and the file contains
# 122 time steps. To read them together, 'merge_across_dims = TRUE' and 'time_across = 'fyear'
# need to be used.
#---------------------------------------------

library(startR)

# Define selectors
  #------- EC-Earth3 ---------
  ## calendar: standard
  ## Prediction period: 2000 11 - 2011 10 (11 years)
  ## time steps per file: 12
  path_ecearth3 <- paste0('/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/',
                          'EC-Earth3/dcppA-hindcast/r1i1p1f1/Omon/$var$/gr/v20190713/',
                          '$var$_Omon_EC-Earth3_dcppA-hindcast_s$sdate$-r1i1p1f1_gr_$fyear$.nc')
  ## file name:
  ## tos_Omon_EC-Earth3_dcppA-hindcast_s2000-r1i1p1f1_gr_200011-200110.nc
  ## tos_Omon_EC-Earth3_dcppA-hindcast_s2000-r1i1p1f1_gr_200111-200210.nc

  #-------- MPI-ESM1.2-HR --------
  ## calendar: standard
  ## Prediction period: 2000 11 - 2010 12 (10 years and 2 months)
  ## time steps per file: 122
  path_mpi_esm <- paste0('/esarchive/exp/CMIP6/dcppA-hindcast/mpi-esm1-2-hr/',
                         'cmip6-dcppA-hindcast_i1p1/DCPP/MPI-M/MPI-ESM1-2-HR/dcppA-hindcast/',
                         'r1i1p1f1/Omon/$var$/gr/v20200320/',
                         '$var$_Omon_MPI-ESM1-2-HR_dcppA-hindcast_s$sdate$-r1i1p1f1_gr_$fyear$.nc')
  ## file name:
  ## tos_Omon_MPI-ESM1-2-HR_dcppA-hindcast_s2000-r1i1p1f1_gr_200011-201012.nc

  # Selectors
  sdate <- '2000'
  fyear <- 2000:2001
  fyear_ecearth3 <- paste0(fyear, '11-', as.numeric(fyear) + 1, '10')
  fyear_mpi_esm <- paste0(sdate, '11-', as.numeric(sdate) + 10, '12')
  time_ecearth3 <- 1:24 #200011-200210
  time_mpi_esm <- 1:24 #200011-200210



res <- multiStart(
         dat = list(list(name = 'ecearth3', path = path_ecearth3),
                    list(name = 'mpi_esm', path = path_mpi_esm)),
         var = 'tos', sdate = '2000',
         fyear = list(list(name = 'ecearth3', fyear = fyear_ecearth3),
                      list(name = 'mpi_esm', fyear = fyear_mpi_esm)),
         time = list(list(name = 'ecearth3', time = time_ecearth3),
                     list(name = 'mpi_esm', time = time_mpi_esm)),
         lat = indices(1), lon = indices(1),
         time_across = 'fyear',
         merge_across_dims = TRUE, 
         return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
         retrieve = TRUE)

dim(res)
#  dat   var sdate  time   lat   lon 
#    2     1     1    24     1     1 
