
context("Decadal monthly data")

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-decadal_visualization.yml"
recipe <- prepare_outputs(recipe_file)

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

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

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


output_conf <- read_yaml("modules/Visualization/output_size.yml",
                         eval.exp = TRUE)$region
outdir <- "./tests/out-logs/"



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


pfm_elements <- plot_forecast_map(recipe = recipe, fcst = data$fcst,
                                  mask = NULL, dots = NULL,
                                  outdir = outdir, output_conf = output_conf,
                                  return_elements = TRUE)


test_that("1. Test plot_forecast_map() decadal metadata. This example has 
          'median' as forecast method, is single-panel, has a cylindrical 
          equidistant projection, and has no masked or dotted points.", {
  
  expect_equal(
    pfm_elements$latitude[1],
    19.99996,
    tolerance = 0.001
  )
  
  expect_equal(
    pfm_elements$longitude[1],
    12.65625,
    tolerance = 0.001
  )
  
  expect_equal(
    pfm_elements$system_name,
    "EC-Earth3-i4"
  )
  
  expect_equal(
    pfm_elements$start_date,
    "2021"
  )
  
  expect_equal(
    pfm_elements$init_date,
    1
  )
  
  expect_equal(
    pfm_elements$palette,
    "RdBu"
  )
  
  expect_equal(
    pfm_elements$brks[1:3],
    c(-300, -250, -200)
  )
  
  expect_equal(
    pfm_elements$cols[1:3],
    c("#33BFD1", "#58CAD9", "#7DD6E1")
  )
  
  expect_equal(
    pfm_elements$toptitle,
    "EC-Earth3-i4 / Near-Surface Air Temperature\nEnsemble median / March 2022 / Start date: 2021"
  )
  
  expect_equal(
    as.character(pfm_elements$labels$time_labels),
    c("January", "February", "March")
  )
  
  expect_equal(
    pfm_elements$labels$years,
    c(2022, 2022, 2022)
  )
  
  expect_equal(
    gsub(pfm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/forecast_ensemble_median-2021_ft03.pdf"
  )
  
})
  

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


pm_elements <- plot_metrics(recipe = recipe, data_cube = data$hcst,
                            metrics = skill_metrics, outdir = outdir,
                            output_conf = output_conf,
                            return_elements = TRUE)

test_that("2. Test plot_metrics() decadal metadata. This example is
          single-panel, has a cylindrical equidistant projection, and has no 
          masked or dotted points.", {
  
  expect_equal(
    pm_elements$latitude[1],
    19.99996,
    tolerance = 0.001
  )
  
  expect_equal(
    pm_elements$longitude[1],
    12.65625,
    tolerance = 0.001
  )

  expect_equal(
    pm_elements$system_name,
    "EC-Earth3-i4"
  )
  
  expect_equal(
    pm_elements$start_date,
    NULL
  )
  
  expect_equal(
    pm_elements$brks[1:3],
    c(-1.0, -0.8, -0.6)
  )
  
  expect_equal(
    pm_elements$cols[1:3],
    c("#1E61A5", "#3C8ABE", "#7CB7D6")
  )
  
  expect_equal(
    pm_elements$toptitle,
    "EC-Earth3-i4 / Near-Surface Air Temperature \n RPSS / March / 1991-1994"
  )
  
  expect_equal(
    pm_elements$nominal_startdate_caption,
    NULL
  )
  
  expect_equal(
    pm_elements$forecast_time_caption,
    "Forecast month: 03"
  )
  
  expect_equal(
    gsub(pm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/rpss_ft03.pdf"
  )
  
})


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


pmlt_elements <- plot_most_likely_terciles(recipe = recipe,
                                           fcst = data$fcst,
                                           probabilities = probabilities$probs_fcst,
                                           outdir = outdir,
                                           output_conf = output_conf,
                                           return_elements = TRUE)


test_that("3. Test plot_metrics() decadal metadata. This example is
          single-panel, has a cylindrical equidistant projection, and has no 
          masked or dotted points.", {
  
  expect_equal(
    pmlt_elements$latitude[1],
    19.99996,
    tolerance = 0.001
  )
  
  expect_equal(
    pmlt_elements$longitude[1],
    12.65625,
    tolerance = 0.001
  )
  
  expect_equal(
    pmlt_elements$system_name,
    "EC-Earth3-i4"
  )
  
  expect_equal(
    pmlt_elements$start_date,
    "2021"
  )
  
  expect_equal(
    pmlt_elements$init_date,
    1
  )
  
  expect_equal(
    pmlt_elements$base_args_brks[[1]],
    4
  )
  
  expect_equal(
    pmlt_elements$cols[[1]],
    c("#A0E5E4", "#33BFD1")
  )
  
  expect_equal(
    pmlt_elements$toptitle,
    "EC-Earth3-i4 / Near-Surface Air Temperature\nMost Likely Tercile / March / Start date: 2021"
  )
  
  expect_equal(
    as.character(pmlt_elements$labels$time_labels),
    c("January", "February", "March")
  )
  
  expect_equal(
    pmlt_elements$labels$years,
    c(2022, 2022, 2022)
  )
  
  expect_equal(
    pmlt_elements$forecast_time,
    "03"
  )
  
  expect_equal(
    pmlt_elements$years,
    c(2022, 2022, 2022)
  )
  
  expect_equal(
    gsub(pmlt_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/forecast_most_likely_tercile-2021_ft03.pdf"
  )

})

