context("Seasonal monthly data")

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

recipe_file <- "tests/recipes/recipe-seasonal_monthly_crossval.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = F)

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

# Calibrate data
source("modules/Crossval/Crossval_calibration.R")
calibrated <- Crossval_calibration(recipe = recipe, data = data)

# Compute skill metrics
source("modules/Crossval/Crossval_metrics.R")
suppressWarnings({invisible(capture.output(
skill_metrics <- Crossval_metrics(recipe, calibrated)
))})

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

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

test_that("2. Crossval_calibration", {

expect_equal(is.list(calibrated), TRUE)

expect_equal(names(calibrated),
             c("hcst", "obs", "fcst", "hcst.full_val", "obs.full_val",
               "cat_lims", "probs", "ref_obs"))

expect_equal(class(calibrated$hcst), "s2dv_cube")

expect_equal(class(calibrated$fcst), "s2dv_cube")

expect_equal(dim(calibrated$hcst$data),
             c(dat = 1, var = 1, sday = 1, sweek = 1, time = 3, latitude = 3,
               longitude = 3, ensemble = 25, syear = 8))

expect_equal(dim(calibrated$fcst$data),
             c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 3,
               latitude = 3, longitude = 3, ensemble = 51))

expect_equal(mean(calibrated$fcst$data),
             291.0119, tolerance = 0.0001)

expect_equal(mean(calibrated$hcst$data),
             290.5232, tolerance = 0.0001)

expect_equal(as.vector(drop(calibrated$hcst$data)[1, , 2, 3, 4]),
             c(291.2313, 290.8534, 291.8334), tolerance = 0.0001)

expect_equal(range(calibrated$fcst$data),
             c(285.3917, 297.5869), tolerance = 0.0001)

})


#======================================
test_that("3. Crossval_metrics", {

expect_equal(is.list(skill_metrics), TRUE)

expect_equal(names(skill_metrics),
             c("crps", "crps_clim", "crpss",
               "crpss_significance", "enscorr", "enscorr_significance",
               "rpss-set1", "rpss-set1_significance",
               "rpss-set2", "rpss-set2_significance"))

expect_equal(class(skill_metrics[['rpss-set1']]), "array")

expect_equal(dim(skill_metrics[['rpss-set1']]),
             c(var = 1, time = 3, latitude = 3, longitude = 3))

expect_equal(dim(skill_metrics[['rpss-set1_significance']]),
             dim(skill_metrics[['rpss-set1']]))

expect_equal(as.vector(skill_metrics[['rpss-set1']][, , 2, 3]),
             c(0.013176471, 0.037741176, -0.001223529), tolerance = 0.0001)

expect_equal(as.vector(skill_metrics[['rpss-set1_significance']][, , 2, 3]),
             c(0,0,1))

})


test_that("5. Visualization", {
plots <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(all(basename(list.files(plots, recursive = T)) %in%
             c("crps_clim-november.pdf", "crps-november.pdf", "crpss-november.pdf",
               "enscorr-november.pdf", "forecast_ensemble_median-20201101.pdf",
               "forecast_most_likely_tercile-20201101.pdf",
               "rpss-set1-november.pdf", "rpss-set2-november.pdf")),
            TRUE)

expect_equal(length(list.files(plots, recursive = T)), 8)

})

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