
# Creation: 6/2016
# Author: Nicola Cortesi
#
# Aim: to visualize the regime anomalies of the weather regimes, their interannual frequencies and their impact on a chosen cliamte variable (i.e: wind speed or temperature)
#
# I/O: input file needed are:
#      1) the output of the weather_regimes.R script, which ends with the suffix "_psl.RData".
#      2) if you need the impact map too, the outputs of the weather_regimes_impact.R script, which end wuth the suffix "_sfcWind.RData" and "_tas.RData"
#
#      Output files are the maps, witch the user can generate as .png or as .pdf
#      Due to the script's length, it is better to run it from the terminal with: Rscript ~/scripts/weather_regimes_maps.R
#      This script doesn't need to be parallelized because it takes only a few seconds to create and save the output maps.
#
# Assumptions: If your regimes derive from a reanalysis, this script must be run twice: once, to create a .pdf file (see below)
#              that the user must open to find visually the order by which the four regimes are stored inside the four clusters. For example, he can find that the
#              sequence of the images in the file (from top to down) corresponds to: Blocking / NAO- / Atl.Ridge / NAO+ regime. Subsequently, he has to change 'ordering'
#              from F to T (see below) and set the four variables 'clusterX.name' (X = 1...4) to follow the same order found inside that file.
#              The second time, you have to run this script only to get the maps in the right order, which by default is, from top to down:
#              "NAO+","NAO-","Blocking" and "Atl.Ridge" (you can change it with the variable 'orden'). You also have to set 'save.names' to TRUE, so an .RData file
#              is written to store the order of the four regimes, in case you need to visualize these maps again in future or create new ones based on the saved data.
#
#              If your regimes derive from a forecast system, you have to compute before the regimes derived from a reanalysis. Then, you can associate the reanalysis
#              to the forecast system, to employ it to automatically aussociate to each simulated cluster one of the
#              observed regimes, the one with the highest spatial correlation. Beware that the two maps of regime anomalies must have the same spatial resolution!
#
# Branch: weather_regimes

rm(list=ls())
library(s2dverification)  # for the function Load()
library(Kendall) # for the MannKendall test
source('/home/Earth/ncortesi/scripts/Rfunctions.R') # for the calendar functions

### set the directory where the weather regimes computed with a reanalysis are stored (xxxxx_psl.RData files):

#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/18) as 12) but with no lat correction"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/18_as_12_but_with_no_lat_correction"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/41_ERA-Interim_monthly_1981-2015_LOESS_filter"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/43_as_41_but_with_running_cluster_and_lat_correction"
rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/44_as_43_but_with_no_lat_correction"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/45_as_43_but_with_Z500"

rean.name <- "ERA-Interim"       # reanalysis name (if input data comes from a reanalysis)
forecast.name <- "ECMWF-S4"      # forecast system name (if input data comes from a forecast system)

# Choose between loading reanalysis ('rean.name') or forecasts ('forecast.name'):
fields.name <- forecast.name        

# Choose one or more periods to plot from 1 to 17 (1-12: Jan - Dec, 13-16: Winter, Spring, Summer, Autumn, 17: Year).
period <- 1:12           # You need to have created before the '_psl.RData' file with the output of 'weather_regimes_vXX'.R for that period. 

# run it twice: once, with: 'ordering <- FALSE', 'save.names <- TRUE', 'as.pdf <- TRUE' and 'composition <- "simple" '
# to look at the cartography and find visually the right regime names of the four clusters; then, insert the regime names in the correct order below and run this script
# a second time one month/season at time with: 'ordering = TRUE', 'save.names = TRUE', 'as.pdf = FALSE' and 'composition = "simple" '
# to save the ordered cartography (then, you can check the correct regime sequence in the .png maps). After that, any time you run this script, remember to set:
# 'ordering = TRUE , 'save.names = FALSE' (and any type of composition you want to plot)  not to overwrite the files which stores the right cluster order.

ordering <- T    # If TRUE, order the clusters following the regimes listed in the 'orden' vector (set it to TRUE only after you manually inserted the names below).
save.names <- F  # if TRUE, also save the cluster names (i.e: the association between clusters and weather regimes); if FALSE, the cluster names are loaded from a file
as.pdf <- F      # FALSE: save results as one .png file for each season/month, TRUE: save only one .pdf file with all seasons/months

composition <- "summary"   # choose which kind of composition you want to plot:
                     #     -  'simple' for monthly graphs of regime anomalies / impact / interannual frequencies in three columns
                     #     -  'psl' for all the regime anomalies for a fixed forecast month
                     #     -  'fre' for all the interannual frequencies for a fixed forecast month
                     #     -  'impact' for all the impact maps for a fixed forecast month and the variable specified in var.num
                     #     -  'summary' for the summary plots of all forecast months (persistence bias, freq.bias, correlations, etc.) [You need all ClusterNames files]
                     #     -  'impact.highest' for all the impact plot of the regime with the highest impact
                     #     -  'single.impact' to save the individual impact maps
                     #     -  'single.psl' to save the individual psl map
                     #     -  'single.fre' to save the individual fre map
                     #     -  'none' doesn't plot anything, it only saves the ClusterName.Rdata files
                     #     -  'taylor' plot the taylor's diagram between the regime anomalies of a monthly reanalaysis and a seasonal one

var.num <- 1         # if fields.name ='rean.name' and composition ='simple', Choose a variable for the impact maps 1: sfcWind  2: tas

mean.freq <- FALSE   # if TRUE, when composition <- "simple", it also add the mean frequency value over each frequency plots

# Associates the regimes to the four cluster:
cluster3.name <- "NAO+"  
cluster2.name <- "NAO-"
cluster1.name <- "Blocking"
cluster4.name <- "Atl.Ridge"

# choose an order to give to the cartograpy, from top to bottom:
orden <- c("NAO+","NAO-","Blocking","Atl.Ridge")

####### if fields.name='forecast.name', you have to specify these additional variables:

# working dir with the input files with '_psl.RData' suffix:
work.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/42_ECMWF_S4_monthly_1981-2015_LOESS_filter"  

lead.months <- 0:6                  # in case you selected 'fields.name = forecast.name', specify a leadtime (0 = same month of the start month) to plot
                                    # (and variable 'period' becomes the start month)
forecast.month <- 1                 # in case composition = 'psl' or 'fre' or 'impact', choose a target month to forecast, from 1 to 12, to plot its composition
                                    # if you want to plot all compositions from 1 to 12  at once, just enable the line below and add a { at the end of the script
                                    # DO NOT run the loop below when composition="summary" or "taylor", because it will modify the data in the ClusterName.RData files!!!
