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

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.", {

  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(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 = 1, member = 25, sdate = 12, ftime = 2, lat = 11, lon = 5)
  )
  expect_equal(
  dim(data$obs),
  c(dataset = 1, member = 1, sdate = 12, ftime = 2, lat = 11, lon = 5)
  )
  expect_equal(
  as.vector(data$mod[1, 1, , 2, 2, 3]),
  c(101000, 100910, 100940, 101010, 101180, 101500, 101370, 101280, 101170, 101060, 100930, 101010),
  tolerance = 0.0001
  )
  
})

test_that("1-4.", {
  
  exp <- list(list(
      name = "system5_m1",
      path = file.path("/esarchive/exp/ecmwf/system5_m1/monthly_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc")
    ))
  
  suppressWarnings(
  res <- Load(
      var = "tas",
      exp = exp,
      obs = NULL,
      sdates = c('19930201'),
      output = "lonlat",
      leadtimemin = 1,
      leadtimemax = 1,
      nmember = 2,
      latmin = -90, #10,
      latmax = 90, #12,
      lonmin = 0,
      lonmax = 359.9,
      grid = 'r1296x640', #'t426grid',
      dimnames = list(lon='longitude', lat='latitude', member='ensemble'),
      nprocs = 1)
  )
  expect_equal(
  dim(res$mod),
  c(dataset = 1, member = 2, sdate = 1, ftime = 1, lat = 640, lon = 1296)
  )
  expect_equal(
  as.vector(res$mod[1, 1, 1, 1, 100, 1:4]),
  c(277.38, 277.38, 277.37, 277.37),
  tolerance = 0.0001
  )
  expect_equal(
  any(is.na(res$mod)),
  FALSE
  )
  
})
