library(startR)
  repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
  data <- Start(dat = repos,
                var = 'tas',
                sdate = c('20170101', '20180101'),
                ensemble = indices(1:20),
                time = 'all',
                latitude = 'all',
                longitude = indices(1:40),
                return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
                retrieve = FALSE)
  
  library(multiApply)
  fun_spring <- function(x) {
      source("/esarchive/scratch/nperez/Season_v2.R")
      y <- Season_v2(x,  monini = 1, moninf = 3, monsup = 5)
      return(y)
  }
  
  step1 <- Step(fun = fun_spring,
               target_dims = c('time'),
               output_dims = c('time'))
  
  wf1 <- AddStep(data, step1)
 
## on Nord 3
#-----------modify according to your personal info---------
  queue_host = 'nord3'   
  temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
  ecflow_suite_dir = '/home/Earth/nperez/startR_local/'

  res <- Compute(wf1,
                 chunks = list(ensemble = 20,
                               sdate = 2),
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(queue_host = queue_host,
                                queue_type = 'lsf',
                                extra_queue_params = list('#BSUB -q bsc_es'),
                                cores_per_job = 2,
                                temp_dir = temp_dir,
                                polling_period = 10,
                                job_wallclock = '01:00',
                                max_jobs = 40,
                                bidirectional = FALSE),
                 ecflow_suite_dir = ecflow_suite_dir,
                 wait = TRUE)
 
 
## locally
  res1 <- Compute(wf1,
              chunks = list(ensemble = 2,
                            sdate = 2))
  
  dim(res1$output1)
  str(res1$output1)
  summary(res1$output1)
# -----------------------------------------------------------------
#> dim(res1$output1)
#     time       dat       var     sdate  ensemble  latitude longitude 
#        1         1         1         2        20       640        40 
#> str(res1$output1)
# num [1, 1, 1, 1:2, 1:20, 1:640, 1:40] 260 257 257 260 257 ...
#> summary(res1$output1)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  220.5   273.3   282.7   279.9   295.6   306.8 
# ----------------------------------------------------------------

## on Power9
#-----------modify according to your personal info---------
  queue_host = 'p1'   #your own host name for power9
  temp_dir =  '/gpfs/scratch/bsc32/bsc32734/startR_hpc/'
  ecflow_suite_dir = '/home/Earth/aho/startR_local/'  #your own local directory
#------------------------------------------------------------
  res <- Compute(wf1,
                 chunks = list(ensemble = 20,
                               sdate = 2),
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(queue_host = queue_host,  
                                queue_type = 'slurm',
                                cores_per_job = 2,
                                temp_dir = temp_dir,
                                r_module = 'R/3.5.0-foss-2018b',
                                polling_period = 10,
                                job_wallclock = '01:00:00',
                                max_jobs = 40,
                                bidirectional = FALSE),
                 ecflow_suite_dir = ecflow_suite_dir,  
                 wait = TRUE)


# -------------------------- CARLOS CODE

library(startR)
path = '/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$member$/Omon/$var$/gr/v20190713/$var$_Omon_EC-Earth3_dcppA-hindcast_s$sdate$-$member$_gr_$fyear$.nc'

  ## Regions for AMV (Doblas-Reyes et al., 2013)

  regions = NULL
  regions$reg1 = c(0, 60, 0, 40)

 data <- Start(dataset = path,
                     var = 'tos',
                     sdate = paste0(1990:1992),
                     fmonth = c(1:5),
                     lat = values(list(0, 20)),
                     lon = values(list(0, 40)),
                     fyear = 'all',
                     member = 'all',
                     fyear_depends = 'sdate',
                     fmonth_across = 'fyear',
                     merge_across_dims = TRUE,
                     synonims = list(fmonth = c('fmonth','time'),
                                     lon = c('lon', 'longitude'),
                                     lat = c('lat', 'latitude')),
                     return_vars = list(lat = 'dataset', lon = 'dataset'),
                     num_procs = 4, retrieve = FALSE)

data_lats = as.vector(attributes(data)$Variables$dat1$lat)
data_lons = as.vector(attributes(data)$Variables$dat1$lon)


fun_amv_ipo <- function(data, data_lats, data_lons, index, regions) {
  
  data = s2dv::Season(data = data, time_dim = 'fmonth',
                      monini = 11, moninf = 1, monsup = 12,
                      method = mean, na.rm = TRUE)
  names(dim(data))[which(names(dim(data))=='fmonth')] = 'fyear'
  mean_1 = ClimProjDiags::WeightedMean(data = data, lon = data_lons,
               lat = data_lats, region = regions$reg1,
               londim = which(names(dim(data))=='lon'),
               latdim = which(names(dim(data))=='lat'))
return(mean_1)
}

step = Step(fun = fun_amv_ipo,
            #use_libraries = c('startR','s2dv','multiApply','ClimProjDiags'),
            target_dims = c('dataset','var','sdate','fmonth','lat','lon'),
            output_dims = c('dataset','var','sdate','fyear'))

wf = AddStep(inputs = data,
             data_lats = data_lats,
             data_lons = data_lons,
             index = 'AMV',
             regions = regions,
             step = step)

res = Compute(workflow = wf,
               chunks = list(member = 10),
               threads_load = 2,
               threads_compute = 4,
               cluster = list(queue_host = 'nord3',
                              queue_type = 'lsf',
                        temp_dir = '/gpfs/scratch/bsc32/bsc32339/startR_hpc/',
                              cores_per_job = 2,
                              job_wallclock = '01:00',
                              max_jobs = 10,
                              extra_queue_params = list('#BSUB -q bsc_es'),
                              bidirectional = FALSE,
                              polling_period = 10),
               ecflow_suite_dir = '/home/Earth/nperez/startR_local/',
               wait = TRUE)
res <- Compute(workflow = wf, chunks = list(member = 10))
range(res)

