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")

recipe_file <- "tests/recipes/recipe-seasonal_monthly_1.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 <- load_datasets(recipe)
))})

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

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

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

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

# Plotting
suppressWarnings({invisible(capture.output(
plot_data(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. 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. Calibration", {

expect_equal(
is.list(calibrated_data),
TRUE
)
expect_equal(
names(calibrated_data),
c("hcst", "obs", "fcst")
)
expect_equal(
class(calibrated_data$hcst),
"s2dv_cube"
)
expect_equal(
class(calibrated_data$fcst),
"s2dv_cube"
)
expect_equal(
dim(calibrated_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(calibrated_data$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_data$fcst$data),
291.6433,
tolerance = 0.0001
)
expect_equal(
mean(calibrated_data$hcst$data),
290.9006,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(calibrated_data$hcst$data)[1, , 2, 3, 4]),
c(291.8887, 287.0233, 289.8808),
tolerance = 0.0001
)
expect_equal(
range(calibrated_data$fcst$data),
c(283.8926, 299.0644),
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", "corr_significance", "enscorr_specs")
)
expect_equal(
class(skill_metrics$rpss),
"array"
)
expect_equal(
dim(skill_metrics$rpss),
c(var = 1, time = 3, 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(-0.2918857, -1.4809143, -1.3842286),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$rpss_significance[, , 2, 3]),
rep(FALSE, 3)
)

})

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.png", "enscorr_specs-november.png", "enscorr-november.png",
  "forecast_ensemble_mean-20201101.png", "forecast_most_likely_tercile-20201101.png",
  "rpss-november.png")),
TRUE
)
expect_equal(
length(list.files(plots, recursive = T)),
6
)

})

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