context("Seasonal monthly data")

source("modules/Loading/Loading.R")
source("modules/Calibration/Calibration.R")
source("modules/Skill/Skill.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")
source("modules/Aggregation/Aggregation.R")

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

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

# Aggregated data
suppressWarnings({invisible(capture.output(
aggregated_data <- Aggregation(recipe, data)
))})

# Calibrate data
suppressWarnings({invisible(capture.output(
calibrated_data <- Calibration(recipe, aggregated_data)
))})

# Compute skill metrics
suppressWarnings({invisible(capture.output(
skill_metrics <- Skill(recipe, calibrated_data)
))})

suppressWarnings({invisible(capture.output(
probs <- Probabilities(recipe, calibrated_data)
))})

# Saving
suppressWarnings({invisible(capture.output(
Saving(recipe = recipe, data = calibrated_data,
       skill_metrics = skill_metrics, probabilities = probs)
))})

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

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

test_that("1. Time Aggregation", {
expect_equal(
is.list(aggregated_data),
TRUE
)
expect_equal(
"time_bounds" %in% names(aggregated_data$hcst$attrs),
TRUE
)
expect_equal(
is.list(aggregated_data$hcst$attrs$time_bounds),
TRUE
)
expect_equal(
names(aggregated_data$hcst$attrs$time_bounds),
c("start", "end")
)
expect_equal(
dim(aggregated_data$hcst$attrs$time_bounds$start),
dim(aggregated_data$hcst$attrs$time_bounds$end)
)
expect_equal(
dim(aggregated_data$hcst$attrs$time_bounds$start),
dim(aggregated_data$hcst$attrs$Dates)
)
expect_equal(
"plotting_attr" %in% names(attributes(aggregated_data$hcst$attrs$time_bounds)),
TRUE
)
expect_equal(
attributes(aggregated_data$hcst$attrs$time_bounds)$plotting_attr$ini_ftime,
c(1,2)
)
expect_equal(
attributes(aggregated_data$hcst$attrs$time_bounds)$plotting_attr$end_ftime,
c(3,4)
)
expect_equal(
names(aggregated_data),
c("hcst", "fcst", "obs")
)
expect_equal(
class(aggregated_data$hcst),
"s2dv_cube"
)
expect_equal(
class(aggregated_data$fcst),
"s2dv_cube"
)
expect_equal(
dim(aggregated_data$hcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 2, latitude = 3, longitude = 3, ensemble = 25)
)
expect_equal(
dim(aggregated_data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 2, latitude = 3, longitude = 3, ensemble = 51)
)
expect_equal(
mean(aggregated_data$fcst$data),
291.6194,
tolerance = 0.0001
)
expect_equal(
mean(aggregated_data$hcst$data),
290.6241,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(aggregated_data$hcst$data)[1, , 2, 3, 4]),
c(290.5897, 290.2554),
tolerance = 0.0001
)

expect_equal(
range(aggregated_data$fcst$data),
c(287.4032, 296.3530),
tolerance = 0.0001
)

})


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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("rpss", "rpss_significance", "crpss", "crpss_significance", "enscorr",
  "enscorr_significance", "corr_individual_members", "corr_individual_members_significance",
  "enscorr_specs")
)
expect_equal(
class(skill_metrics$rpss),
"array"
)
expect_equal(
dim(skill_metrics$rpss),
c(var = 1, time = 2, latitude = 3, longitude = 3)
)
expect_equal(
dim(skill_metrics$rpss_significance),
dim(skill_metrics$rpss)
)
expect_equal(
as.vector(skill_metrics$rpss[, , 2, 3]),
c(-1.384229, -1.147657),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$rpss_significance[, , 2, 3]),
rep(FALSE, 2)
)

})

test_that("4. Saving", {
outputs <- paste0(recipe$Run$output_dir, "/outputs/")
expect_equal(
all(basename(list.files(outputs, recursive = T)) %in%
c("tas_19931101.nc", "tas_19941101.nc", "tas_19951101.nc", 
  "tas_19961101.nc", "tas_20201101.nc", "tas-corr_month11.nc", 
  "tas-obs_19931101.nc", "tas-obs_19941101.nc", "tas-obs_19951101.nc",
  "tas-obs_19961101.nc", "tas-percentiles_month11.nc", "tas-probs_19931101.nc",
  "tas-probs_19941101.nc", "tas-probs_19951101.nc", "tas-probs_19961101.nc", 
  "tas-probs_20201101.nc", "tas-skill_month11.nc")),
TRUE
)
expect_equal(
length(list.files(outputs, recursive = T)),
17
)

})

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

})

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