path <- '/esarchive/exp/ecearth/a1tr/cmorfiles/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r24i1p1f1/Amon/$var$/gr/v20190312/$var$_Amon_EC-Earth3_historical_r24i1p1f1_gr_$period$.nc'
var <- 'tas'

library(startR)

data <- Start(dat = path,
              period = 'all',
              var = var,
              time = 'all',
              lat = 'all',
              lon = 'all',
              return_vars = list(lon = NULL, lat = NULL),
              synonims = list(lat = c('lat','latitude'),
                              lon = c('lon','longitude')),
              retrieve = FALSE)

fun_cal <- function(x) {
  a <- apply(x, c(which(names(dim(x)) != 'period')), mean)
  dim(a) <- c(time = length(a))
  return(a)
}

step <- Step(fun_cal,
             target_dims = c('period', 'time'),
             output_dims = c('time'))

wf <- AddStep(data, step)

#ws
#res <- Compute(wf,
#               chunk = list(lat = 2, lon = 2))$output1

#P9
.libPaths(new = '/gpfs/projects/bsc32/share/R_libs/3.5')

library(startR)
res <- Compute(wf,
               chunks = list(lat = 2,
                             lon = 2),
               threads_load = 2,
               threads_compute = 4,
               cluster = list(queue_host = 'p1',
                              queue_type = 'slurm',
                              temp_dir = '/gpfs/scratch/bsc32/bsc32734/startR_hpc/',
                              lib_dir = '/gpfs/projects/bsc32/share/R_libs/3.5/',
                              #r_module = 'R/3.5.0-foss-2018b',
                              r_module = 'startR/0.1.2-foss-2018b-R-3.5.0',
                              job_wallclock = '00:10:00',
                              cores_per_job = 4,
                              max_jobs = 4,
                              bidirectional = FALSE,
                              polling_period = 50
               ),
               ecflow_suite_dir = '/home/Earth/aho/startR_local/',
               wait = TRUE
)$output1

  saveRDS(res, 'Compute_test_data_p9v012_with_libdir.Rds')
#  data1 <- readRDS('data_fatnode_012_sub4.Rds')

#-------------plotting-----------------
library(easyNCDF)

path1 <- '/esarchive/exp/ecearth/a1tr/cmorfiles/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r24i1p1f1/Amon/tas/gr/v20190312/tas_Amon_EC-Earth3_historical_r24i1p1f1_gr_201401-201412.nc'

asd<-NcOpen(path1)
lons <- as.vector(NcToArray(asd, vars_to_read='lon'))
lats <- as.vector(NcToArray(asd, vars_to_read='lat'))

#> dim(res)
#time  date var  lat  lon 
#12    1    1  256  512 

res_sub <- Subset(res, c(1,2,3), list(1,1,1), drop = TRUE)

library(s2dverification)

PlotEquiMap(res_sub,lons, lats,
            axes_label_scale = 0.7,
            intylat = 5, intxlon = 5,
            filled.continents = FALSE
)

