set.seed(1)
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Interpolation.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Utils.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Analogs.R')
require(testthat)
require(CSTools)
require(multiApply)
require(s2dv)

context("Generic tests")
test_that("Sanity checks", {
  expect_error(
    CST_Analogs(exp = 1),
    "Parameter 'exp' must be of the class 's2dv_cube'")
  
  exp <- rnorm(500)
  dim(exp) <- c(member = 5, lat = 4, lon = 5, sdate = 5, time  = 1)
  exp_lons <- 1:5
  exp_lats <- 1:4
  exp <- suppressWarnings(s2dv_cube(data = exp, lat = exp_lats, lon = exp_lons)) 

  expect_error(CST_Analogs(exp = exp), "argument \"obs\" is missing, with no default")

  obs <- rnorm(900)
  dim(obs) <- c(lat = 12, lon = 15, sdate = 5, time = 1)
  obs_lons <- seq(1,5, 4/14)
  obs_lats <- seq(1,4, 3/11)
  obs <- s2dv_cube(data = obs, lat = obs_lats, lon = obs_lons)

  expect_error(
    CST_Analogs(exp = exp, obs = obs, grid_exp = 1),
    paste0("Parameter 'grid_exp' must be of class 'character'. It can be either a path ",
           "to another NetCDF file which to read the target grid from (a single grid must ",
           "be defined in such file) or a character vector indicating the coarse grid to be ",
           "passed to CDO, and it must be a grid recognised by CDO or a NetCDF file."))

  exp$data[1,1,1,1,1] <- NA

  l <- CST_Analogs(exp = exp, obs = obs, grid_exp = 'r360x180')
  })
    



