#---------------------------------------------------------------------
# This usecase shows the things to be noticed when the parameters 
# 'split_multiselected_dims' and 'merge_across_dims' are both used.
# The problem may occur when the dimension number of the splitted selector is 
# more than two. If you're not familiar with the usage of these parameters, 
# please see usecases ex1_2 and ex1_3 first, which are less complicated.
# See FAQ How-to-#17 for more explanation.

# In this example, hindcast data corresponding to 2 start dates ('20160704', '20161222') 
# is retrieved. The hindcasts consist of daily values for the 47 following days 
# after each start date (in this case only the first 12 days are retrieved) and 
# for the 20 years of hindacst (in this case only the first 3 years are retrieved). 
# Once the hindacst data is retrieved/read, the corresponding ERA5 values for each 
# hindcast time and year are retrieved using the 'dates' metadata from the hindcast object.
#---------------------------------------------------------------------

library(startR)

var_name <- 'sfcWind'

# experimental data
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)

#-----------------------------------------------------------------------
# The time attribute is going to be used in retrieving observational data.
# Because 'time' depends on both 'sdate' and 'syear', the dimension number 
# is 3. 

dates <- attr(hcst, 'Variables')$common$time
dim(dates)
#sdate syear  time
#    2     3    12
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# If you need to reorder the dimensions of the 'time' selector, you can use
# s2dv::Reorder function. These two lines are not used in the following example.
library(s2dv)
dates <- Reorder(dates, c('syear', 'sdate', 'time'))
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Use 'dates' generated above to get the required files. 
# The order of file_date is not important. sort() is not necessary to use.
file_date <- sort(unique(gsub('-', '', 
                              sapply(as.character(dates), substr, 1, 7))))
print(file_date)
#[1] "199607" "199612" "199701" "199707" "199712" "199801" "199807" "199812"
#[9] "199901"
#-----------------------------------------------------------------------

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

obs <- Start(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)

# check obs data
dim(obs)
#      dat       var  latitude longitude     sdate     syear      time 
#        1         1        10        10         2         3        12 
obs[1, 1, 1, 1, 2, 1:2, ]
#         [,1]     [,2]     [,3]      [,4]     [,5]     [,6]     [,7]     [,8]
#[1,] 4.402223 2.657466 7.296539 10.263944 6.367464 5.433421 3.021327 7.498292
#[2,] 6.802123 7.110264 7.584915  4.255134 2.047740 3.619044 5.648496 8.322672
#          [,9]    [,10]    [,11]    [,12]
#[1,] 15.321060 1.131008 6.326981 5.301850
#[2,]  7.942419 7.594263 6.189313 7.627579

# check with ncdf4
library(ncdf4)
file199612 <- nc_open('/esarchive/recon/ecmwf/era5/1hourly/sfcWind/sfcWind_199612.nc')
wind199612 <- ncvar_get(file199612, 'sfcWind')
file199701 <- nc_open('/esarchive/recon/ecmwf/era5/1hourly/sfcWind/sfcWind_199701.nc')
wind199701 <- ncvar_get(file199701, 'sfcWind')

data_wanted_199612 <- seq(506, 722, 24)
wind199612[1, 1, data_wanted_199612]
# [1]  4.402223  2.657466  7.296539 10.263944  6.367464  5.433421  3.021327
# [8]  7.498292 15.321060  1.131008
data_wanted_199701 <- seq(2, 26, 24)
wind199701[1, 1, data_wanted_199701]
#[1] 6.326981 5.301850

