  source("modules/Loading/Loading.R")
  source("modules/Saving/Saving.R")
  source("modules/Units/Units.R")
  source("modules/Visualization/Visualization.R")
  source("modules/Aggregation/Aggregation.R")
  
  #args = commandArgs(trailingOnly = TRUE)
  #recipe_file <- args[1]
  
  recipe_file <- "recipe_hw36_seasonal_daily_indicators.yml"
  #recipe_file <- "recipe_fdd_seasonal_daily_indicators.yml"
  recipe_file2 <- "recipe_ecvs_cal_seas_monthly.yml"
  indicator_name <- 'Heat waves (3 consecutive days exceeding 36°C)'
  #indicator_name <- 'Frost days'
  #recipe_file <- "recipe_subseasonal_ecvs.yml"
  #recipe_file <- "recipe_prlr_cal_subseas.yml"
  
  #recipe <- read_atomic_recipe(recipe_file)
  recipe <- prepare_outputs(recipe_file)

 # maybe this is due to the   
  recipe$Analysis$Workflow$Visualization$plots <- 'skill_metrics forecast_ensemble_mean most_likely_terciles'
  
  
  # Load datasets
  data <- Loading(recipe)
  data <- Units(recipe, data)

  source('modules/Crossval/Crossval_Calibration_Indicators.R')
  #source("modules/Crossval/Crossval_Calibration.R")
  res <- Crossval_Calibration_Indicators(recipe = recipe, data = data, indicator='HW36', wdw=15) 
  recipe$Analysis$Variables$freq <- 'monthly_mean' # from now on we have monthly data 

  source("modules/Crossval/Crossval_metrics.R")
  skill_metrics <- Crossval_metrics(recipe = recipe, data_crossval = res,
                                    fair = FALSE, nmemb = NULL, nmemb_ref = NULL)
  
  # trick to avoid the crash of plotequimap when we have NAs or -Inf skill values
  for (l in 1:length(skill_metrics)){
    skill_metrics[[l]][which(skill_metrics[[l]]=='-Inf')] <- NaN
    skill_metrics[[l]][is.na(skill_metrics[[l]])] <- -100
  }
  
  # I think there is a problem with the dimensions or something, because if I use directly the 
  #
  recipe2 <- prepare_outputs(recipe_file2) 
  data <- Loading(recipe2)
  recipe$Run$output_dir <- recipe$Run$output_dir 

  data$fcst$data <- aperm(res$fcst$data,c(4,5,6,7,3,1,8,9,2))
  tmp_probs <- list(probs_fcst = res$probs$fcst[[1]])
  nlats <- as.numeric(dim(res$hcst$data)['latitude'])
  nlons <- as.numeric(dim(res$hcst$data)['longitude'])
  ntimes <- as.numeric(dim(res$hcst$data)['time'])
  tmp_probs$probs_fcst <- aperm(Subset(res$probs$fcst[[1]],along=c('dat','sday','sweek'),indices=list(1,1,1),drop='selected'),c(1,2,4,3,5,6))
  
#dim(tmp_probs$probs_fcst) <- c(bin = 3, syear = 1, var = 1, time = ntimes,
   #                              latitude = nlats, longitude = nlons)
  
data$hcst$attrs$Variable$metadata[['tas']]$long_name <- data$fcst$attrs$Variable$metadata[['tas']]$long_name <-
    data$obs$attrs$Variable$metadata[['tas']]$long_name <- indicator_name
  
data$hcst$attrs$Variable$metadata[['tas']]$long <- data$fcst$attrs$Variable$metadata[['tas']]$long <-
    data$obs$attrs$Variable$metadata[['tas']]$long <- indicator_name

  recipe2$Analysis$Workflow$Visualization$plots <- 'skill_metrics forecast_ensemble_mean most_likely_terciles'

  Visualization(recipe = recipe2, data = data, skill_metrics = skill_metrics,
                significance = 'dots', probabilities = tmp_probs,
                logo = NULL)



if (recipe$Analysis$Region$name=='Catalonia'){
 source('./BarPlotCat.R')
 indicator <- indicator_name
 sys <- recipe$Analysis$Datasets$System$name
 ##### to be generalised #########
 latidx  <- 2
 lonidx <- 1
 location <- 'Raimat' 
 #################################

 startdate <- paste0(recipe$Analysis$Time$fcst_year,recipe$Analysis$Time$sdate)
 panel_title <- month.name[month(data$fcst$coords$time)]
 for (i in 1:length(panel_title)){
  mon <- panel_title[i]
  #source("/esarchive/scratch/nperez/git/Flor/Valencia/BarPlotCat.R")
  #lims <- Apply(res$cat_lims$fcst,target_dims=c('ensemble','syear'),function(x){quantile(as.vector(x),probs=c(1/3,2/3))})[[1]]
  probs <- drop(Subset(tmp_probs[[1]],along=c('time','latitude','longitude'),indices=list(i,latidx,lonidx),drop='selected'))
  lims<- drop(Subset(res$cat_lims$hcst_lims[[1]],along=c('time','latitude','longitude'),indices=list(i,latidx,lonidx),drop='selected'))
  skill_value <- drop(Subset(skill_metrics$rpss,along=c('time','latitude','longitude'),indices=list(i,latidx,lonidx),drop='selected'))
  tit <- paste0(location,'/ ',sys,' /',indicator,'\n',mon,' 2025 / Start date:',startdate)
  names(dim(lims)) <- names(dim(probs))
  sd <- paste0(recipe$Analysis$Time$fcst_year,recipe$Analysis$Time$sdate)
  fileout <- paste0(recipe2$Run$output_dir, "/plots/Bars_",location,'',
                   sd, "_ft", sprintf('%02d',i), ".ps")
  cairo_ps(fileout,width=12,height=8)
  BarPlotCat(probs=probs, lims=round(lims,1),toptitle = tit,
             cat_dim = 'cat', panel_dim = 'time', color.set = "s2s4e",
             category_names = c('Below', 'Normal', 'Above'),skill=skill_value,
             legend_width = 0.7, xaxis_title = indicator,
             #panel_title = panel_title[1 ], panel_subtitle = NULL,
             panel_bottom_name = '', lims_pos = -3,
             toptitle_size = 16, toptitle_pos = "center",
             fileout = NULL, width = NULL, height = NULL,
             size_units = NULL, res = NULL)
  dev.off()

 }
}