for(forecast.month in 1:12){
####### Derived variables ###############################################################################################################################################

if(fields.name != rean.name && fields.name != forecast.name) stop("variable 'fields.name' is not properly set")
regime1.name <- orden[1]
regime2.name <- orden[2]
regime3.name <- orden[3]
regime4.name <- orden[4]
    
var.name <- c("sfcWind","tas")
var.name.full <- c("Wind Speed","Temperature")   # full name of the 'predictand' variable to put in the title of the graphs
var.unit <- c("m/s", "ºC")                       # unit of measure of var (for drawing color scales)

var.num.orig <- var.num  # loading _psl files, this value can change!
fields.name.orig <- fields.name
period.orig <- period
work.dir.orig <- work.dir
pdf.suffix <- ifelse(length(period)==1, my.period[period], "all_periods")

n.map <- 0
###################################################################################################################################################################
    
if(composition != "summary" && composition != "impact.highest" && composition != "taylor"){
    
    if(composition == "psl" || composition == "fre" || composition == "impact") {
        if(as.pdf && fields.name == forecast.name) pdf(file=paste0(work.dir,"/",fields.name,"_",pdf.suffix,"_forecast_month_",forecast.month,"_",composition,".pdf"),width=110,height=60)
        if(!as.pdf && fields.name == forecast.name) png(filename=paste0(work.dir,"/",fields.name,"_forecast_month_",forecast.month,"_",composition,".png"),width=6000,height=2300)

        lead.months <- c(6:0,0)
        plot.new()
        
        # Sheet title:
        par(fig=c(0, 1, 0.94, 0.98), new=TRUE)
        if(composition == "psl") mtext(paste0(fields.name, " simulated Weather Regimes for ", month.name[forecast.month]), cex=5.5, font=2)
        if(composition == "fre") mtext(paste0(fields.name, " simulated interannual frequencies for ", month.name[forecast.month]), cex=5.5, font=2)
        if(composition == "impact") mtext(paste0(fields.name, " simulated ",var.name.full[var.num], " impact for ", month.name[forecast.month]), cex=5.5, font=2)

    }

    if(fields.name == rean.name) lead.months <- 1 # just to be able to enter the loop below once!

    for(lead.month in lead.months){
        #lead.month=lead.months[1] # for the debug

        if(composition == "simple"){
            if(as.pdf && fields.name == rean.name) pdf(file=paste0(rean.dir,"/",fields.name,"_",var.name[var.num],"_",pdf.suffix,".pdf"),width=40, height=60)
            if(as.pdf && fields.name == forecast.name) pdf(file=paste0(work.dir,"/",fields.name,"_",var.name[var.num],"_",pdf.suffix,"_leadmonth_",lead.month,".pdf"),width=40,height=60)
        }
        
        if(composition == 'psl' || composition == 'fre' || composition == 'impact') {
            period <- forecast.month - lead.month
            if(period < 1) period <- period + 12
        }

        impact.data <- FALSE
        for(p in period){
            #p <- period[1] # for the debug
            p.orig <- p

            if(fields.name == rean.name) print(paste("p =",p))
            if(fields.name == forecast.name) print(paste("p =",p,"lead.month =", lead.month))
            
            # load regime data (for forecasts, it load only the data corresponding to the chosen start month p and lead month 'lead.month')
            if(fields.name == rean.name || (fields.name == forecast.name && n.map == 7)) {
                load(file=paste0(rean.dir,"/",rean.name,"_",my.period[p],"_","psl",".RData"))
                if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
                if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
                if(work.dir != work.dir.orig) work.dir <- work.dir.orig # ripristinate original values of this script

                # load impact data only if it is available, if not it will leave an empty space in the composition:
                if(file.exists(paste0(rean.dir,"/",rean.name,"_",my.period[p],"_",var.name[var.num],".RData"))){
                    impact.data <- TRUE
                    print("Impact data available for reanalysis")
                    load(file=paste0(rean.dir,"/",rean.name,"_",my.period[p],"_",var.name[var.num],".RData"))
                } else {
                    impact.data <- FALSE
                    print("Impact data for reanalysis not available")
                }      
            }
            
            if(fields.name == forecast.name && n.map < 7){
                load(file=paste0(work.dir,"/",forecast.name,"_",my.period[p],"_leadmonth_",lead.month,"_","psl",".RData"))  # load cluster time series and mean psl data
                
                if(file.exists(paste0(work.dir,"/",forecast.name,"_",my.period[p],"_leadmonth_",lead.month,"_",var.name[var.num],".RData"))){
                    impact.data <- TRUE
                    print("Impact data available for forecasts")
                    if((composition == "simple" || composition == "impact")) load(file=paste0(work.dir,"/",forecast.name,"_",my.period[p],"_leadmonth_",lead.month,"_",var.name[var.num],".RData")) # load mean var data for impact maps
                } else {
                    impact.data <- FALSE
                    print("Impact data for forecasts not available")
                }
               
                if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
                if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script

            }

            if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
            if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
            if(work.dir != work.dir.orig) work.dir <- work.dir.orig # ripristinate original values of this script
            if(p != p.orig) p <- p.orig
            
            if(fields.name == forecast.name && n.map < 7){
 
                # compute the simulated interannual monthly variability:
                n.days.month <- dim(my.cluster.array[[p]])[1] # number of days in the forecasted month
                
                freq.sim1 <- apply(my.cluster.array[[p]] == 1, c(2,3), sum)
                freq.sim2 <- apply(my.cluster.array[[p]] == 2, c(2,3), sum)
                freq.sim3 <- apply(my.cluster.array[[p]] == 3, c(2,3), sum)
                freq.sim4 <- apply(my.cluster.array[[p]] == 4, c(2,3), sum)
                
                sd.freq.sim1 <- sd(freq.sim1) / n.days.month
                sd.freq.sim2 <- sd(freq.sim2) / n.days.month
                sd.freq.sim3 <- sd(freq.sim3) / n.days.month
                sd.freq.sim4 <- sd(freq.sim4) / n.days.month

                # compute the transition probabilities:
                j1 <- j2 <- j3 <- j4 <- 1
                transition1 <- transition2 <- transition3 <- transition4 <- c()

                n.members <-  dim(my.cluster.array[[p]])[3]
                
                for(m in 1:n.members){
                   for(y in 1:n.years){
                      for (d in 1:(n.days.month-1)){
                         
                         if (my.cluster.array[[p]][d,y,m] == 1 && (my.cluster.array[[p]][d + 1,y,m] !=  1)){
                             transition1[j1] <- my.cluster.array[[p]][d + 1,y,m]
                             j1 <- j1 + 1
                         }
                         if (my.cluster.array[[p]][d,y,m] == 2 && (my.cluster.array[[p]][d + 1,y,m] !=  2)){
                             transition2[j2] <- my.cluster.array[[p]][d + 1,y,m]
                             j2 <- j2 + 1
                         }
                         if (my.cluster.array[[p]][d,y,m] == 3 && (my.cluster.array[[p]][d + 1,y,m] !=  3)){
                             transition3[j3] <- my.cluster.array[[p]][d + 1,y,m]
                             j3 <- j3 +1
                         }
                         if (my.cluster.array[[p]][d,y,m] == 4 && (my.cluster.array[[p]][d + 1,y,m] !=  4)){
                             transition4[j4] <- my.cluster.array[[p]][d + 1,y,m]
                             j4 <- j4 +1
                         }
                        
                      }
                   }
                }

                trans.sim <- matrix(NA,4,4 , dimnames= list(c("cluster1.sim", "cluster2.sim", "cluster3.sim", "cluster4.sim"),c("cluster1.sim","cluster2.sim","cluster3.sim","cluster4.sim")))
                trans.sim[1,2] <- length(which(transition1 == 2))
                trans.sim[1,3] <- length(which(transition1 == 3))
                trans.sim[1,4] <- length(which(transition1 == 4))
                
                trans.sim[2,1] <- length(which(transition2 == 1))
                trans.sim[2,3] <- length(which(transition2 == 3))
                trans.sim[2,4] <- length(which(transition2 == 4))

                trans.sim[3,1] <- length(which(transition3 == 1))
                trans.sim[3,2] <- length(which(transition3 == 2))
                trans.sim[3,4] <- length(which(transition3 == 4))

                trans.sim[4,1] <- length(which(transition4 == 1))
                trans.sim[4,2] <- length(which(transition4 == 2))
                trans.sim[4,3] <- length(which(transition4 == 3))

                trans.tot <- apply(trans.sim,1,sum,na.rm=T)
                for(i in 1:4) trans.sim[i,] <- trans.sim[i,]/trans.tot[i]

                
                # compute cluster persistence:
                my.cluster.vector <- as.vector(my.cluster.array[[p]])  # reduce it to a vector with the order: day1month1member1 , day2month1member1, ... , day1month2member1, day2month2member1, ,,,

                sequ1 <- sequ2 <- sequ3 <- sequ4 <- rep(0,10000)
                i1 <- i2 <- i3 <- i4 <- 1

                if(my.cluster.vector[1] != 1) i1 <- 0
                if(my.cluster.vector[1] == 1) sequ1[i1] <- sequ1[i1]+1
                if(my.cluster.vector[1] != 2) i2 <- 0
                if(my.cluster.vector[1] == 2) sequ2[i2] <- sequ2[i2]+1
                if(my.cluster.vector[1] != 3) i3 <- 0
                if(my.cluster.vector[1] == 3) sequ3[i3] <- sequ3[i3]+1
                if(my.cluster.vector[1] != 4) i4 <- 0
                if(my.cluster.vector[1] == 4) sequ4[i4] <- sequ4[i4]+1
                n.dd <- length(my.cluster.vector)
                
                for(d in 1:(n.dd-1)){
                    if(my.cluster.vector[d] != 1 && my.cluster.vector[d+1] == 1)  i1 <- i1+1
                    if(my.cluster.vector[d] == 1) sequ1[i1] <- sequ1[i1]+1
                    
                    if(my.cluster.vector[d] != 2 && my.cluster.vector[d+1] == 2)  i2 <- i2+1
                    if(my.cluster.vector[d] == 2) sequ2[i2] <- sequ2[i2]+1
                    
                    if(my.cluster.vector[d] != 3 && my.cluster.vector[d+1] == 3)  i3 <- i3+1
                    if(my.cluster.vector[d] == 3) sequ3[i3] <- sequ3[i3]+1
                    
                    if(my.cluster.vector[d] != 4 && my.cluster.vector[d+1] == 4)  i4 <- i4+1
                    if(my.cluster.vector[d] == 4) sequ4[i4] <- sequ4[i4]+1
                }

                if(my.cluster.vector[n.dd] == 1) sequ1[i1] <- sequ1[i1]+1
                if(my.cluster.vector[n.dd] == 2) sequ2[i2] <- sequ2[i2]+1
                if(my.cluster.vector[n.dd] == 3) sequ3[i3] <- sequ3[i3]+1
                if(my.cluster.vector[n.dd] == 4) sequ4[i4] <- sequ4[i4]+1

                pers1 <- mean(sequ1[which(sequ1 != 0)])
                pers2 <- mean(sequ2[which(sequ2 != 0)])
                pers3 <- mean(sequ3[which(sequ3 != 0)])
                pers4 <- mean(sequ4[which(sequ4 != 0)])
                
                # compute correlations between simulated clusters and observed regime's anomalies:
                cluster1.sim <- pslwr1mean; cluster2.sim <- pslwr2mean; cluster3.sim <- pslwr3mean; cluster4.sim <- pslwr4mean
                
                if(composition == 'impact' && impact.data == TRUE) {
                    cluster1.sim.imp <- varwr1mean; cluster2.sim.imp <- varwr2mean; cluster3.sim.imp <- varwr3mean; cluster4.sim.imp <- varwr4mean
                    #cluster1.sim.imp.pv <- pvalue1; cluster2.sim.imp.pv <- pvalue2; cluster3.sim.imp.pv <- pvalue3; cluster4.sim.imp.pv <- pvalue4
                }
                
                lat.forecast <- lat; lon.forecast <- lon
                
                if((p + lead.month) > 12) { load.month <- p + lead.month - 12 } else { load.month <- p + lead.month }  # reanalysis forecast month to load
                load(file=paste0(rean.dir,"/",rean.name,"_",my.period[load.month],"_","psl",".RData")) # load reanalysis data, which overwrities the pslwrXmean variables
                load(paste0(rean.dir,"/",rean.name,"_", my.period[load.month],"_","ClusterNames",".RData")) # load also reanalysis regime names

                # load reanalysis varwrXmean impact data:
                if(composition == 'impact' && impact.data == TRUE) load(file=paste0(rean.dir,"/",rean.name,"_",my.period[load.month],"_",var.name[var.num],".RData"))
                    
                if(var.num != var.num.orig) var.num <- var.num.orig # ripristinate original values of this script
                if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
                if(!identical(period.orig,period)) period <- period.orig
                if(work.dir != work.dir.orig) work.dir <- work.dir.orig # ripristinate original values of this script
                if(p.orig != p) p <- p.orig

                # compute the observed transition probabilities:
                n.days.month <- n.days.in.a.period(load.month,2001)
                j1o <- j2o <- j3o <- j4o <- 1; transition.obs1 <- transition.obs2 <- transition.obs3 <- transition.obs4 <- c()
                
                for (d in 1:(length(my.cluster$cluster)-1)){    
                    if (my.cluster$cluster[d] == 1 && (my.cluster$cluster[d + 1] !=  my.cluster$cluster[d])){
                        transition.obs1[j1o] <- my.cluster$cluster[d + 1]
                        j1o <- j1o + 1
                    }
                    if (my.cluster$cluster[d] == 2 && (my.cluster$cluster[d + 1] !=  my.cluster$cluster[d])){
                        transition.obs2[j2o] <- my.cluster$cluster[d + 1]
                        j2o <- j2o + 1
                    }
                    if (my.cluster$cluster[d] == 3 && (my.cluster$cluster[d + 1] !=  my.cluster$cluster[d])){
                        transition.obs3[j3o] <- my.cluster$cluster[d + 1]
                        j3o <- j3o + 1
                    }
                    if (my.cluster$cluster[d] == 4 && (my.cluster$cluster[d + 1] !=  my.cluster$cluster[d])){
                        transition.obs4[j4o] <- my.cluster$cluster[d + 1]
                        j4o <- j4o +1
                    }
                    
                }
                
                trans.obs <- matrix(NA,4,4 , dimnames= list(c("cluster1.obs", "cluster2.obs", "cluster3.obs", "cluster4.obs"),c("cluster1.obs","cluster2.obs","cluster3.obs","cluster4.obs")))
                trans.obs[1,2] <- length(which(transition.obs1 == 2))
                trans.obs[1,3] <- length(which(transition.obs1 == 3))
                trans.obs[1,4] <- length(which(transition.obs1 == 4))
                
                trans.obs[2,1] <- length(which(transition.obs2 == 1))
                trans.obs[2,3] <- length(which(transition.obs2 == 3))
                trans.obs[2,4] <- length(which(transition.obs2 == 4))

                trans.obs[3,1] <- length(which(transition.obs3 == 1))
                trans.obs[3,2] <- length(which(transition.obs3 == 2))
                trans.obs[3,4] <- length(which(transition.obs3 == 4))

                trans.obs[4,1] <- length(which(transition.obs4 == 1))
                trans.obs[4,2] <- length(which(transition.obs4 == 2))
                trans.obs[4,3] <- length(which(transition.obs4 == 3))

                trans.tot.obs <- apply(trans.obs,1,sum,na.rm=T)
                for(i in 1:4) trans.obs[i,] <- trans.obs[i,]/trans.tot.obs[i]
                
                # compute the observed interannual monthly variability:
                freq.obs1 <- freq.obs2 <- freq.obs3 <- freq.obs4 <- c()
                
                for(y in year.start:year.end){
                    # for both reanalysis and S4, the time order is always: year1day1, year1day2, ..., year1day31, year2day1, year2day2, ..., year2day28, etc,
                    freq.obs1[y-year.start+1] <- length(which(my.cluster$cluster[(y-year.start)*n.days.month + (1:n.days.month)] == 1))
                    freq.obs2[y-year.start+1] <- length(which(my.cluster$cluster[(y-year.start)*n.days.month + (1:n.days.month)] == 2))
                    freq.obs3[y-year.start+1] <- length(which(my.cluster$cluster[(y-year.start)*n.days.month + (1:n.days.month)] == 3))
                    freq.obs4[y-year.start+1] <- length(which(my.cluster$cluster[(y-year.start)*n.days.month + (1:n.days.month)] == 4))
                }
      
                sd.freq.obs1 <- sd(freq.obs1) / n.days.month
                sd.freq.obs2 <- sd(freq.obs2) / n.days.month
                sd.freq.obs3 <- sd(freq.obs3) / n.days.month
                sd.freq.obs4 <- sd(freq.obs4) / n.days.month
                
                wr1y.obs <- wr1y;   wr2y.obs <- wr2y;   wr3y.obs <- wr3y;   wr4y.obs <- wr4y # observed frecuencies of the regimes
                
                regimes.obs <- c(cluster1.name, cluster2.name, cluster3.name, cluster4.name)
                
                if(length(unique(regimes.obs)) < 4) stop("Two or more names of the weather types in the reanalsyis input file are repeated!")

                if(identical(rev(lat),lat.forecast)) {lat.forecast <- rev(lat.forecast); print("Warning: forecast latitudes are in the opposite order than renalysis's")}
                if(!identical(lat, lat.forecast)) stop("forecast latitudes are different from reanalysis latitudes!")
                if(!identical(lon, lon.forecast)) stop("forecast longitude are different from reanalysis longitudes!")
                
                cluster.corr <- array(NA, c(4,4), dimnames=list(c("cluster1.sim","cluster2.sim","cluster3.sim","cluster4.sim"), regimes.obs))
                cluster.corr[1,1] <- cor(as.vector(cluster1.sim), as.vector(pslwr1mean))
                cluster.corr[1,2] <- cor(as.vector(cluster1.sim), as.vector(pslwr2mean))
                cluster.corr[1,3] <- cor(as.vector(cluster1.sim), as.vector(pslwr3mean))
                cluster.corr[1,4] <- cor(as.vector(cluster1.sim), as.vector(pslwr4mean))
                cluster.corr[2,1] <- cor(as.vector(cluster2.sim), as.vector(pslwr1mean))
                cluster.corr[2,2] <- cor(as.vector(cluster2.sim), as.vector(pslwr2mean))
                cluster.corr[2,3] <- cor(as.vector(cluster2.sim), as.vector(pslwr3mean))
                cluster.corr[2,4] <- cor(as.vector(cluster2.sim), as.vector(pslwr4mean))
                cluster.corr[3,1] <- cor(as.vector(cluster3.sim), as.vector(pslwr1mean))
                cluster.corr[3,2] <- cor(as.vector(cluster3.sim), as.vector(pslwr2mean))
                cluster.corr[3,3] <- cor(as.vector(cluster3.sim), as.vector(pslwr3mean))
                cluster.corr[3,4] <- cor(as.vector(cluster3.sim), as.vector(pslwr4mean))
                cluster.corr[4,1] <- cor(as.vector(cluster4.sim), as.vector(pslwr1mean))
                cluster.corr[4,2] <- cor(as.vector(cluster4.sim), as.vector(pslwr2mean))
                cluster.corr[4,3] <- cor(as.vector(cluster4.sim), as.vector(pslwr3mean))
                cluster.corr[4,4] <- cor(as.vector(cluster4.sim), as.vector(pslwr4mean))

                # associate each simulated cluster to one observed regime:
                max1 <- which(cluster.corr == max(cluster.corr), arr.ind=T)
                cluster.corr2 <- cluster.corr
                cluster.corr2[max1[1],] <- -999 # set this row to negative values so it won't be found as a maximum anymore
                cluster.corr2[,max1[2]] <- -999 # set this column to negative values so it won't be found as a maximum anymore
                max2 <- which(cluster.corr2 == max(cluster.corr2), arr.ind=T)
                cluster.corr3 <- cluster.corr2
                cluster.corr3[max2[1],] <- -999
                cluster.corr3[,max2[2]] <- -999 
                max3 <- which(cluster.corr3 == max(cluster.corr3), arr.ind=T)
                cluster.corr4 <- cluster.corr3
                cluster.corr4[max3[1],] <- -999
                cluster.corr4[,max3[2]] <- -999 
                max4 <- which(cluster.corr4 == max(cluster.corr4), arr.ind=T)
                
                seq.max1 <- c(max1[1],max2[1],max3[1],max4[1])
                seq.max2 <- c(max1[2],max2[2],max3[2],max4[2])

                cluster1.regime <- seq.max2[which(seq.max1 == 1)]
                cluster2.regime <- seq.max2[which(seq.max1 == 2)]
                cluster3.regime <- seq.max2[which(seq.max1 == 3)]
                cluster4.regime <- seq.max2[which(seq.max1 == 4)]
                
                cluster1.name <- regimes.obs[cluster1.regime]
                cluster2.name <- regimes.obs[cluster2.regime]
                cluster3.name <- regimes.obs[cluster3.regime]
                cluster4.name <- regimes.obs[cluster4.regime]

                regimes.sim <- c(cluster1.name, cluster2.name, cluster3.name, cluster4.name)
                cluster.corr2 <- array(cluster.corr, c(4,4), dimnames=list(regimes.sim, regimes.obs))

                spat.cor1 <- cluster.corr[1,seq.max2[which(seq.max1 == 1)]]
                spat.cor2 <- cluster.corr[2,seq.max2[which(seq.max1 == 2)]]
                spat.cor3 <- cluster.corr[3,seq.max2[which(seq.max1 == 3)]]
                spat.cor4 <- cluster.corr[4,seq.max2[which(seq.max1 == 4)]]

                # measure persistence for the reanalysis dataset:
                my.cluster.vector <- my.cluster[[1]]

                sequ1 <- sequ2 <- sequ3 <- sequ4 <- rep(0,10000)
                i1 <- i2 <- i3 <- i4 <- 1

                if(my.cluster.vector[1] != 1) i1 <- 0
                if(my.cluster.vector[1] == 1) sequ1[i1] <- sequ1[i1]+1
                if(my.cluster.vector[1] != 2) i2 <- 0
                if(my.cluster.vector[1] == 2) sequ2[i2] <- sequ2[i2]+1
                if(my.cluster.vector[1] != 3) i3 <- 0
                if(my.cluster.vector[1] == 3) sequ3[i3] <- sequ3[i3]+1
                if(my.cluster.vector[1] != 4) i4 <- 0
                if(my.cluster.vector[1] == 4) sequ4[i4] <- sequ4[i4]+1
                
                n.dd <- length(my.cluster.vector)
                for(d in 1:(n.dd-1)){
                    if(my.cluster.vector[d] != 1 && my.cluster.vector[d+1] == 1)  i1 <- i1+1
                    if(my.cluster.vector[d] == 1) sequ1[i1] <- sequ1[i1]+1
                    
                    if(my.cluster.vector[d] != 2 && my.cluster.vector[d+1] == 2)  i2 <- i2+1
                    if(my.cluster.vector[d] == 2) sequ2[i2] <- sequ2[i2]+1
                    
                    if(my.cluster.vector[d] != 3 && my.cluster.vector[d+1] == 3)  i3 <- i3+1
                    if(my.cluster.vector[d] == 3) sequ3[i3] <- sequ3[i3]+1
                    
                    if(my.cluster.vector[d] != 4 && my.cluster.vector[d+1] == 4)  i4 <- i4+1
                    if(my.cluster.vector[d] == 4) sequ4[i4] <- sequ4[i4]+1
                }

                if(my.cluster.vector[n.dd] == 1) sequ1[i1] <- sequ1[i1]+1
                if(my.cluster.vector[n.dd] == 2) sequ2[i2] <- sequ2[i2]+1
                if(my.cluster.vector[n.dd] == 3) sequ3[i3] <- sequ3[i3]+1
                if(my.cluster.vector[n.dd] == 4) sequ4[i4] <- sequ4[i4]+1

                persObs1 <- mean(sequ1[which(sequ1 != 0)])
                persObs2 <- mean(sequ2[which(sequ2 != 0)])
                persObs3 <- mean(sequ3[which(sequ3 != 0)])
                persObs4 <- mean(sequ4[which(sequ4 != 0)])
                
                ### insert here all the manual corrections to the regime assignation due to misasignment in the automatic selection:
                ### forecast month: September
                #if(p == 9 && lead.month == 0) { cluster1.name <- "Blocking"; cluster2.name <- "Atl.Ridge"; cluster3.name <- "NAO-"; cluster4.name <- "NAO+"}
                #if(p == 8 && lead.month == 1) { cluster1.name <- "NAO+"; cluster2.name <- "NAO-"; cluster3.name <- "Blocking"; cluster4.name <- "Atl.Ridge"}
                #if(p == 6 && lead.month == 3) { cluster1.name <- "Atl.Ridge"; cluster2.name <- "NAO+"}
                #if(p == 4 && lead.month == 5) { cluster1.name <- "Blocking"; cluster2.name <- "NAO+"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO-"}

                if(p == 9 && lead.month == 0) { cluster1.name <- "Blocking"; cluster2.name <- "Atl.Ridge"; cluster3.name <- "NAO-"; cluster4.name <- "NAO+" }
                if(p == 8 && lead.month == 1) { cluster1.name <- "NAO+"; cluster2.name <- "NAO-"; cluster3.name <- "Blocking"; cluster4.name <- "Atl.Ridge" }
                if(p == 7 && lead.month == 2) { cluster1.name <- "Atl.Ridge"; cluster2.name <- "Blocking"; cluster3.name <- "NAO-"; cluster4.name <- "NAO+" }
                if(p == 6 && lead.month == 3) { cluster1.name <- "Atl.Ridge"; cluster2.name <- "NAO-"; cluster3.name <- "NAO+"; cluster4.name <- "Blocking" }
                if(p == 5 && lead.month == 4) { cluster1.name <- "Atl.Ridge"; cluster2.name <- "NAO+"; cluster3.name <- "NAO-"; cluster4.name <- "Blocking" }
                if(p == 4 && lead.month == 5) { cluster1.name <- "Blocking"; cluster2.name <- "NAO+"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO-" }
                if(p == 3 && lead.month == 6) { cluster2.name <- "NAO-"; cluster3.name <- "Atl.Ridge"} # cluster1.name <- "Blocking";  cluster4.name <- "NAO+"

                # regimes.sim # for the debug
                
                ### forecast month: October
                #if(p == 4 && lead.month == 6) { cluster1.name <- "Atl.Ridge"; cluster2.name <- "Blocking"; cluster3.name <- "NAO+"; cluster4.name <- "NAO-"}
                #if(p == 5 && lead.month == 5) { cluster1.name <- "NAO+"; cluster2.name <- "Blocking"; cluster3.name <- "NAO-"; cluster4.name <- "Atl.Ridge"}
                #if(p == 7 && lead.month == 3) { cluster1.name <- "NAO-"; cluster2.name <- "Atl.Ridge"; cluster3.name <- "Blocking"; cluster4.name <- "NAO+"}
                #if(p == 8 && lead.month == 2) { cluster1.name <- "Blocking"; cluster2.name <- "NAO-"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO+"}
                #if(p == 9 && lead.month == 1) { cluster1.name <- "NAO-"; cluster2.name <- "Blocking"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO+"}

                if(p == 10 && lead.month == 0) { cluster1.name <- "Blocking"; cluster2.name <- "NAO+"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO-"}
                if(p == 9 && lead.month == 1) { cluster1.name <- "NAO-"; cluster2.name <- "Blocking"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO+"}
                if(p == 8 && lead.month == 2) { cluster1.name <- "Blocking"; cluster2.name <- "NAO-"; cluster3.name <- "Atl.Ridge"; cluster4.name <- "NAO+"}
                if(p == 7 && lead.month == 3) { cluster1.name <- "NAO-"; cluster2.name <- "Atl.Ridge"; cluster3.name <- "Blocking"; cluster4.name <- "NAO+"}
                if(p == 6 && lead.month == 4) { cluster1.name <- "NAO+"; cluster2.name <- "Blocking"; cluster3.name <- "NAO-"; cluster4.name <- "Atl.Ridge"}

                ### forecast month: November
                #if(p == 6 && lead.month == 5) { cluster2.name <- "Atl.Ridge"; cluster3.name <- "NAO+"; cluster4.name <- "Blocking"}
                #if(p == 7 && lead.month == 4) { cluster1.name <- "NAO+"; cluster2.name <- "Blocking"; cluster4.name <- "Atl.Ridge"}
                #if(p == 8 && lead.month == 3) { cluster2.name <- "Blocking"; cluster4.name <- "Atl.Ridge"}
                #if(p == 9 && lead.month == 2) { cluster2.name <- "Atl.Ridge"; cluster3.name <- "NAO+"; cluster4.name <- "Blocking"}
                #if(p == 10 && lead.month == 1) { cluster2.name <- "Blocking"; cluster4.name <- "Atl.Ridge"}
                                
                regimes.sim2 <- c(cluster1.name, cluster2.name, cluster3.name, cluster4.name) # Simulated regimes after the manual correction
                #regimes.obs <- c(cluster1.name, cluster2.name, cluster3.name, cluster4.name) # already defined above, included only as reference

                order.sim <- match(regimes.sim2, orden)
                order.obs <- match(regimes.obs, orden)

                clusters.sim <- list(cluster1.sim, cluster2.sim, cluster3.sim, cluster4.sim)
                clusters.obs <- list(pslwr1mean, pslwr2mean, pslwr3mean, pslwr4mean)
                
                # recompute the spatial correlations, because they might have changed because of the manual corrections above:
                spat.cor1 <- cor(as.vector(clusters.sim[[which(order.sim == 1)]]), as.vector(clusters.obs[[which(order.obs == 1)]]))
                spat.cor2 <- cor(as.vector(clusters.sim[[which(order.sim == 2)]]), as.vector(clusters.obs[[which(order.obs == 2)]]))
                spat.cor3 <- cor(as.vector(clusters.sim[[which(order.sim == 3)]]), as.vector(clusters.obs[[which(order.obs == 3)]]))
                spat.cor4 <- cor(as.vector(clusters.sim[[which(order.sim == 4)]]), as.vector(clusters.obs[[which(order.obs == 4)]]))

                if(composition == 'impact' && impact.data == TRUE) {
                   clusters.sim.imp <-  list(cluster1.sim.imp, cluster2.sim.imp, cluster3.sim.imp, cluster4.sim.imp)
                   #clusters.sim.imp.pv <-  list(cluster1.sim.imp.pv, cluster2.sim.imp.pv, cluster3.sim.imp.pv, cluster4.sim.imp.pv)

                   clusters.obs.imp <- list(varwr1mean, varwr2mean, varwr3mean, varwr4mean)
                   #clusters.obs.imp.pv <- list(pvalue1, pvalue2, pvalue3, pvalue4)

                   cor.imp1 <- cor(as.vector(clusters.sim.imp[[which(order.sim == 1)]]), as.vector(clusters.obs.imp[[which(order.obs == 1)]]))
                   cor.imp2 <- cor(as.vector(clusters.sim.imp[[which(order.sim == 2)]]), as.vector(clusters.obs.imp[[which(order.obs == 2)]]))
                   cor.imp3 <- cor(as.vector(clusters.sim.imp[[which(order.sim == 3)]]), as.vector(clusters.obs.imp[[which(order.obs == 3)]]))
                   cor.imp4 <- cor(as.vector(clusters.sim.imp[[which(order.sim == 4)]]), as.vector(clusters.obs.imp[[which(order.obs == 4)]]))

                }

                load(file=paste0(work.dir,"/",fields.name,"_",my.period[p],"_leadmonth_",lead.month,"_","psl",".RData")) # reload forecast cluster time series and mean psl data
                load(file=paste0(work.dir,"/",fields.name,"_",my.period[p],"_leadmonth_",lead.month,"_",var.name[var.num],".RData")) # reload mean var data for impact maps
                
                if(var.num != var.num.orig) var.num <- var.num.orig # ripristinate original values of this script
                if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
                if(!identical(period.orig, period)) period <- period.orig
                if(p.orig != p) p <- p.orig
                if(identical(rev(lat),lat.forecast)) lat <- rev(lat) # ripristinate right lat order
                if(work.dir != work.dir.orig) work.dir <- work.dir.orig # ripristinate original values of this script

            } # close for on 'forecast.name'

            if(fields.name == rean.name || (fields.name == forecast.name && n.map == 7)){
                if(save.names){
                    save(cluster1.name, cluster2.name, cluster3.name, cluster4.name, file=paste0(rean.dir,"/",fields.name,"_", my.period[p],"_","ClusterNames",".RData"))
                    
                } else { # in this case, we load the cluster names from the file already saved, if regimes come from a reanalysis:
                    load(paste0(rean.dir,"/",rean.name,"_", my.period[p],"_","ClusterNames",".RData"))
                    
                    if(var.num != var.num.orig) var.num <- var.num.orig # ripristinate original values of this script
                    if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
                }
            }
            
            # assign to each cluster its regime:
            #if(ordering == FALSE && (fields.name == rean.name || (fields.name == forecast.name && n.map == 7))){
            if(ordering == FALSE){
                cluster1 <- 1; cluster2 <- 2; cluster3 <- 3; cluster4 <- 4
            } else {
                cluster1 <- which(orden == cluster1.name)
                cluster2 <- which(orden == cluster2.name)
                cluster3 <- which(orden == cluster3.name)
                cluster4 <- which(orden == cluster4.name)
            }
            
            assign(paste0("map",cluster1), pslwr1mean)
            assign(paste0("map",cluster2), pslwr2mean)
            assign(paste0("map",cluster3), pslwr3mean)
            assign(paste0("map",cluster4), pslwr4mean)

            assign(paste0("fre",cluster1), wr1y)
            assign(paste0("fre",cluster2), wr2y)
            assign(paste0("fre",cluster3), wr3y)
            assign(paste0("fre",cluster4), wr4y)

            #assign(paste0("withinss",cluster1), my.cluster[[p]]$withinss[1]/my.cluster[[p]]$size[1])
            #assign(paste0("withinss",cluster2), my.cluster[[p]]$withinss[2]/my.cluster[[p]]$size[2])
            #assign(paste0("withinss",cluster3), my.cluster[[p]]$withinss[3]/my.cluster[[p]]$size[3])
            #assign(paste0("withinss",cluster4), my.cluster[[p]]$withinss[4]/my.cluster[[p]]$size[4])
            
            if(impact.data == TRUE && (composition == "simple" || composition == "single.impact" || composition == 'impact')){
                assign(paste0("imp",cluster1), varwr1mean)
                assign(paste0("imp",cluster2), varwr2mean)
                assign(paste0("imp",cluster3), varwr3mean)
                assign(paste0("imp",cluster4), varwr4mean)

                assign(paste0("sig",cluster1), pvalue1)
                assign(paste0("sig",cluster2), pvalue2)
                assign(paste0("sig",cluster3), pvalue3)
                assign(paste0("sig",cluster4), pvalue4)
            }
            
            if(fields.name == forecast.name && n.map < 7){    
                assign(paste0("freMax",cluster1), wr1yMax)
                assign(paste0("freMax",cluster2), wr2yMax)
                assign(paste0("freMax",cluster3), wr3yMax)
                assign(paste0("freMax",cluster4), wr4yMax)

                assign(paste0("freMin",cluster1), wr1yMin)
                assign(paste0("freMin",cluster2), wr2yMin)
                assign(paste0("freMin",cluster3), wr3yMin)
                assign(paste0("freMin",cluster4), wr4yMin)

                #assign(paste0("spatial.cor",cluster1), spat.cor1)
                #assign(paste0("spatial.cor",cluster2), spat.cor2)
                #assign(paste0("spatial.cor",cluster3), spat.cor3)
                #assign(paste0("spatial.cor",cluster4), spat.cor4)

                assign(paste0("persist",cluster1), pers1)
                assign(paste0("persist",cluster2), pers2)
                assign(paste0("persist",cluster3), pers3)
                assign(paste0("persist",cluster4), pers4)

                clusters.all <- c(cluster1, cluster2, cluster3, cluster4)
                ordered.sequence <- c(which(clusters.all == 1), which(clusters.all == 2), which(clusters.all == 3), which(clusters.all == 4))
                
                assign(paste0("transSim",cluster1), unname(trans.sim[1,ordered.sequence]))
                assign(paste0("transSim",cluster2), unname(trans.sim[2,ordered.sequence]))
                assign(paste0("transSim",cluster3), unname(trans.sim[3,ordered.sequence]))
                assign(paste0("transSim",cluster4), unname(trans.sim[4,ordered.sequence]))

                cluster1.obs <- which(orden == regimes.obs[1])
                cluster2.obs <- which(orden == regimes.obs[2])
                cluster3.obs <- which(orden == regimes.obs[3])
                cluster4.obs <- which(orden == regimes.obs[4])

                clusters.all.obs <- c(cluster1.obs, cluster2.obs, cluster3.obs, cluster4.obs)
                ordered.sequence.obs <- c(which(clusters.all.obs == 1), which(clusters.all.obs == 2), which(clusters.all.obs == 3), which(clusters.all.obs == 4))
                
                assign(paste0("freObs",cluster1.obs), wr1y.obs)
                assign(paste0("freObs",cluster2.obs), wr2y.obs)
                assign(paste0("freObs",cluster3.obs), wr3y.obs)
                assign(paste0("freObs",cluster4.obs), wr4y.obs)

                assign(paste0("persistObs",cluster1.obs), persObs1)
                assign(paste0("persistObs",cluster2.obs), persObs2)
                assign(paste0("persistObs",cluster3.obs), persObs3)
                assign(paste0("persistObs",cluster4.obs), persObs4)

                assign(paste0("transObs",cluster1.obs), unname(trans.obs[1,ordered.sequence.obs]))
                assign(paste0("transObs",cluster2.obs), unname(trans.obs[2,ordered.sequence.obs]))
                assign(paste0("transObs",cluster3.obs), unname(trans.obs[3,ordered.sequence.obs]))
                assign(paste0("transObs",cluster4.obs), unname(trans.obs[4,ordered.sequence.obs]))

            }
            
            # position of long values of Europe only (without the Atlantic Sea and America):
            #if(fields.name == "ERA-Interim") EU <- c(1:which(lon >= lon.max)[1], (length(lon)-30):length(lon))  # restrict area to continental europe only
            EU <- c(1:which(lon >= lon.max)[1], (which(lon >= 337)[1]:length(lon)))  # restrict area to continental europe only

            # breaks and colors of the geopotential fields:
            if(psl == "g500"){
                #my.brks <- c(-300,-199:200,300) # % Mean anomaly of a WR
                my.brks <- c(-300,seq(-200,200,10),300) # % Mean anomaly of a WR
                my.brks2 <- c(-300,seq(-200,200,10),300) # % Mean anomaly of a WR for the contour lines
                #my.cols <- colorRampPalette((brewer.pal(9,"PuBu")))(length(my.brks)-1)
                values.to.plot <- c(-200, -100, 0, 100, 200)  # values we want to show in the labels
            }

            if(psl == "psl"){
                my.brks <- c(seq(-19,-1,2),0,seq(1,19,2)) # % Mean anomaly of a WR
                #  my.brks <- c(seq(-19,-1,2),0,seq(1,19,2)) # % Mean anomaly of a WR

                my.brks2 <- my.brks #c(seq(-20,20,2)) # % Mean anomaly of a WR for the contour lines
                values.to.plot <- my.brks #c(-17,-15,-13,-11,-9,-7,-5,-3,-1,0,1,3,5,7,9,11,13,15,17)
            }

            my.cols <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks)-1)    # blue--white--red colors
            my.cols[floor(length(my.cols)/2)] <- my.cols[floor(length(my.cols)/2)+1] <- "white"
            
            # breaks and colors of the impact maps (valid both for Wind speed anomalies and Temperature anomalies):
            #my.brks.var <- c(-20,seq(-10,-3,1),seq(-2.9,2.9,0.1),seq(3,10,1),20) # % Mean anomaly of a WR
            #my.brks.var <- c(-20,-2,-1.5,-1,-0.5,-0.2,0.2,0.5,1,1.5,2,20) # % Mean anomaly of a WR
            #my.brks.var <- c(-20,seq(-3,3,0.5),20) # % Mean anomaly of a WR
            if(var.name[var.num] == "sfcWind") {
                my.brks.var <- seq(-3,3,0.5)
                values.to.plot2 <- c(-3,-2,-1,0,1,2,3)
            }
            if(var.name[var.num] == "tas") {
                my.brks.var <- seq(-5,5,1) 
                values.to.plot2 <- my.brks.var #c(-5,-3,-1,0,1,3,5)
            }
            
            #my.cols <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks)-1)  # blue--yellow-red colors
            my.cols.var <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks.var)-1)    # blue--white--red colors
            #my.cols.var[floor(length(my.cols.var)/2)] <- my.cols.var[floor(length(my.cols.var)/2)+1] <- "white"

            freq.max=100
            if(fields.name == forecast.name){
                pos.years.present <- match(year.start:year.end, years)
                years.missing <- which(is.na(pos.years.present))
                fre1.NA <- fre2.NA <- fre3.NA <- fre4.NA <- freMax1.NA <- freMax2.NA <- freMax3.NA <- freMax4.NA <- freMin1.NA <- freMin2.NA <- freMin3.NA <- freMin4.NA <- c()              
                k <- 0
                for(y in year.start:year.end) {
                    if(y %in% (years.missing + year.start - 1)) {
                        fre1.NA <- c(fre1.NA,NA); fre2.NA <- c(fre2.NA,NA); fre3.NA <- c(fre3.NA,NA); fre4.NA <- c(fre4.NA,NA)
                        freMax1.NA <- c(freMax1.NA,NA); freMax2.NA <- c(freMax2.NA,NA); freMax3.NA <- c(freMax3.NA,NA); freMax4.NA <- c(freMax4.NA,NA)
                        freMin1.NA <- c(freMin1.NA,NA); freMin2.NA <- c(freMin2.NA,NA); freMin3.NA <- c(freMin3.NA,NA); freMin4.NA <- c(freMin4.NA,NA)
                        k=k+1
                    } else {
                        fre1.NA <- c(fre1.NA,fre1[y - year.start + 1 - k]); fre2.NA <- c(fre2.NA,fre2[y - year.start + 1 - k])
                        fre3.NA <- c(fre3.NA,fre3[y - year.start + 1 - k]); fre4.NA <- c(fre4.NA,fre4[y - year.start + 1 - k])
                        freMax1.NA <- c(freMax1.NA,freMax1[y - year.start + 1 - k]); freMax2.NA <- c(freMax2.NA,freMax2[y - year.start + 1 - k])
                        freMax3.NA <- c(freMax3.NA,freMax3[y - year.start + 1 - k]); freMax4.NA <- c(freMax4.NA,freMax4[y - year.start + 1 - k])
                        freMin1.NA <- c(freMin1.NA,freMin1[y - year.start + 1 - k]); freMin2.NA <- c(freMin2.NA,freMin2[y - year.start + 1 - k])
                        freMin3.NA <- c(freMin3.NA,freMin3[y - year.start + 1 - k]); freMin4.NA <- c(freMin4.NA,freMin4[y - year.start + 1 - k])            
                    }    
                }

                sp.cor1 <- round(cor(fre1.NA,freObs1, use="complete.obs"),2)            
                sp.cor2 <- round(cor(fre2.NA,freObs2, use="complete.obs"),2)            
                sp.cor3 <- round(cor(fre3.NA,freObs3, use="complete.obs"),2)            
                sp.cor4 <- round(cor(fre4.NA,freObs4, use="complete.obs"),2)

            } else {
                fre1.NA <- fre1; fre2.NA <- fre2; fre3.NA <- fre3; fre4.NA <- fre4
            }
            
            
            # Visualize the composition with the 3 graphs for all the 4 regimes: its pressure fields, its impact on var and its frequency series:
            if(composition == "simple"){
                if(!as.pdf && fields.name == rean.name) png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],".png"),width=3000,height=3700)
                if(!as.pdf && fields.name == forecast.name) png(filename=paste0(work.dir,"/",fields.name,"_",my.period[p],"_leadmonth_",lead.month,"_",var.name[var.num],".png"),width=3000,height=3700)
                
                plot.new()

                # Sheet title:
                par(fig=c(0, 1, 0.94, 0.98), new=TRUE)
                if(fields.name == forecast.name) {forecast.title <- paste0(" startdate and ",lead.month," forecast month ")} else {forecast.title <- ""}
                mtext(paste0(fields.name, " Weather Regimes for ", my.period[p], forecast.title," (",year.start,"-",year.end,")"), cex=5.5, font=2)
                
                # Centroid maps:
                map.xpos <- 0
                map.width <- 0.46
                par(fig=c(map.xpos + 0.003, map.xpos + map.width - 0.003, 0.745, 0.925), new=TRUE)
                PlotEquiMap2(rescale(map1,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map1), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, xlabel.dist=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, 0.51, 0.69), new=TRUE)
                PlotEquiMap2(rescale(map2,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, 0.275, 0.455), new=TRUE)
                PlotEquiMap2(rescale(map3,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, 0.04, 0.22), new=TRUE)
                PlotEquiMap2(rescale(map4,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")

                ## # for the debug:
                ## obs <- read.table("/scratch/Earth/ncortesi/RESILIENCE/Regimes/NAO_series.txt")
                ## mes <- p
                ## fre.obs <- obs$V3[seq(mes,12*35,12)] # get the ovserved freuency of the NAO
                ## #plot(1981:2015,fre.obs,type="l")
                ## #lines(1981:2015,fre1,type="l",col="red")
                ## #lines(1981:2015,fre2,type="l",col="blue")
                ## #lines(1981:2015,fre4,type="l",col="green")
                ## cor1 <- cor(fre.obs, fre1)
                ## cor2 <- cor(fre.obs, fre2)
                ## cor3 <- cor(fre.obs, fre3)
                ## cor4 <- cor(fre.obs, fre4)
                ## round(c(cor1,cor2,cor3,cor4),2)
                
                # Legend centroid maps:
                legend1.xpos <- 0.10
                legend1.width <- 0.30
                legend1.cex <- 2
                my.subset <- match(values.to.plot, my.brks)    
                par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0.719, 0.744), new=TRUE)  # syntax fig=c(xmin, xmax, ymin, ymax)
                ColorBar3(my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, subset=my.subset)
                if(psl=="g500") {mtext(side=4," m", cex=2.5)} else {mtext(side=4," hPa", cex=legend1.cex)}
                par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0.485, 0.508), new=TRUE)
                ColorBar3(my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, subset=my.subset)
                if(psl=="g500") {mtext(side=4," m", cex=2.5)} else {mtext(side=4," hPa", cex=legend1.cex)}
                par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0.250, 0.273), new=TRUE)
                ColorBar3(my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, subset=my.subset)
                if(psl=="g500") {mtext(side=4," m", cex=2.5)} else {mtext(side=4," hPa", cex=legend1.cex)}
                par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0.015, 0.038), new=TRUE)
                ColorBar3(my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, subset=my.subset)
                if(psl=="g500") {mtext(side=4," m", cex=2.5)} else {mtext(side=4," hPa", cex=legend1.cex)}

                # Subtitle centroid maps:
                map.title.xpos <- 0.76
                map.title.width <- 0.2
                par(fig=c(map.title.xpos, map.title.xpos + map.title.width, 0.728, 0.729), new=TRUE)
                mtext("year", cex=3)
                par(fig=c(map.title.xpos, map.title.xpos + map.title.width, 0.494, 0.495), new=TRUE)
                mtext("year", cex=3)
                par(fig=c(map.title.xpos, map.title.xpos + map.title.width, 0.260, 0.261), new=TRUE)
                mtext("year", cex=3)
                par(fig=c(map.title.xpos, map.title.xpos + map.title.width, 0.026, 0.027), new=TRUE)
                mtext("year", cex=3)

                # Title Centroid Maps:
                title1.xpos <- 0.02
                title1.width <- 0.4
                par(fig=c(title1.xpos, title1.xpos + title1.width, 0.9305, 0.9355), new=TRUE)
                mtext(paste0(regime1.name,": ", psl.name, " Anomaly"), font=2, cex=4)
                par(fig=c(title1.xpos, title1.xpos + title1.width, 0.6955, 0.7005), new=TRUE)
                mtext(paste0(regime2.name,": ", psl.name, " Anomaly"), font=2, cex=4)
                par(fig=c(title1.xpos, title1.xpos + title1.width, 0.4605, 0.4655), new=TRUE)
                mtext(paste0(regime3.name,": ", psl.name, " Anomaly"), font=2, cex=4)
                par(fig=c(title1.xpos, title1.xpos + title1.width, 0.2255, 0.2305), new=TRUE)
                mtext(paste0(regime4.name,": ", psl.name, " Anomaly"), font=2, cex=4)
                
                # Impact maps:
                if(impact.data == TRUE ){ # it won't enter here never because we are already inside an if con composition="simple"
                    impact.xpos <- 0.47
                    impact.width <- 0.26    
                    par(fig=c(impact.xpos, impact.xpos + impact.width, 0.745, 0.925), new=TRUE)
                    PlotEquiMap2(rescale(imp1[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig1[,EU] < 0.05), cex.lab=1.5)
                    par(fig=c(impact.xpos, impact.xpos + impact.width, 0.51, 0.69), new=TRUE)
                    PlotEquiMap2(rescale(imp2[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig2[,EU] < 0.05), cex.lab=1.5)
                    par(fig=c(impact.xpos, impact.xpos + impact.width, 0.275, 0.455), new=TRUE)
                    PlotEquiMap2(rescale(imp3[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig3[,EU] < 0.05), cex.lab=1.5)
                    par(fig=c(impact.xpos, impact.xpos + impact.width, 0.04, 0.22), new=TRUE)
                    PlotEquiMap2(rescale(imp4[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig4[,EU] < 0.05), cex.lab=1.5)

                    # Title impact maps:
                    title2.xpos <- 0.42
                    title2.width <- 0.35
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.935, 0.940), new=TRUE)
                    mtext(paste0(regime1.name, ": Impact on ", var.name.full[var.num]), font=2, cex=4)
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.700, 0.705), new=TRUE)
                    mtext(paste0(regime2.name, ": Impact on ", var.name.full[var.num]), font=2, cex=4)
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.465, 0.470), new=TRUE)
                    mtext(paste0(regime3.name, ": Impact on ", var.name.full[var.num]), font=2, cex=4)
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.230, 0.235), new=TRUE)
                    mtext(paste0(regime4.name, ": Impact on ", var.name.full[var.num]), font=2, cex=4)

                    # Legend:
                    legend2.xpos <- 0.48
                    legend2.width <- 0.25
                    legend2.cex <- 1.8
                    
                    my.subset2 <- match(values.to.plot2, my.brks.var)
                    par(fig=c(legend2.xpos, legend2.xpos + legend2.width, 0.719 + 0.001, 0.744), new=TRUE)
                    ColorBar3(my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend2.cex, subset=my.subset2)
                    mtext(side=4,paste0(" ",var.unit[var.num]), cex=legend2.cex)
                    par(fig=c(legend2.xpos, legend2.xpos + legend2.width, 0.485, 0.508), new=TRUE)
                    ColorBar3(my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend2.cex, subset=my.subset2)
                    mtext(side=4,paste0(" ",var.unit[var.num]), cex=legend2.cex)  
                    par(fig=c(legend2.xpos, legend2.xpos + legend2.width, 0.250, 0.273), new=TRUE)
                    ColorBar3(my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend2.cex, subset=my.subset2)
                    mtext(side=4,paste0(" ",var.unit[var.num]), cex=legend2.cex)  
                    par(fig=c(legend2.xpos, legend2.xpos + legend2.width, 0.015, 0.038), new=TRUE)
                    ColorBar3(my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend2.cex, subset=my.subset2)
                    mtext(side=4,paste0(" ",var.unit[var.num]), cex=legend2.cex)  
                
                }
                
                # Frequency plots:
                barplot.xpos <- 0.75
                barplot.width <- 0.25

                par(fig=c(barplot.xpos, barplot.xpos + barplot.width, 0.745, 0.915), new=TRUE)
                if(fields.name == rean.name) barplot.freq(100*fre1.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                if(fields.name == forecast.name) barplot.freq.sim2(100*fre1.NA, 100*freMax1.NA, 100*freMin1.NA, 100*freObs1, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                
                par(fig=c(barplot.xpos, barplot.xpos + barplot.width,0.510,0.680), new=TRUE)
                if(fields.name == rean.name) barplot.freq(100*fre2.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                if(fields.name == forecast.name) barplot.freq.sim2(100*fre2.NA, 100*freMax2.NA, 100*freMin2.NA, 100*freObs2, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                
                par(fig=c(barplot.xpos, barplot.xpos + barplot.width,0.275,0.445), new=TRUE)
                if(fields.name == rean.name) barplot.freq(100*fre3.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                if(fields.name == forecast.name) barplot.freq.sim2(100*fre3.NA, 100*freMax3.NA, 100*freMin3.NA, 100*freObs3, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                
                par(fig=c(barplot.xpos, barplot.xpos + barplot.width,0.040,0.210), new=TRUE)
                if(fields.name == rean.name) barplot.freq(100*fre4.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                if(fields.name == forecast.name) barplot.freq.sim2(100*fre4.NA, 100*freMax4.NA, 100*freMin4.NA, 100*freObs4, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)

                # Title Frequency maps:
                title3.xpos <- 0.75
                title3.width <-0.25
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.935, 0.940), new=TRUE)
                mtext(paste0(regime1.name, " Frequency"), font=2, cex=4) # paste0 doesn't work inside an expression!
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.700, 0.705), new=TRUE)
                mtext(paste0(regime2.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.465, 0.470), new=TRUE)
                mtext(paste0(regime3.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.230, 0.235), new=TRUE)
                mtext(paste0(regime4.name, " Frequency"), font=2, cex=4)

                # % on Frequency plots:
                symbol.xpos <- 0.735
                symbol.width <- 0.01
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.82, 0.83), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.59, 0.60), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.35, 0.36), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.12, 0.13), new=TRUE)
                mtext("%", cex=3.3)

                # mean frequency on Frequency plots:
                if(mean.freq == TRUE){
                    symbol.xpos <- 0.9
                    symbol.width <- 0.1
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.908, 0.918), new=TRUE)
                    mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre1.NA),1),"%"))),cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.673, 0.683), new=TRUE)
                    mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre2.NA),1),"%"))),cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.44, 0.45), new=TRUE)
                    mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre3.NA),1),"%"))),cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.203, 0.213), new=TRUE)
                    mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre4.NA),1),"%"))),cex=4.5)
                }
                
                # add corr between obs.time series and ensemble mean time series on Frequency plots:
                if(fields.name == forecast.name){
                    symbol.xpos <- 0.76
                    symbol.width <- 0.1
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.908, 0.918), new=TRUE)
                    sp.cor1 <- round(cor(fre1.NA,freObs1, use="complete.obs"),2)
                    mtext(paste0("r= ",sp.cor1), cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.673, 0.683), new=TRUE)
                    sp.cor2 <- round(cor(fre2.NA,freObs2, use="complete.obs"),2)
                    mtext(paste0("r= ",sp.cor2), cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.44, 0.45), new=TRUE)
                    sp.cor3 <- round(cor(fre3.NA,freObs3, use="complete.obs"),2)
                    mtext(paste0("r= ",sp.cor3), cex=4.5)
                    par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.203, 0.213), new=TRUE)
                    sp.cor4 <- round(cor(fre4.NA,freObs4, use="complete.obs"),2)
                    mtext(paste0("r= ",sp.cor4), cex=4.5)
                }

                if(!as.pdf) dev.off() # for saving 4 png

            } # close if on: composition == 'simple'

            
            # Visualize the composition with the regime anomalies for a selected forecasted month:
            if(composition == "psl"){
                # Centroid maps:
                n.map <- n.map + 1  # n.maps starts from 0
                map.width <- 0.115
                map.xpos <- 0.06 + map.width * (n.map - 1)
                map.ypos <- 0.08
                map.height <- 0.19
                map.ysep <- 0.015
                
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
                PlotEquiMap2(rescale(map1,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map1), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, xlabel.dist=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
                PlotEquiMap2(rescale(map2,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + map.height + map.ysep, map.ypos + 2*map.height + map.ysep), new=TRUE)
                PlotEquiMap2(rescale(map3,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos, map.ypos + map.height), new=TRUE)
                PlotEquiMap2(rescale(map4,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")

                # Sheet subtitles:
                par(fig=c(map.xpos, map.xpos + map.width, 0.86, 0.90), new=TRUE)
                if(n.map < 8) {
                    mtext(paste0(my.month.short[p], " --> ", my.month.short[forecast.month]), cex=5.5, font=2)
                } else{
                    mtext(paste0(rean.name," ", my.month.short[forecast.month]), cex=5.5, font=2)
                }  

            } # close if on composition == 'psl'

            # Visualize the composition if the impact maps for a selected forecasted month:
            if(composition == "impact"){
                # Centroid maps:
                n.map <- n.map + 1  # n.maps starts from 0
                map.width <- 0.115
                map.xpos <- 0.06 + map.width * (n.map - 1)
                map.ypos <- 0.08
                map.height <- 0.19
                map.ysep <- 0.015
                
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
                #PlotEquiMap2(rescale(imp1[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig1[,EU] < 0.05), cex.lab=1.5)
                PlotEquiMap2(rescale(imp1[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5)

                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
                #PlotEquiMap2(rescale(imp2[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig2[,EU] < 0.05), cex.lab=1.5)
                PlotEquiMap2(rescale(imp2[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5)

                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + map.height + map.ysep, map.ypos + 2*map.height + map.ysep), new=TRUE)
                #PlotEquiMap2(rescale(imp3[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig3[,EU] < 0.05), cex.lab=1.5)
                PlotEquiMap2(rescale(imp3[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5)

                par(fig=c(map.xpos, map.xpos + map.width, map.ypos, map.ypos + map.height), new=TRUE)
                #PlotEquiMap2(rescale(imp4[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig4[,EU] < 0.05), cex.lab=1.5)
                PlotEquiMap2(rescale(imp4[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5)

                
                # Sheet subtitles:
                par(fig=c(map.xpos, map.xpos + map.width, 0.86, 0.90), new=TRUE)
                if(n.map < 8) {
                    mtext(paste0(my.month.short[p], " --> ", my.month.short[forecast.month]), cex=5.5, font=2)
                } else{
                    mtext(paste0(rean.name," ", my.month.short[forecast.month]), cex=5.5, font=2)
                }  

            } # close if on composition == 'impact'

             # Visualize the composition if the impact maps for a selected forecasted month:
            if(composition == "single.impact"){
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_impact_NAO+.png"),width=3000,height=3700)
                PlotEquiMap2(rescale(imp1[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig1[,EU] < 0.05), cex.lab=1.5)
                dev.off()
                
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_impact_NAO-.png"),width=3000,height=3700)
                PlotEquiMap2(rescale(imp2[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig2[,EU] < 0.05), cex.lab=1.5)
                dev.off()
                
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_impact_Blocking.png"),width=3000,height=3700)
                PlotEquiMap2(rescale(imp3[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig3[,EU] < 0.05), cex.lab=1.5)
                dev.off()
                
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_impact_Atl_Ridge.png"),width=3000,height=3700)
                PlotEquiMap2(rescale(imp4[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, intxlon=10, intylat=10, drawleg=F, dots=t(sig4[,EU] < 0.05), cex.lab=1.5)
                dev.off()
            }

             # Visualize the composition if the impact maps for a selected forecasted month:
            if(composition == "single.psl"){
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_pattern_cluster1.png"),width=2000,height=1400, res=300)
                PlotEquiMap2(rescale(map1,my.brks[1],tail(my.brks,1)), lon, lat,filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1, contours=t(map1), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=0.5, xlabel.dist=0, contours.lty="F1FF1F", toptitle=paste0(my.period[p]," WithinSS=", round(withinss1/1000000,2)))
                dev.off()

                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_pattern_cluster2.png"),width=2000,height=1400, res=300)
                PlotEquiMap2(rescale(map2,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1, contours=t(map2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=0.5, xlabel.dist=0, contours.lty="F1FF1F", toptitle=paste0(my.period[p]," WithinSS=", round(withinss2/1000000,2)))
                dev.off()

                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_pattern_cluster3.png"),width=2000,height=1400, res=300)
                PlotEquiMap2(rescale(map3,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1, contours=t(map3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=0.5, xlabel.dist=0, contours.lty="F1FF1F", toptitle=paste0(my.period[p]," WithinSS=", round(withinss3/1000000,2)))
                dev.off()
                
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_pattern_cluster4.png"),width=2000,height=1400, res=300)
                PlotEquiMap2(rescale(map4,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1, contours=t(map4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=0.5, xlabel.dist=0, contours.lty="F1FF1F", toptitle=paste0(my.period[p]," WithinSS=", round(withinss4/1000000,2)))
                dev.off()

                # Legend centroid maps:
                #my.subset <- match(values.to.plot, my.brks)    
                #ColorBar3(my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, subset=my.subset)
                #mtext(side=4," hPa", cex=legend1.cex)
                
            }
            
            # Visualize the composition with the interannual frequencies for a selected forecasted month:
            if(composition == "fre"){        
                # Centroid maps:
                n.map <- n.map + 1  # n.maps starts from 0
                map.width <- 0.115
                map.xpos <- 0.06 + map.width * (n.map - 1)
                map.ypos <- 0.08
                map.height <- 0.19
                map.ysep <- 0.015

                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
                if(n.map < 8) barplot.freq.sim2(100*fre1.NA, 100*freMax1.NA, 100*freMin1.NA, 100*freObs1, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                if(n.map == 8) barplot.freq(100*fre1.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
                if(n.map < 8) if(fields.name == forecast.name) barplot.freq.sim2(100*fre2.NA, 100*freMax2.NA, 100*freMin2.NA, 100*freObs2, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                if(n.map == 8) barplot.freq(100*fre2.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                    
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos + map.height + map.ysep, map.ypos + 2*map.height + map.ysep), new=TRUE)
                if(n.map < 8) if(fields.name == forecast.name) barplot.freq.sim2(100*fre3.NA, 100*freMax3.NA, 100*freMin3.NA, 100*freObs3, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                if(n.map == 8) barplot.freq(100*fre3.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                    
                par(fig=c(map.xpos, map.xpos + map.width, map.ypos, map.ypos + map.height), new=TRUE)
                if(n.map < 8) if(fields.name == forecast.name) barplot.freq.sim2(100*fre4.NA, 100*freMax4.NA, 100*freMin4.NA, 100*freObs4, year.start, year.end, cex.y=2, cex.x=2, freq.min=-2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0), col.line="gray20", cex.r=3, cex.mean=2, cex.obs=2)
                if(n.map == 8) barplot.freq(100*fre4.NA, year.start, year.end, cex.y=2, cex.x=2, freq.max=freq.max, ylab="", mgp=c(3,1.5,0))
                    
                # Sheet subtitles:
                par(fig=c(map.xpos, map.xpos + map.width, 0.86, 0.90), new=TRUE)
                if(n.map < 8) {
                    mtext(paste0(my.month.short[p], " --> ", my.month.short[forecast.month]), cex=5.5, font=2)
                } else{
                    mtext(paste0(rean.name," ", my.month.short[forecast.month]), cex=5.5, font=2)
                }

                # % on Frequency plots:
                par(fig=c(map.xpos - 0.006, map.xpos, map.ypos + 3.9*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(map.xpos - 0.006, map.xpos, map.ypos + 2.9*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(map.xpos - 0.006, map.xpos, map.ypos + 1.9*map.height + 1*map.ysep, map.ypos + 2*map.height + 1*map.ysep), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(map.xpos - 0.006, map.xpos, map.ypos + 0.9*map.height + 0*map.ysep, map.ypos + 1*map.height + 0*map.ysep), new=TRUE)
                mtext("%", cex=3.3)

                # mean frequency on Frequency plots:
                par(fig=c(map.xpos + 0.02, map.xpos + 0.04, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 3.1*map.height + 3*map.ysep), new=TRUE)
                mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre1.NA),1),"%"))),cex=3.5)
                par(fig=c(map.xpos + 0.02, map.xpos + 0.04, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 2.1*map.height + 2*map.ysep), new=TRUE)
                mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre2.NA),1),"%"))),cex=3.5)
                par(fig=c(map.xpos + 0.02, map.xpos + 0.04, map.ypos + 1*map.height + 1*map.ysep, map.ypos + 1.1*map.height + 1*map.ysep), new=TRUE)
                mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre3.NA),1),"%"))),cex=3.5)
                par(fig=c(map.xpos + 0.02, map.xpos + 0.04, map.ypos + 0*map.height + 0*map.ysep, map.ypos + 0.1*map.height + 0*map.ysep), new=TRUE)
                mtext(bquote(bar(nu) == .(paste0(round(mean(100*fre4.NA),1),"%"))),cex=3.5)

                # add corr between obs.time series and ensemble mean time series on Frequency plots:
                if(n.map < 8){
                 par(fig=c(map.xpos + map.width - 0.04, map.xpos + map.width - 0.02, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 3.1*map.height + 3*map.ysep), new=TRUE)
                 mtext(paste0("r= ",sp.cor1), cex=3.5)
                 par(fig=c(map.xpos + map.width - 0.04, map.xpos + map.width - 0.02, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 2.1*map.height + 2*map.ysep), new=TRUE)
                 mtext(paste0("r= ",sp.cor2), cex=3.5)
                 par(fig=c(map.xpos + map.width - 0.04, map.xpos + map.width - 0.02, map.ypos + 1*map.height + 1*map.ysep, map.ypos + 1.1*map.height + 1*map.ysep), new=TRUE)
                 mtext(paste0("r= ",sp.cor3), cex=3.5)
                 par(fig=c(map.xpos + map.width - 0.04, map.xpos + map.width - 0.02, map.ypos + 0*map.height + 0*map.ysep, map.ypos + 0.1*map.height + 0*map.ysep), new=TRUE)
                 mtext(paste0("r= ",sp.cor4), cex=3.5)                
                }
            } # close if on composition == 'fre'
            
            # save indicators for each startdate and forecast time:
            # (map1, map2, map3, map4, fre1, fre2, etc. already refer to the regimes in the same order as listed in the 'orden' vector)
            if(fields.name == forecast.name) {
                if (composition != "impact") {
                    save(orden, map1, map2, map3, map4, fre1.NA, fre2.NA, fre3.NA, fre4.NA, freObs1, freObs2, freObs3, freObs4, sp.cor1, sp.cor2, sp.cor3, sp.cor4, persist1, persist2, persist3, persist4, persistObs1, persistObs2, persistObs3, persistObs4, spat.cor1, spat.cor2, spat.cor3, spat.cor4, sd.freq.sim1, sd.freq.sim2, sd.freq.sim3, sd.freq.sim4, sd.freq.obs1, sd.freq.obs2, sd.freq.obs3, sd.freq.obs4, transSim1, transSim2, transSim3, transSim4, transObs1, transObs2, transObs3, transObs4, file=paste0(work.dir,"/",fields.name,"_", my.period[p],"_leadmonth_",lead.month,"_","ClusterNames",".RData"))
                } else { # also save impX objects
                    save(orden, map1, map2, map3, map4, fre1.NA, fre2.NA, fre3.NA, fre4.NA, freObs1, freObs2, freObs3, freObs4, sp.cor1, sp.cor2, sp.cor3, sp.cor4, persist1, persist2, persist3, persist4, persistObs1, persistObs2, persistObs3, persistObs4, spat.cor1, spat.cor2, spat.cor3, spat.cor4, sd.freq.sim1, sd.freq.sim2, sd.freq.sim3, sd.freq.sim4, sd.freq.obs1, sd.freq.obs2, sd.freq.obs3, sd.freq.obs4, transSim1, transSim2, transSim3, transSim4, transObs1, transObs2, transObs3, transObs4, imp1, imp2, imp3, imp4,cor.imp1,cor.imp2,cor.imp3,cor.imp4, file=paste0(work.dir,"/",fields.name,"_", my.period[p],"_leadmonth_",lead.month,"_","ClusterNames",".RData"))
                }
            }
            
        } # close 'p' on 'period'

        if(as.pdf) dev.off() # for saving a single pdf with all months/seasons

    } # close 'lead.month' on 'lead.months'

    if(composition == "psl" || composition == "fre" || composition == "impact"){
        # Regime's names:
        title1.xpos <- 0.01
        title1.width <- 0.04
        par(fig=c(title1.xpos, title1.xpos + title1.width, 0.7905, 0.7955), new=TRUE)
        mtext(paste0(regime1.name), font=2, cex=5)
        par(fig=c(title1.xpos, title1.xpos + title1.width, 0.5855, 0.5905), new=TRUE)
        mtext(paste0(regime2.name), font=2, cex=5)
        par(fig=c(title1.xpos, title1.xpos + title1.width, 0.3805, 0.3955), new=TRUE)
        mtext(paste0(regime3.name), font=2, cex=5)
        par(fig=c(title1.xpos, title1.xpos + title1.width, 0.1755, 0.1805), new=TRUE)
        mtext(paste0(regime4.name), font=2, cex=5)

        if(composition == "psl") {
            # Color legend:
            legend1.xpos <- 0.10
            legend1.width <- 0.80
            legend1.cex <- 4
            
            par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0, 0.065), new=TRUE)  # syntax fig=c(xmin, xmax, ymin, ymax)
            ColorBar2(brks=my.brks, cols=my.cols, vert=FALSE, cex=legend1.cex, label.dist=2, my.ticks=-0.5 + 1:length(my.brks), my.labels=my.brks)
            par(fig=c(legend1.xpos + legend1.width - 0.02, legend1.xpos + legend1.width + 0.05, 0, 0.02), new=TRUE)  # syntax fig=c(xmin, xmax, ymin, ymax)
            mtext("hPa", cex=legend1.cex*1.3)
        }

        if(composition == "impact") {
            # Color legend:
            legend1.xpos <- 0.10
            legend1.width <- 0.80
            legend1.cex <- 4
                    
            #my.subset2 <- match(values.to.plot2, my.brks.var)
            par(fig=c(legend1.xpos, legend1.xpos + legend1.width, 0, 0.065), new=TRUE)
            ColorBar2(brks=my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend1.cex, label.dist=2, my.ticks=-0.5 + 1:length(my.brks.var), my.labels=my.brks.var)
            par(fig=c(legend1.xpos + legend1.width - 0.02, legend1.xpos + legend1.width + 0.05, 0, 0.02), new=TRUE)  # syntax fig=c(xmin, xmax, ymin, ymax)
            #ColorBar2(brks=my.brks.var, cols=my.cols.var, vert=FALSE, cex=legend2.cex, subset=my.subset2)
            mtext(var.unit[var.num], cex=legend1.cex*1.3)
            
        }

        
        dev.off()
    } # close if on composition

    print("Finished!")
} # close if on composition != "summary"

}

if(composition == "taylor"){
   library("plotrix")

   # choose a reference season from 13 (winter) to 16 (Autumn):
   season.ref <- 13

   # set the first WR classification:
   rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/41_ERA-Interim_monthly_1981-2015_LOESS_filter"

   # load data of the reference season of the first classification (this line should be modified to load the chosen season):
   #load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/18) as 12) but with no lat correction/ERA-Interim_Winter_psl.RData") # load pslwrXmean data
   #load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/18) as 12) but with no lat correction/ERA-Interim_Winter_ClusterNames.RData") # load clusterX.name.period
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_12_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_psl.RData") # load pslwrXmean data
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_12_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_ClusterNames.RData") # load clusterX.name.period
   
   if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
   if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script

   cluster1.ref <- which(orden == cluster1.name)
   cluster2.ref <- which(orden == cluster2.name)
   cluster3.ref <- which(orden == cluster3.name)
   cluster4.ref <- which(orden == cluster4.name)
   
   #cluster1.ref <- cluster1.name.period[13]
   #cluster2.ref <- cluster2.name.period[13]
   #cluster3.ref <- cluster3.name.period[13]
   #cluster4.ref <- cluster4.name.period[13]

   cluster1.ref <- 3  # bypass the previous values because for dataset num.46 the blocking and atlantic regimes were saved swapped!
   cluster2.ref <- 2
   cluster3.ref <- 1
   cluster4.ref <- 4

   assign(paste0("map.ref",cluster1.ref), pslwr1mean) # NAO+
   assign(paste0("map.ref",cluster2.ref), pslwr2mean) # NAO-
   assign(paste0("map.ref",cluster3.ref), pslwr3mean) # Blocking
   assign(paste0("map.ref",cluster4.ref), pslwr4mean) # Atl.ridge

   # set a second WR classification (i.e: with running cluster) to contrast with the new one:
   #rean2.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/41_ERA-Interim_monthly_1981-2015_LOESS_filter"
   rean2.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/44_as_43_but_with_no_lat_correction"

   # load data of the reference season of the second classification (this line should be modified to load the chosen season):
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_12_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_psl.RData") # load pslwrXmean data
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_12_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_ClusterNames.RData") # load clusterX.name.period

   if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
   if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
   
   #cluster1.name.ref <- cluster1.name.period[season.ref]
   #cluster2.name.ref <- cluster2.name.period[season.ref]
   #cluster3.name.ref <- cluster3.name.period[season.ref]
   #cluster4.name.ref <- cluster4.name.period[season.ref]

   cluster1.ref <- which(orden == cluster1.name)
   cluster2.ref <- which(orden == cluster2.name)
   cluster3.ref <- which(orden == cluster3.name)
   cluster4.ref <- which(orden == cluster4.name)

   cluster1.ref <- 3  # bypass the previous values because for dataset num.46 the blocking and atlantic regimes were saved swapped!
   cluster2.ref <- 2
   cluster3.ref <- 1
   cluster4.ref <- 4

   assign(paste0("map.old.ref",cluster1.ref), pslwr1mean) # NAO+
   assign(paste0("map.old.ref",cluster2.ref), pslwr2mean) # NAO-
   assign(paste0("map.old.ref",cluster3.ref), pslwr3mean) # Blocking
   assign(paste0("map.old.ref",cluster4.ref), pslwr4mean) # Atl.ridge

   
   # breaks and colors of the geopotential fields:
   if(psl == "g500"){
       my.brks <- c(-300,seq(-200,200,10),300) # % Mean anomaly of a WR
       my.brks2 <- c(-300,seq(-200,200,10),300) # % Mean anomaly of a WR for the contour lines
       values.to.plot <- c(-200, -100, 0, 100, 200)  # values we want to show in the labels
   }

   if(psl == "psl"){
       my.brks <- c(seq(-19,-1,2),0,seq(1,19,2)) # % Mean anomaly of a WR
       my.brks2 <- my.brks #c(seq(-20,20,2)) # % Mean anomaly of a WR for the contour lines
       values.to.plot <- my.brks #c(-17,-15,-13,-11,-9,-7,-5,-3,-1,0,1,3,5,7,9,11,13,15,17)
   }

   my.cols <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks)-1)    # blue--white--red colors
   my.cols[floor(length(my.cols)/2)] <- my.cols[floor(length(my.cols)/2)+1] <- "white"

   # plot the composition with the regime anomalies of each monthly regimes:
   png(filename=paste0(rean.dir,"/",fields.name,"_taylor_source",".png"),width=6000,height=2000)

   plot.new()
        
   # Sheet title:
   par(fig=c(0, 1, 0.94, 0.98), new=TRUE)
   mtext(paste0(fields.name, " observed monthly regime anomalies"), cex=5.5, font=2)

   n.map <- 0
   for(p in c(9:12,1:8)){
        p.orig <- p

        load(file=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_","psl",".RData"))  # load cluster names and summarized data
        load(file=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_","ClusterNames",".RData"))  # load cluster names and summarized data

        if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
        if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
        if(p != p.orig) p <- p.orig

        cluster1 <- which(orden == cluster1.name)
        cluster2 <- which(orden == cluster2.name)
        cluster3 <- which(orden == cluster3.name)
        cluster4 <- which(orden == cluster4.name)

        assign(paste0("map",cluster1), pslwr1mean) # NAO+
        assign(paste0("map",cluster2), pslwr2mean) # NAO-
        assign(paste0("map",cluster3), pslwr3mean) # Blocking
        assign(paste0("map",cluster4), pslwr4mean) # Atl.ridge

        n.map <- n.map + 1  # n.maps starts from 0
        map.width <- 0.07
        map.xpos <- 0.06 + map.width * (n.map - 1)
        map.ypos <- 0.08
        map.height <- 0.19
        map.ysep <- 0.015
        
        par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
        PlotEquiMap2(rescale(map1,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map1), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, xlabel.dist=1.5, contours.lty="F1FF1F")
        par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
        PlotEquiMap2(rescale(map2,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
        par(fig=c(map.xpos, map.xpos + map.width, map.ypos + map.height + map.ysep, map.ypos + 2*map.height + map.ysep), new=TRUE)
        PlotEquiMap2(rescale(map3,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
        par(fig=c(map.xpos, map.xpos + map.width, map.ypos, map.ypos + map.height), new=TRUE)
        PlotEquiMap2(rescale(map4,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")

        # Sheet subtitles:
        par(fig=c(map.xpos, map.xpos + map.width, 0.86, 0.90), new=TRUE)
        mtext(my.month.short[p], cex=5.5, font=2)
        
   }

   # draw the last map to the right of the composition, that with the seasonal anomalies:
   n.map <- n.map + 1  # n.maps starts from 0
   map.width <- 0.07
   map.xpos <- 0.06 + map.width * (n.map - 1)
   map.ypos <- 0.08
   map.height <- 0.19
   map.ysep <- 0.015
   
   par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 3*map.height + 3*map.ysep, map.ypos + 4*map.height + 3*map.ysep), new=TRUE)
   PlotEquiMap2(rescale(map.ref1,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map.ref1), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, xlabel.dist=1.5, contours.lty="F1FF1F")
   par(fig=c(map.xpos, map.xpos + map.width, map.ypos + 2*map.height + 2*map.ysep, map.ypos + 3*map.height + 2*map.ysep), new=TRUE)
   PlotEquiMap2(rescale(map.ref2,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map.ref2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
   par(fig=c(map.xpos, map.xpos + map.width, map.ypos + map.height + map.ysep, map.ypos + 2*map.height + map.ysep), new=TRUE)
   PlotEquiMap2(rescale(map.ref3,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map.ref3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")
   par(fig=c(map.xpos, map.xpos + map.width, map.ypos, map.ypos + map.height), new=TRUE)
   PlotEquiMap2(rescale(map.ref4,my.brks[1],tail(my.brks,1)), lon, lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, sizetit=1.2, contours=t(map.ref4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F")

   # subtitle:
   par(fig=c(map.xpos, map.xpos + map.width, 0.86, 0.90), new=TRUE)
   mtext(my.period[season.ref], cex=5.5, font=2)
   
   dev.off()

   corr.first <- corr.second <- rmse.first <- rmse.second <- array(NA,c(4,12))
   
   # Plot the Taylor diagrams:   
   for(regime in 1:4){

       png(filename=paste0(rean.dir,"/",fields.name,"_taylor_",orden[regime],".png"), width=1200, height=800)
       
       for(p in 1:12){
           p.orig <- p
           
           load(file=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_","psl",".RData"))  # load cluster names and summarized data
           load(file=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_","ClusterNames",".RData"))  # load cluster names and summarized data

           if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
           if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
           if(p != p.orig) p <- p.orig

           cluster1 <- which(orden == cluster1.name)
           cluster2 <- which(orden == cluster2.name)
           cluster3 <- which(orden == cluster3.name)
           cluster4 <- which(orden == cluster4.name)

           assign(paste0("map",cluster1), pslwr1mean) # NAO+
           assign(paste0("map",cluster2), pslwr2mean) # NAO-
           assign(paste0("map",cluster3), pslwr3mean) # Blocking
           assign(paste0("map",cluster4), pslwr4mean) # Atl.ridge

           # load data from the second classification:
           load(file=paste0(rean2.dir,"/",fields.name,"_",my.period[p],"_","psl",".RData"))  # load cluster names and summarized data
           load(file=paste0(rean2.dir,"/",fields.name,"_",my.period[p],"_","ClusterNames",".RData"))  # load cluster names and summarized data

           if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
           if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
           if(p != p.orig) p <- p.orig

           cluster1.old <- which(orden == cluster1.name)
           cluster2.old <- which(orden == cluster2.name)
           cluster3.old <- which(orden == cluster3.name)
           cluster4.old <- which(orden == cluster4.name)

           assign(paste0("map.old",cluster1.old), pslwr1mean) # NAO+
           assign(paste0("map.old",cluster2.old), pslwr2mean) # NAO-
           assign(paste0("map.old",cluster3.old), pslwr3mean) # Blocking
           assign(paste0("map.old",cluster4.old), pslwr4mean) # Atl.ridge
           
           add.mod <- ifelse(p == 1, FALSE, TRUE)
           main.mod <- ifelse(p == 1, orden[regime],"")

           color.first <- "red"
           color.second <- "blue"
           sd.arcs.mod <- c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.3) #c(0,0.5,1,1.5) # doesn't work!
           if(regime == 1) my.taylor(as.vector(map.ref1),as.vector(map1), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.first, main=main.mod)
           if(regime == 2) my.taylor(as.vector(map.ref2),as.vector(map2), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.first, main=main.mod)
           if(regime == 3) my.taylor(as.vector(map.ref3),as.vector(map3), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.first, main=main.mod)
           if(regime == 4) my.taylor(as.vector(map.ref4),as.vector(map4), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.first, main=main.mod)

           # add the points from the second classification:
           add.mod=TRUE
           if(regime == 1) my.taylor(as.vector(map.old.ref1),as.vector(map.old1), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.second)
           if(regime == 2) my.taylor(as.vector(map.old.ref2),as.vector(map.old2), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.second)
           if(regime == 3) my.taylor(as.vector(map.old.ref3),as.vector(map.old3), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.second)
           if(regime == 4) my.taylor(as.vector(map.old.ref4),as.vector(map.old4), normalize=TRUE, add=add.mod, pos.cor=FALSE, sd.arcs=sd.arcs.mod, ref.sd=F, cex.axis=1.7, text.cex=1, my.text=my.month.short[p], col = color.second)

           if(regime == 1) { corr.first[1,p] <- cor(as.vector(map.ref1),as.vector(map1)); rmse.first[1,p] <- RMSE(as.vector(map.ref1),as.vector(map1)) }
           if(regime == 2) { corr.first[2,p] <- cor(as.vector(map.ref2),as.vector(map2)); rmse.first[2,p] <- RMSE(as.vector(map.ref2),as.vector(map2)) }
           if(regime == 3) { corr.first[3,p] <- cor(as.vector(map.ref3),as.vector(map3)); rmse.first[3,p] <- RMSE(as.vector(map.ref3),as.vector(map3)) }
           if(regime == 4) { corr.first[4,p] <- cor(as.vector(map.ref4),as.vector(map4)); rmse.first[4,p] <- RMSE(as.vector(map.ref4),as.vector(map4)) }

           if(regime == 1) { corr.second[1,p] <- cor(as.vector(map.old.ref1),as.vector(map.old1)); rmse.second[1,p] <- RMSE(as.vector(map.old.ref1),as.vector(map.old1)) }
           if(regime == 2) { corr.second[2,p] <- cor(as.vector(map.old.ref2),as.vector(map.old2)); rmse.second[2,p] <- RMSE(as.vector(map.old.ref2),as.vector(map.old2)) }
           if(regime == 3) { corr.second[3,p] <- cor(as.vector(map.old.ref3),as.vector(map.old3)); rmse.second[3,p] <- RMSE(as.vector(map.old.ref3),as.vector(map.old3)) }
           if(regime == 4) { corr.second[4,p] <- cor(as.vector(map.old.ref4),as.vector(map.old4)); rmse.second[4,p] <- RMSE(as.vector(map.old.ref4),as.vector(map.old4)) }
           
       } # close for on 'p'

       dev.off()
       
   } # close for on 'regime'
   
   corr.first.mean <- apply(corr.first,1,mean)
   corr.second.mean <- apply(corr.second,1,mean)
   corr.diff <- corr.second.mean - corr.first.mean

   rmse.first.mean <- apply(rmse.first,1,mean)
   rmse.second.mean <- apply(rmse.second,1,mean)
   rmse.diff <- rmse.second.mean - rmse.first.mean
   
} # close if on composition == "taylor"






# Summary graphs:
if(composition == "summary" && fields.name == forecast.name){
   # array storing correlations in the format: [startdate, leadmonth, regime]
   array.diff.sd.freq <- array.sd.freq <- array.cor <- array.sp.cor <- array.freq <- array.diff.freq <- array.rpss <- array.pers <- array.diff.pers <- array(NA,c(12,7,4)) 
   array.spat.cor <- array.imp <- array.trans.sim1 <- array.trans.sim2 <- array.trans.sim3 <- array.trans.sim4 <- array(NA,c(12,7,4))
   array.trans.diff1 <- array.trans.diff2 <- array.trans.diff3 <- array.trans.diff4 <- array(NA,c(12,7,4))
    
   for(p in 1:12){
        p.orig <- p

        for(l in 0:6){
        
            load(file=paste0(work.dir,"/",fields.name,"_",my.period[p],"_leadmonth_",l,"_","ClusterNames",".RData"))  # load cluster names and summarized data

            if(var.num != var.num.orig) var.num <- var.num.orig  # ripristinate original values of this script (necessary after loading regime data)
            if(fields.name != fields.name.orig) fields.name <- fields.name.orig # ripristinate original values of this script
            if(p != p.orig) p <- p.orig

            array.spat.cor[p,1+l, 1] <- spat.cor1  # associates NAO+ to the first triangle (at left)
            array.spat.cor[p,1+l, 3] <- spat.cor2  # associates NAO- to the third triangle (at right)
            array.spat.cor[p,1+l, 4] <- spat.cor3  # associates Blocking to the fourth triangle (top one)
            array.spat.cor[p,1+l, 2] <- spat.cor4  # associates Atl.Ridge to the second triangle (bottom one)
            
            array.cor[p,1+l, 1] <- sp.cor1  # associates NAO+ to the first triangle (at left)
            array.cor[p,1+l, 3] <- sp.cor2  # associates NAO- to the third triangle (at right)
            array.cor[p,1+l, 4] <- sp.cor3  # associates Blocking to the fourth triangle (top one)
            array.cor[p,1+l, 2] <- sp.cor4  # associates Atl.Ridge to the second triangle (bottom one)

            array.freq[p,1+l, 1] <- 100*(mean(fre1.NA))  # NAO+
            array.freq[p,1+l, 3] <- 100*(mean(fre2.NA))  # NAO-
            array.freq[p,1+l, 4] <- 100*(mean(fre3.NA))  # Blocking
            array.freq[p,1+l, 2] <- 100*(mean(fre4.NA))  # Atl.Ridge
            
            array.diff.freq[p,1+l, 1] <- 100*(mean(fre1.NA) - mean(freObs1))  # NAO+
            array.diff.freq[p,1+l, 3] <- 100*(mean(fre2.NA) - mean(freObs2))  # NAO-
            array.diff.freq[p,1+l, 4] <- 100*(mean(fre3.NA) - mean(freObs3))  # Blocking
            array.diff.freq[p,1+l, 2] <- 100*(mean(fre4.NA) - mean(freObs4))  # Atl.Ridge

            array.pers[p,1+l, 1] <- persist1  # NAO+
            array.pers[p,1+l, 3] <- persist2  # NAO-
            array.pers[p,1+l, 4] <- persist3  # Blocking
            array.pers[p,1+l, 2] <- persist4  # Atl.Ridge

            array.diff.pers[p,1+l, 1] <- persist1 - persistObs1  # NAO+
            array.diff.pers[p,1+l, 3] <- persist2 - persistObs2 # NAO-
            array.diff.pers[p,1+l, 4] <- persist3 - persistObs3  # Blocking
            array.diff.pers[p,1+l, 2] <- persist4 - persistObs4  # Atl.Ridge

            array.sd.freq[p,1+l, 1] <- sd.freq.sim1  # NAO+
            array.sd.freq[p,1+l, 3] <- sd.freq.sim2  # NAO-
            array.sd.freq[p,1+l, 4] <- sd.freq.sim3  # Blocking
            array.sd.freq[p,1+l, 2] <- sd.freq.sim4  # Atl.Ridge

            array.diff.sd.freq[p,1+l, 1] <- sd.freq.sim1 / sd.freq.obs1  # NAO+
            array.diff.sd.freq[p,1+l, 3] <- sd.freq.sim2 / sd.freq.obs2  # NAO-
            array.diff.sd.freq[p,1+l, 4] <- sd.freq.sim3 / sd.freq.obs3  # Blocking
            array.diff.sd.freq[p,1+l, 2] <- sd.freq.sim4 / sd.freq.obs4  # Atl.Ridge

            array.imp[p,1+l, 1] <- cor.imp1  # NAO+
            array.imp[p,1+l, 3] <- cor.imp2  # NAO-
            array.imp[p,1+l, 4] <- cor.imp3  # Blocking
            array.imp[p,1+l, 2] <- cor.imp4  # Atl.Ridge
            
            array.trans.sim1[p,1+l, 1] <- NA  # Transition from NAO+ to NAO+
            array.trans.sim1[p,1+l, 3] <- 100*transSim1[2]  # Transition from NAO+ to NAO-
            array.trans.sim1[p,1+l, 4] <- 100*transSim1[3]  # Transition from NAO+ to blocking
            array.trans.sim1[p,1+l, 2] <- 100*transSim1[4]  # Transition from NAO+ to Atl.Ridge
            
            array.trans.sim2[p,1+l, 1] <- 100*transSim2[1]  # Transition from NAO- to NAO+
            array.trans.sim2[p,1+l, 3] <- NA  # Transition from NAO- to NAO-
            array.trans.sim2[p,1+l, 4] <- 100*transSim2[3]  # Transition from NAO- to blocking
            array.trans.sim2[p,1+l, 2] <- 100*transSim2[4]  # Transition from NAO- to Atl.Ridge

            array.trans.sim3[p,1+l, 1] <- 100*transSim3[1]  # Transition from blocking to NAO+
            array.trans.sim3[p,1+l, 3] <- 100*transSim3[2]  # Transition from blocking to NAO-
            array.trans.sim3[p,1+l, 4] <- NA  # Transition from blocking to blocking
            array.trans.sim3[p,1+l, 2] <- 100*transSim3[4]  # Transition from blocking to Atl.Ridge

            array.trans.sim4[p,1+l, 1] <- 100*transSim4[1]  # Transition from Atl.ridge to NAO+
            array.trans.sim4[p,1+l, 3] <- 100*transSim4[2]  # Transition from Atl.ridge to NAO-
            array.trans.sim4[p,1+l, 4] <- 100*transSim4[3]  # Transition from Atl.ridge to blocking
            array.trans.sim4[p,1+l, 2] <- NA  # Transition from Atl.ridge to Atl.Ridge

            array.trans.obs1[p,1+l, 1] <- NA  # Transition from NAO+ to NAO+
            array.trans.obs1[p,1+l, 3] <- 100*transObs1[2]  # Transition from NAO+ to NAO-
            array.trans.obs1[p,1+l, 4] <- 100*transObs1[3]  # Transition from NAO+ to blocking
            array.trans.obs1[p,1+l, 2] <- 100*transObs1[4]  # Transition from NAO+ to Atl.Ridge
            
            array.trans.obs2[p,1+l, 1] <- 100*transObs2[1]  # Transition from NAO- to NAO+
            array.trans.obs2[p,1+l, 3] <- NA  # Transition from NAO- to NAO-
            array.trans.obs2[p,1+l, 4] <- 100*transObs2[3]  # Transition from NAO- to blocking
            array.trans.obs2[p,1+l, 2] <- 100*transObs2[4]  # Transition from NAO- to Atl.Ridge

            array.trans.obs3[p,1+l, 1] <- 100*transObs3[1]  # Transition from blocking to NAO+
            array.trans.obs3[p,1+l, 3] <- 100*transObs3[2]  # Transition from blocking to NAO-
            array.trans.obs3[p,1+l, 4] <- NA  # Transition from blocking to blocking
            array.trans.obs3[p,1+l, 2] <- 100*transObs3[4]  # Transition from blocking to Atl.Ridge

            array.trans.obs4[p,1+l, 1] <- 100*transObs4[1]  # Transition from Atl.ridge to NAO+
            array.trans.obs4[p,1+l, 3] <- 100*transObs4[2]  # Transition from Atl.ridge to NAO-
            array.trans.obs4[p,1+l, 4] <- 100*transObs4[3]  # Transition from Atl.ridge to blocking
            array.trans.obs4[p,1+l, 2] <- NA  # Transition from Atl.ridge to Atl.Ridge
            
            array.trans.diff1[p,1+l, 1] <- NA  # Transition from NAO+ to NAO+
            array.trans.diff1[p,1+l, 3] <- 100*(transSim1[2] - transObs1[2])  # Transition from NAO+ to NAO-
            array.trans.diff1[p,1+l, 4] <- 100*(transSim1[3] - transObs1[3])  # Transition from NAO+ to blocking
            array.trans.diff1[p,1+l, 2] <- 100*(transSim1[4] - transObs1[4])  # Transition from NAO+ to Atl.Ridge

            array.trans.diff2[p,1+l, 1] <- 100*(transSim2[1] - transObs2[1])  # Transition from NAO+ to NAO+
            array.trans.diff2[p,1+l, 3] <- NA
            array.trans.diff2[p,1+l, 4] <- 100*(transSim2[3] - transObs2[3])  # Transition from NAO+ to blocking
            array.trans.diff2[p,1+l, 2] <- 100*(transSim2[4] - transObs2[4])  # Transition from NAO+ to Atl.Ridge

            array.trans.diff3[p,1+l, 1] <- 100*(transSim3[1] - transObs3[1])  # Transition from NAO+ to NAO+
            array.trans.diff3[p,1+l, 3] <- 100*(transSim3[2] - transObs3[2])  # Transition from NAO+ to NAO-
            array.trans.diff3[p,1+l, 4] <- NA
            array.trans.diff3[p,1+l, 2] <- 100*(transSim3[4] - transObs3[4])  # Transition from NAO+ to Atl.Ridge

            array.trans.diff4[p,1+l, 1] <- 100*(transSim4[1] - transObs4[1])  # Transition from NAO+ to NAO+
            array.trans.diff4[p,1+l, 3] <- 100*(transSim4[2] - transObs4[2])  # Transition from NAO+ to NAO-
            array.trans.diff4[p,1+l, 4] <- 100*(transSim4[3] - transObs4[3])  # Transition from NAO+ to blocking
            array.trans.diff4[p,1+l, 2] <- NA
            
            #array.rpss[p,1+l, 1] <-   # NAO+
            #array.rpss[p,1+l, 3] <-   # NAO-
            #array.rpss[p,1+l, 4] <-   # Blocking
            #array.rpss[p,1+l, 2] <-   # Atl.Ridge
        }
   }

   # Spatial Corr summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_spat_corr.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   sp.corr.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-1,0,0.4,0.5,0.6,0.7,0.8,0.9,1)
    
   array.sp.cor.colors <- array(sp.corr.cols[8],c(12,7,4))
   array.sp.cor.colors[array.spat.cor < my.seq[8]] <- sp.corr.cols[7]
   array.sp.cor.colors[array.spat.cor < my.seq[7]] <- sp.corr.cols[6]
   array.sp.cor.colors[array.spat.cor < my.seq[6]] <- sp.corr.cols[5]
   array.sp.cor.colors[array.spat.cor < my.seq[5]] <- sp.corr.cols[4]
   array.sp.cor.colors[array.spat.cor < my.seq[4]] <- sp.corr.cols[3]
   array.sp.cor.colors[array.spat.cor < my.seq[3]] <- sp.corr.cols[2]
   array.sp.cor.colors[array.spat.cor < my.seq[2]] <- sp.corr.cols[1]

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("Spatial correlation between S4 and ERA-Interim regime anomalies", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.sp.cor.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.sp.cor.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.sp.cor.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.sp.cor.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = sp.corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

    
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_spat_corr_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Spatial correlation between S4 and ERA-Interim regime anomalies", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.spat.cor.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.spat.cor.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.spat.cor.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.spat.cor.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = sp.corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_spat_corr_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Spatial correlation between S4 and ERA-Interim regime anomalies", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.spat.cor.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.spat.cor.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.spat.cor.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.spat.cor.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = sp.corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()







    
   # Corr summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_corr.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   corr.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1)

   array.cor.colors <- array(corr.cols[8],c(12,7,4))
   array.cor.colors[array.cor < 0.75] <- corr.cols[7]
   array.cor.colors[array.cor < 0.5] <- corr.cols[6]
   array.cor.colors[array.cor < 0.25] <- corr.cols[5]
   array.cor.colors[array.cor < 0] <- corr.cols[4]
   array.cor.colors[array.cor < -0.25] <- corr.cols[3]
   array.cor.colors[array.cor < -0.5] <- corr.cols[2]
   array.cor.colors[array.cor < -0.75] <- corr.cols[1]

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("Correlation between S4 and ERA-Interim frequencies", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.cor.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.cor.colors[p,1+l,2])
            polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.cor.colors[p,1+l,3])
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.cor.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = seq(-1,1,0.25), cols = corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(seq(-1,1,0.25)), my.labels=seq(-1,1,0.25))
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_corr_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Correlation between S4 and ERA-Interim frequencies", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.cor.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.cor.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.cor.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.cor.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_corr_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Correlation between S4 and ERA-Interim frequencies", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.cor.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.cor.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.cor.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.cor.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = corr.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()



    

    
   # Freq. summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_freq.png"), width=550, height=400)
   
   # create an array similar to array.diff.freq but with colors instead of frequencies:
   freq.cols <- rev(c('#d73027','#f46d43','#fdae61','#fee090','#e0f3f8','#abd9e9','#74add1','#4575b4'))
   my.seq <- c(NA,20,22,24,26,28,30,32,NA)
       
   array.freq.colors <- array(freq.cols[8],c(12,7,4))
   array.freq.colors[array.freq < my.seq[[8]]] <- freq.cols[7]
   array.freq.colors[array.freq < my.seq[[7]]] <- freq.cols[6]
   array.freq.colors[array.freq < my.seq[[6]]] <- freq.cols[5]
   array.freq.colors[array.freq < my.seq[[5]]] <- freq.cols[4]
   array.freq.colors[array.freq < my.seq[[4]]] <- freq.cols[3]
   array.freq.colors[array.freq < my.seq[[3]]] <- freq.cols[2]
   array.freq.colors[array.freq < my.seq[[2]]] <- freq.cols[1]
 
   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("Mean S4 frequency (%)", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.freq.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.freq.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.freq.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.freq.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_freq_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Mean S4 frequency (%)", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.freq.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.freq.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.freq.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.freq.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_freq_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext(text="Mean S4 frequency (%)", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.freq.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.freq.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.freq.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.freq.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()



    

    
   
   # Delta freq. summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_freq.png"), width=550, height=400)
   
   # create an array similar to array.diff.freq but with colors instead of frequencies:
   diff.freq.cols <- rev(c('#d73027','#f46d43','#fdae61','#fee090','#e0f3f8','#abd9e9','#74add1','#4575b4'))
   my.seq <- c(-20,-10,-5,-2,0,2,5,10,20)
    
   array.diff.freq.colors <- array(diff.freq.cols[8],c(12,7,4))
   array.diff.freq.colors[array.diff.freq < my.seq[[8]]] <- diff.freq.cols[7]
   array.diff.freq.colors[array.diff.freq <my.seq[[7]]] <- diff.freq.cols[6]
   array.diff.freq.colors[array.diff.freq < my.seq[[6]]] <- diff.freq.cols[5]
   array.diff.freq.colors[array.diff.freq < my.seq[[5]]] <- diff.freq.cols[4]
   array.diff.freq.colors[array.diff.freq < my.seq[[4]]] <- diff.freq.cols[3]
   array.diff.freq.colors[array.diff.freq < my.seq[[3]]] <- diff.freq.cols[2]
   array.diff.freq.colors[array.diff.freq < my.seq[[2]]] <- diff.freq.cols[1]
 
   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("Frequency difference (%) between S4 and ERA-Interim", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.diff.freq.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.diff.freq.colors[p,1+l,2])
            polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.diff.freq.colors[p,1+l,3])
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.diff.freq.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_freq_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Frequency difference (%) between S4 and ERA-Interim", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.freq.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.freq.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.freq.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.freq.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_freq_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Frequency difference (%) between S4 and ERA-Interim", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.freq.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.freq.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.freq.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.freq.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()






    
   
   # Persistence summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_pers.png"), width=550, height=400)
   
   # create an array similar to array.pers but with colors instead of frequencies:
   pers.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(NA, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, NA)
               
   array.pers.colors <- array(pers.cols[8],c(12,7,4))
   array.pers.colors[array.pers < my.seq[8]] <- pers.cols[7]
   array.pers.colors[array.pers < my.seq[7]] <- pers.cols[6]
   array.pers.colors[array.pers < my.seq[6]] <- pers.cols[5]
   array.pers.colors[array.pers < my.seq[5]] <- pers.cols[4]
   array.pers.colors[array.pers < my.seq[4]] <- pers.cols[3]
   array.pers.colors[array.pers < my.seq[3]] <- pers.cols[2]
   array.pers.colors[array.pers < my.seq[2]] <- pers.cols[1]
 
   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("S4 Regime persistence (in days/month)", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.pers.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.pers.colors[p,1+l,2])
            polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.pers.colors[p,1+l,3])
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.pers.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_pers_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("S4 Regime persistence (in days/month)", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.pers.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.pers.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.pers.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.pers.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_pers_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("S4 Regime persistence (in days/month)", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.pers.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.pers.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.pers.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.pers.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()



    



    
   # Delta persistence summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_pers.png"), width=550, height=400)
   
   # create an array similar to array.pers but with colors instead of frequencies:
   diff.pers.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(NA, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, NA)
               
   array.diff.pers.colors <- array(diff.pers.cols[8],c(12,7,4))
   array.diff.pers.colors[array.diff.pers < my.seq[8]] <- diff.pers.cols[7]
   array.diff.pers.colors[array.diff.pers < my.seq[7]] <- diff.pers.cols[6]
   array.diff.pers.colors[array.diff.pers < my.seq[6]] <- diff.pers.cols[5]
   array.diff.pers.colors[array.diff.pers < my.seq[5]] <- diff.pers.cols[4]
   array.diff.pers.colors[array.diff.pers < my.seq[4]] <- diff.pers.cols[3]
   array.diff.pers.colors[array.diff.pers < my.seq[3]] <- diff.pers.cols[2]
   array.diff.pers.colors[array.diff.pers < my.seq[2]] <- diff.pers.cols[1]
 
   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("Diff. between S4 and ERA-Interim regime persistence (in days/month)", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.diff.pers.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.diff.pers.colors[p,1+l,2])
            polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.diff.pers.colors[p,1+l,3])
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.diff.pers.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_pers_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Diff. between S4 and ERA-Interim regime persistence (in days/month)", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.pers.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.pers.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.pers.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.pers.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_diff_pers_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Diff. between S4 and ERA-Interim regime persistence (in days/month)", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.pers.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.pers.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.pers.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.pers.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.pers.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()









   # St.Dev.freq ratio summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_ratio_sd_freq.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   diff.sd.freq.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,0.7,0.8,0.9,1.0,1.1,1.2,1.3,2)
    
   array.diff.sd.freq.colors <- array(diff.sd.freq.cols[8],c(12,7,4))
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[8]] <- diff.sd.freq.cols[7]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[7]] <- diff.sd.freq.cols[6]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[6]] <- diff.sd.freq.cols[5]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[5]] <- diff.sd.freq.cols[4]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[4]] <- diff.sd.freq.cols[3]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[3]] <- diff.sd.freq.cols[2]
   array.diff.sd.freq.colors[array.diff.sd.freq < my.seq[2]] <- diff.sd.freq.cols[1]

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("St.Dev. ratio between sim. and obs. average frequencies", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.diff.sd.freq.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.diff.sd.freq.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.sd.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()


   png(filename=paste0(work.dir,"/",forecast.name,"_summary_ratio_sd_freq_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("St.Dev. ratio between sim. and obs. average frequencies", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.diff.sd.freq.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.sd.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_ratio_sd_freq_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("St.Dev. ratio between sim. and obs. average frequencies", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.sd.freq.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.sd.freq.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.sd.freq.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.diff.sd.freq.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = diff.sd.freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()











    
   
   # Impact summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_impact_",var.name[var.num],".png"), width=550, height=400)
   
   # create an array similar to array.pers but with colors instead of frequencies:
   imp.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-1,0,0.4,0.5,0.6,0.7,0.8,0.9,1)
               
   array.imp.colors <- array(imp.cols[8],c(12,7,4))
   array.imp.colors[array.imp < my.seq[8]] <- imp.cols[7]
   array.imp.colors[array.imp < my.seq[7]] <- imp.cols[6]
   array.imp.colors[array.imp < my.seq[6]] <- imp.cols[5]
   array.imp.colors[array.imp < my.seq[5]] <- imp.cols[4]
   array.imp.colors[array.imp < my.seq[4]] <- imp.cols[3]
   array.imp.colors[array.imp < my.seq[3]] <- imp.cols[2]
   array.imp.colors[array.imp < my.seq[2]] <- imp.cols[1]
 
   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title(paste0("S4 spatial correlation of impact on ",var.name[var.num]), cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.imp.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.imp.colors[p,1+l,2])
            polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.imp.colors[p,1+l,3])
            polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.imp.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = imp.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_impact_",var.name[var.num],"_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("S4 spatial correlation of impact on ",var.name[var.num], cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.imp.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.imp.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.imp.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.imp.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = imp.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_impact_",var.name[var.num],"_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("S4 spatial correlation of impact on ",var.name[var.num], cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.imp.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.imp.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.imp.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.imp.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = imp.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()






    
   # Simulated Transition probability from NAO+ to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,10,20,30,40,50,60,70,100)
    
   array.trans.sim1.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.sim1.colors[array.trans.sim1 < my.seq[8]] <- trans.cols[7]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[7]] <- trans.cols[6]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[6]] <- trans.cols[5]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[5]] <- trans.cols[4]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[4]] <- trans.cols[3]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[3]] <- trans.cols[2]
   array.trans.sim1.colors[array.trans.sim1 < my.seq[2]] <- trans.cols[1]
   array.trans.sim1.colors[is.na(array.trans.sim1)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Transition from NAO+ regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.sim1.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.sim1.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from NAO+ regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+_1table.png"), width=600, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from NAO+ regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.28, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim1.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.28, 0.56, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.28, 0.56, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim1.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.56, 0.84, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.56, 0.84, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim1.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.88, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()

    




  # Observed Transition probability from NAO+ to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+_obs.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,10,20,30,40,50,60,70,100)
    
   array.trans.obs1.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.obs1.colors[array.trans.obs1 < my.seq[8]] <- trans.cols[7]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[7]] <- trans.cols[6]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[6]] <- trans.cols[5]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[5]] <- trans.cols[4]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[4]] <- trans.cols[3]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[3]] <- trans.cols[2]
   array.trans.obs1.colors[array.trans.obs1 < my.seq[2]] <- trans.cols[1]
   array.trans.obs1.colors[is.na(array.trans.obs1)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Obs.transition from NAO+ regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.sim1.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.sim1.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.sim1.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+_obs_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Obs.transition from NAO+ regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.obs1.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.obs1.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.obs1.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.obs1.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO+_obs_target_month.png"), width=600, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Obs.transition from NAO+ regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.28, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.obs1.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.28, 0.56, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.28, 0.56, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.obs1.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.56, 0.84, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.56, 0.84, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.obs1.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.88, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()








    

    
   # Simulated transition probability from NAO- to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO-.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,10,20,30,40,50,60,70,100)
 
   array.trans.sim2.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.sim2.colors[array.trans.sim2 < my.seq[8]] <- trans.cols[7]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[7]] <- trans.cols[6]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[6]] <- trans.cols[5]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[5]] <- trans.cols[4]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[4]] <- trans.cols[3]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[3]] <- trans.cols[2]
   array.trans.sim2.colors[array.trans.sim2 < my.seq[2]] <- trans.cols[1]
   array.trans.sim2.colors[is.na(array.trans.sim2)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Transition from NAO- regime      ", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.sim2.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.sim2.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO-_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from NAO- regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim2.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_NAO-_1table.png"), width=600, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from NAO- regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.1, 0.28, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.28, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim2.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.28, 0.56, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.28, 0.56, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim2.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.56, 0.84, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.56, 0.84, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim2.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.88, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()


    


    

    
   # Transition probability from blocking to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_blocking.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,10,20,30,40,50,60,70,100)
 
   array.trans.sim3.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.sim3.colors[array.trans.sim3 < my.seq[8]] <- trans.cols[7]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[7]] <- trans.cols[6]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[6]] <- trans.cols[5]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[5]] <- trans.cols[4]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[4]] <- trans.cols[3]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[3]] <- trans.cols[2]
   array.trans.sim3.colors[array.trans.sim3 < my.seq[2]] <- trans.cols[1]
   array.trans.sim3.colors[is.na(array.trans.sim3)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Transition from blocking regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.sim3.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.sim3.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_blocking_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from blocking regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim3.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_blocking_1table.png"), width=600, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from blocking regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.28, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim3.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.28, 0.56, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.28, 0.56, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim3.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.56, 0.84, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.56, 0.84, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim3.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.88, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()

    


    





    
   # Transition probability from Atlantic ridge to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_Atlantic_ridge.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(0,10,20,30,40,50,60,70,100)

   array.trans.sim4.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.sim4.colors[array.trans.sim4 < my.seq[8]] <- trans.cols[7]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[7]] <- trans.cols[6]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[6]] <- trans.cols[5]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[5]] <- trans.cols[4]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[4]] <- trans.cols[3]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[3]] <- trans.cols[2]
   array.trans.sim4.colors[array.trans.sim4 < my.seq[2]] <- trans.cols[1]
   array.trans.sim4.colors[is.na(array.trans.sim4)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Transition from Atlantic ridge regime      ", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.sim4.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.sim4.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_Atlantic_ridge_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from Atlantic Ridge regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.sim4.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_trans_Atlantic_ridge_1table.png"), width=600, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Transition from Atlantic Ridge regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.1, 0.28, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.28, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim4.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.28, 0.56, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.28, 0.56, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim4.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.56, 0.84, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.56, 0.84, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.sim4.colors[i2,1+6-j,4])}}  
    
   par(fig=c(0.88, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()





    


   # Bias of the Transition probability from NAO+ to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO+.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-20,-10,-5,-2,0,2,5,10,20)

   array.trans.diff1.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.diff1.colors[array.trans.diff1 < my.seq[8]] <- trans.cols[7]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[7]] <- trans.cols[6]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[6]] <- trans.cols[5]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[5]] <- trans.cols[4]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[4]] <- trans.cols[3]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[3]] <- trans.cols[2]
   array.trans.diff1.colors[array.trans.diff1 < my.seq[2]] <- trans.cols[1]
   array.trans.diff1.colors[is.na(array.trans.diff1)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Bias transition from NAO+ regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.diff1.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.diff1.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO+_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from NAO+ regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff1.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO+_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from NAO+ regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff1.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff1.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff1.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff1.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()















   # Bias of the Transition probability from NAO- to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO-.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-20,-10,-5,-2,0,2,5,10,20)

   array.trans.diff2.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.diff2.colors[array.trans.diff2 < my.seq[8]] <- trans.cols[7]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[7]] <- trans.cols[6]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[6]] <- trans.cols[5]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[5]] <- trans.cols[4]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[4]] <- trans.cols[3]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[3]] <- trans.cols[2]
   array.trans.diff2.colors[array.trans.diff2 < my.seq[2]] <- trans.cols[1]
   array.trans.diff2.colors[is.na(array.trans.diff2)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Bias transition from NAO- regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.diff2.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.diff2.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO-_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from NAO- regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff2.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_NAO-_1table.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from NAO- regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff2.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff2.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff2.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff2.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()

    







    

   # Bias of the Transition probability from blocking to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_blocking.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-20,-10,-5,-2,0,2,5,10,20)

   array.trans.diff3.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.diff3.colors[array.trans.diff3 < my.seq[8]] <- trans.cols[7]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[7]] <- trans.cols[6]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[6]] <- trans.cols[5]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[5]] <- trans.cols[4]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[4]] <- trans.cols[3]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[3]] <- trans.cols[2]
   array.trans.diff3.colors[array.trans.diff3 < my.seq[2]] <- trans.cols[1]
   array.trans.diff3.colors[is.na(array.trans.diff3)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Bias transition from blocking regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.diff3.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.diff3.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_blocking_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from blocking regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff3.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_blocking_1table.png"), width=800, height=300)
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from Blocking regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff3.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff3.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff3.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff3.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()


    
    






   # Bias of the Transition probability from Atlantic Ridge to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_Atlantic_ridge.png"), width=550, height=400)
   
   # create an array similar to array.cor but with colors instead of corr.values:
   trans.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
   my.seq <- c(-20,-10,-5,-2,0,2,5,10,20)

   array.trans.diff4.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.diff4.colors[array.trans.diff4 < my.seq[8]] <- trans.cols[7]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[7]] <- trans.cols[6]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[6]] <- trans.cols[5]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[5]] <- trans.cols[4]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[4]] <- trans.cols[3]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[3]] <- trans.cols[2]
   array.trans.diff4.colors[array.trans.diff4 < my.seq[2]] <- trans.cols[1]
   array.trans.diff4.colors[is.na(array.trans.diff4)] <- "white"

   par(mar=c(5,4,4,4.5))
   plot(1,1, type="n", xaxt="n", yaxt="n", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   title("% Bias transition from Atlantic Ridge regime", cex=1.5)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)

   for(p in 1:12){
        for(l in 0:6){
            polygon(c(0.5+p-1, 0.5+p-1, 1+p-1), c(-0.5+l, 0.5+l, 0+l), col=array.trans.diff4.colors[p,1+l,1])  # first triangle
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(-0.5+l, 0+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,2])
            polygon(c(1+p-1, 1.5+p-1, 1.5+p-1), c(l, 0.5+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,3])
            polygon(c(0.5+p-1, 1+p-1, 1.5+p-1), c(0.5+l, 0+l, 0.5+l), col=array.trans.diff4.colors[p,1+l,4])
        }
   }

   par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_Atlantic_ridge_4tables.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from Atlantic_Ridge_regime", cex=1.5)

   # NAO+ : 
   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.85, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,1])}}  

   par(mar=c(0,0,4,0), fig=c(0, 0.45, 0.42, 0.5), new=TRUE) 
   mtext("Blocking", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0, 0.45, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,4])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.85, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.6, 0.9), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,3])}}

   par(mar=c(0,0,4,0), fig=c(0.45, 0.9, 0.42, 0.5), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.5)
   par(mar=c(0,4,1,0), fig=c(0.45, 0.9, 0.12, 0.42), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Lead time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   mtext(side = 2, text = "Lead time", line = 2.5, cex=1.5)
   axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   axis(2, at=seq(0,6), las=2, cex.axis=1.5, labels=0:6)
   for(p in 1:12){ for(l in 0:6){ polygon(c(0.5+p-1, 0.5+p-1, 1.5+p-1, 1.5+p-1), c(-0.5+l, 0.5+l, 0.5+l, -0.5+l), col=array.trans.diff4.colors[p,1+l,2])}}

   par(fig=c(0.91, 1, 0.1, 0.9), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)

   dev.off()

   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_trans_Atlantic_ridge_1table.png"), width=800, height=300)
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Bias transition from Atlantic Ridge regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.22, 0.8, 0.98), new=TRUE) 
   mtext("NAO+", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0, 0.22, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff4.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.22, 0.44, 0.8, 0.98), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.22, 0.44, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff4.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.44, 0.66, 0.8, 0.98), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.44, 0.66, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff4.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.68, 0.88, 0.8, 0.98), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.66, 0.88, 0.1, 0.8), new=TRUE)
   plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,6.2), ann=F)
   mtext(side = 1, text = "Lead time", line = 2, cex=1)
   mtext(side = 2, text = "Target month", line = 2, cex=1)
   axis(1, at=seq(0,6), las=1, cex.axis=1, labels=6:0, mgp=c(3,0.7,0))
   axis(2, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1)
   for(i in 1:12){ for(j in 0:6){ i2=ifelse(i-(6-j) > 0, i-(6-j), i-(6-j)+12) ; polygon(c(-0.5+j, 0.5+j, 0.5+j, -0.5+j), c(0.5+i-1, 0.5+i-1, 1.5+i-1, 1.5+i-1), col=array.trans.diff4.colors[i2,1+6-j,2])}}  
    
   par(fig=c(0.91, 1, 0.1, 0.8), new=TRUE)
   ColorBar2(brks = my.seq, cols = trans.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
    
   dev.off()

   ## # FairRPSS summary:
   ## png(filename=paste0(work.dir,"/",forecast.name,"_summary_rpss.png"), width=550, height=400)
   
   ## # create an array similar to array.diff.freq but with colors instead of frequencies:
   ## freq.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

   ## array.freq.colors <- array(freq.cols[8],c(12,7,4))
   ## array.freq.colors[array.diff.freq < 10] <- freq.cols[7]
   ## array.freq.colors[array.diff.freq < 5] <- freq.cols[6]
   ## array.freq.colors[array.diff.freq < 2] <- freq.cols[5]
   ## array.freq.colors[array.diff.freq < 0] <- freq.cols[4]
   ## array.freq.colors[array.diff.freq < -2] <- freq.cols[3]
   ## array.freq.colors[array.diff.freq < -5] <- freq.cols[2]
   ## array.freq.colors[array.diff.freq < -10] <- freq.cols[1]
 
   ## par(mar=c(5,4,4,4.5))
   ## plot(1,1, type="n", xaxt="n", yaxt="n",ylab="Forecast time", xlim=c(1,12.04), ylim=c(-0.23,6.2), ann=F)
   ## title("Frequency difference (%) between S4 and ERA-Interim", cex=1.5)
   ## mtext(side = 1, text = "Startdate", line = 4, cex=1.5)
   ## mtext(side = 2, text = "Forecast time", line = 2.5, cex=1.5)
   ## axis(1, at=seq(1,12), las=2, labels=my.month.short, cex.axis=1.5)
   ## axis(2, at=seq(0,6), las=2, cex.axis=1.5)

   ## for(p in 1:12){
   ##      for(l in 0:6){
   ##          polygon(c(0.5+p-1,0.5+p-1,1+p-1),c(-0.5+l,0.5+l,0+l), col=array.freq.colors[p,1+l,1])  # first triangle
   ##          polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(-0.5+l,0+l,-0.5+l), col=array.freq.colors[p,1+l,2])
   ##          polygon(c(1+p-1,1.5+p-1,1.5+p-1),c(l,0.5+l,-0.5+l), col=array.freq.colors[p,1+l,3])
   ##          polygon(c(0.5+p-1,1+p-1,1.5+p-1),c(0.5+l,0+l,0.5+l), col=array.freq.colors[p,1+l,4])
   ##      }
   ## }

   ## par(fig=c(0.875, 1, 0.14, 0.89), new=TRUE)
   ## ColorBar2(brks = c(-20,-10,-5,-2,0,2,5,10,20), cols = freq.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(c(-20,-10,-5,-2,0,2,5,10,20)), my.labels=c(-20,-10,-5,-2,0,2,5,10,20))
   ## dev.off()

} # close if on composition == "summary"




