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(272.8613, 271.0689, 270.8007, 273.5594, 272.1561, 272.8729),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
269.8822,
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(271.7708, 271.8424, 272.4980, 273.5842),
tolerance = 0.0001
)
expect_equal(
mean(data$fcst$data),
271.2158,
tolerance = 0.0001
)

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

})

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

expect_equal(
names(calibrated_data),
c("hcst", "obs", "fcst")
)
## TODO: Ask An-Chi about this test
# expect_equal(
# calibrated_data,
# data[1:2]
# )

})


##======================================
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", "frps_clim")
)
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.3333333),
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.4474382, 0.1026333, 0.4042823),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$frps_specs[, 6:8, 1, 2]),
c(0.4444444, 0.2222222, 0.4444444),
tolerance = 0.0001
)
expect_equal(
as.vector(skill_metrics$frpss_specs[, 4:7, 1, 5]),
c( 1.0, -0.5, -0.5, 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.4444444),
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.0, 0.3333333, 0.6666667),
tolerance = 0.0001
)
expect_equal(
as.vector(probs$percentiles$percentile_33[, 1:3, 1, 2]),
c(271.7508, 273.1682, 274.1937),
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_1990.nc", "tas_1991.nc", "tas_1992.nc", "tas_2020.nc", "tas_2021.nc",
  "tas-obs_1990.nc", "tas-obs_1991.nc", "tas-obs_1992.nc",
  "tas-percentiles.nc", "tas-probs_1990.nc", "tas-probs_1991.nc",
  "tas-probs_1992.nc", "tas-probs_2020.nc", "tas-probs_2021.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.png", "enscorr_specs.png", "forecast_ensemble_mean-2020.png", "forecast_ensemble_mean-2021.png", "forecast_most_likely_tercile-2020.png", "forecast_most_likely_tercile-2021.png", "frps_specs.png", "frps.png", "rpss_specs.png")
),
TRUE
)
expect_equal(
length(list.files(plots, recursive = T)),
9
)

})

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

