context("Seasonal monthly data")

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

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

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


# ------- 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 = 1, sweek = 1, syear = 4, time = 3, latitude = 4, longitude = 4, ensemble = 25)
)
expect_equal(
dim(data$fcst$data),
c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 4, longitude = 4, ensemble = 51)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 4, time = 3)
)
expect_equal(
as.vector(drop(data$hcst$data)[1:2,1:2,1,2,3]),
c(285.5869, 287.8836, 285.9362, 289.0483),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
290.1099,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(283.2845, 299.7845),
tolerance = 0.0001
)
expect_equal(
(data$hcst$attrs$Dates)[1],
as.POSIXct("1993-01-31 18:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[2],
as.POSIXct("1994-01-31 18:00:00", tz = 'UTC')
)
expect_equal(
(data$hcst$attrs$Dates)[5],
as.POSIXct("1993-02-28 18:00:00", tz = 'UTC')
)
expect_equal(
(data$obs$attrs$Dates)[10],
as.POSIXct("1994-03-15 12:00:00", tz = 'UTC')
)

})

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