rm(list=ls()); gc()

library(CSTools)
load("/esarchive/scratch/nperez/CSTools_manuscript/RF100/Dataset_Section3-1-ExpQM.RDS")
load("/esarchive/scratch/nperez/CSTools_manuscript/RF100/weightsRF100.RDS")
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)

## Review weights order:
#library(ragg); library(s2dv)
#for (i in 1:12) {
#  agg_png(paste0("/esarchive/scratch/nperez/CSTools_manuscript/RF100/weights/weights_month_", i, ".png"),
#           width = 500, height = 500, units = 'px',res = 144)
#  PlotEquiMap(weight$data[,,i], 
#              lon = weight$lon, lat = weight$lat, filled.continents = FALSE,
#              intylat = 2, intxlon = 2, toptitle = paste("Weights month", i),
#               sizetit = 0.5) 
#  dev.off()
#}

weights <- ClimProjDiags::Subset(weight$data, along = 'monthly', indices = c(11,12,1:1:6))
dim(slope) <- c(dataset = 1, monthly = 12)
slope <- ClimProjDiags::Subset(slope, along = 'monthly', 
                               indices = c(11,12,1:1:6), drop = 'non-selected')
# for (memb in 1:10) {
  fs <- CST_RainFARM(exp.qm, nf = 100,
                     weights = weights, slope = slope,
                     kmin = 1, nens = 1, verbose = TRUE,
                     time_dim = c("member", "sdate", "ftime"), nprocs = 64,
                     drop_realization = TRUE)
  fs <- CST_MergeDims(fs, merge_dims = c("ftime", "monthly"),
                         na.rm = TRUE)

CST_SaveExp(fs, destination = '/gpfs/scratch/bsc32/bsc32339/CSTools_manuscript/')
#}

