##############################################

test_that("1-1.", {

path <- "/esarchive/exp/meteofrance/system6c3s/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc"
dateseq <- c("19930501", "19940501", "19950501")
suppressWarnings(
res1 <- Load(var = 'sfcWind',
             exp = list(list(name = 'meteofrance/system6c3s', path = path)),
             obs = 'erainterim',
             sdates = dateseq, leadtimemin = 2, leadtimemax = 3,
             lonmin = -9, lonmax = 1.5, latmin = 75, latmax = 79.5,
             storefreq = "daily", sampleperiod = 1,  nmember = 3,
             output = "lonlat", method = "bilinear",
             grid = "r360x180", nprocs = 1)
)
#suppressWarnings(
#res2 <- Load(var = 'sfcWind',
#             exp = list(list(name = 'meteofrance/system6c3s', path = path)),
#             obs = 'erainterim',
#             sdates = dateseq, leadtimemin = 2, leadtimemax = 3,
#             lonmin = -9, lonmax = 1.5, latmin = 75, latmax = 79.5,
#             storefreq = "daily", sampleperiod = 1,  nmember = 3,
#             output = "lonlat", method = "bilinear",
#             grid = "r360x180")
#)
#expect_equal(
#res1$mod, res2$mod
#)
#expect_equal(
#res1$obs, res2$obs
#)
#expect_equal(
#names(res1), names(res2)
#)
expect_equal(
dim(res1$mod),
c(dataset = 1, member = 3, sdate = 3, ftime = 2, lat = 5, lon = 11)
)
expect_equal(
dim(res1$obs),
c(dataset = 1, member = 1, sdate = 3, ftime = 2, lat = 5, lon = 11)
)
expect_equal(
as.vector(drop(res1$mod)[1, , 2, 3, 4]),
c(2.0504, 2.2746, 3.4189),
tolerance = 0.0001
)
expect_equal(
as.vector(drop(res1$obs)[, 2, 3, 4]),
c(5.9923, 2.7927, 2.5408),
tolerance = 0.0001
)
expect_equal(
mean(res1$mod, na.rm = T),
6.007487,
tolerance = 0.0001
)
expect_equal(
mean(res1$obs, na.rm = T),
5.043017,
tolerance = 0.0001
)

})


test_that("1-2.", {

path <- "/esarchive/exp/meteofrance/system6c3s/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc"
dateseq <- c("19930501", "19940501", "19950501")
suppressWarnings(
res1 <- Load(var = 'sfcWind',
             exp = list(list(name = 'meteofrance/system6c3s', path = path)),
             obs = 'erainterim',
             sdates = dateseq, leadtimemin = 2, leadtimemax = 3,
             lonmin = -9, lonmax = 1.5, latmin = 75, latmax = 79.5,
             storefreq = "daily", sampleperiod = 1,  nmember = 3,
             output = "areave", 
             nprocs = 1)
)
#suppressWarnings(
#res2 <- Load(var = 'sfcWind',
#             exp = list(list(name = 'meteofrance/system6c3s', path = path)),
#             obs = 'erainterim',
#             sdates = dateseq, leadtimemin = 2, leadtimemax = 3,
#             lonmin = -9, lonmax = 1.5, latmin = 75, latmax = 79.5,
#             storefreq = "daily", sampleperiod = 1,  nmember = 3,
#             output = "areave")
#)
#expect_equal(
#res1$mod, res2$mod
#)
#expect_equal(
#res1$obs, res2$obs
#)
#expect_equal(
#names(res1), names(res2)
#)
expect_equal(
dim(res1$mod),
c(dataset = 1, member = 3, sdate = 3, ftime = 2)
)
expect_equal(
dim(res1$obs),
c(dataset = 1, member = 1, sdate = 3, ftime = 2)
)
expect_equal(
as.vector(drop(res1$mod)[1, , 2]),
c(5.037364, 2.395024, 5.418090),
tolerance = 0.0001
)
expect_equal(
as.vector(drop(res1$obs)[, 2]),
c(8.728424, 2.931754, 3.918583),
tolerance = 0.0001
)
expect_equal(
mean(res1$mod, na.rm = T),
6.162087,
tolerance = 0.0001
) 
expect_equal(
mean(res1$obs, na.rm = T),
5.178091,
tolerance = 0.0001
)

})


test_that("1-3.", {

  ecearth <- list(name = 'm04o',
                  path = file.path('/esarchive/exp/ecearth',
                                   '/$EXP_NAME$/monthly_mean',
                                   '/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))

  ecmwf <- list(name = 'system5c3s',
                path = file.path('/esarchive/exp/ecmwf',
                                 '/$EXP_NAME$/monthly_mean',
                                 '/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))

  era5 <- list(name = 'era5',
               path = file.path('/esarchive/recon/ecmwf',
                                '$OBS_NAME$/monthly_mean',
                                '$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc'))

  startDates <- format(seq(as.POSIXct('1999-01-01'), as.POSIXct('1999-12-01'), by = 'months'), '%Y%m%d')
  time_length <- 2

suppressWarnings(
  data <- Load(var = 'psl',
               exp = list(ecearth, ecmwf),
               obs = list(era5),
               sdates = startDates,
               storefreq = "monthly",
               leadtimemax = time_length,
               output = 'lonlat',
               lonmin = -2, lonmax = 2,
               latmin = -5, latmax = 5,
               nprocs = 1)
)

expect_equal(
dim(data$mod),
c(dataset = 2, member = 10, sdate = 12, ftime = 2, lat = 14, lon = 5)
)
expect_equal(
dim(data$obs),
c(dataset = 1, member = 1, sdate = 12, ftime = 2, lat = 14, lon = 5)
)
expect_equal(
as.vector(data$mod[1, 1, , 2, 2, 3]),
c(rep(NA, 4), 101250, rep(NA, 5), 100940, NA),
tolerance = 0.0001
)


})
