library(startR)

  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/$var$_$sdate$.nc"
  var <- 'tas'
  #sdate <- c('20170101', '20170201')
  data <- Start(dat = repos,
                var = var,
                sdate = indices(1:8),
                ensemble = 'all',
                time = 'all',
                latitude = 'all', #indices(1:100),
                longitude = 'all', #indices(1:100),
                return_vars = list(time = c('sdate'), longitude = NULL, latitude = NULL),
                retrieve = FALSE
               )
  func <- function(x) {
#     apply(x + 1, 2, mean)
    a <- apply(x + 1, 2, mean)
    b <- mean(a)
    dim(b) <- c(time = length(b))   #give it a name!!!!
    return(b)
  }
  step <- Step(func, target_dims = c('ensemble', 'time'),
                    output_dims = c('time'))
  wf <- AddStep(data, step)

#-------------------user-defined---------------------
  queue_host <- 'p1'
  temp_dir <- '/gpfs/scratch/bsc32/bsc32734/startR_hpc/'
  ecflow_suite_dir <- '/home/Earth/aho/startR_local/'
#----------------------------------------------------

  res <- Compute(wf,
                 #chunks = list(latitude = 4,
                 #              longitude = 4),#$output1
                 chunks = list(sdate = 8),
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(queue_host = queue_host,
                                queue_type = 'slurm',
                                temp_dir = temp_dir,
                                #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',
                                extra_queue_params = list('#SBATCH --mem-per-cpu=30000'),
                                job_wallclock = '01:00:00',
                                cores_per_job = 4,
                                max_jobs = 8,
                                bidirectional = FALSE,
                                polling_period = 50
                               ),
                 ecflow_suite_dir = ecflow_suite_dir,
                 #ecflow_suite_dir = '/esarchive/scratch/aho/ecFlow/',
                 wait = TRUE
                 )  #$output1

#if wait = FALSE
  saveRDS(res, file = 'test_list_p9.Rds')  #not data, a list
  res <- readRDS('test_data_p9.Rds')
  result <- Collect(res, wait = TRUE)  #it will update ecflow_ui status
  saveRDS(result, file = 'test_data_p9.Rds')  #real data

