library(startR)

# Get time values for later use
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1),
              time = indices(1:4), 
              latitude = indices(1), longitude = indices(1),
              return_vars = list(time = 'sdate'),
              retrieve = F)
time_val <- attr(data, 'Variables')$common$time

#time_val
#[1] "2017-02-01 UTC" "2018-02-01 UTC" "2017-03-01 UTC" "2018-03-01 UTC"
#[5] "2017-04-01 UTC" "2018-04-01 UTC" "2017-05-01 UTC" "2018-05-01 UTC"

# The arguments which won't change in the tests
basic_list <- list(
  dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2),time  retrieve = T)
  retrieve = T)

test_batteries <- list(
  # 1: ensemble and time are indices. no *_var assigned
  c(basic_list, list(ensemble = c(1, 3)), list(time = indices(1:4))),
  # 2: ensemble is values. ensemble_var assigned.
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = indices(1:4)),  
    ensemble_var = 'realization'),
  # 3: ensemble and time are values. ensemble_var assigned. time_var will be added automatically.
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = time_val),
    ensemble_var = 'realization'),
  # 4: same as 2 but time_var is assigned, and return_vars = list(time = NULL).
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = indices(1:4)),
    ensemble_var = 'realization', time_var = 'time',
    list(return_vars = list(time = NULL))),
  # 5: same as 4 but return_var = list(time = 'sdate'). ERROR!!
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = indices(1:4)),
    ensemble_var = 'realization', time_var = 'time',
    list(return_vars = list(time = 'sdate'))),
  # 6: same as 5 but time with dim.
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = array(1:4, dim = c(time = 4, sdate = 2))),
    ensemble_var = 'realization', time_var = 'time',
    list(return_vars = list(time = 'sdate'))),
  # 7: latitude_var ?
  c(basic_list, list(ensemble = c(1, 3)), list(time = indices(1:4)),
    latitude_var = 'latitude'),
  # 8: latitude_var ?
  c(basic_list, list(ensemble = c(1, 3)), list(time = indices(1:4)),
    list(return_vars = list(latitude = NULL))),
  # 9: ftime name:
  c(basic_list, list(ensemble = c(1,3)), list(ftime = indices(1:4)), ftime_var = 'time'),
  # 10: + depends same as 4 but return_var = list(time = 'sdate'). ERROR!!
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = indices(1:4)),
    ensemble_var = 'realization', time_var = 'time', time_depends = 'sdate',
    list(return_vars = list(time = 'sdate'))),
  c(basic_list, list(ensemble = values(c(1, 3))), list(time = array(1:4, dim = c(time = 4, sdate = 2))),
    ensemble_var = 'realization', time_var = 'time',
    list(return_vars = list(time = NULL))))

battery_ind = 1
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data1 <- do.call(Start, battery)

battery_ind = 2
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data2 <- do.call(Start, battery)

all(data1 == data2)
names(attributes(data1)$Variables$common) # tas
names(attributes(data2)$Variables$common) # realization tas
names(attributes(data1)$Variables$dat1) # NULL
names(attributes(data2)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data2)$Vatiables$common$tas)

battery_ind = 3
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data3 <- do.call(Start, battery)

all(data1 == data3)
names(attributes(data3)$Variables$common) # realization time tas
names(attributes(data3)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data3)$Vatiables$common$tas)
all(attributes(data1)$Variables$common$realization == 
    attributes(data3)$Vatiables$common$realization)

battery_ind = 4
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data4 <- do.call(Start, battery)

all(data1 == data4)
names(attributes(data4)$Variables$common) # time realization tas
names(attributes(data4)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data4)$Vatiables$common$tas)
all(attributes(data1)$Variables$common$realization == 
    attributes(data4)$Vatiables$common$realization)

battery_ind = 5
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data5 <- do.call(Start, battery)

battery_ind = 6
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data6 <- do.call(Start, battery)

all(data1 == data6)
names(attributes(data6)$Variables$common) # time realization tas
names(attributes(data6)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data6)$Variables$common$tas)
all(attributes(data1)$Variables$common$realization ==
    attributes(data6)$Vatiables$common$realization)


battery_ind = 7
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data7 <- do.call(Start, battery)

all(data1 == data7)
names(attributes(data7)$Variables$common) # time realization tas
names(attributes(data7)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data7)$Variables$common$tas)
all(attributes(data1)$Variables$common$realization ==
    attributes(data7)$Vatiables$common$realization)

battery_ind = 8
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data8 <- do.call(Start, battery)

all(data1 == data8)
names(attributes(data8)$Variables$common) # time realization tas
names(attributes(data8)$Variables$dat1) # NULL
all(attributes(data1)$Variables$common$tas == attributes(data8)$Variables$common$tas)
all(attributes(data1)$Variables$common$realization ==
    attributes(data8)$Vatiables$common$realization)

battery_ind = 9
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data9 <- do.call(Start, battery)

battery_ind = 10
  battery <- test_batteries[[battery_ind]]
  call <- list()
  cat(paste0("Test ", battery_ind, "...\n"))
  call[names(battery[[battery_ind]])] <- battery
  data10 <- do.call(Start, battery)

data9 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = values(c(89.7848769072186, 89.5062027382064, 89.2258828476116)), #indices(1:3),
  longitude = indices(1:2), retrieve = T,
ensemble = c(1, 3), time = indices(1:4), latitude_var = 'latitude')

data11 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = time_val, return_vars = list(time = NULL))
data12 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = time_val, time_var = 'time')

data13 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = time_val, time_var = 'time',
  return_vars = list(time = NULL))
data14 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = time_val, time_var = 'time',
  return_vars = list(time = 'sdate'))
data15 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = array(1:4, dim = c(time = 4, sdate = 2)))
data16 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = array(1:4, dim = c(time = 4, sdate = 2)),
  return_vars = list(time = NULL))
data17 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = array(1:4, dim = c(time = 4, sdate = 2)),
  time_var = 'time')

data18 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
  var = 'tas',
  sdate = c('20170101', '20180101'),
  latitude = indices(1:3),
  longitude = indices(1:2), retrieve = T,
  ensemble = c(1, 3), time = array(1:4, dim = c(time = 4, sdate = 2)),
  return_vars = list(time = 'sdate'), time_var = 'time')
