#!/usr/bin/env Rscript
rm(list=ls()); gc();
library(CSTools)
library(zeallot)
# Already computed weight and slopes:
load('/esarchive/scratch/nperez/CSTools_manuscript/Dataset_weight.RDS')
weights <- Subset(weight$data, along = 'monthly', indices = c(11,12,1:1:6))
slope <- c(2.45, 2.43, 2.44, 2.36, 2.20, 2.05, 1.98, 2.06, 2.37, 2.47, 2.52, 2.46)
dim(slope) <- c(dataset = 1, monthly = 12)
slope <- Subset(slope, along = 'monthly', indices = c(11,12,1:1:6), drop = 'non-selected')

# Load version that doesn't check the first time step in files:
source("/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/Load.R")
source("/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/Utils.R")

StartDates <- paste0(1993:2018, '1101')
exp_path <- list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")
obs_path <- list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')

c(exp, obs) %<-% CST_Load(var = 'prlr', exp = list(exp_path), obs = list(obs_path),
                          sdates = StartDates, nmember = 25,
                          storefreq = "daily", sampleperiod = 1,
                          latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
                          output = 'lonlat', nprocs = 1)
data <- Load(var = 'prlr', exp = list(exp_path), obs = list(obs_path),
                          sdates = StartDates, nmember = 25,
                          storefreq = "daily", sampleperiod = 1,
                          latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
                          output = 'lonlat', nprocs = 1)

