library(startR)
library(s2dverification)
library(tictoc)

# Test 1: using retrieve = TRUE
path_exp <- paste0('/esarchive/exp/ncar/cesm-dple/daily_mean/',
                   '$var$_f24h/$var$_$sdate$.nc')
variable<-'tasmax'
y1<-1961
y2<-2014
monini<-'11'
sdates_exp<-paste0(y1:y2,monini,'01')
nmemb<-2

lats.min=25
lats.max=30
lons.min=40
lons.max=44

tic()
data_exp <- Start(dat = list(list(path=path_exp)),
                  var = variable,
                  member = indices(1:nmemb),
                  time = indices(62:426),
                  # time= indices(1:2),
                  sdate = sdates_exp,
                  #time =as.POSIXct(dates,'18:00:00'),
                  latitude = values(list(lats.min, lats.max)),
                  longitude = values(list(lons.min, lons.max)),
                  longitude_reorder = CircularSort(0, 360),
                  # transform = CDORemapper,
                  # transform_params = list(grid ='r192x288',
                  #                         method = 'con',
                  #                         crop = c(lons.min,lons.max,lats.min,lats.max)),
                  # transform_vars = c('latitude', 'longitude'),
                  synonims = list(latitude=c('lat','latitude'),
                                  longitude=c('lon','longitude'),
                                  member=c('ensemble','realization')),
                  # transform_extra_cells = 2,
                  return_vars = list(latitude = 'dat',
                                     longitude = 'dat',
                                     time = 'dat'),
                  num_procs = 4,
                  retrieve = TRUE)
toc()

# Test function:
# a <- 1 : c(2 * 18 * 5 * 7)
# 
# dim(a) <- c(var = 7, member = 2, sdate = 5, ftime = 18)
# dim(hmd_fun(a))

hmd_fun <- function(x) {
  # Expected inputs:
  #   x: array with dimensions ('ensemble', 'time', 'sdate')
  source("/home/Earth/bsolaraj/Documents/R/DecadalScript/JRC/DailyData/hmd_accum_mod.R")
  
  tasmax <- drop(x)
  memb <- dim(tasmax)[1]
  ftime_a <- dim(tasmax)[2]
  sdate_a <- dim(tasmax)[3]
  dim_days <- ftime_a*sdate_a
  dim(tasmax) <- c(memb,dim_days)
  
  Tref <- tasmax[,7300:18979]
  Temp <- tasmax
  
  hmd_all <- hmd_x(1980, Tref, 1961, Temp)
  return(hmd_all)
}

step <- Step(hmd_fun, 
             target_dims = c('member', 'time','sdate'), 
             output_dims = c('dim1','dim2', 'dim3'))

wf <- AddStep(data_exp, step)


res <- Compute(wf,
               chunks = list(latitude = 5,
                             longitude = 4),
               threads_load = 1,
               threads_compute = 4,
               cluster = list(#queue_host = 'power9',
                 queue_host = 'bsceslogin01.bsc.es',
                 queue_type = 'slurm',
                 #temp_dir = '/gpfs/scratch/bsc32/bsc32016/startR_hpc/',
                 temp_dir = '/home/Earth/bsolaraj/startR_local/',
                 #lib_dir = '/gpfs/projects/bsc32/share/R_libs/3.5/',
                 r_module = 'R',
                 # job_wallclock = '00:10:00',
                 cores_per_job = 4,
                 max_jobs = 4,
                 bidirectional = TRUE,
                 polling_period = 10
               ),
               ecflow_suite_dir = '/home/Earth/bsolaraj/startR_local/')





# To use Compute() locally

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