# Figure 2A:
library(CSTools)
library(zeallot)
load(file = "/esarchive/scratch/nperez/git/tas_toydata.RData")
Lat <- exp$lat
Lon <- exp$lon
c(ano_exp, ano_obs) %<-% CST_Anomaly(exp = exp, obs = obs, cross = TRUE,
                                     memb = TRUE)
AnomDJF <- CST_MultiMetric(exp = ano_exp, obs = ano_obs, metric = 'correlation', 
                          multimodel = TRUE)
PlotCombinedMap(AnomDJF$data$corr[,1,,], lon = Lon, lat = Lat, map_select_fun = max,
                display_range = c(0, 1), map_dim = 'nexp',
                legend_scale = 0.8, brks = 11,
                axes_label_scale = 1.4,
                cols = list(c('white', 'black'),
                            c('white', 'darkblue'),
                            c('white', 'darkred'),
                            c('white', 'darkorange')),
                bar_titles = c("MMM", names(AnomDJF$Datasets)),
                fileout = "/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/fig02A.tiff",
                width = 12, height = 8, res = 300)

# Figure 2B:

#saveRDS(list(prob_map, Lat, Lon, mask_rpss, clim_var), "TercilesVignettePlot.rds")
terciles <- readRDS("/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/TercilesVignettePlot.rds")
PlotMostLikelyQuantileMap(probs = prob_map, lon = Lon, lat = Lat, coast_width = 1.5,
                          legend_scale = 0.8,
    mask = ClimProjDiags::Subset(s2dv::Reorder(mask_rpss, c('lat', 'lon', 'dataset')), along = 'dataset', indices = 1, drop = TRUE),
          col_unknown_cat = "white", col_mask = 'white',
                          toptitle = paste('Most likely tercile -', clim_var,
                                           '- SEAS5 - JJA 2020'),
                 colNA = 'white', res = 300, height = 7, width = 9,
    fileout = "/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/fig02B.tiff")

# Figure 2C:
libray(CSTools)
fcst <- abind::abind(lonlat_data$exp$data[1,,1,1,1,1] - 273.15,
              lonlat_data$exp$data[1,,1,2,1,1] - 273.15, along = 2)
names(dim(fcst)) <- c('members', 'fcst')
PlotForecastPDF(fcst, tercile.limits = c(5, 7), extreme.limits = c(4, 8), 
  var.name = "Temperature (ºC)",
  title = "Forecasts initialized on Nov 2000 at sample Mediterranean region", 
  fcst.names = c("November", "December"), obs = c(6, 4),
  plotfile = "/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/fig02C.tiff")

# Figure 2D:
pdf_1 <- c(0.4,0.8)
attr(pdf_1, "name") <- "NAO1"
dim(pdf_1) <- c(statistic = 2)
pdf_2 <- c(1,0.5)
attr(pdf_2, "name") <- "NAO2"
dim(pdf_2) <- c(statistic = 2)
PlotPDFsOLE(pdf_1, pdf_2, legendPos = "right", dpi = 300,
            plotfile = "/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/fig02D.tiff")

# Figure 2E:
library(CSTools)

datos <- array(sample.int(20, 20*12*4, replace = TRUE), 
               c(dimy = 20, dimcat = 4, dimx = 12))

PlotTriangles4Categories(datos, 
             ytitle = '', xlabels = month.name,
             ylabels = 1991:2010, cex_leg = 0.6, cex_axis = 1,
             lab_legend = c('AR', 'NAO-', 'BL', 'NAO+'),
             res = 300, figure.width = 1.8, mar = c(4.5, 2, 1, 1), 
fileout = "/esarchive/scratch/nperez/git/Flor/CSTools_Manuscript/fig02E.tiff")


