#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)

# Author: N.Pérez-Zanón 26th July 2021
# execution: Rscript OperationalCS.R conf_user.yml recipe.yml
# This code should process and check the recipe and build the workflow
print(args)
library(yaml)
conf <- read_yaml(args[1]) # conf <- read_yaml("conf_user.yml")
recipe <- read_yaml(args[2]) # recipe <- read_yaml("recipe_seasonal.yml")

# Load required libraries
  library(s2dverification)
  library(ncdf4)
  library(abind)
  library(easyVerification)
  library(multiApply)
  library(startR)
  library(easyNCDF)
  library(CSTools)

  library(parallel)
  library(pryr) # To check mem usage.
setwd("/esarchive/scratch/nperez/git/S2S4E-backend-BSC/data-analysis/")
  source('export_2_nc.R')
  source('S2S/s2s.filefmt.R')
  source('s2s.calib.R')

  source("s2s_tools.R")
  source("Calibration_fcst4.R")
  source("R_Reorder.R")
  source("R_CST_MergeDims.R")
setwd(conf$code_dir)
  # To be removed when new package is done by library(CSOperational)
  source("/esarchive/scratch/nperez/git/Flor/OperationalCS/data_analysis/check_recipe.R")
  source("/esarchive/scratch/nperez/git/Flor/OperationalCS/data_analysis/check_conf.R")

check_conf(conf, file = args[1]) # check_conf(conf, file = "conf_user.yml")
check_recipe(recipe, file = args[2], conf) # check_recipe(recipe, file = "recipe.yml", conf)

fcst.sdate <- recipe$Analysis$Time$forecast_sdate
fcst.freq <- recipe$Analysis$Time$freq
hcst.inityear <- recipe$Analysis$Time$hindcast_start
hcst.endyear <- recipe$Analysis$Time$hindcast_end
variable <- as.character(recipe$Analysis$Variables[[1]][1])
fcst.name <- recipe$Analysis$Datasets$System[[1]]$name
ref.name <- recipe$Analysis$Datasets$Reference[[1]]$name
ltmin <- recipe$Analysis$Time$leadtimemin 
ltmax <- recipe$Analysis$Time$leadtimemax
lats.min <- recipe$Analysis$Region$Regional[[1]]$latmin
lats.max <- recipe$Analysis$Region$Regional[[1]]$latmax
lons.min <- recipe$Analysis$Region$Regional[[1]]$lonmin
lons.max <- recipe$Analysis$Region$Regional[[1]]$lonmax

stream <- "fcst"

source("data_analysis//seas5.load.R")



