context("Decadal monthly data - 3")

###########################################

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

recipe_file <- "tests/recipes/recipe-decadal_monthly_3.yml"
recipe <- prepare_outputs(recipe_file)
archive <- read_yaml(paste0(recipe$Run$code_dir, "conf/archive_decadal.yml"))$archive

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

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

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

#======================================

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(
data$fcst,
NULL
)
expect_equal(
class(data$obs),
"s2dv_cube"
)
expect_equal(
names(data$hcst),
c("data", "dims", "coords", "attrs")
)
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 = 25, longitude = 16, ensemble = 3)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 4, time = 3)
)
# hcst data
expect_equal(
as.vector(aperm(drop(data$hcst$data), c(5, 1:4))[3, , 2, 2, 2]),
c(281.8123, 283.5582, 283.3947, 283.7838),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
289.9328,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(274.9858, 302.5142),
tolerance = 0.0001
)

expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("2016-06-16", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("2017-06-16", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("2016-07-16 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[12],
as.POSIXct("2019-08-16 12:00:00", tz = 'UTC')
)

})

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

expect_equal(
names(calibrated_data),
c("hcst", "obs", "fcst")
)
expect_equal(
as.vector(aperm(drop(calibrated_data$hcst$data), c(5, 1:4))[3, , 2, 2, 2]),
c(284.4783, 285.9470, 285.4653, 285.9937),
tolerance = 0.0001
)
expect_equal(
calibrated_data$fcst,
NULL
)
})


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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("bss10", "bss10_significance",
  "corr_individual_members", "corr_individual_members_significance")
)
expect_equal(
class(skill_metrics[[1]]),
"array"
)
expect_equal(
all(unlist(lapply(lapply(skill_metrics, dim)[1:2], all.equal, c(var = 1, time = 3, latitude = 25, longitude = 16)))),
TRUE
)
expect_equal(
all(unlist(lapply(lapply(skill_metrics, dim)[3:4], all.equal, c(ensemble = 3, var = 1, time = 3, latitude = 25, longitude = 16)))),
TRUE
)
expect_equal(
as.vector(skill_metrics$bss10[, , 1, 2]),
c(-0.1904762, -0.1904762, -0.1904762),
tolerance = 0.0001
)
expect_equal(
any(as.vector(skill_metrics$bss10_significance)),
FALSE
)
expect_equal(
as.vector(skill_metrics$corr_individual_members[2, , , 1, 2]),
c(-0.1019575, 0.1528021, -0.9536640),
tolerance = 0.0001
)

# Probs
expect_equal(
names(probs),
c('probs', 'percentiles')
)
expect_equal(
names(probs$probs),
c('prob_b33', 'prob_33_to_66', 'prob_a66')
)
expect_equal(
names(probs$percentiles),
c('percentile_33', 'percentile_66')
)
expect_equal(
dim(probs$probs$prob_b33),
c(var = 1, syear = 4, time = 3, latitude = 25, longitude = 16)
)
expect_equal(
dim(probs$percentiles$percentile_33),
c(var = 1, time = 3, latitude = 25, longitude = 16)
)
expect_equal(
as.vector(probs$probs$prob_b33[, , 1, 2, 2]),
c(0.3333333, 0.3333333, 0.6666667, 0.0),
tolerance = 0.0001
)
expect_equal(
as.vector(probs$percentiles$percentile_33[, 1:3, 1, 2]),
c(282.0237, 284.8125, 284.9764),
tolerance = 0.0001
)

})

unlink(recipe$Run$output_dir, recursive = TRUE)
