library(startR)

StartDates <- paste0(1993:2018, '1101')
StartDates <- paste0(1993:1996, '1101')
exp_path <- list(name = 'ecmwfS5-QM', 
  path = "/esarchive/scratch/nperez/CSTools_manuscript/v30032021/Corrected/ecmwfS5/$var$/$var$_$sdate$.nc")
exp <- Start(dat = exp_path,
             var = 'prlr',
             member = 'first', #'all',
             sdate = StartDates,
             time = 'all',
             lat = startR::values(list(42, 49)),
             lon = startR::values(list(11, 4)),
             lat_reorder = Sort(decreasing = TRUE),
             #lon_reorder = CircularSort(180, -180),
             split_multiselected_dims = TRUE,
             synonims = list(member = c('member', 'ensemble'),
                             lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(time = 'sdate', lat = NULL, lon = NULL),
             retrieve = FALSE)
as.numeric(attributes(exp)$Variable$common$lon)
as.numeric(attributes(exp)$Variable$common$lat)
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/weights.RDS")
weights <- ClimProjDiags::Subset(weight$data, along = 'monthly',
                                 indices = c(11,12,1:1:6))

Chunk_RF <- function(x, nf, destination, slope, weights, lon, lat, startdates, nchunks = chunk_indices) {
print(dim(x))
    timesteps <- attributes(x)$Variables$common$time
    x <- CSTools::SplitDim(x, split_dim = c('time'), indices = timesteps)
    x <- x[,,,1]
    slope <- slope[1]
    weights <- weights[,,1]
print(dim(x))
print(dim(weights))
print(dim(slope))
print(nf)
print(lat)
print(lon)
source("/esarchive/scratch/nperez/git/cstools/R/CST_RainFARM.R")
source("/esarchive/scratch/nperez/git/cstools/R/zzz.R")
    down_data <- RainFARM(x, lon = lon, lat = lat, time_dim = 'time', 
                          nf = nf, #weights = weights, slope = slope,
                          nens = 1, lon_dim = 'lon', lat_dim = 'lat', nprocs = 1)

print(dim(down_data$data))

    # detect the dates of forecast time for different start dates
    time <- attributes(x)$Variables$common$time 
print(dim(time))
print(head(time))
    dates <- lapply(startdates, function(x) {seq(as.Date(x, format = "%Y-%m-%d"), 
                                                         x + length(time) - 1, 'day')})
print("HERE")
    dimname <- names(dim(down_data$data)) 
    var_dims <- list(ncdim_def(name = 'lon', units = 'degrees',
                               vals = as.vector(down_data$lon), longname = 'longitude'),
                     ncdim_def(name = 'lat', units = 'degrees',
                               vals = as.vector(down_data$lat), longname = 'longitude'),
                     ncdim_def(name = 'ensemble', units = "adim",
                               vals = 1 : dim(down_data$data)[which(dimname == 'member')],
                               longname = 'ensemble', create_dimvar = TRUE))
# startdates and dates depends on the chunk:
    CSTools:::.saveExp(down_data$data, dims_var = var_dims, 
          var_name = 'tas', units = 'K', sdate =  startdates[nchunks['sdate']],
          Dates = dates[[nchunks['sdate']]], 
          cdo_grid_name = paste0('r', length(lon), 'x', length(lat)),
          projection = 'none', destination = destination)
    down_data_mean <- MeanDims(down_data$data, c('member', 'longitude', 'latitude'))
    return(down_data_mean)
}

step <- Step(Chunk_RF,
             target_dims = c('lon', 'lat', 'time'),
             output_dims = 'time',
             use_libraries = c('CSTools', 'ncdf4'),
             use_attributes = list(exp = "Variables"))

workflow <- AddStep(exp, step, nf = 4,
                    destination = "/esarchive/scratch/nperez/git/Flor/cstools/test_RF_start/",
                    slope = slope, weights = weights, 
                    lon = as.numeric(attributes(exp)$Variable$common$lon),
                    lat = as.numeric(attributes(exp)$Variables$common$lat),
                    startdates = as.Date(StartDates, format = "%Y%m%d"))

res <- Compute(workflow,
               chunks = list(member = 1, sdate = 4),
               threads_load = 2,
               threads_compute = 1)



