
# Seasonal 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-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)
))})


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() seasonal metadata. This example has 
          'median' as forecast method, is single-panel, has a Robinson 
          projection, and has no masked or dotted points.", {

  expect_equal(
    pfm_elements$latitude[1],
    34.5
  )

  expect_equal(
    pfm_elements$longitude[1],
    -9.5
  )

  expect_equal(
    pfm_elements$system_name,
    "ECMWF SEAS5 (v5.1)"
  )

  expect_equal(
    pfm_elements$start_date,
    "20230601"
  )

  expect_equal(
    pfm_elements$init_date,
    6
  )

  expect_equal(
    pfm_elements$palette,
    "RdBu"
  )

  expect_equal(
    pfm_elements$brks[1:3],
    c(-350, -300, -250)
  )

  expect_equal(
    pfm_elements$cols[1:3],
    c("#33BFD1", "#52C8D8", "#71D2DF")
  )

  expect_equal(
    pfm_elements$toptitle,
    "ECMWF SEAS5 (v5.1) / 2 Metre Temperature\nEnsemble median / June 2023 / Start date: 01-06-2023"
  )

  expect_equal(
    as.character(pfm_elements$labels$time_labels),
    "June"
  )

  expect_equal(
    pfm_elements$labels$years,
    2023
  )
  
  expect_equal(
    gsub(pfm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/forecast_ensemble_median-20230601_ft01.png"
  )

})


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


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

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() seasonal metadata. This example is 
          single-panel, has a Robinson projection, and has no masked or dotted 
          points.", {
  
  expect_equal(
    pm_elements$latitude[1],
    34.5
  )
  
  expect_equal(
    pm_elements$longitude[1],
    -9.5
  )
  
  expect_equal(
    pm_elements$system_name,
    "ECMWF SEAS5 (v5.1)"
  )
  
  expect_equal(
    pm_elements$start_date,
    "0601"
  )
  
  expect_equal(
    pm_elements$brks[1:3],
    c(-1.0, -0.8, -0.6)
  )
  
  expect_equal(
    pm_elements$cols[1:3],
    c("#502383", "#7865A5", "#A49BC7")
  )
  
  expect_equal(
    pm_elements$toptitle,
    "ECMWF SEAS5 (v5.1) / 2 Metre Temperature \n RPSS / June / 2013-2016"
  )
  
  expect_equal(
    pm_elements$nominal_startdate_caption,
    "1st of June"
  )
  
  expect_equal(
    pm_elements$forecast_time_caption,
    "Forecast month: 01"
  )
  
  expect_equal(
    gsub(pm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/rpss-june_ft01.png"
  )
  
})
  
  
# ----------------------


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_most_likely_terciles() seasonal metadata. This example is 
          single-panel, has a Robinson projection, and has no masked or dotted 
          points.", {
  
  expect_equal(
    pmlt_elements$latitude[1],
    34.5
  )
  
  expect_equal(
    pmlt_elements$longitude[1],
    -9.5
  )
  
  expect_equal(
    pmlt_elements$system_name,
    "ECMWF SEAS5 (v5.1)"
  )
  
  expect_equal(
    pmlt_elements$start_date,
    "20230601"
  )
  
  expect_equal(
    pmlt_elements$init_date,
    6
  )
  
  expect_equal(
    pmlt_elements$base_args_brks,
    list(4, 2, 4)
  )
  
  expect_equal(
    pmlt_elements$cols[[1]],
    c("#A0E5E4", "#33BFD1")
  )
  
  expect_equal(
    pmlt_elements$toptitle,
    "ECMWF SEAS5 (v5.1) / 2 Metre Temperature\nMost Likely Tercile / June 2023 / Start date: 01-06-2023"
  )
  
  expect_equal(
    as.character(pmlt_elements$labels$time_labels),
    "June"
  )
  
  expect_equal(
    pmlt_elements$labels$years,
    2023
  )
  
  expect_equal(
    pmlt_elements$forecast_time,
    "01"
  )
  
  expect_equal(
    pmlt_elements$years,
    2023
  )
  
  expect_equal(
    gsub(pmlt_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/forecast_most_likely_tercile-20230601_ft01.png"
  )
  
})
  

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