#!/usr/bin/env Rscript
start.time <- Sys.time()
library(startR)
library(multiApply)
library(ncdf4)
library(abind)

path <- '/esarchive/exp/monarch/t0kc/regional/3hourly/'
vars <- list.files(path) 
date <- '20131221'
variable <- vars[1]

startR:::.warning("Data reading...")
time1 <- Sys.time()
  data <- NULL
  for (i in 0:11) {
    pathvar <- paste0(path, variable, "/", variable, "-0", sprintf('%02d', i),
                    "_", date, "03.nc")
    nc <- nc_open(pathvar) 
    dat <- ncvar_get(nc, variable,  count = c(-1,-1,-1,-1))
    data <- abind(data, dat, along = 5)
    nc_close(nc)
  }
  names(dim(data)) <- c('time', 'lev', 'rlat', 'rlon', 'memb') 
time2 <- Sys.time()
time.taken <- time2 - time1
startR:::.warning(time.taken)
startR:::.warning("Data readed")

stats <- function(x, probs = 0.75) {
#  # x has member dimension
#  # more than one prob is possible
#  perc <- quantile(as.vector(x), probs)
  av <- mean(x, na.rm = TRUE) # to be confirmed
#  sd <- sd(x, na.rm = TRUE)
#  max <- max(x, na.rm = TRUE)
#  median <- median(x, na.rm = TRUE)
#  res <- array(c(av, sd, median, max, perc), c(stats = (4 + length(probs))))
#  return(res)
  return(av)
}
result <- Apply(list(data), target_dims = 'memb', fun = stats, 
                ncores = 16)$output1
save(result, file = '/esarchive/scratch/nperez/git/Flor/Monarch/test.RData', 
     version = 2)
startR:::.warning("Computation Done")

end.time <- Sys.time()
time.taken <- end.time - start.time
startR:::.warning("DONE")
startR:::.warning(time.taken)
