library(startR)
t <- 4
model_version <- "INIT"   # INIT or NoINIT -> Initialized or uninitialized
path_exp <- paste0('/esarchive/exp/ncar/cesm-dple/daily/',
                       '$var$/$var$_$sdate$.nc')


data_exp <- Start(dat = list(list(path=path_exp)),
                  var = 'tasmax',
                  member = indices(1:40),
                  time = indices(1096:1521),
                  sdate = c('19601101', '19611101'),
                  latitude = values(list(42, 65)),
                  latitude_reorder = Sort(),
                  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 = F)

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(lats.min, lats.max)),
               latitude_reorder = Sort(),
               longitude = values(list(lons.min, lons.max)),
               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, mask, 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'),
                                mask = c('dat')),
             output_dims = c('member','sdate', 'var'))

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

#-----------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 -M 7000'),
                              max_jobs = 12,
                              bidirectional = FALSE),
               ecflow_suite_dir = ecflow_suite_dir,
               wait = TRUE)


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)
