absolute_threshold <- function(data, threshold, operator, spell,
                               forecast_periods, 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()
  
  if (!is.null(data$obs)) {
    result_obs <- c()
    result_aux <- 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)
      
      ## Loop over forecast time
      for(ftime in forecast_periods){
        
        ## need to subset data by forecast time
        data_ftime <- CST_SelectPeriodOnData(data = data_temp$obs, 
                                             start = list(1, ftime), 
                                             end = list(31, ftime),
                                             ncores = ncores)
        
        days_per_month <- as.numeric(dim(data_ftime$data)['time'])
        
        if(spell == 1){
          ## Calculate number of days exceeding threshold
          time_exceeded <- CST_TotalTimeExceedingThreshold(
            data = data_ftime, threshold = threshold, 
            start = list(1,ftime), end = list(31,ftime),
            op = operator, time_dim = time_dim, ncores = ncores)
          
        } else if(spell > 1){
          ## calculate number of days part of a spell
          time_exceeded <- CST_TotalSpellTimeExceedingThreshold(
            data = data_ftime, threshold = threshold,
            start = list(1,ftime), end = list(31,ftime),
            spell = spell, op = operator, time_dim = time_dim,
            ncores = ncores)
        }
        ## remove time dimension
        dims <- dim(time_exceeded$data)
        dims_without_time <- dims[-which(names(dims) == "time")]
        time_exceeded$data <- array(time_exceeded$data, 
                                    dim = dims_without_time, 
                                    dimnames = dimnames(time_exceeded$data)
                                    [-which(names(dims) == "time")])
        
        ## return percentage of days instead of number
        if (return.percent) {
          time_exceeded$data <- round((time_exceeded$data/days_per_month)*100, 
                                      digits = 1)
        }
        
        ## Combine results for each forecast month
        result_aux <- abind(result_aux, time_exceeded$data,
                            along = length(dim(time_exceeded$data))+1,
                            new.names = "new_dim")
      }
      
      result_obs <- abind(result_obs, result_aux, along = dim_var) 
      names(dim(result_obs)) <- names(time_exceeded$dims)
    }
  }
  
  if (!is.null(data$hcst)) {
    result_hcst <- c()
    result_aux <- 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)
      
      ## Loop over forecast time
      for(ftime in forecast_periods){
        
        ## need to subset data by forecast time
        data_ftime <- CST_SelectPeriodOnData(data = data_temp$hcst, 
                                             start = list(1, ftime), 
                                             end = list(31, ftime),
                                             ncores = ncores)
        
        days_per_month <- as.numeric(dim(data_ftime$data)['time'])
        
        if(spell == 1){
          ## Calculate number of days exceeding threshold
          time_exceeded <- CST_TotalTimeExceedingThreshold(
            data = data_ftime, threshold = threshold, 
            start = list(1,ftime), end = list(31,ftime),
            op = operator, time_dim = time_dim, ncores = ncores)
          
        } else if(spell > 1){
          ## calculate number of days part of a spell
          time_exceeded <- CST_TotalSpellTimeExceedingThreshold(
            data = data_ftime, threshold = threshold,
            start = list(1,ftime), end = list(31,ftime),
            spell = spell, op = operator, time_dim = time_dim,
            ncores = ncores)
        }
        ## remove time dimension
        dims <- dim(time_exceeded$data)
        dims_without_time <- dims[-which(names(dims) == "time")]
        time_exceeded$data <- array(time_exceeded$data, 
                                    dim = dims_without_time, 
                                    dimnames = dimnames(time_exceeded$data)
                                    [-which(names(dims) == "time")])
        
        ## return percentage of days instead of number
        if (return.percent) {
          time_exceeded$data <- round((time_exceeded$data/days_per_month)*100, 
                                      digits = 1)
        }
        
        ## Combine results for each forecast month
        result_aux <- abind(result_aux, time_exceeded$data,
                            along = length(dim(time_exceeded$data))+1,
                            new.names = "new_dim")
      }
      
      result_hcst <- abind(result_hcst, result_aux, along = dim_var) 
      names(dim(result_hcst)) <- names(time_exceeded$dims)
    }
  }
  
  if (!is.null(data$fcst)) {
    result_fcst <- c()
    result_aux <- 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)
      
      ## Loop over forecast time
      for(ftime in forecast_periods){
        
        ## need to subset data by forecast time
        data_ftime <- CST_SelectPeriodOnData(data = data_temp$fcst, 
                                             start = list(1, ftime), 
                                             end = list(31, ftime),
                                             ncores = ncores)
        
        days_per_month <- as.numeric(dim(data_ftime$data)['time'])
        
        if(spell == 1){
          ## Calculate number of days exceeding threshold
          time_exceeded <- CST_TotalTimeExceedingThreshold(
            data = data_ftime, threshold = threshold, 
            start = list(1,ftime), end = list(31,ftime),
            op = operator, time_dim = time_dim, ncores = ncores)
          
        } else if(spell > 1){
          ## calculate number of days part of a spell
          time_exceeded <- CST_TotalSpellTimeExceedingThreshold(
            data = data_ftime, threshold = threshold,
            start = list(1,ftime), end = list(31,ftime),
            spell = spell, op = operator, time_dim = time_dim,
            ncores = ncores)
        }
        ## remove time dimension
        dims <- dim(time_exceeded$data)
        dims_without_time <- dims[-which(names(dims) == "time")]
        time_exceeded$data <- array(time_exceeded$data, 
                                    dim = dims_without_time, 
                                    dimnames = dimnames(time_exceeded$data)
                                    [-which(names(dims) == "time")])
        
        ## return percentage of days instead of number
        if (return.percent) {
          time_exceeded$data <- round((time_exceeded$data/days_per_month)*100, 
                                      digits = 1)
        }
        
        ## Combine results for each forecast month
        result_aux <- abind(result_aux, time_exceeded$data,
                            along = length(dim(time_exceeded$data))+1,
                            new.names = "new_dim")
      }
      
      result_fcst <- abind(result_fcst, result_aux, along = dim_var) 
      names(dim(result_fcst)) <- names(time_exceeded$dims)
    }
  } 
  
  ## 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
  
  if (!is.null(data$fcst)){
    data$fcst$data <- result_fcst
  }else {
    data$fcst$data <- NULL
  }
  
  return(data)
}
