
model_version <- "INIT"   # INIT or NoINIT -> Initialized or uninitialized
path_exp <- '/esarchive/exp/ncar/cesm-dple/daily/$var$/$var$_$sdate$.nc'
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
path_exp <- '/esarchive/exp/ncar/lens/daily/$var$/$var$_$sdate$.nc'

data_exp <- Start(dat = path_exp,
                  var = 'prlr',
                  member = indices(1:25),
                  time = indices(1:7),
                  sdate = c('19931101', '19941101'),
                  latitude = values(list(42, 65)),
                  latitude_reorder = Sort(decreasing = TRUE),
                  longitude = values(list(0, 18)),
                  longitude_reorder = CircularSort(0, 360),
                  synonims = list(latitude=c('lat','latitude'),
                                  longitude=c('lon','longitude'),
                                  member=c('ensemble','realization')),
                  return_vars = list(latitude = 'dat',
                                     longitude = 'dat',
                                     time = 'dat'),
retrieve = T, num_procs = 2)
                  retrieve = T,debug = F,
 num_procs = 2)

path_mask <- "/esarchive/scratch/bsolaraj/gitlab/multi-annual-heatwave/HMD_study/Scripts/mask_harvest_month/mask_harvestmonth.nc"
mask <-  Start(dat = path_mask,
               var = 'mask',
               latitude = values(list(42, 65)),
               latitude_reorder = Sort(),
               longitude = values(list(0, 18)),
               longitude_reorder = CircularSort(0, 360),
               return_vars = list(latitude = 'dat',
                                  longitude = 'dat'),
               synonims = list(latitude=c('lat','latitude'),
                               longitude=c('lon','longitude')),
               retrieve = F)

atomic_x <- function(x, t) {
  object <- s2dv::MeanDims(x, dims = c('dat', 'time'))
  return(object)
}
step <- Step(fun = atomic_x,
             target_dims = list(x = c('dat','var','member','time','sdate')),
             output_dims = c('sdate', 'var', 'member'))

wf <- AddStep(list(data_exp), step, t = 1)

#-----------modify according to your personal info---------
queue_host = 'nord3'   #your own host name for nord3
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(latitude = 4,
                             longitude = 3),
               threads_load = 2,
               threads_compute = 12,
               cluster = list(queue_host = queue_host,
                              queue_type = 'lsf',
                              cores_per_job = 12,
                              job_wallclock = '02:00',
                              temp_dir = temp_dir,
                              #r_module = 'R/3.6.2-foss-2019b',
                              polling_period = 10,
                              #extra_queue_params = list('#BSUB -q debug'),
                              #extra_queue_params = list('#BSUB -M 7000'),
                              max_jobs = 12,
                              #lib_dir = "/home/bsc32/bsc32339/R/x-86_64-pc-linux-gnu-library/",
                              bidirectional = FALSE),
               ecflow_suite_dir = ecflow_suite_dir,
               wait = TRUE)

source('https://earth.bsc.es/gitlab/es/startR/raw/master/inst/PlotProfiling.R')
jpeg("/esarchive/scratch/nperez/git/Flor/Nord3/Config2.jpg")
PlotProfiling(attr(res, 'startR_compute_profiling'))
dev.off()

queue_host = 'cte-power'
res <- Compute(wf,
               chunks = list(latitude = 4,
                             longitude = 3),
               threads_load = 2,
               threads_compute = 12,
               cluster = list(queue_host = queue_host,
                              queue_type = 'slurm',
                              cores_per_job = 12,
                              job_wallclock = '02:00:00',
                              temp_dir = temp_dir,
                              #r_module = 'R/3.6.2-foss-2019b',
                              polling_period = 10,
                              #extra_queue_params = list('#BSUB -M 7000'),
                              max_jobs = 12,
                              bidirectional = FALSE),
               ecflow_suite_dir = ecflow_suite_dir,
               wait = TRUE)
