context("Seasonal monthly data")

source("modules/Loading/Loading.R")
source("modules/Statistics/Statistics.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")

recipe_file <- "tests/recipes/recipe-seasonal_monthly_1_statistics.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = F)
archive <- read_yaml(paste0(recipe$Run$code_dir, "conf/archive.yml"))$archive

# Load datasets
suppressWarnings({invisible(capture.output(
data <- Loading(recipe)
))})

# Compute statistics
suppressWarnings({invisible(capture.output(
statistics <- Statistics(recipe, data)
))})

# Saving
suppressWarnings({invisible(capture.output(
Saving(recipe = recipe, data = data,
       skill_metrics = c(statistics, metadata = data$hcst))
))})

# Plotting
suppressWarnings({invisible(capture.output(
Visualization(recipe = recipe,  data = data,
	      statistics = statistics,
	      significance = T)
))})
outdir <- get_dir(recipe = recipe, variable = data$hcst$attrs$Variable$varName)

# ------- TESTS --------

test_that("1. Loading", {

expect_equal(
is.list(data),
TRUE
)
expect_equal(
names(data),
c("hcst", "fcst", "obs")
)
expect_equal(
class(data$hcst),
"s2dv_cube"
)
expect_equal(
class(data$fcst),
"s2dv_cube"
)
expect_equal(
class(data$obs),
"s2dv_cube"
)
expect_equal(
names(data$hcst),
c("data", "dims", "coords", "attrs")
)
expect_equal(
names(data$hcst),
names(data$fcst)
)
expect_equal(
names(data$hcst),
names(data$obs)
)
expect_equal(
dim(data$hcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 3, longitude = 3, ensemble = 25)
)
expect_equal(
dim(data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 3, longitude = 3, ensemble = 51)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 4, time = 3)
)
expect_equal(
as.vector(drop(data$hcst$data)[1:2,1:2,1,2,3]),
c(293.9651, 295.9690, 290.6771, 290.7957),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
290.8758,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(284.7413, 299.6219),
tolerance = 0.0001
)
expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("1993-11-30 23:59:59", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("1994-11-30 23:59:59", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("1993-12-31 23:59:59", tz = 'UTC')
)
expect_equal(
(data$obs$attrs$Dates)[10],
as.POSIXct("1995-01-15 12:00:00", tz = 'UTC')
)

})

#======================================
test_that("2. Statistics", {

expect_equal(
is.list(statistics),
TRUE
)
expect_equal(
names(statistics),
c("cov", "std_hcst", "std_obs", "var_hcst", "var_obs", "n_eff")
)
expect_equal(
class(statistics$cov),
"array"
)
expect_equal(
dim(statistics$cov),
c(var = 1, time = 3, latitude = 3, longitude = 3)
)
expect_equal(
dim(statistics$cov),
dim(statistics$var_hcst)
)
expect_equal(
as.vector(statistics$cov[, , 2, 3]),
c(1.14846389, 0.05694802, 0.02346492),
tolerance = 0.0001
)
expect_equal(
as.vector(statistics$var_hcst[, , 2, 3]),
c(0.74897676, 0.14698283, 0.04864656),
tolerance = 0.0001
)

})

test_that("3. Saving", {
outputs <- paste0(recipe$Run$output_dir, "/outputs/")
expect_equal(
all(basename(list.files(outputs, recursive = T)) %in%
c("scorecards_Meteo-France-System7_ERA5_tas_cov_1993-1996_s11.nc",     
  "scorecards_Meteo-France-System7_ERA5_tas_n_eff_1993-1996_s11.nc",  
  "scorecards_Meteo-France-System7_ERA5_tas_std_hcst_1993-1996_s11.nc",
  "scorecards_Meteo-France-System7_ERA5_tas_std_obs_1993-1996_s11.nc",
  "scorecards_Meteo-France-System7_ERA5_tas_var_hcst_1993-1996_s11.nc",
  "scorecards_Meteo-France-System7_ERA5_tas_var_obs_1993-1996_s11.nc")),
TRUE
)
expect_equal(
length(list.files(outputs, recursive = T)),
6
)

})

test_that("4. Visualization", {
plots <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(
all(basename(list.files(plots, recursive = T)) %in%
c("cov-november.pdf", "n_eff-november.pdf", "std_hcst-november.pdf",
  "std_obs-november.pdf", "var_hcst-november.pdf", "var_obs-november.pdf" )),
TRUE
)
expect_equal(
length(list.files(plots, recursive = T)),
6
)

})

# Delete files
unlink(recipe$Run$output_dir, recursive = T)
