#Clear workspace
rm(list = ls())

.libPaths(new = '/gpfs/projects/bsc32/share/R_libs/3.5') 
library(maps)
library(boot)
library(RColorBrewer)
library(easyVerification)
library(SpecsVerification)
library(s2dverification)
library(abind)
library(multiApply)
library(ncdf4)
library(startR)
library(plyr)
source('/esarchive/scratch/Earth/rfernand/dcpp/R_WeightedMean.R')

#-----------------

vari = 'tos'
mod = 'Omon'
grid = 'gr'

#Load CMIP6 DCPP
repos <- paste0('/esarchive/exp/ecearth/a1ua/original_files/cmorfiles_corrupt/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$memb$/',mod,'/$var$/gr/v20190713/$var$_',mod,'_EC-Earth3_dcppA-hindcast_s$sdate$-$memb$_',grid,'_$chunk$.nc',sep='')
data_cmip6_1 <- Start(dat = repos,
              var = vari,
              memb = c('r1i1p1f1'),
              sdate = paste(seq(1960,2015)),
              time = 'all', #c(1:62),
              chunk = 'all',
              lat = 'all',
              lon = 'all',
              chunk_depends = 'sdate',
              time_across = 'chunk',
              merge_across_dims = TRUE,
              return_vars = list(lat = 'dat', lon = 'dat'),
              retrieve = F)

#lat_cmip6 = attr(data_cmip6_1, 'Variables')$common$lat
#lon_cmip6 = attr(data_cmip6_1, 'Variables')$common$lon

#source('Season.R')
fun_an <- function(x) {
    y <- s2dverification::Season(x,  posdim = 1, monini = 11, moninf = 1, monsup = 12)
    return(y)
}

step1 <- Step(fun = fun_an,
             target_dims = c('time'),
             output_dims = c('time'))#,

wf <- AddStep(data_cmip6_1, step1)

print('Start Compute')
#power9
res <- Compute(wf,
               chunks = list(sdate = 4),
               threads_load = 2,
               threads_compute = 4,
               cluster = list(queue_host = 'p1',
                              queue_type = 'slurm',
#                              temp_dir = '/gpfs/scratch/bsc32/bsc32135/startR_hpc/',
                              temp_dir = '/gpfs/scratch/bsc32/bsc32734/startR_hpc/',
                              lib_dir = '/gpfs/projects/bsc32/share/R_libs/3.5/',
                              #r_module = 'R/3.5.0-foss-2018b',
                              r_module = 'startR/0.1.2-foss-2018b-R-3.5.0',
                              cores_per_job = 4,
                              job_wallclock = '01:00:00',
                              max_jobs = 4,
                              extra_queue_params = list('#SBATCH --mem-per-cpu=30000'),
                              bidirectional = FALSE,
                              polling_period = 50
                             ),
#               ecflow_suite_dir = '/home/Earth/rfernand/startR_local/',
                 ecflow_suite_dir = '/home/Earth/aho/startR_local/',
               wait = TRUE
              )

#fatnodes
  res <- Compute(wf,
                 chunks = list(lat = 2,
                               lon = 2),#$output1
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(queue_host = 'fat',
                                queue_type = 'slurm',
                                cores_per_job = 2,
                                job_wallclock = '01:00:00',
                                max_jobs = 4,
                                bidirectional = TRUE,
                                polling_period = 10
                 ),
                 ecflow_suite_dir = '/home/Earth/aho/startR_local/'
#                 wait = TRUE
         )

#local
  res <- Compute(wf,
                 chunks = list(lat = 2,
                               lon = 2))


saveRDS(res, file = 'test_collect_rfernand.Rds')
