## TODO: Document header

compute_quants <- function(data, thresholds,
                           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) {
    .get_quantiles <- function(x, t) {
      quantile(as.vector(x), t, na.rm = TRUE)
    }
  } else {
    .get_quantiles <- function(x, t) {
      if (any(is.na(x))) {
	# If the array contains any NA values, return NA vector
	rep(NA, length(t))
      } else {
        # If there are no NAs, call quantile()
	quantile(as.vector(x), t, na.rm = FALSE)
      }
    }
  }
                            
  quantiles <- Apply(data,
                     target_dims = quantile_dims,
		     function(x, t) {.get_quantiles(as.vector(x), thresholds)},
                     output_dims = "bin",
                     ncores = ncores, 
                     split_factor = split_factor)[[1]]
  
  return(quantiles)
}
