library(startR)
library(s2dverification)
path <- '/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$member$/Amon/$var$/gr/v20190713/$var$_Amon_*_s$sdate$-$member$_gr_$leadyear$.nc'
path <- '/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$member$/Amon/$var$/gr/v20190713/$var$_Amon_*_s$sdate$-$member$_gr_$leadyear$.nc'

sdate <- 2000:2004
path <- paste0('/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$member$/Amon/$var$/gr/v20190713/$var$_Amon_*_s', sdate, '-$member$_gr_$leadyear$.nc')
library(multiApply)

data = Start(dataset = path,
             var = 'tas',
             sdate = paste0(2000:2004),
             month = 'all',
             lat = values(list(0,14)),
             lon = values(list(0,28)),
             leadyear = 'all',
             member = 1:4,
             leadyear_depends = 'sdate',
             #month_across = 'leadyear',
             synonims = list(month = c('month','time'), lon = c('lon', 'longitude'), lat = c('lat', 'latitude')),
             transform = CDORemapper,
             transform_extra_cells = 2,
             transform_params = list(grid = 'r200x100',
                                     method = 'con', crop = c(0,28,0,14)),
             transform_vars = c('lat', 'lon'),
             return_vars = list(lat = 'dataset', lon = 'dataset'),
             lat_reorder = Sort(),
             lon_reorder = CircularSort(-180, 180),
             num_procs = 1, retrieve = FALSE)

fun = function(exp){
  return(exp)
}

step <- Step(fun = fun,
             use_libraries = c('s2dverification'),
             target_dims = c('lat','lon'),
             output_dims = c('lat','lon'))

wf <- AddStep(inputs = data,
              step = step)

res <- Compute(workflow = wf,
               chunks = list(member = 4, month = 6),
               threads_load = 2,
               threads_compute = 4)

