context("Decadal monthly data - 2")

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

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-decadal_monthly_2.yml"
recipe <- prepare_outputs(recipe_file)

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

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

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

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


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

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 = 3, time = 14, latitude = 8, longitude = 5, ensemble = 3)
)
expect_equal(
dim(data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 2, time = 14, latitude = 8, longitude = 5, ensemble = 3)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 3, time = 14)
)
#expect_equal(
#dim(data$fcst$Dates$start),
#c(time = 14)
#)
# hcst data
expect_equal(
as.vector(aperm(drop(data$hcst$data), c(5, 1:4))[1,, 1:2, 2, 2]),
c(275.0768, 273.4615, 273.9182, 275.8002, 274.1852, 274.8814),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
270.2828,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(253.8541, 276.6805),
tolerance = 0.0001
)
# fcst data
expect_equal(
as.vector(aperm(drop(data$fcst$data), c(5, 1:4))[1, , 1:2, 2, 2]),
c(274.2464, 275.2312, 275.5695, 275.7522),
tolerance = 0.0001
)
expect_equal(
mean(data$fcst$data),
271.7678,
tolerance = 0.0001
)

expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("1991-01-16 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("1992-01-16 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("1992-02-15 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[10],
as.POSIXct("1991-04-16", tz = 'UTC')
)

})

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

expect_equal(
names(calibrated_data),
c("hcst", "obs", "fcst")
)

})


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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("rpss_specs", "enscorr_specs", "frps_specs", "frpss_specs", "bss10_specs", "frps")
)
expect_equal(
class(skill_metrics$rpss_specs),
"array"
)
expect_equal(
all(unlist(lapply(lapply(skill_metrics, dim), all.equal, c(var = 1, time = 14, latitude = 8, longitude = 5)))),
TRUE
)
expect_equal(
as.vector(skill_metrics$rpss_specs[, 6:8, 1, 2]),
c(-0.3333333, 0.1666667, 0.6666667),
tolerance = 0.0001
)
#expect_equal(
#all(is.na(skill_metrics$bss90_specs)),
#TRUE
#)
expect_equal(
as.vector(skill_metrics$enscorr_specs[, 6:8, 1, 2]),
c(0.4042823, 0.9487580, 0.6592351),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$frps_specs[, 6:8, 1, 2]),
c(4.444444e-01, 2.222222e-01, 1.850372e-17),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$frpss_specs[, 4:7, 1, 5]),
c(-0.5, 0.5, 1.110223e-16, 0.5),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$bss10_specs[, 6:8, 1, 2]),
c(-0.5, 0.5, 0.5),
)
expect_equal(
as.vector(skill_metrics$frps[, 6:8, 1, 2]),
c(0.4444444, 0.2222222, 0),
tolerance = 0.0001
)

# Probs
expect_equal(
names(probs),
c('probs', 'probs_fcst', '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 = 3, time = 14, latitude = 8, longitude = 5)
)
expect_equal(
dim(probs$percentiles$percentile_33),
c(var = 1,time = 14, latitude = 8, longitude = 5)
)
expect_equal(
as.vector(probs$probs$prob_b33[, , 1, 2, 2]),
c(0, 1, 0),
tolerance = 0.0001
)
expect_equal(
as.vector(probs$percentiles$percentile_33[, 1:3, 1, 2]),
c(274.1937, 275.0280, 275.2046),
tolerance = 0.0001
)

})

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

test_that("4. Saving", {
outputs <- paste0(recipe$Run$output_dir, "/outputs/")
expect_equal(
all(basename(list.files(outputs, recursive = T)) %in%
c("tas_1991.nc", "tas_1992.nc", "tas_1993.nc", "tas_2021.nc", "tas_2022.nc",
  "tas-obs_1991.nc", "tas-obs_1992.nc", "tas-obs_1993.nc",
  "tas-percentiles.nc", "tas-probs_1991.nc", "tas-probs_1992.nc",
  "tas-probs_1993.nc", "tas-probs_2021.nc", "tas-probs_2022.nc", "tas-skill.nc")),
TRUE
)
expect_equal(
length(list.files(outputs, recursive = T)),
15
)

})

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

test_that("5. Visualization", {
plots <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(
all(basename(list.files(plots, recursive = T)) %in%
c("bss10_specs.pdf", "enscorr_specs.pdf", "forecast_ensemble_median-2020.pdf", "forecast_ensemble_median-2021.pdf", "forecast_most_likely_tercile-2020.pdf", "forecast_most_likely_tercile-2021.pdf", "frps_specs.pdf", "frps.pdf", "rpss_specs.pdf", "frpss_specs.pdf")
),
TRUE
)
expect_equal(
length(list.files(plots, recursive = T)),
10
)

})

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

