library(CSTools)
library(s2dverification)
library(abind)
library(qmap)
library(zeallot)
library(parallel)
library(multiApply)

source("cstools/R/CST_SplitDim.R")
source("cstools/R/CST_QuantileMapping.R")

c(exp, obs) %<-% CST_Load('prlr', exp = 'system5c3s',
                         obs = 'era5',
                         sdates = c('20100501', '20110501', '20120501'),
                         #leadtimemin = 121, leadtimemax = 151,
                         latmin = 44, latmax = 47,
                         lonmin = 5, lonmax = 9,
                         nmember = 25,
                         storefreq = "daily", sampleperiod = 1,
                         output = "lonlat", config = "../BSC.conf")

#> dim(lonlat_prec$data)
#dataset  member   sdate   ftime     lat     lon 
#      1      25       3     214       4       5 
#> class(lonlat_prec)
#[1] "s2dv_cube"

prec_exp <- CST_SplitDim(exp, split_dim = 'ftime')

# -----------------------------
data <- Load('prlr', exp = 'system5c3s',
                         obs = 'era5',
                         sdates = c('20100501', '20110501', '20120501'),
                         #leadtimemin = 121, leadtimemax = 151,
                         latmin = 44, latmax = 47,
                         lonmin = 5, lonmax = 9,
                         nmember = 25,
                         storefreq = "daily", sampleperiod = 1,
                         output = "lonlat", config = "../../BSC.conf")
library(startR)
exp_path <- '/esarchive/exp/meteofrance/system5c3s/daily_mean/$var$_s0-24h/$var$_$sdate$.nc'

lons.min <- 5
lons.max <- 9
lats.min <- 44
lats.max <- 47

expStartR <- Start(dat = exp_path,
             var = 'prlr',
             sdate = c('20100501', '20110501', '20120501'),
             time = 'all',
             ensemble = 'all',
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(longitude = c('lon', 'longitude'),
                             latitude = c('lat', 'latitude')),
             transform_vars = c('latitude', 'longitude'),
             return_vars = list(time = 'dat',
                                latitude = 'dat',
                                longitude = 'dat'),
             #num_procs = 4,
             retrieve = TRUE)
#---------------------------------------------------




> dim(prec_exp$data)
dataset  member   sdate   ftime     lat     lon monthly 
      1      25       3      31       4       5       8 

prec_exp$data[1, 1, sdate = 1, , 1, 1, monthly = 8 ]
30 + 31 + 31 + 28 + 31 + 30 + 31 + 2
30 + 31 + 31 + 29 + 31 + 30 + 31 + 1
30 + 31 + 31 + 28 + 31 + 30 + 31 + 2

# ----------------
dim(exp$data) <- c(dataset = 1, member = 25, time = 3 *214,
                           lat = 4, lon = 5)
attr(exp$data, 'dimensions') <- names(dim(exp$data))
#> dim(lonlat_prec$data)
#dataset  member    time     lat     lon 
#      1      25     642       4       5 

dim(obs$data) <- c(dataset = 1, member = 1, time = 3 *214,
                           lat = 4, lon = 5)
attr(obs$data, 'dimensions') <- names(dim(obs$data))
#> dim(lonlat_prec$data)
#dataset  member    time     lat     lon 
#      1      25     642       4       5 

exp <- CST_SplitDim(exp, split_dim = 'time')
obs <- CST_SplitDim(obs, split_dim = 'time')

> dim(exp$data)
dataset  member    time     lat     lon monthly 
      1      25      31       4       5      24 
exp$data <- Subset(exp$data, along = 'monthly', indices = -c(8, 16, 24))
obs$data <- Subset(obs$data, along = 'monthly', indices = -c(8, 16, 24))


Quant <- CST_QuantileMapping(exp, obs,  
                      sample_dims = c('time', 'member'),
                      ncores = detectCores() - 1)
31 + 30 + 31 + 31 + 30 + 31 + 30
