library(abind)
context("Generic tests")
test_that("Sanity checks", {
  expect_error(
    CST_EnsBiasCorrection(exp = 1),
    paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
         "as output by CSTools::CST_Load."))
  

  amt.dataset <- 2
  lst.dataset <- paste0("dataset_",seq(1,amt.dataset))
  amt.member <- 5
  lst.member <- paste0("mbr_",seq(1,amt.member))
  amt.sdate <- 30
  lst.sdate <- paste0(1980+seq(1,amt.sdate),"-01-01")
  amt.ftime <- 2
  lst.ftime <- paste0("month_",seq(1,amt.ftime))
  amt.lon <- 2
  lst.lon <- paste0("lon_",seq(1,amt.lon))
  amt.lat <- 3 
  lst.lat <- paste0("lat_",seq(1,amt.lat))
  
  
  bias <- 1. #define some parameters that distinguish observations from forecast&
  slope <- 2.
  noise.level.fc <- 0.1 * slope
  noise.level.obs <- 0.1 
  
  amt.data.pts <- amt.dataset*amt.sdate*amt.ftime*amt.lon*amt.lat
  
  obs1 <- array(data = rnorm(amt.data.pts),
    dim = c(dataset = amt.dataset, member = 1, sdate = amt.sdate, ftime = amt.ftime, lon = amt.lon, lat = amt.lat),
    dimnames = list(dataset = lst.dataset, member = c("obs"), sdate = lst.sdate, ftime = lst.ftime, lon = lst.lon, lat = lst.lat))
  
  tmp.noise.obs <- array(data = rnorm(amt.data.pts, 0, noise.level.obs),
    dim = c(dataset = amt.dataset, member = 1, sdate = amt.sdate, ftime = amt.ftime, lon = amt.lon, lat = amt.lat),
    dimnames = list(dataset = lst.dataset, member = c("obs"), sdate = lst.sdate, ftime = lst.ftime, lon = lst.lon, lat = lst.lat))
  
  mod1 <- array(data = rnorm(amt.data.pts*amt.member, 0, noise.level.fc),
    dim = c(dataset = amt.dataset, member = amt.member, sdate = amt.sdate, ftime = amt.ftime, lon = amt.lon, lat = amt.lat),
    dimnames = list(dataset = lst.dataset, member = lst.member, sdate = lst.sdate, ftime = lst.ftime, lon = lst.lon, lat = lst.lat))
  
  
  for(i.mbr in seq(1, amt.member)){
    mod1[ , i.mbr, , , , ] <- (obs1[ , 1, , , , ]  + mod1[ , i.mbr, , , , ] + bias) / slope
  }
  obs1 <- obs1 + tmp.noise.obs
  
  lon <- seq(0, 30, amt.lon)
  lat <- seq(0, 25, amt.lat)
  exp <- list(data = mod1, lat = lat, lon = lon)
  obs <- list(data = obs1, lat = lat, lon = lon)
  attr(exp, 'class') <- 's2dv_cube'
  attr(obs, 'class') <- 's2dv_cube'
      
  bc <- CST_EnsBiasCorrection(exp = exp, obs = obs)

  expect_equal(length(bc), 3)
  expect_equal(as.numeric(dim(bc$data)), 
               as.numeric(c(dataset = amt.dataset, member = amt.member, sdate = amt.sdate, ftime = amt.ftime, 
                 lon = amt.lon, lat = amt.lat)))
  expect_equal(bc$lat, lat)
  expect_equal(bc$lon, lon)
  
  expect_error(CST_EnsBiasCorrection(exp = exp, obs = exp),
        paste0("The length of the dimension 'member' in the component 'data' ",
         "of the parameter 'obs' must be equal to 1."))
  
  exp2 <- exp
  exp2$data[1, 2, 1, 1, 1, 1] <- NA
  expect_warning(CST_EnsBiasCorrection(exp = exp2, obs = obs),
    "Parameter 'exp' contains NA values.")
  
  obs2 <- obs
  obs2$data[1, 1, 2, 1, 1, 1] <- NA
  expect_warning(CST_EnsBiasCorrection(exp = exp, obs = obs2),
    "Parameter 'obs' contains NA values.")
  
  expect_warning(CST_EnsBiasCorrection(exp = exp2, obs = obs2),
    "Parameter 'obs' contains NA values", "Parameter 'exp' contains NA values.")
})
