 ref_dates <- seq(as.Date("01-01-1993", "%d-%m-%Y", tz = 'UTC'),
               as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day")
 data_dates <- seq(as.Date("01-07-1993", "%d-%m-%Y", tz = 'UTC'),
               as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day")
 library(multiApply)
 source("/esarchive/scratch/nperez/git/csindicators/R/SelectPeriodOnDates.R")
source("/esarchive/scratch/nperez/git/csindicators/R/SelectPeriodOnData.R")
source("/esarchive/scratch/nperez/git/csindicators/R/zzz.R")
  start1 = list(21, 6)
  end1 = list(30, 6)
  start2 = list(1,7)
  end2 = list(21, 9)
  ref <- array(1:335, c(time = 335))
  data <- array(1:(2*154), c(time = 154, member= 2))
 time_dim = 'time'
 sdate_dim = 'sdate'
memb_dim = 'member' 
ncores = NULL
data1 = ref
data2 = data
dates1 = ref_dates
dates2 = data_dates

source("/esarchive/scratch/nperez/git/csindicators/R/MergeRefToExp.R")
res <- MergeRefToExp(data1 = ref, dates1 = ref_dates, start1, end1,
                     data2 = data, dates2 = data_dates, start2, end2)


 data_dates <- c(seq(as.Date("01-07-1993", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day"),
                 seq(as.Date("01-07-1994", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day"))
 dim(data_dates) <- c(time = 154, sdate = 2)
  ref_dates <- seq(as.Date("01-01-1993", "%d-%m-%Y", tz = 'UTC'),
               as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day")
  ref <- array(1001:1700, c(time = 700))
  data <- array(1:(2*154*2), c(time = 154, sdate = 2, member= 2))

res <- MergeRefToExp(data1 = ref, dates1 = ref_dates, start1, end1,
                     data2 = data, dates2 = data_dates, start2, end2)

data_dates <- c(seq(as.Date("01-07-1993", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1993","%d-%m-%Y", tz = 'UTC'), "day"),
                 seq(as.Date("01-07-1994", "%d-%m-%Y", tz = 'UTC'),
                 as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day"))
 dim(data_dates) <- c(time = 154, sdate = 2)
  ref_dates <- seq(as.Date("01-01-1993", "%d-%m-%Y", tz = 'UTC'),
               as.Date("01-12-1994","%d-%m-%Y", tz = 'UTC'), "day")
  dim(ref_dates) <- c(time = 350, sdate = 2)
  ref <- array(1001:1700, c(time = 350, sdate = 2))
  data <- array(1:(2*154*2), c(time = 154, sdate = 2, member= 2))

res <- MergeRefToExp(data1 = ref, dates1 = ref_dates, start1, end1,
                     data2 = data, dates2 = data_dates, start2, end2)

data <- s2dv::Reorder(data, c('member', 'sdate', 'time'))
data_dates <- s2dv::Reorder(data_dates, c('sdate', 'time'))

res <- MergeRefToExp(data1 = ref, dates1 = ref_dates, start1, end1,
                     data2 = data, dates2 = data_dates, start2, end2)

 dim(ref) <- c(time = 350, sdate = 2, member = 1)
# This should fail:
res <- MergeRefToExp(data1 = ref, dates1 = ref_dates, start1, end1,
                     data2 = data, dates2 = data_dates, start2, end2)

 
