#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 = 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

fun_an <- function(x) {
    y <- 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')
res <- Compute(wf,
               chunks = list(sdate = 4))

# Output:
> dim(res$output1)
 time   dat   var  memb sdate   lat   lon 
    5     1     1     1    56   256   512 
> str(res$output1)
 num [1:5, 1, 1, 1, 1:56, 1:256, 1:512] NA NA NA NA NA NA NA NA NA NA ...
> summary(res$output1)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NAs
     271      274      288      287      298      304 12363680 
> res$output1[1,1,1,1,1,,1]
  [1]       NA       NA       NA       NA       NA       NA       NA       NA
  [9]       NA       NA       NA       NA       NA       NA       NA       NA
 [17]       NA       NA       NA       NA       NA       NA       NA       NA
 [25]       NA       NA       NA       NA       NA 271.2958 271.3076 271.3376
 [33] 271.3700 271.4138 271.5148 271.5919 271.6633 271.7086 271.7568 271.7890

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



res <- Compute(wf,
               chunks = list(sdate = 4),
               threads_load = 2,
               threads_compute = 4,
               cluster = list(queue_host = 'cte-power',
                              queue_type = 'slurm',
                              lib_dir = '/gpfs/projects/bsc32/share/R_libs/3.5/',
                              r_module = 'startR/0.1.2-foss-2018b-R-3.5.0',
                              cores_per_job = 4,
                              job_wallclock = '00:10:00',
                              max_jobs = 4,
                              extra_queue_params = list('#SBATCH --mem-per-cpu=30000'),
                              bidirectional = FALSE),
               ecflow_suite_dir = '/esarchive/scratch/nperez/',
               wait = TRUE)
