## TODO: Document header
compute_probs <- function(data, quantiles,
                          ncores=1, quantile_dims=c('syear', 'ensemble'),
                          probs_dims=list('ensemble', 'bin'),
                          split_factor=1, na.rm=FALSE) {

  # Define na.rm behavior
  if (na.rm) {
    .c2p <- function(x, t) {
      if (any(!is.na(x))) {
	# If the array contains any non-NA values, call convert2prob
        colMeans(convert2prob(as.vector(x), threshold = as.vector(t)))
      } else {
	# If the array only contains NAs, return NA vector
	rep(NA, dim(t)[['bin']] + 1) # vector with as many NAs as prob bins.
      }
    }
  } else {
    .c2p <- function(x, t) {
      if (any(is.na(x))) {
	# If the array contains any NA values, return NA vector
	rep(NA, dim(t)[['bin']] + 1)
      } else {
	# If there are no NAs, call convert2prob
        colMeans(convert2prob(as.vector(x), threshold = as.vector(t)))
      }
    }
  }
  
  probs <- Apply(data = list(x = data, t = quantiles),
                 target_dims = probs_dims,
                 .c2p,
                 output_dims = "bin",
                 split_factor = split_factor,
                 ncores = ncores)[[1]]

  return(probs)
}     
