context("Testing Load with ECMWF System5c3s daily data")
test_that("First time step is correctly interpreted:", {
  system5 <- Load(var = 'prlr',
                  exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
                  sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
                  nmember = 5,leadtimemin = 1,
                  leadtimemax = NULL,
                  storefreq = "daily", sampleperiod = 1,
                  latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
                  output = 'lonlat', nprocs = 1,
                  method = 'conservative', grid = 'r360x181',
                  maskmod = vector("list", 15), maskobs = vector("list", 15),
                  configfile = NULL, varmin = NULL, varmax = NULL,
                  silent = FALSE, dimnames = NULL,
                  remapcells = 2, path_glob_permissive = 'partial',
                  nmemberobs = NULL, nleadtime = NULL)

  
  data <- Load(var = 'prlr',
               exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
               obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 5,leadtimemin = 1,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)
  
  expect_equal(dim(data$mod), c(dataset = 1, member = 5, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(dim(data$obs), c(dataset = 1, member = 1, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$mod)), 0)
  expect_equal(sum(is.na(data$obs)), 0)
  dates <- c(seq(as.POSIXct("1993-11-02", tz = 'UTC'), 
                                       as.POSIXct("1994-06-03", tz = 'UTC'), "d"),
                                   seq(as.POSIXct("1994-11-02", tz = 'UTC'),
                                       as.POSIXct("1995-06-03", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$start, dates)
  dates <- c(seq(as.POSIXct("1993-11-03", tz = 'UTC'),
                 as.POSIXct("1994-06-04", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-03", tz = 'UTC'),
                 as.POSIXct("1995-06-04", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$end, dates)
  
  expect_equal(data$mod, system5$mod)
  
  era5 <- Load(var = 'prlr', exp = NULL,
              obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'),
               nmember = 1, leadtimemin = 1,
               leadtimemax = 215,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = 1, nleadtime = NULL)
  # The values of observation when loading exp and obs simultaneously corresponds to the second day of the observations in the file:
  expect_equal(data$obs[1,1,1,,,], era5$obs[1,1,1,2:215,,])
  expect_equal(data$obs[1,1,2,,,], era5$obs[1,1,2,2:215,,]) 

 # Test when 2 observational datasets are requested:
  data <- Load(var = 'prlr',
             exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
             obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc'), list(name = 'erainterim')),
             sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
             nmember = 5, leadtimemin = 1,
             leadtimemax = NULL,
             storefreq = "daily", sampleperiod = 1,
             latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
             output = 'lonlat', nprocs = 1,
             method = 'conservative', grid = 'r360x181',
             maskmod = vector("list", 15), maskobs = vector("list", 15),
             configfile = NULL, varmin = NULL, varmax = NULL,
             silent = FALSE, dimnames = NULL,
             remapcells = 2, path_glob_permissive = 'partial',
             nmemberobs = NULL, nleadtime = NULL)  
  expect_equal(dim(data$obs), c(dataset = 2, member = 1, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(dim(data$mod), c(dataset = 1, member = 5, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$mod)), 0)
  expect_equal(sum(is.na(data$obs)), 0)
  expect_equal(data$obs[1,1,,,,], era5$obs[1,1,,2:215,,])
  expect_equal(system5$mod, data$mod)
  dates <- c(seq(as.POSIXct("1993-11-02", tz = 'UTC'),
                 as.POSIXct("1994-06-03", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-02", tz = 'UTC'),
                 as.POSIXct("1995-06-03", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(dates, data$Dates$start)
  dates <- c(seq(as.POSIXct("1993-11-03", tz = 'UTC'),
                 as.POSIXct("1994-06-04", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-03", tz = 'UTC'),
                 as.POSIXct("1995-06-04", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'  
  expect_equal(dates, data$Dates$end)
  
  eraint <- Load(var = 'prlr', exp = NULL,
              obs = list(list(name = 'erainterim')),
              sdates = c('19931101', '19941101'),
              nmember = 1, leadtimemin = 1,
              leadtimemax = 215,
              storefreq = "daily", sampleperiod = 1,
              latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
              output = 'lonlat', nprocs = 1,
              method = 'conservative', grid = 'r360x181',
              maskmod = vector("list", 15), maskobs = vector("list", 15),
              configfile = NULL, varmin = NULL, varmax = NULL,
              silent = FALSE, dimnames = NULL,
              remapcells = 2, path_glob_permissive = 'partial',
              nmemberobs = 1, nleadtime = NULL)
  expect_equal(data$obs[2,1,,,,], eraint$obs[1,1,,2:215,,])

  # Test for 2 experimental datasets
  data <- Load(var = 'prlr',
               exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc"),
                          list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc")),
               obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 5,leadtimemin = 1,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)
  expect_equal(dim(data$obs), c(dataset = 1, member = 1, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(dim(data$mod), c(dataset = 2, member = 5, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$obs)), 0)
  expect_equal(sum(is.na(data$mod)), 0)
  dates <- c(seq(as.POSIXct("1993-11-02", tz = 'UTC'),
                                       as.POSIXct("1994-06-03", tz = 'UTC'), "d"),
                                   seq(as.POSIXct("1994-11-02", tz = 'UTC'),
                                       as.POSIXct("1995-06-03", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$start, dates)
  dates <- c(seq(as.POSIXct("1993-11-03", tz = 'UTC'),
                 as.POSIXct("1994-06-04", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-03", tz = 'UTC'),
                 as.POSIXct("1995-06-04", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$end, dates)
  expect_equal(data$obs[,,,,,], era5$obs[,,,2:215,,])
  expect_equal(system5$exp, data$exp[1,,,,,])
  
  system4 <- Load(var = 'prlr',
                  exp = list(list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc")),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 1, leadtimemin = 1,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)

  expect_equal(system4$exp, data$exp[2,,,,,])
  expect_equal(system4$Dates$start[c(-1,-216)], data$Dates$start)
  
  
  # Test for 2 experimental datasets oposite order
  data <- Load(var = 'prlr',
               exp = list(list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc"), 
                          list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
               obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 5,leadtimemin = 1,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)
  expect_equal(dim(data$mod), c(dataset = 2, member = 5, sdate = 2, ftime = 215,
                                lat = 8, lon = 8))
  expect_equal(dim(data$obs), c(dataset = 1, member = 1, sdate = 2, ftime = 215,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$mod[1,,,,,])), 0)
  expect_equal(sum(is.na(data$mod[2,,,-215,,])),0)
  expect_equal(sum(is.na(data$mod[2,,,215,,])), 640)
  expect_equal(data$Dates$start, system4$Dates$start)
  expect_equal(data$obs, era5$obs)
  # Test leadtimemin > 1
  data <- Load(var = 'prlr',
               exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc"),
                          list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc")),
               obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 5,leadtimemin = 2,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)
  expect_equal(dim(data$mod), c(dataset = 2, member = 5, sdate = 2, ftime = 213,
                                lat = 8, lon = 8))
  expect_equal(dim(data$obs), c(dataset = 1, member = 1, sdate = 2,ftime = 213,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$mod)), 0)
  expect_equal(sum(is.na(data$obs)), 0)
  expect_equal(system4$mod[,1,,3:215,,], data$mod[2,1,,,,])
  expect_equal(system5$mod[,,,2:214,,], data$mod[1,,,,,]) 
  expect_equal(era5$obs[,,,3:215,,], data$obs[1,,,,,], tolerance = 0.001)
  dates <- c(seq(as.POSIXct("1993-11-03", tz = 'UTC'),
                 as.POSIXct("1994-06-03", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-03", tz = 'UTC'),
                 as.POSIXct("1995-06-03", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$start, dates)
  
  data <- Load(var = 'prlr',
               exp = list(list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc"),
                          list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
               obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
               sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
               nmember = 5,leadtimemin = 2,
               leadtimemax = NULL,
               storefreq = "daily", sampleperiod = 1,
               latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
               output = 'lonlat', nprocs = 1,
               method = 'conservative', grid = 'r360x181',
               maskmod = vector("list", 15), maskobs = vector("list", 15),
               configfile = NULL, varmin = NULL, varmax = NULL,
               silent = FALSE, dimnames = NULL,
               remapcells = 2, path_glob_permissive = 'partial',
               nmemberobs = NULL, nleadtime = NULL)
  expect_equal(system4$mod[1,1,,2:215,,], data$mod[1,1,,1:214,,])
  expect_equal(system5$mod[1,1,,1:214,,], data$mod[2,1,,1:214,,])
  expect_equal(dim(data$obs), c(dataset = 1, member = 1, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(dim(data$mod), c(dataset = 2, member = 5, sdate = 2, ftime = 214,
                                lat = 8, lon = 8))
  expect_equal(sum(is.na(data$obs)), 0)
  expect_equal(sum(is.na(data$mod)), 0)
  expect_equal(data$obs[,,,1,,], era5$obs[,,,2,,])
  dates <- c(seq(as.POSIXct("1993-11-02", tz = 'UTC'),
                 as.POSIXct("1994-06-03", tz = 'UTC'), "d"),
             seq(as.POSIXct("1994-11-02", tz = 'UTC'),
                 as.POSIXct("1995-06-03", tz = 'UTC'), "d"))
  attributes(dates)$tzone <- 'UTC'
  expect_equal(data$Dates$start, dates)
})
