plot_NAO <- function(recipe, nao, data) {

# Read variable long_name to plot it
  conf <- yaml::read_yaml("conf/variable-dictionary.yml")
  var_name <- conf$vars[[which(names(conf$vars) ==
                               recipe$Analysis$Variables$name)]]$long
  plot_ts <- TRUE
  plot_sp <- TRUE
  alpha <- 0.05
  lons <- data$hcst$coords$longitude
  lats <- data$hcst$coords$latitude
   nao_region <- c(lonmin = -80, lonmax = 40,
                  latmin = 20, latmax = 80)

  if (tolower(recipe$Analysis$Horizon) == "seasonal") {
  # Use startdates param from SaveExp to correctly name the files:
    if (length(data$hcst$attrs$source_files) == dim(data$hcst$data)['syear']) {
      file_dates <- Apply(data$hcst$attrs$source_files, target_dim = NULL,
                          fun = function(x) {
                            pos <- which(strsplit(x, "")[[1]] == ".")
                            res <- substr(x, pos-8, pos-1)
                          })$output1
    }
  } else if (tolower(recipe$Analysis$Horizon) == "decadal"){
    file_dates <- paste0('s',
           recipe$Analysis$Time$hcst_start : recipe$Analysis$Time$hcst_end)
  }

  if (plot_ts) {
    dir.create(paste0(recipe$Run$output_dir, "/plots/Indices/"),
               showWarnings = F, recursive = T)
    source("modules/old_modules/Indices/R/plot_deterministic_forecast.R")
    for (tstep in 1:dim(nao$hcst$data)['time']) {
        mes <- as.numeric(substr(recipe$Analysis$Time$sdate, 1,2)) +
               (tstep - 1) + (recipe$Analysis$Time$ftime_min - 1)
        mes <- ifelse(mes > 12, mes - 12, mes)
      fmonth <- sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min)
      obs <- Subset(nao$obs$data, along = 'time', ind = tstep)
      exp <- Subset(nao$hcst$data, along = 'time', ind = tstep) 
      if (gsub(".", "", recipe$Analysis$Datasets$System$name) == "") {
        system <- recipe$Analysis$Datasets$System$name
      } else {
        system <-gsub(".", "", recipe$Analysis$Datasets$System$name)
      }
      if (tolower(recipe$Analysis$Horizon) == "seasonal") {
        toptitle <- paste("NAO Index\n",  
                          month.abb[mes],
                          "/", recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_",
                           system, "_", recipe$Analysis$Datasets$Reference$name,
                           "_s", recipe$Analysis$Time$sdate, "_ftime",
                           sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min), ".pdf") 
        caption <- paste0("NAO method: ",
                          ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                 "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                          "Nominal start date: 1st of ",
                          month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
                          "\n",
                          "Forecast month: ", fmonth, "\n")
        xlabs <- as.numeric(substr(file_dates, 1, 4))
      } else if (tolower(recipe$Analysis$Horizon) == "decadal"){
        toptitle <- paste("NAO Index\n",  
                          "Lead time", fmonth,
                          " / Start dates", recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_",
                           system, "_", recipe$Analysis$Datasets$Reference$name,
                           "_ftime",
                           sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min), ".pdf") 
        caption <- paste0("NAO method: ",
                          ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                 "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                          "Start date month: ",
                          month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
                          "\n",
                          "Lead time: ", fmonth, "\n")
        xlabs <- file_dates
      } else {
        toptitle <- NULL
        warning("The plot title is not defined for this time horizon. ",
                "The plots will not have a title.")
      }
      plot_deterministic_forecast(obs, exp, 
                                  time_dim = 'syear',
                                  member_dim = 'ensemble', style = 'boxplot',
                                  xlabs = xlabs,
                                  title = toptitle, fileout = plotfile,
                                  caption = caption, caption_line = 6.5,
                                  legend_text = c(
                                  recipe$Analysis$Datasets$Reference$name,
                                  recipe$Analysis$Datasets$System$name))
    }
  }
  if (plot_sp) {
    ## TODO: To be removed when s2dv is released:
    source("modules/Visualization/R/tmp/PlotRobinson.R")
    source("modules/old_modules/Indices/R/correlation_eno.R")
    source("modules/Visualization/R/get_proj_code.R")
    dir.create(paste0(recipe$Run$output_dir, "/plots/Indices/"),
               showWarnings = F, recursive = T)
    # Get correct code for stereographic projection
    projection_code <- get_proj_code(proj_name = "stereographic")
    correl_obs <- Apply(list(data$obs$data, nao$obs$data), 
                        target_dims = 'syear', fun = .correlation_eno, 
                        time_dim = 'syear', method = 'pearson', alpha = alpha, 
                        test.type = 'two-sided', pval = FALSE, 
                        ncores = recipe$Analysis$ncores)
    correl_hcst <- Apply(list(data$hcst$data, nao$hcst$data), 
                         target_dims = c('syear', 'ensemble'),
                         fun = function(x, y) {
                         x <- apply(x, 1, mean, na.rm = TRUE)
                         y <- apply(y, 1, mean, na.rm = TRUE)
                         dim(y) <- c(syear = length(y))
                         dim(x) <- c(syear = length(x))
                         res <- .correlation_eno(x, y,
                           time_dim = 'syear', method = 'pearson', alpha = alpha, 
                           test.type = 'two-sided', pval = FALSE)}, 
                           ncores = recipe$Analysis$ncores)
    correl_hcst_full <- Apply(list(data$hcst$data, nao$hcst$data), 
                              target_dims = c('syear', 'ensemble'), 
                              fun = function(x,y) {
                              dim(y) <- c(syear = length(y))
                              dim(x) <- c(syear = length(x))
                           res <- .correlation_eno(x, y,
                              time_dim = 'syear', method = 'pearson', alpha = alpha, 
                              test.type = 'two-sided', pval = FALSE)}, 
                                ncores = recipe$Analysis$ncores)
    
    for (tstep in 1:dim(nao$obs$data)['time']) {
      fmonth <- sprintf("%02d", tstep - 1 + recipe$Analysis$Time$ftime_min)
      ## Observations
      map <- drop(Subset(correl_obs$r, along = 'time', ind = tstep))
      sig <- drop(Subset(correl_obs$sign, along = 'time', ind = tstep))
      if (tolower(recipe$Analysis$Horizon) == "seasonal") {
        mes <- as.numeric(substr(recipe$Analysis$Time$sdate, 1,2)) +
               (tstep - 1) + (recipe$Analysis$Time$ftime_min - 1)
        mes <- ifelse(mes > 12, mes - 12, mes)
        toptitle <- paste(recipe$Analysis$Datasets$Reference$name, "\n",
                          "NAO Index -",var_name, "\n",
                          " Correlation /",  month.abb[mes], 
                          "/", recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_",
                           recipe$Analysis$Datasets$Reference$name,
                           "_s", recipe$Analysis$Time$sdate,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Nominal start date: 1st of ",
                           month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else if (tolower(recipe$Analysis$Horizon) == "decadal") {
        toptitle <- paste(recipe$Analysis$Datasets$Reference$name, "\n",
                          "NAO Index -",var_name, "\n",
                          " Correlation / Start dates ", 
                          recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_",
                           recipe$Analysis$Datasets$Reference$name,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Start date: month ",
                           month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else {
        toptitle <- NULL
        warning("The plot title is not defined for this time horizon. ",
                "The plots will not have a title.")
      }
      if (gsub(".", "", recipe$Analysis$Datasets$System$name) == "") {
        system <- recipe$Analysis$Datasets$System$name
      } else {
        system <- gsub(".", "", recipe$Analysis$Datasets$System$name)
      }

      PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
                   lat_dim = 'latitude', lon_dim = 'longitude',
                   legend = 's2dv', style = 'polygon',
                   toptitle = toptitle, crop_coastlines = nao_region,
                   caption = caption, mask = sig, bar_extra_margin = c(4,0,4,0),
                   fileout = plotfile, width = 8, height = 6,
                   brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
      ## Ensemble-mean
      map <- drop(Subset(correl_hcst$r, along = 'time', ind = tstep))
      sig <- drop(Subset(correl_hcst$sign, along = 'time', ind = tstep))
      if (tolower(recipe$Analysis$Horizon) == "seasonal") {
        toptitle <- paste(recipe$Analysis$Datasets$System$name, "\n",
                          "NAO Index -", var_name, "\n",
                          " Correlation /", month.abb[mes],
                          "/", recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_ensmean_",
                           system,
                           "_s", recipe$Analysis$Time$sdate,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Correlation ensemble mean\n",
                           "Nominal start date: 1st of ",
                           month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else if (tolower(recipe$Analysis$Horizon) == "decadal"){
        toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
                          "NAO Index -",var_name, "\n",
                          " Correlation / Start dates ", 
                          recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_ensmean_",
                           system, 
                           recipe$Analysis$Datasets$Reference$name,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Correlation ensemble mean\n",
                           "Start date month: ",
                           month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else {
        toptitle <- NULL
        warning("The plot title is not defined for this time horizon. ",
                "The plots will not have a title.")
      }

      PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
                   lat_dim = 'latitude', lon_dim = 'longitude',
                   legend = 's2dv', bar_extra_margin = c(4,0,4,0),
                   toptitle = toptitle, style = 'polygon',
                   caption = caption, mask = sig, crop_coastline = nao_region,
                   fileout = plotfile, width = 8, height = 6,
                   brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
      
      # Full hcst corr
      map <- drop(Subset(correl_hcst_full$r, along = 'time', ind = tstep))
      sig <- drop(Subset(correl_hcst_full$sign, along = 'time', ind = tstep))
      if (tolower(recipe$Analysis$Horizon) == "seasonal") {
        toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
                          "NAO Index -",var_name, "\n",
                          " Correlation /", month.abb[mes],
                          "/", recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_member_",
                           system,
                           "_s", recipe$Analysis$Time$sdate,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Correlation all members\n",
                           "Nominal start date: 1st of ",
                           month.name[as.numeric(substr(recipe$Analysis$Time$sdate, 1,2))],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else if (tolower(recipe$Analysis$Horizon) == "decadal"){
        toptitle <- paste(recipe$Analysis$Datasets$System$name,"\n",
                          "NAO Index -",var_name, "\n",
                          " Correlation / Start dates ", 
                          recipe$Analysis$Time$hcst_start, "-",
                          recipe$Analysis$Time$hcst_end)
        plotfile <- paste0(recipe$Run$output_dir, "/plots/Indices/NAO_correlation_",
                           recipe$Analysis$Variable$name, "_member_",
                           system, 
                           recipe$Analysis$Datasets$Reference$name,
                           "_ftime", fmonth, ".pdf")
        caption <-  paste0("NAO method: ",
                           ifelse(recipe$Analysis$Workflow$Indices$NAO$obsproj,
                                  "Pobs", "Pmod"), " (Doblas-Reyes et al., 2003)\n",
                           "Correlation all members\n",
                           "Start date month: ",
                           month.name[get_archive(recipe)$System[[recipe$Analysis$Datasets$System$name]]$initial_month],
                           "\n",
                           "Forecast month: ", fmonth, "\n",
                           "Pearson correlation; alpha = ", alpha)
      } else {
        toptitle <- NULL
        warning("The plot title is not defined for this time horizon. ",
                "The plots will not have a title.")
      }
      PlotRobinson(map, lon = lons, lat = lats, target_proj = projection_code,
                   lat_dim = 'latitude', lon_dim = 'longitude',
                   legend = 's2dv', bar_extra_margin = c(4,0,4,0),
                   toptitle = toptitle, style = 'polygon',
                   caption = caption, mask = sig, crop_coastline = nao_region,
                   fileout = plotfile, width = 8, height = 6,
                   brks = seq(-1, 1, 0.2), cols = brewer.pal(10, 'PuOr'))
    } # end tstep loop
  }
}