# impact map of the stronger WR:
if(composition == "impact.highest" && fields.name == rean.name){
    
   var.num <- 2  # choose a variable (1:sfcWind, 2:tas)
   index <- 1   # chose an index: 1: most influent, 2: most influent positively, 3: most influent negatively
    
   if ( index == 1 ) png(filename=paste0(rean.dir,"/",rean.name,"_",var.name.full[var.num],"_most_influent.png"), width=550, height=650)
   if ( index == 2 ) png(filename=paste0(rean.dir,"/",rean.name,"_",var.name.full[var.num],"_most_influent_positively.png"), width=550, height=650)
   if ( index == 3 ) png(filename=paste0(rean.dir,"/",rean.name,"_",var.name.full[var.num],"_most_influent_negatively.png"), width=550, height=650)

   plot.new()    
   #par(mfrow=c(4,3))
   #col.regimes <- c('#7b3294','#c2a5cf','#a6dba0','#008837')
   col.regimes <- c('#e66101','#fdb863','#b2abd2','#5e3c99')
    
   for(p in 13:16){  # or 1:12 for monthly maps
        load(file=paste0(rean.dir,"/",rean.name,"_",my.period[p],"_",var.name[var.num],".RData"))
        load(paste0(rean.dir,"/",rean.name,"_",my.period[p],"_ClusterNames.RData")) # they should not depend on var.name!!!

        # position of long values of Europe only (without the Atlantic Sea and America):
        #if(fields.name == "ERA-Interim") EU <- c(1:which(lon >= lon.max)[1], (length(lon)-30):length(lon))  # restrict area to continental europe only
        lon.max = 45
        EU <- c(1:which(lon >= lon.max)[1], (which(lon >= 337)[1]:length(lon)))  # restrict area to continental europe only
        
        cluster1 <- which(orden == cluster1.name.period[p]) # in future it will be == cluster1.name
        cluster2 <- which(orden == cluster2.name.period[p])
        cluster3 <- which(orden == cluster3.name.period[p])
        cluster4 <- which(orden == cluster4.name.period[p])

        assign(paste0("imp",cluster1), varPeriodAnom1mean)
        assign(paste0("imp",cluster2), varPeriodAnom2mean)
        assign(paste0("imp",cluster3), varPeriodAnom3mean)
        assign(paste0("imp",cluster4), varPeriodAnom4mean)

        #most influent WT:
        if (index == 1) {
            imp.all <- imp1
            for(i in 1:dim(imp1)[1]){
                for(j in 1:dim(imp1)[2]){
                    if(abs(imp1[i,j]) >= max(abs(imp1[i,j]), abs(imp2[i,j]), abs(imp3[i,j]), abs(imp4[i,j]))) imp.all[i,j] <- 1.5
                    if(abs(imp2[i,j]) >= max(abs(imp1[i,j]), abs(imp2[i,j]), abs(imp3[i,j]), abs(imp4[i,j]))) imp.all[i,j] <- 2.5
                    if(abs(imp3[i,j]) >= max(abs(imp1[i,j]), abs(imp2[i,j]), abs(imp3[i,j]), abs(imp4[i,j]))) imp.all[i,j] <- 3.5
                    if(abs(imp4[i,j]) >= max(abs(imp1[i,j]), abs(imp2[i,j]), abs(imp3[i,j]), abs(imp4[i,j]))) imp.all[i,j] <- 4.5
                }
            }
        }

        #most influent WT with positive impact:
        if (index == 2) {
            imp.all <- imp1
            for(i in 1:dim(imp1)[1]){
                for(j in 1:dim(imp1)[2]){
                    if((imp1[i,j]) >= max((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 1.5
                    if((imp2[i,j]) >= max((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 2.5
                    if((imp3[i,j]) >= max((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 3.5
                    if((imp4[i,j]) >= max((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 4.5
                }
            }

        }

        # most influent WT with negative impact:
        if (index == 3) {
            imp.all <- imp1
            for(i in 1:dim(imp1)[1]){
                for(j in 1:dim(imp1)[2]){
                    if((imp1[i,j]) <= min((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 1.5
                    if((imp2[i,j]) <= min((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 2.5
                    if((imp3[i,j]) <= min((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 3.5
                    if((imp4[i,j]) <= min((imp1[i,j]), (imp2[i,j]), (imp3[i,j]), (imp4[i,j]))) imp.all[i,j] <- 4.5
                }
            }
        }
        
        if(p<=3) yMod <- 0.7
        if(p>=4 && p<=6) yMod <- 0.5
        if(p>=7 && p<=9) yMod <- 0.3
        if(p>=10) yMod <- 0.1
        if(p==13 || p==14) yMod <- 0.52
        if(p==15 || p==16) yMod <- 0.1
        
        if(p==1 || p==4 || p==7 || p==10) xMod <- 0.05
        if(p==2 || p==5 || p==8 || p==11) xMod <- 0.35
        if(p==3 || p==6 || p==9 || p==12) xMod <- 0.65
        if(p==13 || p==15) xMod <- 0.05
        if(p==14 || p==16) xMod <- 0.50
        
        # impact map:
        if(p <= 12) par(fig=c(xMod, xMod + 0.3, yMod, yMod + 0.17), new=TRUE)
        if(p > 12) par(fig=c(xMod, xMod + 0.45, yMod, yMod + 0.38), new=TRUE)
        PlotEquiMap(imp.all[,EU], lon[EU], lat, filled.continents = FALSE, brks=1:5, cols=col.regimes, axelab=F, drawleg=F)
        
        # title map:
        if(p <= 12) par(fig=c(xMod, xMod + 0.3, yMod + 0.162, yMod + 0.172), new=TRUE)
        if(p > 12) par(fig=c(xMod + 0.07, xMod + 0.07 + 0.3, yMod +0.21 + 0.166, yMod +0.21 + 0.176), new=TRUE)
        mtext(my.period[p], font=2, cex=1.5)
        
    } # close 'p' on 'period'
    
    par(fig=c(0.1,0.9, 0.03, 0.11), new=TRUE)  
    ColorBar2(1:5, cols=col.regimes, vert=FALSE, my.ticks=1:4, draw.ticks=FALSE, my.labels=orden)
    
    par(fig=c(0.1,0.9, 0.92, 0.97), new=TRUE)
    if( index == 1 ) mtext(paste0("Most influent WR on ",var.name[var.num]), font=2, cex=1.5)
    if( index == 2 ) mtext(paste0("Most positively influent WR on ",var.name[var.num]), font=2, cex=1.5)
    if( index == 3 ) mtext(paste0("Most negatively influent WR on ",var.name[var.num]), font=2, cex=1.5)

    dev.off()
    
} # close if on composition == "impact.highest"

