# This is an example for forecast verification and evaluation.

Sys.setenv(LANG = "en")
source("modules/Loading/Loading.R")
source("modules/Calibration/Calibration.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Skill/Skill.R")
source("modules/Saving/Saving.R")
source("modules/Units/Units.R")
source("modules/Scorecards/Scorecards.R")
source("modules/Visualization/Visualization.R")
source("modules/Downscaling/Downscaling.R")

args = commandArgs(trailingOnly = TRUE)
#recipe_file <- args[1]
#recipe_file <- "recipe_forecast_quality_assessment.yml"
recipe <- prepare_outputs(recipe_file)
# Load datasets
data <- Loading(recipe)

# Units
data <- Units(recipe, data)

# Downscale anomalies
data <- Downscaling(recipe, data)

# Calculate anomalies
#data <- Anomalies (recipe, data)

# Calibrate data
data <- Calibration(recipe, data)

# Calculate skill metrics
skill_metrics <- Skill(recipe, data)

# Calculate probabilities
#probabilities <- Probabilities(recipe, data)

# Visualize data
Visualization(recipe, data, skill_metrics, significance = T)

## Add BSC logo
logo <- "tools/BSC_logo_95.jpg"
system <- list.files(paste0(recipe$Run$output_dir, "/plots"))
variable <- strsplit(recipe$Analysis$Variable$name, ", | |,")[[1]]
files <- lapply(variable, function(x) {
           f <- list.files(paste0(recipe$Run$output_dir, "/plots/",
                                   system, "/", x))
           full_path <- paste0(recipe$Run$output_dir, "/plots/",
                                   system, "/", x,"/", f)})[[1]]
dim(files) <- c(file = length(files))
Apply(list(files), target_dims = NULL, function(x) {
  system(paste("composite -gravity southeast -geometry +10+10",
               logo, x, x))}, ncores = recipe$Analysis$ncores)


###############################################################################################################3

# Compute anomalies
#data <- Anomalies(recipe, data)

#dim(data$obs$data)
#class(data$obs)
#names(data$obs)
#names(data$obs$attr)
# [dat, var, sady, sweek, syear, time, latitude, longitude, ensemble] 

# Test with just 1 dimension

#data$hcst$[1,1,1,1,1]
#data$obs[1]

# Only from workstation (clone repository if using it in Nord3)
# Do I want to change units?
#library(CSIndicators)
#source("https://earth.bsc.es/gitlab/es/csindicators/-/raw/master/R/TotalTimeExceedingThreshold.R")
#ind_obs <- CST_TotalTimeExceedingThreshold(data$obs, threshold = 2, time_dim = "time", start = list(1,1), end = list(30,6))
#ind_hcst <- CST_TotalTimeExceedingThreshold(data$hcst, threshold = 2, time_dim = "time", start = list(1,1), end = list(30,6))

#ind <- list(hcst = ind_hcst,obs = ind_obs, fcst = NULL)
#ind$hcst <- CST_InsertDim(ind$hcst, posdim = 5, lendim = 1, name = "time", values = 1)
#ind$obs <- CST_InsertDim(ind$obs, posdim = 5, lendim = 1, name = "time", values = 1)


#ind$hcst$attrs$Dates <- ind$hcst$attrs$Dates[1,1,]
#ind$obs$attrs$Dates <- ind$obs$attrs$Dates[1,1,]
#dim(ind$hcst$attrs$Dates) <- c(syear = length(ind$hcst$attrs$Dates), time = 1)
#dim(ind$obs$attrs$Dates) <- c(syear = length(ind$obs$attrs$Dates), time = 1)


# Compute skill metrics 
#skill_metrics <- Skill(recipe, ind)

# get the scorecard plots:
#Visualization(recipe, data, skill_metrics, probabilities, significance = T)
