# Seasonal monthly data

## TO REMOVE ## ****************************************************************
## setwd("/home/Earth/abatalla/SUNSET_RS_GIT/sunset") ## dev:
## # setwd("/home/Earth/abatalla/SUNSET_RS_GIT/sunset/tests/testthat")
## library(testthat)
# ******************************************************************************

source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Calibration/Calibration.R")
source("modules/Skill/Skill.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")


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

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

# Units transformation
suppressWarnings({invisible(capture.output(
  data <- Units(recipe, data)
))})

# Calibrate data
suppressWarnings({invisible(capture.output(
  data <- Calibration(recipe, data)
))})

# Compute skill metrics
suppressWarnings({invisible(capture.output(
  skill_metrics <- Skill(recipe, data)
))})

# Compute percentiles and probability bins
suppressWarnings({invisible(capture.output(
  probabilities <- Probabilities(recipe, data)
))})



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


test_that("1. Loading", {
  
  expect_equal(
    is.list(data),
    TRUE
  )
  
  expect_true(
    all(c("hcst", "fcst", "obs") %in% names(data)) ## alternatively: setequal(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 = 1, latitude = 12, longitude = 15, ensemble = 25)
  )
  
  expect_equal(
    dim(data$fcst$data),
    c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 1, latitude = 12, longitude = 15, ensemble = 51)
  )
  
  expect_equal(
    dim(data$hcst$attrs$Dates),
    c(sday = 1, sweek = 1, syear = 4, time = 1)
  )
  
})


# ----------------------


# output_conf <- read_yaml("modules/Visualization/output_size.yml",
#                          eval.exp = TRUE)$region
# outdir <- "./tests/testthat/_snaps/" ## "./_snaps/"  ## dev: "./tests/testthat/_snaps/"
# pem_elements <- plot_ensemble_mean(recipe = recipe, fcst = data$fcst,
#                                    mask = NULL, dots = NULL,
#                                    outdir = outdir, output_conf = output_conf,
#                                    return_elements = TRUE)
# 
# test_that("2. Test plot_ensemble_mean()", {
#   
#   expect_equal(
#     pem_elements$latitude[1],
#     34.5
#   )
#   
#   expect_equal(
#     pem_elements$longitude[1],
#     -9.5
#   )
#   
#   expect_equal(
#     pem_elements$system_name, 
#     "ECMWF SEAS5 (v5.1)"
#   )
#   
#   expect_equal(
#     pem_elements$start_date,
#     "20230601"
#   )
#   
#   expect_equal(
#     pem_elements$init_date,
#     6
#   )
#   
#   expect_equal(
#     pem_elements$projection,
#     "robinson"
#   )
#   
#   expect_equal(
#     pem_elements$ensemble_mean[, , 1, 1, 1, 1, 1, 1],
#     295.0356,
#     tolerance = 0.001
#   )
#   
#   expect_equal(
#     pem_elements$palette,
#     "RdBu"
#   )
#   
#   expect_equal(
#     pem_elements$brks[1:3],
#     c(284, 285, 286)
#   )
#   
#   expect_equal(
#     pem_elements$cols[1:3],
#     c(284, 285, 286)
#   )
#   
#   expect_equal(
#     pem_elements$toptitle,
#     "ECMWF SEAS5 (v5.1) / 2 Metre Temperature\nEnsemble Mean / June 2023 / Start date: 01-06-2023"
#   )
#   
#   expect_equal(
#     class(pem_elements$labels$time_labels),
#     c("ordered", "factor")
#   )
#   
#   expect_equal(
#     pem_elements$labels$years,
#     2023
#   )
#   
# })


# Snapshot test

Sys.setenv(TESTTHAT_EDITION = "3")

save_fun <- function(...) {
  path <- "./_snaps/"  ## dev: "./tests/testthat/_snaps/"
  ## png(path)
  do.call(plot_ensemble_mean, list(...))
  ## dev.off()
  return(path)
}

outdir <- "./tests/testthat/_snaps/seasonal_visualization_metadata/"

dots <- skill_metrics$rpss_significance
dots[1, 1, 1:6, ] <- TRUE

test_that("2. Snapshot checks", {
  
  expect_snapshot_file(
    paste0(save_fun(recipe = recipe, fcst = data$fcst,
                    outdir = outdir, output_conf = output_conf), 
           "forecast_ensemble_mean-20230601_ft01.png"),
    name = "forecast_ensemble_mean-20230601_ft01.png")
  
  expect_snapshot_file(
    paste0(save_fun(recipe = recipe, fcst = data$fcst,
                    dots = dots,
                    outdir = outdir, output_conf = output_conf),
           ## "seasonal_visualization_metadata/forecast_ensemble_mean-20230601_enscordots_ft01.pdf"),
           "forecast_ensemble_mean-20230601_enscordots_ft01.png"),
    name = "forecast_ensemble_mean-20230601_enscordots_ft01.png")
  
  expect_snapshot_file(
    paste0(save_fun(recipe = recipe, fcst = data$fcst,
                    mask = dots,
                    outdir = outdir, output_conf = output_conf),
           "forecast_ensemble_mean-20230601_enscormask_ft01.png"),
    name = "forecast_ensemble_mean-20230601_enscormask_ft01.png")
})


# ----------------------


# testthat::test_file("./tests/testthat/test-seasonal_visualization_metadata.R")

# plot_ensemble_mean(recipe = recipe, fcst = data$fcst,
#                    mask = dots,
#                    outdir = outdir, output_conf = output_conf)
