context("Subsseasonal weekly data")

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

recipe_file <- "tests/recipes/recipe-subseasonal_weekly.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(sday = 1, dat = 1, var = 1, sweek = 5, time = 4, latitude = 10,
               longitude = 21, ensemble = 12, syear = 5))

expect_equal(dim(calibrated$fcst$data),
             c(dat = 1, var = 1, sday = 1, syear = 1, sweek = 1, time = 4,
               latitude = 10, longitude = 21, ensemble = 48))

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

expect_equal(mean(calibrated$hcst$data, na.rm = TRUE),
             299.4878, tolerance = 0.0001)

expect_equal(as.vector(drop(calibrated$hcst$data)[2,, 1, 2, 3, 4]),
             c(298.6695, 298.6082, 299.1191, 299.1713), tolerance = 0.0001)

expect_equal(range(calibrated$fcst$data),
             c(297.8898, 302.3096), 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",
               "enssprerr", "enssprerr_significance",
               "enscorr", "enscorr_significance",
               "rpss-set1", "rpss-set1_significance",
               "mean_bias", "mean_bias_significance",
               "rpss-set2", "rpss-set2_significance",
               "rpss-set3", "rpss-set3_significance"))

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

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

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.22625000, 0.41974432, -0.07348901, -0.06593407),
             tolerance = 0.0001)

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

})


test_that("5. Visualization", {
plots <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(all(basename(list.files(plots, recursive = T)) %in%
             c("forecast_ensemble_median-20240104_ft01.png",
               "forecast_ensemble_median-20240104_ft02.png",
               "forecast_ensemble_median-20240104_ft03.png",
               "forecast_ensemble_median-20240104_ft04.png")),
            TRUE)

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

})

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