# Create inputs from use case 1.7
library(startR)
var_name <- 'sfcWind'
path.exp <- '/esarchive/exp/ecmwf/s2s-monthly_ensforhc/daily/$var$_f24h/$sdate$/$var$_$syear$.nc'

hcst <- Start(dat = path.exp,
              var =  var_name,
              sdate = c('20160704', '20161222'),
              syear = indices(1:3),  #1996:1998
              syear_depends = 'sdate',
              time = indices(1:12),  #4th-15th Jul; 22nd Dec-2nd Jan
              latitude = indices(1:10),
              longitude = indices(1:10),
              ensemble = 'all',
              return_vars = list(latitude = 'dat',
                                 longitude = 'dat',
                                 time = c('sdate', 'syear') #time depends on both sdate and syear
                                 ),
              retrieve = F)


dates <- attr(hcst, 'Variables')$common$time
dim(dates)
library(s2dv)
dates <- Reorder(dates, c('syear', 'sdate', 'time'))
file_date <- sort(unique(gsub('-', '', 
                              sapply(as.character(dates), substr, 1, 7))))

path.obs <- '/esarchive/recon/ecmwf/era5/1hourly/$var$/$var$_$file_date$.nc'

# inputs to run the inner code of Start:
 dim_params <- list(
dat = path.obs,
var = var_name,
file_date = file_date,  # a vector with the information of sdate and syear
latitude = indices(1:10),
longitude = indices(1:10),
time = values(dates),    # a 3-dim array (sdate, syear, time)
time_across = 'file_date')
merge_across_dims = TRUE
merge_across_dims_narm = TRUE
split_multiselected_dims = TRUE
synonims = list(latitude = c('lat','latitude'),
                longitude = c('lon','longitude'))
return_vars = list(latitude = 'dat',
                   longitude = 'dat',
                   time = 'file_date')
retrieve = T
                  file_opener = NcOpener
                  file_var_reader = NcVarReader 
                  file_dim_reader = NcDimReader
                  file_data_reader = NcDataReader 
                  file_closer = NcCloser 
                  transform = NULL 
                  transform_params = NULL 
                  transform_vars = NULL 
                  transform_extra_cells = 2 
                  apply_indices_after_transform = FALSE 
                  pattern_dims = NULL
                  metadata_dims = NULL
                  selector_checker = SelectorChecker
                  path_glob_permissive = FALSE
                  largest_dims_length = FALSE
                  num_procs = 1 
                  ObjectBigmemory = NULL
                  silent = FALSE; debug = FALSE
# New functions:
source("/esarchive/scratch/nperez/git/Flor/startR/split/chunk_indices.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/take_var_params.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/take_var_across.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/take_var_depends.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/take_var_reorder.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/rebuild_dim_params.R")
source("/esarchive/scratch/nperez/git/Flor/startR/split/look_for_chunks.R")
