context("Generic tests")
test_that("Sanity checks", {
library(testthat)
library(multiApply)
library(abind)
library(s2dverification)
source("R/Clim.R")
source("R/Regression.R")
source("R/Trend.R")


		var <- array(rnorm(16), c(2, 2, 2, 2))
  names(dim(var)) <- c("memb", "lon", "sdates", "lat")
  clim <- apply(var, c(1, 2, 4), mean)
  names(dim(clim)) <- NULL

# memb = TRUE and same length of members
  var_exp <- 1:(1 * 3 * 4 * 2 * 2)
  dim(var_exp) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)
  var_obs <- var_exp[, 1, , , , , ]
  dim(var_obs) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)

   c3 <- Clim(var_exp, var_obs, memb = TRUE) # Clim for each member
  expect_equal(c3,
clim <-  Clim2(var_exp, var_obs, memb = TRUE, method = "clim"))

               #clim <- Apply(list(var_exp, var_obs),
               #              target_dims = list(c('member', 'sdates'), c('sdates')),
               #              fun = .Clim, method = "clim"))

dim(clim$clim_exp)
dim(c3$clim_exp)
clim$clim_exp[1, , ,1,1,1]
c3$clim_exp[1, , ,1,1,1]



# memb = TRUE and different length of members
  var_exp <- 1:(5 * 3 * 4 * 2 * 2)
  dim(var_exp) <- c(mod = 1, member = 5, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)
  var_obs <- var_exp[, 1, , , , , ]
  dim(var_obs) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)

   c3 <- Clim(var_exp, var_obs, memb = TRUE) # Clim for each member
  expect_equal(c3,
clim <-  Clim2(var_exp, var_obs, memb = TRUE, method = "clim"))

               #clim <- Apply(list(var_exp, var_obs),
               #              target_dims = list(c('member', 'sdates'), c('sdates')),
               #              fun = .Clim, method = "clim"))

dim(clim$clim_exp)
dim(c3$clim_exp)
clim$clim_exp[1, , ,1,1,1]
c3$clim_exp[1, , ,1,1,1]


#working: but different dimensions order
  var_exp <- 1:(1 * 3 * 4 * 2 * 2)
  dim(var_exp) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)
  var_obs <- var_exp[, 1, , , , , ]
  dim(var_obs) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)

   c3 <- Clim(var_exp, var_obs, memb = FALSE) # Clim for each member
  expect_equal(c3,
clim <-  Clim2(var_exp, var_obs, memb = FALSE, method = "clim"))

               #clim <- Apply(list(var_obs, var_obs),
               #              target_dims = c('sdates'),
               #              fun = .Clim, method = "clim"))

dim(clim$clim_exp)
dim(c3$clim_exp)
clim$clim_exp[1, ,1,1,1]
c3$clim_exp[1, ,1,1,1]



# basic considering member different: working:
  var_exp <- 1:(5 * 3 * 4 * 2 * 2)
  dim(var_exp) <- c(mod = 1, member = 5, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)
  var_obs <- var_exp[, 1, , , , , ]
  dim(var_obs) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)

   c3 <- s2dverification::Clim(var_exp, var_obs, memb = FALSE) # Clim for each member
   expect_equal(c3,
clim <-  Clim2(var_exp, var_obs, memb = FALSE, method = "clim"))
       
#         clim <- Apply(data = list(var_exp, var_obs),
#                              target_dims = c('member', 'sdates'),
#                              fun = .Clim, method = "clim"))
#   expect_equal(c3, #working with different order of the dimensions:
#                clim <- Apply(list(var_exp, var_obs),
#                              target_dims = list(c('member', 'sdates'), 'sdates'),
#                          fun = .Clim, method = "clim"),
#                 check.attributes = FALSE)

dim(clim$clim_exp)
dim(c3$clim_exp)
clim$clim_exp[1, ,1,1,1]
c3$clim_exp[1, ,1,1,1]
source("R/Clim.R")

# kharin:
source("/home/Earth/aho/aho-testtest/s2dv/multiApply/Trend.R")
  exp <- 1:(5 * 3 * 4 * 2 * 2)
  dim(exp) <- c(mod = 1, member = 5, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)
  obs <- var_exp[, 1, , , , , ]
  dim(obs) <- c(mod = 1, member = 1, sdates = 3, ltime = 4, level = 1, lon = 2, lat = 2)

   c3 <- s2dverification::Clim(exp, obs, memb = FALSE, kharin = TRUE) # Clim for each member
   expect_equal(c3,
clim <-  Clim2(exp, obs, memb = FALSE, method = "kharin"))


})
