percentile_threshold <- function(data, percentile, operator, var.list, return.percent = TRUE) {
  
  library(CSIndicators)
  
  time_dim <- 'time'
  memb_dim <- 'ensemble'
  sdate_dim <- 'syear'
  
  data_temp <- data
  
  # browser()
  
  if (!is.null(data$obs)) {
    result_obs <- c()
    dim_var <- which(names(dim(data$obs$data)) == "var")
    for (var in var.list) {
      data_temp$obs$data <- Subset(data$obs$data, along = "var", indices = which(var.list == var), drop = FALSE)

      ## Calculate percentiles
      data_threshold <- CST_Threshold(data = data_temp$obs, threshold = percentile,
                                     time_dim = time_dim, memb_dim = memb_dim,
                                     sdate_dim = sdate_dim, na.rm = TRUE)

      ## Calculate number of days exceeding threshold
      time_exceeded <- CST_TotalTimeExceedingThreshold(data = data$obs, 
                                                       threshold = data_threshold$data,
                                                       op = operator)

      if (return.percent) {
        ## Calculate percentage of days exceeding threshold - NEED TO DO!!
        result_obs <- abind(result_obs, time_exceeded$data, along = dim_var)
      } else {
        ## return number of days
        result_obs <- abind(result_obs, time_exceeded$data, along = dim_var) 
      }
      names(dim(result_obs)) <- names(time_exceeded$dims)
      
    }
    data$obs$data <- result_obs
  }

  if (!is.null(data$hcst)) {
    result_hcst <- c()
    dim_var <- which(names(dim(data$hcst$data)) == "var")
    for (var in var.list) {
      data_temp$hcst$data <- Subset(data$hcst$data, along = "var", indices = which(var.list == var), drop = FALSE)
      
      ## Calculate percentiles
      data_threshold <- CST_Threshold(data = data_temp$hcst, threshold = percentile,
                                      time_dim = time_dim, memb_dim = memb_dim,
                                      sdate_dim = sdate_dim, na.rm = TRUE)
      
      ## Calculate number of days exceeding threshold
      time_exceeded <- CST_TotalTimeExceedingThreshold(data = data$hcst, 
                                                       threshold = data_threshold$data,
                                                       op = operator)
      
      if (return.percent) {
        ## Calculate percentage of days exceeding threshold - NEED TO DO
        result_hcst <- abind(result_hcst, time_exceeded$data, along = dim_var)
      } else {
        ## return number of days
        result_hcst <- abind(result_hcst, time_exceeded$data, along = dim_var) 
      }
      names(dim(result_hcst)) <- names(time_exceeded$dims)
      
    }
    data$hcst$data <- result_hcst
  }

  if (!is.null(data$fcst)) {
    result_fcst <- c()
    dim_var <- which(names(dim(data$fcst$data)) == "var")
    for (var in var.list) {
      data_temp$fcst$data <- Subset(data$fcst$data, along = "var", indices = which(var.list == var), drop = FALSE)
      
      ## Calculate percentiles
      data_threshold <- CST_Threshold(data = data_temp$fcst, threshold = percentile,
                                      time_dim = time_dim, memb_dim = memb_dim,
                                      sdate_dim = sdate_dim, na.rm = TRUE)
      
      ## Calculate number of days exceeding threshold
      time_exceeded <- CST_TotalTimeExceedingThreshold(data = data$fcst, 
                                                       threshold = data_threshold$data,
                                                       op = operator)
      
      if (return.percent) {
        ## Calculate percentage of days exceeding threshold - NEED TO DO
        result_fcst <- abind(result_fcst, time_exceeded$data, along = dim_var)
      } else {
        ## return number of days
        result_fcst <- abind(result_fcst, time_exceeded$data, along = dim_var) 
      }
      names(dim(result_fcst)) <- names(time_exceeded$dims)
    }
    data$fcst$data <- result_fcst
  }

  return(data)
}
