#!/usr/bin/env Rscript
rm(list=ls()); gc();
library(CSTools)
Rprof()
load('/esarchive/scratch/nperez/CSTools_manuscript/Dataset_weight.RDS')
weights <- ClimProjDiags::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 <- ClimProjDiags::Subset(slope, along = 'monthly',
                               indices = c(11,12,1:1:6), drop = 'non-selected')
load('/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Dataset_ExpQM.RDS')
print("START DOWN")
data <- exp.qm
data$data <- exp.qm$data[1,,,,,,2]
ini <- Sys.time()
down <- CST_RainFARM(data, #lat = exp.qm$lat, lon = exp.qm$lon,
                 nf = 100, weight = weights[,,2], slope = slope[2],
                 nprocs = 4, kmin = 1, nens = 1,
                 time_dim = c("sdate", "member", "ftime"),
                 drop = TRUE)
fin <- Sys.time()
print("FINISHED")
print(fin-ini)
print(str(down))
Rprof(NULL)
profile.info <- summaryRprof()

dates <- SplitDim(exp.qm$Dates$start, indices = exp.qm$Dates$start)
dates <- SplitDim(dates, indices = dates[1,], split_dim = 'monthly')
dates <- as.POSIXct(dates, origin = '1970-01-01', format = "%Y-%m-%d", tx = 'UTC') 
print(dim(dates))
print(head(dates))
#CST_SaveExp(data, 
#library(ragg)
#library(s2dv)
#save(down, file = '/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Down.RDS', version = 2)
#agg_png("/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Down_11Dec1993_r1.png",
#        width = 500, height = 600, units = 'px',res = 144)
#PlotEquiMap(down$data[1,11,,],lon = down$lon, lat = down$lat,
#            filled.continents = FALSE, bar_limits = c(0,40),
#           intylat = 2, intxlon = 2, title_scale = 0.7,
#           toptitle = 'Downscaled', units = 'precipitation (mm)')
#dev.off()
#print(pryr::mem_used())
#ini <- Sys.time()
#down <- RainFARM(data, lat = exp.qm$lat, lon = exp.qm$lon,
#                 nf = 100, weight = weights[,,1], slope = slope[2],
#                 nprocs = 4, kmin = 1, nens = 1, time_dim = c("member", "ftime"),
#                 drop = TRUE)
#fin <- Sys.time()
#print("FINISHED")
#print(fin-ini)
#print(str(down))
#library(ragg)
#library(s2dv)
#save(down, file = '/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Down.RDS', version = 2)
#agg_png("/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Down_11Dec1993_r2.png",
#        width = 500, height = 600, units = 'px',res = 144)
#PlotEquiMap(down$data[1,11,,],lon = down$lon, lat = down$lat,
#            filled.continents = FALSE, bar_limits = c(0,40),
#           intylat = 2, intxlon = 2, title_scale = 0.7,
#           toptitle = 'Downscaled', units = 'precipitation (mm)')
#dev.off()
#print(pryr::mem_used())
#-----------------
#lat = exp.qm$lat
#lon = exp.qm$lon
#nf = 100
#weights = weights[,,1]
#slope = slope[2]
#nprocs = 4; kmin = 1; nens = 10; time_dim = c("member", "ftime")
#fglob = FALSE; fsmooth = TRUE
#lon_dim = "lon"; lat_dim = "lat"
#drop_realization_dim = TRUE; verbose = FALSE

