context("Seasonal daily data")

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-seasonal_daily_1.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = F)
# Load datasets
suppressWarnings({invisible(capture.output(
data <- Loading(recipe)
))})

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

# Compute skill metrics
suppressWarnings({invisible(capture.output(
skill_metrics <- Skill(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 = 31, latitude = 4, longitude = 4, ensemble = 25)
)
expect_equal(
dim(data$obs$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 31, latitude = 4, longitude = 4, ensemble = 1)
)
expect_equal(
dim(data$obs$attrs$Dates),
c(sday = 1, sweek = 1, syear = 4, time = 31)
)
expect_equal(
as.vector(drop(data$hcst$data)[1:2,1:2,1,2,3]),
c(295.5691, 291.7752, 294.0874, 290.1173),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
288.3723,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(280.1490, 298.2324),
tolerance = 0.0001
)
expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("1993-12-01 18:00:00 UTC", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("1994-12-01 18:00:00 UTC", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("1993-12-02 18:00:00 UTC", tz = 'UTC')
)
expect_equal(
(data$obs$attrs$Dates)[10],
as.POSIXct("1994-12-03 11:30:00 UTC", 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(
calibrated_data$fcst,
NULL
)
expect_equal(
dim(calibrated_data$hcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 31, latitude = 4, longitude = 4, ensemble = 25)
)
expect_equal(
mean(calibrated_data$hcst$data),
289.5354,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(calibrated_data$hcst$data)[1, 1:4, 2, 3, 4]),
c(291.5555, 291.9029, 293.2685, 290.7782),
tolerance = 0.0001
)
expect_equal(
range(calibrated_data$hcst$data),
c(284.2823, 296.7545),
tolerance = 0.0001
)
})


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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("enscorr_specs")
)
expect_equal(
class(skill_metrics$enscorr_specs),
"array"
)
expect_equal(
dim(skill_metrics$enscorr_specs),
c(var = 1, time = 31, latitude = 4, longitude = 4)
)
expect_equal(
skill_metrics$enscorr_specs[, 1:3, 1, 1],
c(0.7509920, 0.6514916, 0.5118371),
tolerance=0.0001
)
})

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