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



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


test_that("1. Loading", {
  
  expect_equal(
    is.list(data),
    TRUE
  )
  
  expect_true(
    all(c("hcst", "fcst", "obs") %in% names(data))
  )
  
  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/out-logs/"
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("2. Test plot_forecast_map() 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(-350, -300, -250)
  )

  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(
    levels(pfm_elements$labels$time_labels),
    c("January", "February", "March", "April", "May", "June", "July", 
      "August", "September", "October", "November", "December")
  )

  expect_equal(
    pfm_elements$labels$years,
    2023
  )
  
  expect_equal(
    pfm_elements$fileout,
    "./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("3. Test plot_metrics() 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,
    c(-1.0, -0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
  )
  
  expect_equal(
    pm_elements$cols,
    c(-1.0, -0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
  )
  
  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(
    pm_elements$fileout,
    "./tests/out-logs/rpss-june_ft01.png"
  )
  
})
  
  
# ----------------------


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

test_that("3. Test plot_metrics() 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$brks,
    NULL ##
  )
  
  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(
    levels(pmlt_elements$labels$time_labels),
    c("January", "February", "March", "April", "May", "June", "July", 
      "August", "September", "October", "November", "December")
  )
  
  expect_equal(
    pmlt_elements$labels$years,
    2023
  )
  
  expect_equal(
    pmlt_elements$forecast_time,
    "01"
  )
  
  expect_equal(
    pmlt_elements$years,
    2023
  )
  
  expect_equal(
    pmlt_elements$fileout,
    "./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")
