source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Interpolation.R')
require(testthat)
require(CSTools)
require(s2dv)

context("Generic tests")
test_that("Sanity checks", {
  expect_error(
    CST_Interpolation(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)
  lons <- 1:5
  lats <- 1:4
  exp <- suppressWarnings(s2dv_cube(data = exp, lat = lats, lon = lons)) 

  expect_error(
    CST_Interpolation(exp = exp, lon_dim = 'longitude'), 
    paste0("The name of the latitude/longitude dimensions in 'exp$data' must ",
      "match the parametres 'lat_dim' and 'lon_dim'"), fixed = TRUE) # the '$' needs adding fixed = TRUE 
 
  expect_error(
    CST_Interpolation(exp = exp),
    paste0("Parameter 'method_remap' must be a character vector indicating the ",
      "interpolation method. Accepted methods are con, bil, bic, nn, con2"))  

  expect_error(
    CST_Interpolation(exp = exp, method_remap = 'bil'), 
    paste0("Parameter 'target_grid' 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."), fixed = TRUE)

  expect_warning(
    CST_Interpolation(exp = exp, method_remap = 'bil', target_grid = 'r1280x640', region = NULL),
    paste0("The borders of the downscaling region have not been provided. Assuming the ",
      "four borders of the downscaling region are defined by the first and last elements ",
      "of the parametres 'lats' and 'lons'"))

  d <- CST_Interpolation(exp = exp, method_remap = 'bil', target_grid = 'r1280x640')

  expect_equal(length(d$exp), 9)
  expect_equal(d$obs, NULL)
  expect_equal(class(d), "list")
  expect_equal(class(d$exp), "s2dv_cube")
  expect_equal(as.numeric(dim(d$exp$data)[names(dim(d$exp$data)) == 'lat']), length(d$exp$lat))
  expect_equal(as.numeric(dim(d$exp$data)[names(dim(d$exp$data)) == 'lon']), length(d$exp$lon))

  expect_error(
    CST_Interpolation(exp = exp, points = 1),
    paste0("Parameter 'points' must be a list of two elements containing the point latitudes ",
      "and longitudes."))

  expect_error(
    CST_Interpolation(exp = exp, points = list(latitudes = c(1,0), longitudes = c(-1,1)), 
      method_point_interp = 'bilinear', lat_dim = 'lat', lon_dim = 'lon'),
    paste0("The names of the elements in the list 'points' must coincide with the parametres ",
      "'lat_dim' and 'lon_dim'"))

  exp$source_files[1] <- 'null.nc'
  expect_error(
    CST_Interpolation(exp = exp, points = list(lat = c(1,0), lon = c(-1,1)),
      method_point_interp = 'bilinear'),
    "At least one of the points lies outside the model region")	       

  exp <- rep(letters[1:5], 100) 
  dim(exp) <- c(member = 5, lat = 4, lon = 5, sdate = 5, time  = 1)
  lons <- 1:5
  lats <- 1:4
  exp <- suppressWarnings(s2dv_cube(data = exp, lat = lats, lon = lons))
  
  expect_error(
    CST_Interpolation(exp = exp, method_remap = 'bil', target_grid = 'r1280x640', region = c(1, 5, 1, 4)),
    "Parameter 'data_array' must be a numeric array.")

  exp <- array(1:6, c(lat = 3, lon = 2))
  lons <- 1:2
  lats <- 1:3
  exp <- suppressWarnings(s2dv_cube(data = exp, lat = lats, lon = lons))

  d2 <- CST_Interpolation(exp = exp, method_remap = 'bil', target_grid = 'r640x320')
  expect_equal(d2$exp$data, array(c(2, 2, 3, 3, 4, 5), dim = c(lat = 3, lon = 2)))
  }) 

