library(startR)
library(s2dv)

hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = '01',
               time = 'all',
               lat = indices(1),
               lon = indices(1), num_procs = 2,
               retrieve = TRUE)

plot(1979:2018, MeanDims(hrmin, 'time'), type = 'b')

hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
       lon = 'all',
       num_procs = 2,
               debug = TRUE,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/", list.files("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/")) == sort(as.vector(attributes(hrmin)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  print("Calculating")
  s2dv::MeanDims(datos, c('time', 'year'))
}
step <- Step(fun = temp_mean, 
             target_dims = c('time', 'year'),
             output_dims = 'time')
  
wf <- AddStep(hrmin, step)

queue_host = 'nord4'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory
  
res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 2,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'slurm',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             polling_period = 10,
                             job_wallclock = '08:00:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              debug = TRUE,
              ecflow_suite_dir = ecflow_suite_dir,
              wait = FALSE)

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