context("Decadal daily data - 1")

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

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_daily_1.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(data, recipe)
#))})
#
## Compute skill metrics
#suppressWarnings({invisible(capture.output(
#skill_metrics <- Skill(calibrated_data$hcst, data$obs,
#                       recipe, na.rm = T, ncores = 4)
#))})

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

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 = 90, latitude = 7, longitude = 11, ensemble = 3)
)
expect_equal(
dim(data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 2, time = 90, latitude = 7, longitude = 11, ensemble = 3)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 3, time = 90)
)
# hcst data
expect_equal(
as.vector(aperm(drop(data$hcst$data), c(5, 1:4))[1, 2:3, 1:3, 1, 1]),
c(298.5787, 293.6479, 298.5042, 293.7802, 297.8072, 293.0764),
tolerance = 0.0001
)
expect_equal(
as.vector(aperm(drop(data$hcst$data), c(5, 1:4))[2, , 89:90, 1, 1]),
c(301.6978, 308.9792, 308.4501, 302.1620, 307.6034, 307.6388),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
301.2666,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(285.9326, 314.9579),
tolerance = 0.0001
)

# fcst data
expect_equal(
as.vector(aperm(drop(data$fcst$data), c(5, 1:4))[1, , 1:3, 1, 1]),
c(295.0745, 291.1006, 296.2279, 291.6309, 295.3123, 290.8995),
tolerance = 0.0001
)
expect_equal(
as.vector(aperm(drop(data$fcst$data), c(5, 1:4))[2, , 89:90, 1, 1]),
c(305.3428, 305.0657, 305.5445, 305.5681),
tolerance = 0.0001
)

# time value
expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("1991-01-01 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("1992-01-01 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("1992-01-02 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[1, 1, 3, 90],
as.POSIXct("1993-03-31 12:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[1, 1, 2, 90],
as.POSIXct("1992-03-30 12:00:00", tz = 'UTC')
)

})

##======================================
#test_that("2. Calibration", {
#
#expect_equal(
#is.list(calibrated_data),
#TRUE
#)
#expect_equal(
#names(calibrated_data),
#c("hcst", "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, ensemble = 2, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 5, longitude = 4)
#)
#expect_equal(
#dim(calibrated_data$fcst$data),
#c(dat = 1, var = 1, ensemble = 2, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 5, longitude = 4)
#)
#expect_equal(
#mean(calibrated_data$fcst$data),
#291.8375,
#tolerance = 0.0001
#)
#expect_equal(
#mean(calibrated_data$hcst$data),
#289.6679,
#tolerance = 0.0001
#)
#expect_equal(
#as.vector(drop(calibrated_data$hcst$data)[1, , 2, 3, 4]),
#c(286.3895, 286.6408, 290.6652, 288.3759),
#tolerance = 0.0001
#)
#expect_equal(
#range(calibrated_data$fcst$data),
#c(287.2173, 297.4578),
#tolerance = 0.0001
#)
#
#})
#
#
##======================================
#test_that("3. Metrics", {
#
#expect_equal(
#is.list(skill_metrics),
#TRUE
#)
#expect_equal(
#names(skill_metrics),
#c("rpss", "rpss_significance")
#)
#expect_equal(
#class(skill_metrics$rpss[[1]]),
#"array"
#)
#expect_equal(
#dim(skill_metrics$rpss[[1]]),
#c(dat = 1, var = 1, sday = 1, sweek = 1, time = 3, latitude = 5, longitude = 4)
#)
#expect_equal(
#dim(skill_metrics$rpss_significance[[1]]),
#dim(skill_metrics$rpss[[1]])
#)
#expect_equal(
#as.vector(drop(skill_metrics$rpss[[1]])[, 2, 3]),
#c(-0.2857143, -1.2500000, -1.8928571),
#tolerance = 0.0001
#)
#expect_equal(
#as.vector(drop(skill_metrics$rpss_significance[[1]])[, 2, 3]),
#rep(FALSE, 3)
#)
#
#})

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