context("Seasonal NAO monthly data")

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

recipe_file <- "tests/recipes/recipe-seasonal_NAO.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)
))})

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

# Compute index NAO
suppressWarnings({invisible(capture.output(
nao_data <- Indices(recipe = recipe, data = ano_data)
))})

# Compute skill metrics
suppressWarnings({invisible(capture.output(
skill_metrics <- Skill(recipe, nao_data, agg = 'region')
))})


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),
"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 = 8, time = 1, latitude = 30, longitude = 61, ensemble = 25)
)
expect_equal(
dim(data$hcst$attrs$Dates),
c(sday = 1, sweek = 1, syear = 8, time = 1)
)
expect_equal(
as.vector(drop(data$hcst$data)[1:2,1,2,3]),
c(101607.2, 101523.7),
tolerance = 0.0001
)
expect_equal(
mean(data$hcst$data),
101450,
tolerance = 0.0001
)
expect_equal(
range(data$hcst$data),
c(98909.3, 103299.8),
tolerance = 0.0001
)
expect_equal(
month((data$hcst$attrs$Dates)[1]),
month(as.POSIXct("1993-04-30 18:00:00", tz = 'UTC'))
)
expect_equal(
month((data$hcst$attrs$Dates)[2]),
month(as.POSIXct("1994-04-30 18:00:00", tz = 'UTC'))
)
expect_equal(
month((data$hcst$attrs$Dates)[5]),
month(as.POSIXct("1997-04-30 18:00:00", tz = 'UTC'))
)
expect_equal(
month((data$obs$attrs$Dates)[8]),
month(as.POSIXct("2000-04-16", 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 = 8, time = 1, latitude = 30, longitude = 61, ensemble = 25)
)
expect_equal(
mean(ano_data$hcst$data),
-1.044053e-13,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(ano_data$hcst$data)[1:3, 3, 4, 1]),
c(-165.688477, -233.336914, 3.358398),
tolerance = 0.0001
)
expect_equal(
range(ano_data$hcst$data),
c(-2045.577, 2027.675),
tolerance = 0.0001
)

})

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

expect_equal(
is.list(nao_data),
TRUE
)
expect_equal(
names(nao_data),
c("hcst", "obs") 
)
expect_equal(
class(nao_data$hcst),
"s2dv_cube"
)
expect_equal(
class(nao_data$fcst),
"NULL"
)
expect_equal(
dim(nao_data$hcst$data),
c(region = 1, syear = 8, ensemble = 25, dat = 1, var = 1, sday = 1, sweek = 1, time = 1)
)
expect_equal(
mean(nao_data$hcst$data),
8.239937e-18,
tolerance = 0.0001
)
expect_equal(
as.vector(drop(nao_data$hcst$data)[1:3, 1]),
c(0.6778843, -1.6014398, 0.3849058),
tolerance = 0.0001
)
expect_equal(
range(nao_data$hcst$data),
c(-3.038780, 2.193189),
tolerance = 0.0001
)

outputs <- paste0(recipe$Run$output_dir, "/plots/")
expect_equal(
all(list.files(outputs, recursive = T) %in%
paste0("Indices/",
  c("NAO_ECMWF-SEAS5_ERA5_s0301_ftime02.png",
    "NAO_correlation_psl_ERA5_s0301_ftime02.png",
    "NAO_correlation_psl_ensmean_ECMWF-SEAS5_s0301_ftime02.png",
    "NAO_correlation_psl_member_ECMWF-SEAS5_s0301_ftime02.png"))),
TRUE)

expect_equal(
length(list.files(outputs, recursive = T)),
4
)


})

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

expect_equal(
is.list(skill_metrics),
TRUE
)
expect_equal(
names(skill_metrics),
c("mean_bias", "enscorr",
  "enscorr_significance", "rps", "rpss", "rpss_significance",
  "crps", "crpss", "crpss_significance",  "enssprerr")
)
expect_equal(
class(skill_metrics$rpss),
"array"
)
expect_equal(
dim(skill_metrics$rpss),
c(region = 1, var = 1, time = 1)
)
expect_equal(
dim(skill_metrics$rpss_significance),
dim(skill_metrics$rpss)
)
expect_equal(
skill_metrics$rpss[1],
-0.06814118,
tolerance = 0.0001
)
expect_equal(
skill_metrics$rpss_significance[1],
FALSE
)

})

test_that("4. Saving", {
outputs <- paste0(recipe$Run$output_dir, "/outputs/")
expect_equal(
all(list.files(outputs, recursive = T) %in%
c(paste0("Indices/ECMWF-SEAS5/nao/", paste0("nao_", 1993:2000, "0301.nc")),
  paste0("Indices/ERA5/nao/", paste0("nao_", 1993:2000, "0301.nc")),
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_crps_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_crpss_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_crpss_significance_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_enscorr_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_enscorr_significance_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_enssprerr_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_mean_bias_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_rps_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_rpss_1993-2000_s03.nc",
  "Skill/ECMWF-SEAS5/ERA5/raw/nao/scorecards_ECMWF-SEAS5_ERA5_nao_rpss_significance_1993-2000_s03.nc"
  )
), TRUE)

expect_equal(
length(list.files(outputs, recursive = T)),
26
)

})

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