
context("Subseasonal weekly data")


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


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

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

## outdir <- get_dir(recipe = recipe, variable = data$hcst$attrs$Variable$varName)
outdir <- "./tests/out-logs/"

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




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

# Plot forecast map

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() subseasonal metadata. This example has 
          'median' as forecast method, is single-panel, has cylindrical 
          equidistant projection, and has no masked or dotted points.", {
  
  expect_equal(
    pfm_elements$latitude[1],
    -4.251954, 
    tolerance = 0.001
  )
  
  expect_equal(
    pfm_elements$longitude[1],
    -9.375,
    tolerance = 0.001
  )
  
  expect_equal(
    pfm_elements$system_name,
    "NCEP CFSv2"
  )
  
  expect_equal(
    pfm_elements$start_date,
    20240104
  )
  
  expect_equal(
    pfm_elements$init_date,
    20240104
  )
  
  expect_equal(
    pfm_elements$palette,
    "RdBu"
  )
  
  expect_equal(
    pfm_elements$brks[1:3],
    c(-30, -25, -20)
  )
  
  expect_equal(
    pfm_elements$cols[1:3],
    c("#33BFD1", "#58CAD9", "#7DD6E1")
  )
  
  expect_equal(
    pfm_elements$toptitle,
    "NCEP CFSv2 / 2 Metre Temperature\nEnsemble median / Issued on 04-01-2024\nValid from 29-01 to 04-02 of 2024"
  )
  
  expect_equal(
    pfm_elements$labels$time_labels[1],
    "Valid from 08-01 to 14-01 of "
  )
  
  expect_equal(
    pfm_elements$labels$years[1],
    2024
  )
  
  expect_equal(
    gsub(pfm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/forecast_ensemble_median-20240104_ft04.png"
  )
  
})

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

# Plot metrics

## Skill module not adapted for subseasonal data yet
skill_metrics_dims <- c(var = 1, time = 4, latitude = 10, longitude = 21)
bool_array <- array(rep(c(TRUE, FALSE), length.out = prod(skill_metrics_dims)), dim = skill_metrics_dims)
set.seed(0)
runif_array <- array(runif(prod(skill_metrics_dims), min = -1, max = 1), dim = skill_metrics_dims)
skill_metrics_subs <- list(
  rpss = runif_array,
  rpss_significance = bool_array
)

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


test_that("2. Test plot_metrics() subseasonal metadata. This example is 
          single-panel, has a cylindrical equidistant projection, and has no 
          masked or dotted points.", {
  
  expect_equal(
    pm_elements$latitude[1],
    -4.251954, 
    tolerance = 0.001
  )
  
  expect_equal(
    pm_elements$longitude[1],
    -9.187988, ## data$fcst$coords$longitude[1] (pfm)  !=  data$hcst$coords$longitude[1] (pm)  (see Requests #2766)
    tolerance = 0.001
  )
  
  expect_equal(
    pm_elements$system_name,
    "NCEP CFSv2"
  )
  
  expect_equal(
    pm_elements$start_date,
    20240104
  )
  
  expect_equal(
    pm_elements$brks[1:3],
    c(-1, -0.8, -0.6)
  )
  
  expect_equal(
    pm_elements$cols[1:3],
    c("#1E61A5", "#3C8ABE", "#7CB7D6")
  )
  
  expect_equal(
    pm_elements$toptitle,
    "NCEP CFSv2 / 2 Metre Temperature \n RPSS  /  Issued on  04-01-2024  /  1999-2003 \n Valid from 29-01 to 04-02 of 2024"
  )
  
  expect_equal(
    pm_elements$nominal_startdate_caption,
    as.Date("2024-01-04")
  )
  
  expect_equal(
    pm_elements$forecast_time_caption,
    "Forecast week: 04"
  )
  
  expect_equal(
    gsub(pm_elements$fileout, pattern = "//", replacement = "/"),
    "./tests/out-logs/rpss-20240104_ft04.png"
  )

})


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

# Plot most likely terciles

# Probabilities module not adapted for subseasonal data yet

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