context("Subseasonal weekly data")

source("modules/Loading/Loading.R")
source("modules/Saving/R/get_dir.R")

recipe_file <- "tests/recipes/recipe-subseasonal_weekly.yml"
recipe <- prepare_outputs(recipe_file, disable_checks = F)
archive <- read_yaml(paste0(recipe$Run$code_dir, "conf/archive_subseasonal.yml"))$archive

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

outdir <- get_dir(recipe = recipe, variable = data$hcst$attrs$Variable$varName)

# ------- TESTS --------

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 = 3, sweek = 5, syear = 5, time = 4, latitude = 10, longitude = 21, ensemble = 12)
)
expect_equal(
dim(data$obs$data),
c(dat = 1, var = 1, sday = 3, sweek = 5, syear = 5, time = 4, latitude = 10, longitude = 21, ensemble = 1)
)
expect_equal(
dim(data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 4, latitude = 10, longitude = 21, ensemble = 48)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 3, sweek = 5, syear = 5, time = 4)
)
# The metadata in files are the sdate + 7 days
expect_equal(
format(data$hcst$attrs$Dates[2, 3, , 1], "%m%d"),
rep("0111", 5)
)
expect_equal(
format(data$hcst$attrs$Dates[, 3, 3, 1], "%m%d"),
c("0108", "0111", "0115"),
)
expect_equal(
format(data$hcst$attrs$Dates[2, , 3, 1], "%m%d"),
c("0118", "0115", "0111", "0108", "0104")
)
expect_equal(
format(data$hcst$attrs$Dates[2, 3, 3, ], "%Y%m%d"),
c("20010111", "20010118", "20010125", "20010201")
)
# Obs metadata
expect_equal(
dim(data$obs$attrs$Dates),
c(sday = 3, sweek = 5, syear = 5, time = 4)
)
expect_equal(
format(data$obs$attrs$Dates[2, 3, , 1], "%m%d"),
rep("0114", 5)
)
expect_equal(
dim(data$obs$attrs$Dates),
dim(data$obs$attrs$load_parameters$dat1$file_date[[1]])
)
expect_equal(
data$obs$attrs$load_parameters$dat1$file_date[[1]][2, 3, , 1],
c("19990108", "20000108", "20010108", "20020108", "20030108")
)
expect_equal(
data$hcst$data[1, 1, 2, 3, , 1, 1, 1, 1],
c(298.9691, 299.0720, 299.0578, 299.3024, 299.5754),
tolerance = 0.0001
)
})

recipe$Analysis$Time$sweek_window <- 1
recipe$Analysis$Time$sday_window <- 1
# Load datasets
suppressWarnings({invisible(capture.output(
data <- Loading(recipe)
))})

test_that("2. Loading", {
expect_equal(
dim(data$hcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 5, time = 4, latitude = 10, longitude = 21, ensemble = 12)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 5, time = 4)
)
expect_equal(
format(data$hcst$attrs$Dates[1, 1, , 1], "%m%d"),
rep("0111", 5)
)
expect_equal(
format(data$hcst$attrs$Dates[1, 1, , 1], "%Y"),
as.character(1999:2003),
)
expect_equal(
format(data$hcst$attrs$Dates[1, 1, 3, ], "%Y%m%d"),
c("20010111", "20010118", "20010125", "20010201")
)
# Obs metadata
expect_equal(
dim(data$obs$attrs$Dates),
c(sday = 1, sweek = 1, syear = 5, time = 4)
)
expect_equal(
format(data$obs$attrs$Dates[1, 1, , 1], "%m%d"),
rep("0114", 5)
)
expect_equal(
dim(data$obs$attrs$Dates),
dim(data$obs$attrs$load_parameters$dat1$file_date[[1]])
)
expect_equal(
data$obs$attrs$load_parameters$dat1$file_date[[1]][1, 1, , 1],
c("19990108", "20000108", "20010108", "20020108", "20030108")
)
expect_equal(
data$hcst$data[1, 1, 1, 1, , 1, 1, 1, 1],
c(298.9691, 299.0720, 299.0578, 299.3024, 299.5754),
tolerance = 0.0001
)
})

test_that("3. Visualization", {
source("modules/Visualization/Visualization.R")
Visualization(recipe = recipe, data = data)
plots <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(
all(basename(list.files(plots, recursive = T)) %in%
c("forecast_ensemble_mean-20240104_ft01.png",
  "forecast_ensemble_mean-20240104_ft02.png",
  "forecast_ensemble_mean-20240104_ft03.png",
  "forecast_ensemble_mean-20240104_ft04.png")),
TRUE
)
expect_equal(
length(list.files(plots, recursive = T)),
4
)
})

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