#----------------------------------
# To run just once to create weights file NC
# STEP 3: to run with connection to internet
# WorldClim data to s2dv_cube
source("/esarchive/scratch/nperez/git/cstools/R/CST_RFWeights.R")
library(multiApply); library(rainfarmr)
library(CSTools); library(raster); library(s2dv)
worldclim <- getData("worldclim", var = "prec", res = 0.5, lon = 5, lat = 45)
wc_month <- lapply(1:12, FUN = function(x) {
                   res <- crop(worldclim[[x]],
                               extent(3.5, 11.5, 41.5, 49.5))
                   res <- as.array(res)
                   names(dim(res)) <- c('lat', 'lon', 'month')
                   return(res)
                   })
xy <- xyFromCell(crop(worldclim[[1]], extent(3.5, 11.5, 41.5, 49.5)),
                 1:length(crop(worldclim[[1]], extent(3.5, 11.5, 41.5, 49.5))))
lons <- unique(xy[,1])
lats <- unique(xy[,2])
wc_month <- unlist(wc_month)
dim(wc_month) <- c(lat = length(lats), lon = length(lons), monthly = 12)
wc_month <- Reorder(wc_month, c('lon', 'lat', 'monthly'))
wc_month <- s2dv_cube(data = wc_month, lon = lons, lat = lats,
                       Datasets = 'WorldClim')
weight <- CST_RFWeights(wc_month, lon = 4:11, lat = 49:42, nf = 100) 
#weight <- CST_RFWeights(wc_month, lon = exp$lon, lat = exp$lat, nf = 100) # if all step 2 is run

library(ncdf4)
file <- "/esarchive/scratch/nperez/CSTools_manuscript/weights_nf100_4-11E_49-42N.nc"
dim_lon <- ncdim_def(name = 'lon', units = 'degrees',
                     vals = as.vector(weight$lon), longname = 'longitude')
dim_lat <- ncdim_def(name = 'lat', units = 'degrees_north',
                     vals = as.vector(weight$lat), longname = 'latitude')
dim_time <- ncdim_def(name = 'month', units = 'months from 1 (Jan) to 12 (Dec)',
                      vals = 1:12, 
                      longname = 'monthly values')
dims_var <- list(dim_lon, dim_lat, dim_time)
weights  <- ncvar_def(name = 'weights',
                     units = 'adim',
                     dim = dims_var, missval = -99999)


file_nc <- nc_create(file, weights)
ncvar_put(file_nc, weights, weight$data)
nc_close(file_nc)
###-------------------------------------------------

library(startR)

path <- "/esarchive/scratch/nperez/CSTools_manuscript/$var$_nf100_4-11E_49-42N.nc"
weights <- Start(dat = path,
                 var = 'weights',
                 lat = 'all', lon = 'all',
                 month = c(11:12,1:6),
                 return_vars = list(lat = NULL, lon = NULL),
                 retrieve = T)
wlat <- attributes(weights)$Variable$common$lat
wlon <- attributes(weights)$Variable$common$lon
library(ragg)
agg_png("/esarchive/scratch/nperez/CSTools_manuscript/v20210224/Dec-Weights.png", width = 1000, height = 500, units = 'px',res = 144)

s2dv::PlotEquiMap(weights[1,1,,,2], lat = as.vector(wlat),
                  lon = as.vector(wlon),
                  filled.continents = F)
dev.off()

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))


#----------
# STEP 2:
StartDates <- paste0(1993:2018, '1101')
exp <- list(name = 'ecmwfS5',
            path = "/esarchive/exp/ecmwf/system5c3s/daily_mean/$var$_s0-24h/$var$_$sdate$.nc")
exp <- Start(dat = exp,
             var = 'prlr',
             ensemble = 'all',
             sdate = StartDates,
             lat = values(list(42, 49)),
             lat_reorder = Sort(decreasing = TRUE),
             lon = values(list(4, 11)),
             time = 'all',
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lat = NULL, lon = NULL, time = 'sdate'),
             retrieve = T)
lat <- attributes(exp)$Variable$common$lat
lon <- attributes(exp)$Variable$common$lon
dates <- attributes(exp)$Variable$common$time
exp <- exp$data * 24 * 3600 * 1000
exp$data[which(exp$data < 0)] <- 0

agg_png("/esarchive/scratch/nperez/CSTools_manuscript/v20210224/11Dec-S5_raw.png", 
width = 800, height = 500, units = 'px',res = 144)

s2dv::PlotEquiMap(exp[1,1,1,1,,,40], lat = as.vector(lat),
                  lon = as.vector(lon), bar_limits = c(0, 40),
                  filled.continents = F)
dev.off()


obs <- list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$date$.nc')
obs <- Start(dat = obs,
             var = 'prlr',
             date = unique(format(dates, '%Y%m')),
             time = values(dates),
             lat = values(list(42, 49)),
             lat_reorder = Sort(decreasing = TRUE),
             lon = values(list(4, 11)),
             time_across = 'date',
             merge_across_dims = TRUE,
             split_multiselected_dims = TRUE, 
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lat = NULL, lon = NULL, time = 'date'),
             retrieve = TRUE)
olon <- attributes(obs)$Variable$common$lon 
olat <- attributes(obs)$Variable$common$lat
obs <- obs * 24 * 3600 * 1000

agg_png("/esarchive/scratch/nperez/CSTools_manuscript/v20210224/11Dec-Era5_raw.png",
width = 800, height = 500, units = 'px',res = 144)

s2dv::PlotEquiMap(obs[1,1,1,40,,], lat = as.vector(olat),
                  lon = as.vector(olon), bar_limits = c(0, 40),
                  filled.continents = F)
dev.off()


print(range(obs$data, na.rm = TRUE))
print(range(exp$data, na.rm = TRUE))

exp <- CST_SplitDim(exp, split_dim = c('ftime'), insert_ftime = 1)
obs <- CST_SplitDim(obs, split_dim = c('ftime'), insert_ftime = 1)

exp.qm <- CST_QuantileMapping(exp, obs, method = "QUANT",
                                     wet.day = FALSE,
                                     sample_dims = c('member', 'sdate', 'ftime')

