absolute_threshold <- function(data, threshold, operator, spell,
                               var.list, return.percent = TRUE, ncores) {
  
  library(CSIndicators)
  
  if(is.null(spell)){
    spell <- 1
  }
  
  time_dim <- 'time'
  memb_dim <- 'ensemble'
  sdate_dim <- 'syear'
  
  data_temp <- data
  
  # browser()
  
  ## Identify the month for each forecast period
  if (return.percent){ ## works only if forecast months start at 1
    start_month <- as.POSIXct(data$obs$attrs$Dates[[1]], format = "%Y-%m-%d")
    num_forecast_months <- dim(data$obs$data)['ftime'][[1]]
    forecast_months <- ((month(start_month) - 1 + 0:(num_forecast_months - 1)) %% 12) + 1
    days_per_month <- sapply(forecast_months, days_in_month)
  }
  
  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)
    
      if(spell == 1){
        ## Calculate number of days exceeding threshold
        time_exceeded_obs <- CST_TotalTimeExceedingThreshold(
          data = data_temp$obs, threshold = threshold, 
          op = operator, time_dim = time_dim, ncores = ncores)
        
      } else if(spell > 1){
        ## calculate number of days part of a spell
        time_exceeded_obs <- CST_TotalSpellTimeExceedingThreshold(
          data = data_temp$obs, threshold = threshold,
          spell = spell, op = operator, time_dim = time_dim,
          ncores = ncores)
      }
      
      dims <- dim(time_exceeded_obs$data)
      dims_without_time <- dims[-which(names(dims) == "time")]
      time_exceeded_obs$data <- array(time_exceeded_obs$data, 
                                      dim = dims_without_time, 
                                      dimnames = dimnames(time_exceeded_obs$data)
                                      [-which(names(dims) == "time")])
      
      if (return.percent) {
        ## Calculate percentage of days exceeding threshold
        percentage_exceeded_per_month <- multiApply::Apply(
          data = time_exceeded_obs$data, target_dims = "ftime",  
          fun = function(exceedance, days) {(exceedance / days) * 100},
          days = days_per_month)$output1
        
        percentage_exceeded_per_month <- Reorder(percentage_exceeded_per_month, 
                                                 names(dim(time_exceeded_obs$data)))
        
        result_obs <- abind(result_obs, percentage_exceeded_per_month,
                            along = dim_var) 
      } else {
        ## return number of days
        result_obs <- abind(result_obs, time_exceeded_obs$data, along = dim_var) 
      }
      names(dim(result_obs)) <- names(dims_without_time)
    }
  }
  
  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)
      
      if(spell == 1){
        time_exceeded_hcst <- CST_TotalTimeExceedingThreshold(
          data = data_temp$hcst, threshold = threshold, 
          op = operator, time_dim = time_dim, ncores = ncores)
        
      } else if(spell > 1){
        time_exceeded_hcst <- CST_TotalSpellTimeExceedingThreshold(
          data = data_temp$hcst, threshold = threshold,
          spell = spell, op = operator, time_dim = time_dim,
          ncores = ncores)
      }
      
      dims <- dim(time_exceeded_hcst$data)
      dims_without_time <- dims[-which(names(dims) == "time")]
      time_exceeded_hcst$data <- array(time_exceeded_hcst$data, 
                                      dim = dims_without_time, 
                                      dimnames = dimnames(time_exceeded_hcst$data)
                                      [-which(names(dims) == "time")])

      if (return.percent) {
        ## Calculate percentage of days exceeding threshold
        percentage_exceeded_per_month <- multiApply::Apply(
          data = time_exceeded_hcst$data, target_dims = "ftime",  
          fun = function(exceedance, days) {(exceedance / days) * 100},
          days = days_per_month)$output1
        
        percentage_exceeded_per_month <- Reorder(percentage_exceeded_per_month, 
                                                 names(dim(time_exceeded_hcst$data)))
        
        result_hcst <- abind(result_hcst, percentage_exceeded_per_month,
                             along = dim_var) 
      } else {
        ## return number of days
        result_hcst <- abind(result_hcst, time_exceeded_hcst$data, along = dim_var) 
      }
      names(dim(result_hcst)) <- names(dims_without_time)
    }
  }
  
  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)
      
      if(spell == 1){
        time_exceeded_fcst <- CST_TotalTimeExceedingThreshold(
          data = data_temp$fcst, threshold = threshold, 
          op = operator, time_dim = time_dim, ncores = ncores)
        
      } else if(spell > 1){
        time_exceeded_fcst <- CST_TotalSpellTimeExceedingThreshold(
          data = data_temp$fcst, threshold = threshold,
          spell = spell, op = operator, time_dim = time_dim,
          ncores = ncores)
      }
      
      dims <- dim(time_exceeded_fcst$data)
      dims_without_time <- dims[-which(names(dims) == "time")]
      time_exceeded_fcst$data <- array(time_exceeded_fcst$data, 
                                       dim = dims_without_time, 
                                       dimnames = dimnames(time_exceeded_fcst$data)
                                       [-which(names(dims) == "time")])

      if (return.percent) {
        ## Calculate percentage of days exceeding threshold
        percentage_exceeded_per_month <- multiApply::Apply(
          data = time_exceeded_fcst$data, target_dims = "ftime",  
          fun = function(exceedance, days) {(exceedance / days) * 100},
          days = days_per_month)$output1
        
        percentage_exceeded_per_month <- Reorder(percentage_exceeded_per_month, 
                                                 names(dim(time_exceeded_fcst$data)))
        
        result_fcst <- abind(result_obs, percentage_exceeded_per_month,
                             along = dim_var)         
      } else {
        ## return number of days
        result_fcst <- abind(result_fcst, time_exceeded_fcst$data, along = dim_var) 
      }
      names(dim(result_fcst)) <- names(dims_without_time)
    }
  }
  
  ## bias correct (necessary if data is already calibrated ?)
  
  # data_temp$obs$data <- result_obs
  # data_temp$hcst$data <- result_hcst
  # data_temp$fcst$data <- result_fcst
  # 
  # res <- CST_BiasCorrection(obs = data_temp$obs, 
  #                           exp = data_temp$hcst, 
  #                           exp_cor = data_temp$fcst,
  #                           sdate_dim = sdate_dim,
  #                           memb_dim = memb_dim)
  # 
  # ## Set negative values to 0
  # result_hcst <- res$data
  # result_hcst[result_hcst < 0] <- 0
  
  data$obs$data <- result_obs
  data$hcst$data <- result_hcst
  data$fcst$data <- result_fcst
  
  return(data)
}
