# Seasonal monthly data

## TO REMOVE ## ****************************************************************
setwd("/home/Earth/abatalla/SUNSET_RS_GIT/sunset")
# setwd("/esarchive/scratch/vagudets/repos/auto-s2s/")
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, uniqueID = TRUE, 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 = 3, longitude = 3, ensemble = 25)
  )
  
  expect_equal(
    dim(data$fcst$data),
    c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 1, latitude = 3, longitude = 3, ensemble = 51)
  )
  
  expect_equal(
    dim(data$hcst$attrs$Dates),
    c(sday = 1, sweek = 1, syear = 4, time = 1)
  )
  
})

test_that("2. Visualization", {
  
  
  
})


Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'dots')



