library(startR)

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),
                    time = indices(1),
                    sdate = c('19921101'),
                    latitude = "all",
                    longitude = "all",
                    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 = "all",
                        longitude = "all",
                        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){
  source("/esarchive/scratch/bsolaraj/HMD_study/eur/source_files_CC/hmd_atomic_fun.R")
  source("/esarchive/scratch/bsolaraj/HMD_study/eur/source_files_CC/hmd_CC_mod.R")
  ldtmin_ref <- (6936 - (365 * (t - 1)))   # 1980 location
  ldtmax_ref <- (18615 - (365 * (t - 1)))

  hmd_final <- hmd_fun(x = x, ldtmin_ref, ldtmax_ref, har_mon = mask)
  return(hmd_final)
  }


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 = 1)

res <- Compute(wf, chunks = list(latitude = 4, longitude = 6))

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 = 10,
                             longitude = 10),
               threads_load = 1,
               threads_compute = 8,

               cluster = list(queue_host = queue_host,
                              queue_type = 'lsf',
                              cores_per_job = 8,
                              job_wallclock = '12:00',
                              temp_dir = temp_dir,
                              polling_period = 10,
                              extra_queue_params = list('#BSUB -M 3000'),
                              max_jobs = 100,
                              bidirectional = FALSE),
               ecflow_suite_dir = ecflow_suite_dir,
               wait = TRUE)



res <- Compute(wf,
               chunks = list(latitude = 8,
                             longitude = 8),
               threads_load = 32,
               threads_compute = 32,
               cluster = list(queue_host = 'cte-power',
                              queue_type = 'slurm',
                              temp_dir = '/gpfs/scratch/bsc32/bsc32339/startR_hpc/',
                              job_wallclock = '23:00:00',
                              cores_per_job = 32,
                              max_jobs = 64,
                              extra_queue_params = list('#SBATCH --mem 64000'),
                              bidirectional = FALSE,
                              polling_period = 10
               ),
               ecflow_suite_dir = '/home/Earth/nperez/startR_local/',
               wait=TRUE)
