## This function is to calculate a percentile threshold of an input variable
## The function accepts daily data and gives the output as the amount of days 
## which exceed the threshold for the defined forecast period.

## Limitation: function currently requires an input of minimum 2 forecast times.

consecutive_days <- function(data, threshold, operator, 
                             forecast_periods, return.percent = TRUE,
                             indicator, ncores) {

  time_dim <- 'time'
  memb_dim <- 'ensemble'
  sdate_dim <- 'syear'
  
  # browser()
  
  if (!is.null(data$obs)) {
    result_obs <- c()
    result_aux <- c()
    ## Loop over forecast time
    for(ftime in 1:length(forecast_periods)){
      
      consec_days_result <- CST_ConsecutiveDays(data = data$obs, threshold, operator, 
                                      start = list(1, forecast_periods[[ftime]]), 
                                      end = list(31, forecast_periods[[ftime]]),
                                      ncores = ncores)

      ## Combine results in list for each forecast month
      result_aux[[ftime]] <- consec_days_result 
    }
    ## bind list of forecast times
    result_obs <- CST_Bind(x = result_aux, along = 'time', 
                           var_dim = 'var', dat_dim = 'dat')
  }
  
  if (!is.null(data$hcst)) {
    result_hcst <- c()
    result_aux <- c()
    ## Loop over forecast time
    for(ftime in 1:length(forecast_periods)){
      
      consec_days_result <- CST_ConsecutiveDays(data = data$hcst, threshold, operator, 
                                                start = list(1, forecast_periods[[ftime]]), 
                                                end = list(31, forecast_periods[[ftime]]),
                                                ncores = ncores)
      
      ## Combine results in list for each forecast month
      result_aux[[ftime]] <- consec_days_result 
    }
    result_hcst <- CST_Bind(x = result_aux, along = 'time', 
                            var_dim = 'var', dat_dim = 'dat')
  }
  
  if (!is.null(data$fcst)) {
    result_fcst <- c()
    result_aux <- c()
    ## Loop over forecast time
    for(ftime in 1:length(forecast_periods)){
      
      consec_days_result <- CST_ConsecutiveDays(data = data$fcst, threshold, operator, 
                                                start = list(1, forecast_periods[[ftime]]), 
                                                end = list(31, forecast_periods[[ftime]]),
                                                ncores = ncores)
      
      ## Combine results in list for each forecast month
      result_aux[[ftime]] <- consec_days_result 
    }
    result_fcst <- CST_Bind(x = result_aux, along = 'time', 
                            var_dim = 'var', dat_dim = 'dat')
  } else {
    result_fcst <- NULL
  }
  
  rm(result_aux)
  
  result <- list(hcst = result_hcst,
                 obs = result_obs,
                 fcst = result_fcst)
  
  for (element in names(result[!is.null(result)])) {
    old_var_name <- data[[element]]$attrs$Variable$varName
    old_metadata <- data[[element]]$attrs$Variable$metadata[[old_var_name]]
    
    result[[element]]$coords$var <- indicator
    result[[element]]$attrs$Variable$varName <- indicator
    result[[element]]$attrs$Variable$metadata[[indicator]] <- old_metadata
    result[[element]]$attrs$Variable$metadata[[indicator]]$long_name <- indicator ## temporarily setting to short name
    result[[element]]$attrs$Variable$metadata[[indicator]]$units <- 'days'
    
  }
  
  return(result)
  
}
