source("modules/Indices/R/compute_nao.R")
source("modules/Indices/R/compute_nino.R")
source("modules/Indices/R/drop_indices_dims.R")
source("modules/Saving/Saving.R")
Indices <- function(recipe, data) {
  # Define parameters
  nao <- NULL
  if ("nao" %in% tolower(names(recipe$Analysis$Workflow$Indices))) {
    if (!is.null(recipe$Analysis$Workflow$Indices$NAO$obsproj)) {
      obsproj <- recipe$Analysis$Workflow$Indices$NAO$obsproj
    } else {
      obsproj <- TRUE
    }
    if (!is.null(recipe$Analysis$Workflow$Indices$NAO$plot_ts)) {
      plot_ts <- recipe$Analysis$Workflow$Indices$NAO$plot_ts
    } else {
      plot_ts <- TRUE
    }
    if (!is.null(recipe$Analysis$Workflow$Indices$NAO$plot_sp)) {
      plot_sp <- recipe$Analysis$Workflow$Indices$NAO$plot_sp
    } else {
      plot_sp <- TRUE
    }
    if (!is.null(recipe$Analysis$Workflow$Indices$NAO$alpha)) {
      alpha <- recipe$Analysis$Workflow$Indices$NAO$alpha
    } else {
      alpha <- 0.05
    }
    nao <- compute_nao(data = data, recipe = recipe,
                       obsproj = obsproj,
                       plot_ts = plot_ts, plot_sp = plot_sp,
                       alpha = alpha)
  }
  ninos <- list()
  num_ninos <- sum(tolower(substr(names(recipe$Analysis$Workflow$Indices),
                                  1, 4)) == "nino")
  if (num_ninos > 0) { 
    for (nins in 1:num_ninos) {
      if ("nino1+2" %in% 
          tolower(names(recipe$Analysis$Workflow$Indices)[nins])) {
        region <- c(lonmin = -90, lonmax = -80, latmin = -10, latmax = 0)
      } else if ("nino3" %in%
           tolower(names(recipe$Analysis$Workflow$Indices)[nins])) {
        region <- c(lonmin = -150, lonmax = -90, latmin = -5, latmax = 5)
      } else if ("nino3.4" %in%
           tolower(names(recipe$Analysis$Workflow$Indices)[nins])) {
        region <- c(lonmin = -170, lonmax = -120, latmin = -5, latmax = 5)
      } else if ("nino4" %in%
           tolower(names(recipe$Analysis$Workflow$Indices)[nins])) {
        region <- c(lonmin = 160, lonmax = -150, latmin = -5, latmax = 5)
      }
      if (is.null(recipe$Analysis$ncores)) {
        ncores <- 1
      } else {
        ncores <- recipe$Analysis$ncores
      }
      if (is.null(recipe$Analysis$remove_NAs)) {
        na.rm <- TRUE
      } else {
        na.rm <- recipe$Analysis$remove_NAs
      }
      if (is.null(recipe$Analysis$Workflow$Indices[nins]$standardised)) {
        standardised <- TRUE
      } else {
        standardised <-recipe$Analysis$Workflow$Indices[nins]$standardised
      }
      if (is.null(recipe$Analysis$Workflow$Indices[nins]$running_mean)) {
        running_mean <- NULL
      } else {
        running_mean <-recipe$Analysis$Workflow$Indices[nins]$running_mean
      }
      if (!is.null(recipe$Analysis$Workflow$Indices[nins]$plot_ts)) {
        plot_ts <- recipe$Analysis$Workflow$Indices[nins]$plot_ts
      } else {
        plot_ts <- TRUE
      }
      if (!is.null(recipe$Analysis$Workflow$Indices[nins]$plot_sp)) {
        plot_sp <- recipe$Analysis$Workflow$Indices[nins]$plot_sp
      } else {
        plot_sp <- TRUE
      }
      if (!is.null(recipe$Analysis$Workflow$Indices[nins]$alpha)) {
        alpha <- recipe$Analysis$Workflow$Indices[nins]$alpha
      } else {
        alpha <- 0.05
      }
      if (!is.null(recipe$Analysis$Workflow$Indices[nins]$save)) {
        save <- recipe$Analysis$Workflow$Indices[nins]$save
      } else {
        save <- 'all'
      }         
      nino <- compute_nino(data = data, recipe = recipe, 
                           standardised = standardised,
                           region = region, save = save, plot_ts = plot_ts,
                           plot_sp = plot_sp, alpha = alpha,
                           running_mean = running_mean, ncores = ncores,
                           na.rm = na.rm)
      ninos[[nins]] <- nino
      names(ninos)[nins] <- names(recipe$Analysis$Workflow$Indices)[nins]    
    } 
  }
  if (is.null(nao)) {
    return(ninos)
    info(recipe$Run$logger,
         "##### EL NINO INDICES COMPUTED SUCCESSFULLY #####")
  } else {
    return(nao)
    info(recipe$Run$logger,
         "##### NAO INDEX COMPUTED SUCCESSFULLY #####")
  }
}
