library(testthat)
context("Seasonal daily data")

source("modules/Loading/Loading.R")
source("modules/Skill/Skill.R")
source("modules/Downscaling/Downscaling.R")
source("modules/Saving/Saving.R")
source("modules/Anomalies/Anomalies.R")

recipe_file <- "tests/recipes/recipe-seasonal_downscaling.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = F)

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

# Compute anomalies
suppressWarnings({invisible(capture.output(
ano_data <- Anomalies(recipe, data)
))})

# Downscale the data
suppressWarnings({invisible(capture.output(
downscaled_data <- Downscaling(recipe, ano_data)
))})

# Compute skill metrics
suppressWarnings({invisible(capture.output(
skill_metrics <- Skill(recipe, downscaled_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 = 13, longitude = 13, 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. Anomalies", {

expect_equal(
is.list(ano_data),
TRUE
)
expect_equal(
names(ano_data),
c("hcst", "obs", "fcst", "hcst.full_val", "obs.full_val")
)
expect_equal(
class(ano_data$hcst),
"s2dv_cube"
)
expect_equal(
class(ano_data$fcst),
"NULL"
)
expect_equal(
dim(ano_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(ano_data$hcst$data),
-3.466088e-16,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(ano_data$hcst$data)[3, 26:28, 3, 2, 12]),
c(-1.5654303, -0.6506540, -0.4674155),
tolerance = 0.0001
)
expect_equal(
range(ano_data$hcst$data),
c(-6.265505, 9.440247),
tolerance = 0.0001
)

})

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

expect_equal(
is.list(downscaled_data),
TRUE
)
expect_equal(
names(downscaled_data),
c("hcst", "obs", "fcst")
)
expect_equal(
class(downscaled_data$hcst),
"s2dv_cube"
)
expect_equal(
class(downscaled_data$obs),
"s2dv_cube"
)
expect_equal(
downscaled_data$fcst,
NULL
)
expect_equal(
names(downscaled_data$hcst),
c("data", "dims", "coords", "attrs")
)
expect_equal(
names(downscaled_data$hcst),
names(downscaled_data$obs)
)
expect_equal(
dim(downscaled_data$hcst$data),
c(latitude = 13, longitude = 13, syear = 4, dat = 1, var = 1, sday = 1, sweek = 1, time = 31, ensemble = 25)
)
expect_equal(
dim(downscaled_data$obs$data),
c(ensemble = 1, dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 31, latitude = 13, longitude = 13)
)
expect_equal(
as.vector(drop(downscaled_data$hcst$data)[1:2,1:2,1,2,3]),
c(1.482848, 1.503154, 1.599051, 1.591909),
tolerance = 0.0001
)
expect_equal(
mean(downscaled_data$hcst$data),
-0.04462092,
tolerance = 0.0001
)
expect_equal(
range(downscaled_data$hcst$data),
c(-4.611440, 5.771648),
tolerance = 0.0001
)

})

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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("bss10", "bss10_significance", "crpss", "crpss_significance","rpss", "rpss_significance", "mean_bias", "mean_bias_significance")
)
expect_equal(
class(skill_metrics$rpss),
"array"
)
expect_equal(
dim(skill_metrics$rpss),
c(latitude = 13, longitude = 13, var = 1, time = 31)
)
expect_equal(
dim(skill_metrics$rpss_significance),
dim(skill_metrics$rpss)
)
expect_equal(
skill_metrics$rpss[1],
-0.005942857,
tolerance = 0.0001
)
expect_equal(
skill_metrics$rpss_significance[1],
FALSE
)

})

test_that("5. Check saved data", {

outputs <- paste0(recipe$Run$output_dir, "/outputs/Downscaling/")
expect_equal(
all(basename(list.files(outputs, recursive = T)) %in%
c("tas_19931201.nc", "tas_19941201.nc", "tas_19951201.nc",
  "tas_19961201.nc", "tas-obs_19931201.nc", "tas-obs_19941201.nc",
  "tas-obs_19951201.nc", "tas-obs_19961201.nc")),
TRUE
)

})

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

