# data:
library(CSTools)
exp <- lonlat_data$exp # dataset:1  member:15  sdate:6   ftime:3   lat:22     lon:53 
obs <- lonlat_data$obs # dataset:1  member:1   sdate:6   ftime:3   lat:22     lon:53 
wt_exp <- array(unlist(lapply(1:(6*15), function(x) {sample(1:3)})), 
                c(member = 15, sdate = 6, ftime = 3))  
wt_obs <- array(unlist(lapply(1:6, function(x){sample(1:3)})), 
                c(member = 1, sdate = 6, ftime = 3))  
wt_exp_split <- lapply(1:max(wt_exp), function(x){which(wt_exp == x)})
wt_obs_split <- lapply(1:max(wt_obs), function(x){which(wt_obs == x)})
source("/esarchive/scratch/nperez/git/Flor/ArrayIndex.R")
pos_exp <- lapply(1:max(wt_exp), function(x) { 
              lapply(wt_exp_split[[x]], function(x){
              arrayInd(x, dim(wt_exp)[1:3])})})
exp_split <- lapply(1:max(wt_exp), function(x) { 
              lapply(pos_exp[[x]], function(x){
              exp$data[,x[,1], x[,2], x[,3],,]})})
library(abind)
exp_split <-  lapply(1:max(wt_exp), function(x) {abind(exp_split[[x]], along = 3)})
names(dim(exp_split[[1]])) <- c('lat', 'lon', 'time')
pos_obs <- lapply(1:max(wt_obs), function(x) {
              lapply(wt_obs_split[[x]], function(x){
              arrayInd(x, dim(wt_obs)[1:3])})})
obs_split <- lapply(1:max(wt_obs), function(x) {
              lapply(pos_obs[[x]], function(x){
              obs$data[,x[, 1], x[, 2], x[, 3],,]})})
obs_split <-  lapply(1:max(wt_obs), function(x) {abind(obs_split[[x]], along = 3)})
names(dim(obs_split[[1]])) <- c('lat', 'lon', 'time')

source("/esarchive/scratch/nperez/git/cstools/R/CST_QuantileMapping.R")
library(qmap)
CSTools:::QuantileMapping(exp_split[[1]], obs_split[[1]], sample_dims = 'time')

# Option B:
#Object 'wt' indicates the weather type from 1 to 3. Given that I wanted to use 'member', 'sdate' and 'ftime' as sample dimensions to compute the quantile mapping, I can merge those dimensions.

```
# data:
exp <- lonlat_data$exp # dataset:1  member:15  sdate:6   ftime:3   lat:22     lon:53 
obs <- lonlat_data$obs # dataset:1  member:1   sdate:6   ftime:3   lat:22     lon:53 
wt_exp <- array(rep(sample(1:3), 6 * 15), c(member = 15, sdate = 6, ftime = 3))  
wt_obs <- array(rep(sample(1:3), 6 ), c(member = 1, sdate = 6, ftime = 3))  
which(wt_exp == 1)


# merging dims:
expm <- CST_MergeDims(exp, c('member', 'sdate'), 'sample')
  ## understand order: exp$data[1,1:5,1:5,1,1,1] and expm$data[1,c(1:5, 16:20, 31:35, 46:50, 61:65),1,1,1]
expm <- CST_MergeDims(expm, c('sample', 'ftime'), 'time') # dataset:1 sample:270  lat:22  lon:53 
  ## understand order: exp$data[1,1:5,1:5,1,1,1] and expm$data[1,c(1:5, 16:20, 31:35, 46:50, 61:65),1,1,1]
obsm <- CST_MergeDims(obs, c('member', 'sdate'), 'sample')
obsm <- CST_MergeDims(obsm, c('sample', 'ftime'), 'time') # dataset:1 sample:18  lat:22  lon:53 
exp.qm <- CST_QuantileMapping(expm, obsm, method = "QUANT",
                              wet.day = FALSE,
                              sample_dims = c('time'), ncores = 4)

exp <- array(rnorm(2*3*5*6*7*4), c(lon = 2, lat = 3, member = 5, sdate = 6,
             ftime = 7, wt = 4))
obs <- array(rnorm(2*3*1*6*7*4), c(lon = 2, lat = 3, member = 1, sdate = 6,
             ftime = 7, wt = 4))
res <- QuantileMapping(exp, obs, sample_dims = 'ftime')
res <- QuantileMapping(exp, obs, sample_dims = c('sdate','ftime'))
res <- QuantileMapping(exp, obs, sample_dims = c('sdate','ftime', 'member'))
