library(startR)

# exp data
  repos_exp <- paste0('/esarchive/exp/ecmwf/system5_m1/monthly_mean/',
                      '$var$_f6h/$var$_$sdate$.nc')
  sdates_exp <- sapply(1981:2016, paste0, sprintf('%02d', 1:12), '01')
  lon.min <- -80
  lon.max <- 40
  lat.min <- 20
  lat.max <- 80

  exp <- Start(dat = repos_exp,
               var = 'tas',
               sdate = sdates_exp,
               time = indices(1),
               member = 'all',
               latitude = values(list(lat.min, lat.max)),
               latitude_reorder = Sort(),
               longitude = values(list(lon.min, lon.max)),
               longitude_reorder = CircularSort(0, 360),
               transform = CDORemapper,
               transform_extra_cells = 2,
               transform_params = list(grid = 'r360x181',
                                       method = 'conservative',
                                       crop = c(lon.min, lon.max, lat.min, lat.max)),
               transform_vars = c('latitude', 'longitude'),
               synonims = list(longitude = c('lon', 'longitude'),
                               latitude = c('lat', 'latitude'),
                               member = c('member', 'ensemble')),
               return_vars = list(latitude = NULL, longitude = NULL, time = 'sdate'),
               retrieve = FALSE)

# obs data
  repos_obs <- paste0('/esarchive/recon/ecmwf/erainterim/monthly_mean/',
                      '$var$_f6h/$var$_$sdate$.nc')
  sdates_obs <- sapply(1981:2016, paste0, sprintf('%02d', 1:12))

  obs <- Start(dat = repos_obs,
               var = 'tas',
               sdate = sdates_obs,
               time = indices(1),
               latitude = values(list(lat.min, lat.max)),
               latitude_reorder = Sort(),
               longitude = values(list(lon.min, lon.max)),
               longitude_reorder = CircularSort(0, 360),
               transform = CDORemapper,
               transform_extra_cells = 2,
               transform_params = list(grid = 'r360x181',
                                       method = 'conservative',
                                       crop = c(lon.min, lon.max, lat.min, lat.max)),
               transform_vars = c('latitude', 'longitude'),

               synonims = list(longitude = c('lon', 'longitude'),
                               latitude = c('lat', 'latitude')),
               return_vars = list(latitude = NULL, longitude = NULL, time = 'sdate'),
               retrieve = FALSE)

func <- function(exp, obs) {
  # exp: [dat, member, sdate, time]
  # obs: [dat, sdate, time]
  obs <- s2dv::InsertDim(obs, 2, 1, name = 'member')
  clim <- s2dv::Clim(exp, obs, dat_dim = c('dat', 'member'))  # [dat, member, time]
  ano_exp <- s2dv::Ano(exp, clim = clim$clim_exp)  # [dat, member, sdate, time]
  # ensemble mean
  ano_exp <- drop(apply(ano_exp, c(1, 3), mean))  # [sdate]
  ano_obs <- drop(s2dv::Ano(obs, clim$clim_obs))  # [sdate]
  smooth_ano_exp <- s2dv::Smoothing(ano_exp, runmeanlen = 12)
  smooth_ano_obs <- s2dv::Smoothing(ano_obs, runmeanlen = 12)
  return(list(exp = smooth_ano_exp, obs = smooth_ano_obs))
}

  step <- Step(func, target_dims = list(exp = c('dat', 'member', 'sdate', 'time'),
                                        obs = c('dat', 'sdate', 'time')),
               output_dims = list(exp = 'ftime', obs = 'ftime'),
               use_libraries = c('s2dv'))
  wf <- AddStep(list(exp, obs), step)

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

  res <- Compute(wf$exp,
                 chunks = list(latitude = 2,
                               longitude = 2),
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(queue_host = queue_host,
                                queue_type = 'lsf',
                                temp_dir = temp_dir,
                                cores_per_job = 2,
                                job_wallclock = '05:00',
                                max_jobs = 4,
                                extra_queue_params = list('#BSUB -q bsc_es'),
                                bidirectional = FALSE,
                                polling_period = 10
                 ),
                 ecflow_suite_dir = ecflow_suite_dir,
                 wait = FALSE
                 )  #$output1


# Save the header of res
saveRDS(res, file = '~/nord3_demo_header.Rds')

# You can leave now. When you come back next time, just read the .Rds file
res_collect <- readRDS('~/nord3_demo_header.Rds')
result <- Collect(res_collect, wait = TRUE)
# Save the result if needed
saveRDS(result, file = '~/nord3_demo_result.Rds')

# Check the result
str(result)
dim(res$trend)








