
# 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: You need to have created before the '_psl.RData' files which are the output of 'weather_regimes'.R, for each period you want to visualize.
#              If your regimes derive from a reanalysis, this script must be run twice:
#              first, only one month/season at time with: 'period=X', (X=1 .. 12), 'ordering = TRUE', 'save.names = TRUE', 'as.pdf = FALSE' and 'composition = "none"
#              And inserting the regime names 'cluster.name1=...' in the correct order; in this first run, you only save the ordered cartography.
#              You already have to know which is the right regime order by taking a look at the output maps (_clusterX.png) of weather_regimes.R
#              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.
#              You can check if the monthly regimes jave been associated correctly setting composition <- "psl.rean"
#
#              For example, you can find that the sequence of the images in the file (from top to down) corresponds to:
#              Blocking / NAO- / Atl.Ridge / NAO+ regime. Subsequently, you have 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.
#              Then, 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 associate 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
#library("corrplot")
 
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/45_as_43_but_with_Z500"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/44_as_43_but_with_no_lat_correction"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/50_NCEP_monthly_1981-2016_LOESS_filter_lat_corr"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/52_JRA55_monthly_1981-2016_LOESS_filter_lat_corr"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/53_JRA55_seasonal_1981-2016_LOESS_filter_lat_corr"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/54_JRA55_monthly_1981-2016_LOESS_filter_lat_corr_running"
#rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/55_JRA55_monthly_1981-2016_LOESS_filter_lat_corr_running_15days"
rean.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/56_JRA55_monthly_1981-2016_LOESS_filter_lat_corr_ordered4variance"

rean.name <- "JRA-55" #"JRA-55" #"NCEP" #"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)

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

composition <- "corr.matrix" # choose which kind of composition you want to plot:
                     #       'none' doesn't plot anything, it only associates the clusters to the regimes with the manual association in the rows below 
                     #              and saves them in the ClusterName.Rdata files for each period selected, overwriting the eventual pre-existing files.
                     #       '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 four impact maps in a composition 2x2
                     #       'single.psl' to save the individual psl map
                     #       'single.fre' to save the individual fre map
                     #       'taylor' plot the taylor's diagram between the regime anomalies of a monthly reanalaysis and a seasonal one
                     #       'edpr' as 'simple', but swapping the position of the regime anomalie maps with that of the impact maps
                     #       'psl.rean' for all the regime anomalies for all months of a reanalysis
                     #       'psl.rean.unordered': as before, but without ordering the regimes with the same order in vector 'orden'
                     #       'corr.matrix' : as before, but plot the correlation matrix only
                     #       'variance' : as 'none', but instead of associating clusters to regimes, it reordinates the clusters in decreasing order of explained variance

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

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

# 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     

# Manually associates the four clusters to the four regimes, one period at time (only in case composition="none"):
cluster1.name <- "NAO+"  
cluster2.name <- "NAO-"
cluster3.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 monthly_anomalies <- TRUE, you have to specify these additional parameters:
monthly_anomalies <- FALSE # if TRUE, also save maps with with monthly wind speed and slp anomalies for the chosen years and months set below over Europe
year.chosen <- 2016   
month.chosen <- 1:12
NCEP <- '/esnas/recon/noaa/ncep-reanalysis/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc'
JRA55 <- '/esnas/recon/jma/jra55/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc'
ERAint <- '/esarchive/old-files/recon_ecmwf_erainterim/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc'
rean.data <- JRA55  # choose one of the above reanalysis

####### 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 #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 ###############################################################################################################################################

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

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)

    } # close if on composition == "psl" ...

    if(composition == "none") {
        ordering <- TRUE
        save.names <- TRUE
        as.pdf <- FALSE
    }

    if(composition == "variance"){
        ordering <- TRUE
        save.names <- TRUE
        as.pdf <- FALSE
        period <- 1:12
    }

    if(composition == "psl.rean") {
        ordering <- TRUE
        period <- c(9:12, 1:8)  # to start from September instead of January
        png(filename=paste0(rean.dir,"/",fields.name,"_reanalysis_all_months.png"),width=6000,height=2000)
        plot.new()
    }

    if(composition == "psl.rean.unordered") {
        ordering <- FALSE
        period <- c(9:12, 1:8)  # to start from September instead of January
        png(filename=paste0(rean.dir,"/",fields.name,"_reanalysis_all_months_unordered.png"),width=6000,height=2000)
        plot.new()
    }

    if(composition == "corr.matrix") {
        ordering <- FALSE # set it to TRUE if you want to see the correlation matrix of the ordered clusters instead!!!
        period <- c(9:12, 1:8)  # to start from September instead of January
        png(filename=paste0(rean.dir,"/",fields.name,"_reanalysis_all_months_corr_matrix.png"),width=6000,height=2000)
        plot.new()
    }
   
    if(fields.name == rean.name) { lead.month <- 1; 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" || composition == 'edpr'){
            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
        }

        
        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')
            impact.data <- FALSE
            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(paste0("Impact data for variable ",var.name[var.num] ," available for reanalysis ", rean.name))
                    load(file=paste0(rean.dir,"/",rean.name,"_",my.period[p],"_",var.name[var.num],".RData"))
                } else {
                    impact.data <- FALSE
                    print(paste0("Impact data for variable ",var.name[var.num] ," not available for reanalysis ", rean.name))
                }

                if(composition == "variance"){
                     my.cluster2 <- my.cluster # create a copy of my.cluster
                     
                     ss1 <- which(my.cluster$cluster == 1)
                     ss2 <- which(my.cluster$cluster == 2)
                     ss3 <- which(my.cluster$cluster == 3)
                     ss4 <- which(my.cluster$cluster == 4)

                     withinss <- my.cluster$withinss
                     max1 <- which(my.cluster$withinss == max(withinss, na.rm=TRUE))
                     withinss[max1] <- NA
                     
                     max2 <- which(my.cluster$withinss == max(withinss, na.rm=TRUE))
                     withinss[max2] <- NA

                     max3 <- which(my.cluster$withinss == max(withinss, na.rm=TRUE))
                     withinss[max3] <- NA

                     max4 <- which(!is.na(withinss))
                     rm(withinss) 

                     max.seq <- c(max1, max2, max3, max4)
                     
                     assign(paste0("cluster",max1,".name"), orden[1]) # associate the cluster with the highest explained variance to the first regime to plot (usually NAO+)
                     assign(paste0("cluster",max2,".name"), orden[2])
                     assign(paste0("cluster",max3,".name"), orden[3])
                     assign(paste0("cluster",max4,".name"), orden[4])
                     
                 }
             }
            
            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:
                    ClusterName.file <- paste0(rean.dir,"/",rean.name,"_", my.period[p],"_","ClusterNames",".RData")
                    if(!file.exists(ClusterName.file)) stop(paste0("file: ",ClusterName.file," missing"))  # check if file exists or not
                    load(ClusterName.file) # load cluster names
                    
                    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 # same as: cluster1.name=orden[1], cluster2.name=orden[2], cluster3.name=orden[3], etc.
            } 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' || composition == 'edpr')){
                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(-21,-1,2),0,seq(1,21,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", continents.col="black")
                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", continents.col="black")
                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", continents.col="black")
                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", continents.col="black")

                ## # 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 ){ 
                    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, continents.col="black")
                    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, continents.col="black")
                    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, continents.col="black")
                    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, continents.col="black")

                    
                    # Title impact maps:
                    title2.xpos <- 0.42
                    title2.width <- 0.35
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.928, 0.933), 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.693, 0.698), 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.458, 0.463), 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.223, 0.227), 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.928, 0.933), 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.693, 0.698), new=TRUE)
                mtext(paste0(regime2.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.458, 0.463), new=TRUE)
                mtext(paste0(regime3.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.223, 0.228), 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.92, 0.93), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.59, 0.70), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.45, 0.46), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.22, 0.23), 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'


            if(composition == "edpr"){

                # adjust color legends to include triangles to the extremities increasing by two the number of intervals:
                my.cols <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks)+1)    # blue--white--red colors
                my.cols.var <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks.var)+1)    # blue--white--red colors
                
                fileoutput <- paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_edpr.png")
                
                png(filename=fileoutput,width=3000,height=3700)
                
                plot.new()
                
                ## Centroid maps:
                map.xpos <- 0.27
                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[2:(length(my.cols)-1)], 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", continents.col="black")
                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[2:(length(my.cols)-1)], sizetit=1.2, contours=t(map2), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F", continents.col="black")
                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[2:(length(my.cols)-1)], sizetit=1.2, contours=t(map3), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F", continents.col="black")
                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[2:(length(my.cols)-1)], sizetit=1.2, contours=t(map4), brks2=my.brks2, intxlon=10, intylat=10, drawleg=F, cex.lab=1.5, contours.lty="F1FF1F", continents.col="black")

                ## # 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.30
                legend1.width <- 0.40
                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[2:(length(my.cols)-1)], 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[2:(length(my.cols)-1)], 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[2:(length(my.cols)-1)], 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[2:(length(my.cols)-1)], 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.3
                title1.width <- 0.44
                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 ){ 
                    impact.xpos <- 0
                    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[2:(length(my.cols.var)-1)], intxlon=10, intylat=10, drawleg=F, dots=t(sig1[,EU] < 0.05), cex.lab=1.5, continents.col="black")
                    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[2:(length(my.cols.var)-1)], intxlon=10, intylat=10, drawleg=F, dots=t(sig2[,EU] < 0.05), cex.lab=1.5, continents.col="black")
                    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[2:(length(my.cols.var)-1)], intxlon=10, intylat=10, drawleg=F, dots=t(sig3[,EU] < 0.05), cex.lab=1.5, continents.col="black")
                    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[2:(length(my.cols.var)-1)], intxlon=10, intylat=10, drawleg=F, dots=t(sig4[,EU] < 0.05), cex.lab=1.5, continents.col="black")

                    
                    # Title impact maps:
                    title2.xpos <- 0
                    title2.width <- 0.26
                    par(fig=c(title2.xpos, title2.xpos + title2.width, 0.928, 0.933), 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.693, 0.698), 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.458, 0.463), 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.223, 0.227), new=TRUE)
                    mtext(paste0(regime4.name, " impact on ", var.name.full[var.num]), font=2, cex=4)

                    # Legend:
                    legend2.xpos <- 0.01
                    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[2:(length(my.cols.var)-1)], 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[2:(length(my.cols.var)-1)], 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[2:(length(my.cols.var)-1)], 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[2:(length(my.cols.var)-1)], 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.928, 0.933), 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.693, 0.698), new=TRUE)
                mtext(paste0(regime2.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.458, 0.463), new=TRUE)
                mtext(paste0(regime3.name, " Frequency"), font=2, cex=4)
                par(fig=c(title3.xpos, title3.xpos + title3.width, 0.223, 0.228), 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.91, 0.92), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.68, 0.69), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.44, 0.45), new=TRUE)
                mtext("%", cex=3.3)
                par(fig=c(symbol.xpos, symbol.xpos + symbol.width, 0.21, 0.22), new=TRUE)
                mtext("%", cex=3.3)

                # mean frequency on Frequency plots:
                if(mean.freq == TRUE){
                    symbol.xpos <- 0.83
                    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=3.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=3.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=3.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=3.5)
                }


                if(!as.pdf) dev.off() # for saving 4 png
                
                # same image formatted for the catalogue:
                fileoutput2 <- paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_edpr_fig2cat.png")            

                system(paste0("~/scripts/fig2catalog.sh -s 0.8 -r 150 -t '",rean.name," / 10m wind speed and sea level pressure / Monthly anomalies and frequencies \n",month.name[p]," / ",year.start,"-",year.end,"' -c 'Region: North Atlantic (27°N-81°N, 85.5°W-45°E)\nReference dataset: ",rean.name," reanalysis' ", fileoutput," ", fileoutput2))
     


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

            
            # 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'


             if(composition == "psl.rean" || composition == "psl.rean.unordered"){
                 
                # Centroid maps:
                n.map <- n.map + 1  # n.maps starts from 0
                map.width <- 0.08
                map.xpos <- 0 + 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")


            } # close if on composition == 'psl.rean' || 'psl.rean.unordered'


            if(composition == "corr.matrix"){
                
                # matrix correlation with DJF regimes:
                cluster1.monthly <- pslwr1mean; cluster2.monthly <- pslwr2mean; cluster3.monthly <- pslwr3mean; cluster4.monthly <- pslwr4mean

                rean.dir.DJF <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes/53_JRA55_seasonal_1981-2016_LOESS_filter_lat_corr"
 
                load(file=paste0(rean.dir.DJF,"/",rean.name,"_",my.period[13],"_","psl",".RData")) # Load mean slp DJF data from the same reanalysis
                load(paste0(rean.dir.DJF,"/",rean.name,"_", my.period[13],"_","ClusterNames",".RData")) # load also reanalysis DJF regime names

                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.orig != p) 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("psl.ordered",cluster1), pslwr1mean)
                assign(paste0("psl.ordered",cluster2), pslwr2mean)
                assign(paste0("psl.ordered",cluster3), pslwr3mean)
                assign(paste0("psl.ordered",cluster4), pslwr4mean)
                 
                cluster.corr <- array(NA, c(4,4), dimnames=list(c("cl1","cl2","cl3","cl4"), orden))
                cluster.corr[1,1] <- cor(as.vector(cluster1.monthly), as.vector(psl.ordered1))
                cluster.corr[1,2] <- cor(as.vector(cluster1.monthly), as.vector(psl.ordered2))
                cluster.corr[1,3] <- cor(as.vector(cluster1.monthly), as.vector(psl.ordered3))
                cluster.corr[1,4] <- cor(as.vector(cluster1.monthly), as.vector(psl.ordered4))
                cluster.corr[2,1] <- cor(as.vector(cluster2.monthly), as.vector(psl.ordered1))
                cluster.corr[2,2] <- cor(as.vector(cluster2.monthly), as.vector(psl.ordered2))
                cluster.corr[2,3] <- cor(as.vector(cluster2.monthly), as.vector(psl.ordered3))
                cluster.corr[2,4] <- cor(as.vector(cluster2.monthly), as.vector(psl.ordered4))
                cluster.corr[3,1] <- cor(as.vector(cluster3.monthly), as.vector(psl.ordered1))
                cluster.corr[3,2] <- cor(as.vector(cluster3.monthly), as.vector(psl.ordered2))
                cluster.corr[3,3] <- cor(as.vector(cluster3.monthly), as.vector(psl.ordered3))
                cluster.corr[3,4] <- cor(as.vector(cluster3.monthly), as.vector(psl.ordered4))
                cluster.corr[4,1] <- cor(as.vector(cluster4.monthly), as.vector(psl.ordered1))
                cluster.corr[4,2] <- cor(as.vector(cluster4.monthly), as.vector(psl.ordered2))
                cluster.corr[4,3] <- cor(as.vector(cluster4.monthly), as.vector(psl.ordered3))
                cluster.corr[4,4] <- cor(as.vector(cluster4.monthly), as.vector(psl.ordered4))

                #cluster.corr2 <- t(cluster.corr)                

                # Centroid maps:
                n.map <- n.map + 1  # n.maps starts from 0
                map.width <- 0.08
                map.xpos <- 0 + map.width * (n.map - 1)
                map.ypos <- 0.08
                map.height <- 0.19
                map.ysep <- 0.015
                print(paste0("map.xpos= ", map.xpos))
                 
                par(fig=c(0, 1, 0, 1), new=TRUE)
                text.cex <- 2
                text.ypos <- 1.03
                text(x=map.xpos - 0.015, y=text.ypos - 0.02, labels="cl1", cex=text.cex)
                text(x=map.xpos - 0.015, y=text.ypos - 0.04, labels="cl2", cex=text.cex)
                text(x=map.xpos - 0.015, y=text.ypos - 0.06, labels="cl3", cex=text.cex)
                text(x=map.xpos - 0.015, y=text.ypos - 0.08, labels="cl4", cex=text.cex)
                text(x=map.xpos + 0.000, y=text.ypos + 0.00, labels="NAO+", cex=text.cex)
                text(x=map.xpos + 0.015, y=text.ypos + 0.00, labels="NAO-", cex=text.cex)
                text(x=map.xpos + 0.030, y=text.ypos + 0.00, labels="BLO", cex=text.cex)
                text(x=map.xpos + 0.045, y=text.ypos + 0.00, labels="ATL", cex=text.cex)
                 
                text(x=map.xpos + 0.00, y=text.ypos - 0.02, labels=round(cluster.corr,2)[1,1], cex=text.cex)
                text(x=map.xpos + 0.00, y=text.ypos - 0.04, labels=round(cluster.corr,2)[2,1], cex=text.cex)
                text(x=map.xpos + 0.00, y=text.ypos - 0.06, labels=round(cluster.corr,2)[3,1], cex=text.cex)
                text(x=map.xpos + 0.00, y=text.ypos - 0.08, labels=round(cluster.corr,2)[4,1], cex=text.cex)
                text(x=map.xpos + 0.015, y=text.ypos - 0.02, labels=round(cluster.corr,2)[1,2], cex=text.cex)
                text(x=map.xpos + 0.015, y=text.ypos - 0.04, labels=round(cluster.corr,2)[2,2], cex=text.cex)
                text(x=map.xpos + 0.015, y=text.ypos - 0.06, labels=round(cluster.corr,2)[3,2], cex=text.cex)
                text(x=map.xpos + 0.015, y=text.ypos - 0.08, labels=round(cluster.corr,2)[4,2], cex=text.cex)
                text(x=map.xpos + 0.03, y=text.ypos - 0.02, labels=round(cluster.corr,2)[1,3], cex=text.cex)
                text(x=map.xpos + 0.03, y=text.ypos - 0.04, labels=round(cluster.corr,2)[2,3], cex=text.cex)
                text(x=map.xpos + 0.03, y=text.ypos - 0.06, labels=round(cluster.corr,2)[3,3], cex=text.cex)
                text(x=map.xpos + 0.03, y=text.ypos - 0.08, labels=round(cluster.corr,2)[4,3], cex=text.cex)
                text(x=map.xpos + 0.045, y=text.ypos - 0.02, labels=round(cluster.corr,2)[1,4], cex=text.cex)
                text(x=map.xpos + 0.045, y=text.ypos - 0.04, labels=round(cluster.corr,2)[2,4], cex=text.cex)
                text(x=map.xpos + 0.045, y=text.ypos - 0.06, labels=round(cluster.corr,2)[3,4], cex=text.cex)
                text(x=map.xpos + 0.045, y=text.ypos - 0.08, labels=round(cluster.corr,2)[4,4], cex=text.cex)

                ## Centroid maps:
                
                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")

             } # close if on corr.matrix
            
            
            # 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 2x2 composition of the four impact maps for a selected reanalysis or forecasted month:
            if(composition == "single.impact"){
                
                png(filename=paste0(rean.dir,"/",fields.name,"_",my.period[p],"_",var.name[var.num],"_impact_composition.png"),width=2000,height=2000)

                plot.new()
                
                par(fig=c(0, 0.5, 0.95, 0.988), new=TRUE)
                mtext("NAO+",cex=5)
                par(fig=c(0, 0.5, 0.52, 0.95), new=TRUE)
                PlotEquiMap(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=sig1[,EU] < 0.05, dot_size=2, axes_label_scale=2.5)

                par(fig=c(0.5, 1, 0.93, 0.96), new=TRUE)
                mtext("NAO-",cex=5)
                par(fig=c(0.5, 1, 0.52, 0.95), new=TRUE)
                PlotEquiMap(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=sig2[,EU] < 0.05, dot_size=2, axes_label_scale=2.5)

                par(fig=c(0, 0.5, 0.44, 0.47), new=TRUE)
                mtext("Blocking",cex=5)
                par(fig=c(0, 0.5, 0.08, 0.46), new=TRUE)
                PlotEquiMap(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=sig3[,EU] < 0.05, dot_size=2, axes_label_scale=2.5)

                par(fig=c(0.5, 1, 0.44, 0.47), new=TRUE)
                mtext("Atlantic Ridge",cex=5)
                par(fig=c(0.5, 1, 0.08, 0.46), new=TRUE)
                PlotEquiMap(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=sig4[,EU] < 0.05, dot_size=2, axes_label_scale=2.5)
                
                par(fig=c(0.03, 0.96, 0.02, 0.08), new=TRUE)
                ColorBar2(my.brks.var, cols=my.cols.var, vert=FALSE, cex=3, my.ticks=-0.5 + 1:length(my.brks.var), my.labels=my.brks.var, label.dist=3)
                par(fig=c(0.965, 0.99, 0, 0.026), new=TRUE)
                mtext("m/s",cex=3)
                
                dev.off()

                # format it manually from the bash shell (you must run it from your workstation until the identity command of ImageMagick will be loaded un the cluster):
                #sh ~/scripts/fig2catalog.sh -x 20 -t 'NCEP / 10-m wind speed / Regime impact \nOctober / 1981-2016' -c 'Region: North Atlantic (27°N-81°N, 85.5°W-45°E)\nReference dataset: NCEP/NCAR reanalysis'  NCEP_October_sfcWind_impact_composition.png  NCEP_October_sfcWind_impact_composition_catalogue.png


                ### to plot the impact map of all regimes on a particular month:
                #imp.oct <- (imp1*3.2 + imp2*38.7 + imp3*51.6 + imp4*6.4)/100
                year.test <- 2016
                pos.year.test <- year.test - year.start +1 
                imp.test <- imp1*fre1.NA[pos.year.test] + imp2*fre2.NA[pos.year.test] + imp3*fre3.NA[pos.year.test] + imp4*fre4.NA[pos.year.test]
                #imp.test <- imp1*fre1.NA[pos.year.test] + imp3*(fre3.NA[pos.year.test]+0.032) + imp4*(fre4.NA[pos.year.test]+0.032)
                par(fig=c(0, 1, 0.05, 1), new=TRUE)
                PlotEquiMap2(rescale(imp.test[,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)

                # vector with the frequency of the WRs in the chosen month and year:
                wt.test.freq <- c(fre1.NA[pos.year.test],fre2.NA[pos.year.test],fre3.NA[pos.year.test],fre4.NA[pos.year.test])
                
                ## # or save them as individual maps:
                ## 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((composition == 'simple' || composition == 'edpr' ) && 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


    if(composition == "psl.rean" || composition == "psl.rean.unordered" || composition == "corr.matrix") dev.off()
    
    print("Finished!")
} # close if on composition != "summary"



#} # close for on forecasts.month



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

   fields.name="ERA-Interim"

   # 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_18_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_psl.RData") # load pslwrXmean data
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_18_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"
   rean2.name <- "ERA-Interim"
   
   # 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_18_but_with_no_lat_correction_and_with_LOESS/ERA-Interim_Winter_psl.RData") # load pslwrXmean data
   load("/scratch/Earth/ncortesi/RESILIENCE/Regimes/46_as_18_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,"/",rean.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()

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

       png(filename=paste0(rean.dir,"/",rean.name,"_taylor_",orden[regime],".png"), width=1200, height=800)
       
       for(p in 1:12){
           p.orig <- p
           
           load(file=paste0(rean.dir,"/",rean.name,"_",my.period[p],"_","psl",".RData"))  # load cluster names and summarized data
           load(file=paste0(rean.dir,"/",rean.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,"/",rean2.name,"_",my.period[p],"_","psl",".RData"))  # load cluster names and summarized data
           load(file=paste0(rean2.dir,"/",rean2.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" # "black"
           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
           #add.mod <- ifelse(p == 1, FALSE, 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.freq.obs <- array.pers.obs <- array(NA,c(12,7,4))
   array.trans.obs1 <- array.trans.obs2 <- array.trans.obs3 <- array.trans.obs4 <- 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.freq.obs[p,1+l, 1] <- 100*(mean(freObs1))  # NAO+
            array.freq.obs[p,1+l, 3] <- 100*(mean(freObs2))  # NAO-
            array.freq.obs[p,1+l, 4] <- 100*(mean(freObs3))  # Blocking
            array.freq.obs[p,1+l, 2] <- 100*(mean(freObs4))  # 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.pers.obs[p,1+l, 1] <- persistObs1  # NAO+
            array.pers.obs[p,1+l, 3] <- persistObs2  # NAO-
            array.pers.obs[p,1+l, 4] <- persistObs3  # Blocking
            array.pers.obs[p,1+l, 2] <- persistObs4  # 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_spatial_correlations.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.spat.cor.colors <- array(sp.corr.cols[8],c(12,7,4))
   array.spat.cor.colors[array.spat.cor < my.seq[8]] <- sp.corr.cols[7]
   array.spat.cor.colors[array.spat.cor < my.seq[7]] <- sp.corr.cols[6]
   array.spat.cor.colors[array.spat.cor < my.seq[6]] <- sp.corr.cols[5]
   array.spat.cor.colors[array.spat.cor < my.seq[5]] <- sp.corr.cols[4]
   array.spat.cor.colors[array.spat.cor < my.seq[4]] <- sp.corr.cols[3]
   array.spat.cor.colors[array.spat.cor < my.seq[3]] <- sp.corr.cols[2]
   array.spat.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.spat.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.spat.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.spat.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.spat.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_spatial_correlations_startdate.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_spatial_correlations_target_month.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.6, 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.6, 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.6, 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.6, 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()







    
   # Temporal Corr summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_temporal_correlations.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_temporal_correlations_startdate.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_temporal_correlations_target_month.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.6, 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.6, 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.6, 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.6, 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()



    

    
   # Frequency summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_frequency.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_frequency_startdate.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_frequency_target_month.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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1) 
   dev.off()








   # Obs. frequency summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_frequency_obs.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.obs < my.seq[[8]]] <- freq.cols[7]
   array.freq.colors[array.freq.obs < my.seq[[7]]] <- freq.cols[6]
   array.freq.colors[array.freq.obs < my.seq[[6]]] <- freq.cols[5]
   array.freq.colors[array.freq.obs < my.seq[[5]]] <- freq.cols[4]
   array.freq.colors[array.freq.obs < my.seq[[4]]] <- freq.cols[3]
   array.freq.colors[array.freq.obs < my.seq[[3]]] <- freq.cols[2]
   array.freq.colors[array.freq.obs < 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 observed 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_frequency_obs_startdate.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 observed 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_frequency_obs_target_month.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 observed 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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1) 
   dev.off()





    

    
   
   # Bias freq. summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_frequency.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_bias_frequency_startdate.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_bias_frequency_target_month.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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1) 
   dev.off()






    
   
   # Simulated persistence summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_persistence.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("ECMWF-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_persistence_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("ECMWF-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_persistence_target_month.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("ECMWF-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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.9, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "days/month", line = 2, cex=1)
   dev.off()



    



   # Observed persistence summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_persistence_obs.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.obs < my.seq[8]] <- pers.cols[7]
   array.pers.colors[array.pers.obs < my.seq[7]] <- pers.cols[6]
   array.pers.colors[array.pers.obs < my.seq[6]] <- pers.cols[5]
   array.pers.colors[array.pers.obs < my.seq[5]] <- pers.cols[4]
   array.pers.colors[array.pers.obs < my.seq[4]] <- pers.cols[3]
   array.pers.colors[array.pers.obs < my.seq[3]] <- pers.cols[2]
   array.pers.colors[array.pers.obs < 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("Observed 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_persistence_obs_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Observed 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_persistence_obs_target_month.png"), width=800, height=300)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("Observed 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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.9, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "days/month", line = 2, cex=1)
   dev.off()











    
   # Bias persistence summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_persistence.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_bias_persistence_startdate.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_bias_persistence_target_month.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.6, 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.6, 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.6, 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.6, 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)
   par(fig=c(0.9, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "days/month", line = 2, cex=1) 
   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_frequency_startdate.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_frequency_target_month.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.6, 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.6, 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.6, 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.6, 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],"_startdate.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],"_target_month.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.6, 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.6, 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.6, 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.6, 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_transition_prob_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("% ECMWF-S4 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_transition_prob_NAO+_startdate.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)
   mtext("ECMWF-S4 / NAO+ Transition Probability \nJanuary to December / 1994-2013", 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_transition_prob_NAO+_target_month.png"), width=750, height=350)

   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 0.95), 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.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()

    




   # Observed Transition probability from NAO+ to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_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("% Observed 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.obs1.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.obs1.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.obs1.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.obs1.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_transition_prob_NAO+_obs_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Observed 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_transition_prob_NAO+_obs_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)

   dev.off()








    

    
   # Simulated transition probability from NAO- to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_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("% ECMWF-S4 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_transition_prob_NAO-_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% ECMWF-S4 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_transition_prob_NAO-_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.1, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()


    



   # Observed transition probability from NAO- to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_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.obs2.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.obs2.colors[array.trans.obs2 < my.seq[8]] <- trans.cols[7]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[7]] <- trans.cols[6]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[6]] <- trans.cols[5]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[5]] <- trans.cols[4]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[4]] <- trans.cols[3]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[3]] <- trans.cols[2]
   array.trans.obs2.colors[array.trans.obs2 < my.seq[2]] <- trans.cols[1]
   array.trans.obs2.colors[is.na(array.trans.obs2)] <- "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("% Observed 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.obs2.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.obs2.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.obs2.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.obs2.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_transition_prob_NAO-_obs_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Observed 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.obs2.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.obs2.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.obs2.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.obs2.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_transition_prob_NAO-_obs_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.07, 0.28, 0.8, 0.95), 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.8, 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.obs2.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.obs2.colors[i2,1+6-j,4])}}  

   par(mar=c(0,0,4,0), fig=c(0.58, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.58, 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.8, 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.obs2.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()








    
    
   # Simulated transition probability from blocking to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_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("% ECMWF-S4 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_transition_prob_blocking_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% ECMWF-S4 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_transition_prob_blocking_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()

    






   # Observed transition probability from blocking to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_blocking_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.obs3.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.obs3.colors[array.trans.obs3 < my.seq[8]] <- trans.cols[7]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[7]] <- trans.cols[6]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[6]] <- trans.cols[5]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[5]] <- trans.cols[4]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[4]] <- trans.cols[3]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[3]] <- trans.cols[2]
   array.trans.obs3.colors[array.trans.obs3 < my.seq[2]] <- trans.cols[1]
   array.trans.obs3.colors[is.na(array.trans.obs3)] <- "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("% Observed 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.obs3.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.obs3.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.obs3.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.obs3.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_transition_prob_blocking_obs_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Observed 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.obs3.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.obs3.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.obs3.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.obs3.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_transition_prob_blocking_obs_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.10, 0.28, 0.8, 0.95), 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.8, 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.obs3.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.obs3.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.obs3.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()

    





    

    
    





    
   # Simulated transition probability from Atlantic ridge to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_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("% ECMWF-S4 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_transition_prob_Atlantic_ridge_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% ECMWF-S4 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_transition_prob_Atlantic_ridge_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.1, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()





    



   # Observed transition probability from Atlantic ridge to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_transition_prob_Atlantic_ridge_obs.png"), width=550, height=400)
   
   # create an array obsilar 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.obs4.colors <- array(trans.cols[8],c(12,7,4))
   array.trans.obs4.colors[array.trans.obs4 < my.seq[8]] <- trans.cols[7]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[7]] <- trans.cols[6]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[6]] <- trans.cols[5]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[5]] <- trans.cols[4]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[4]] <- trans.cols[3]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[3]] <- trans.cols[2]
   array.trans.obs4.colors[array.trans.obs4 < my.seq[2]] <- trans.cols[1]
   array.trans.obs4.colors[is.na(array.trans.obs4)] <- "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("% Observed 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.obs4.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.obs4.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.obs4.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.obs4.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_transition_prob_Atlantic_ridge_obs_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 1), new=TRUE) 
   mtext("% Observed 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.obs4.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.obs4.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.obs4.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.obs4.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_transition_prob_Atlantic_ridge_obs_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.1, 0.28, 0.8, 0.95), 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.8, 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.obs4.colors[i2,1+6-j,1])}}  

   par(mar=c(0,0,4,0), fig=c(0.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.obs4.colors[i2,1+6-j,3])}}  

   par(mar=c(0,0,4,0), fig=c(0.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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.obs4.colors[i2,1+6-j,4])}}  
    
   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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()












    

   # Bias of the transition probability from NAO+ to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_transition_prob_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_transition_prob_NAO+_startdate.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_transition_prob_NAO+_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.07, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()















   # Bias of the Transition probability from NAO- to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_transition_prob_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_transition_prob_NAO-_startdate.png"), width=750, height=600)
    
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 0.95), 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_transition_prob_NAO-_target_month.png"), width=750, height=350)

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

   par(mar=c(0,0,4,0), fig=c(0.07, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()

    







    

   # Bias of the Transition probability from blocking to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_transition_prob_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_transition_prob_blocking_startdate.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_transition_prob_blocking_target_month.png"), width=750, height=350)
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 0.95), new=TRUE) 
   mtext("% Bias transition from Blocking regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Atlantic Ridge", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   dev.off()


    
    






   # Bias of the Transition probability from Atlantic Ridge to X summary:
   png(filename=paste0(work.dir,"/",forecast.name,"_summary_bias_transition_prob_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_transition_prob_Atlantic_ridge_startdate.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_transition_prob_Atlantic_ridge_target_month.png"), width=750, height=350)
   plot.new()
   par(mar=c(0,0,2,0), fig=c(0, 1, 0.9, 0.95), new=TRUE) 
   mtext("% Bias transition from Atlantic Ridge regime", cex=1.2)

   par(mar=c(0,0,4,0), fig=c(0.07, 0.28, 0.8, 0.95), 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.8, 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.30, 0.58, 0.8, 0.95), new=TRUE) 
   mtext("NAO-", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.30, 0.58, 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.8, 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.60, 0.88, 0.8, 0.95), new=TRUE) 
   mtext("Blocking", cex=1.2)
   par(mar=c(1,4,1,0), fig=c(0.60, 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.8, 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(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)
   par(fig=c(0.91, 1, 0.88, 0.93), new=TRUE)
   mtext(side = 1, text = "%", line = 2, cex=1)
    
   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"













if(monthly_anomalies) {
    # Compute climatology and anomalies (with LOESS filter) for sfcWind data, and draw monthly anomalies, if you want to compare the mean daily wind speed anomalies reconstructed by the weather regimes classification with those observed by the reanalysis:

    load(paste0(rean.dir,"/",rean.name,"_",my.period[month.chosen[1]],"_psl.RData")) # only to load year.start and year.end

    sfcWind366 <-  Load(var = "sfcWind", exp = NULL, obs = list(list(path=rean.data)), paste0(year.start:year.end,'0101'), storefreq = 'daily', leadtimemax = 366, output = 'lonlat', latmin = lat.min, latmax = lat.max, lonmin = lon.min, lonmax = lon.max, nprocs=8)$obs

    sfcWind <- sfcWind366[,,,1:365,,]
    
    for(y in year.start:year.end){
        y2 <- y - year.start + 1
        if(n.days.in.a.year(y) == 366) sfcWind[y2,60:365,,] <- sfcWind366[1,1,y2,61:366,,] # take the march to december period removing the 29th of February
    }

    rm(sfcWind366)
    gc()

    sfcWindClimDaily <- apply(sfcWind, c(2,3,4), mean, na.rm=T)

    sfcWindClimLoess <- sfcWindClimDaily
    for(i in n.pos.lat){
            for(j in n.pos.lon){
                my.data <- data.frame(ens.mean=sfcWindClimDaily[,i,j], day=1:365)
                my.loess <- loess(ens.mean ~ day, my.data, span=0.35)
                sfcWindClimLoess[,i,j] <- predict(my.loess)
            }
    }

    rm(sfcWindClimDaily)
    gc()

    sfcWindClim2 <- InsertDim(sfcWindClimLoess, 1, n.years)
    
    sfcWindAnom <- sfcWind - sfcWindClim2
    
    rm(sfcWindClimLoess)
    gc()

    # same as above but for computing climatology and anomalies (with LOESS filter) for psl data, and draw monthly slp anomalies:
    slp366 <-  Load(var = psl, exp = NULL, obs = list(list(path=rean.data)), paste0(year.start:year.end,'0101'), storefreq = 'daily', leadtimemax = 366, output = 'lonlat', latmin = lat.min, latmax = lat.max, lonmin = lon.min, lonmax = lon.max, nprocs=8)$obs
    
    slp <- slp366[,,,1:365,,]
    
    for(y in year.start:year.end){
        y2 <- y - year.start + 1
        if(n.days.in.a.year(y) == 366) slp[y2,60:365,,] <- slp366[1,1,y2,61:366,,] # take the march to december period removing the 29th of February
    }

    rm(slp366)
    gc()

    slpClimDaily <- apply(slp, c(2,3,4), mean, na.rm=T)
    
    slpClimLoess <- slpClimDaily
    for(i in n.pos.lat){
            for(j in n.pos.lon){
                my.data <- data.frame(ens.mean=slpClimDaily[,i,j], day=1:365)
                my.loess <- loess(ens.mean ~ day, my.data, span=0.35)
                slpClimLoess[,i,j] <- predict(my.loess)
            }
    }

    rm(slpClimDaily)
    gc()

    slpClim2 <- InsertDim(slpClimLoess, 1, n.years)
    
    slpAnom <- slp - slpClim2
    
    rm(slpClimLoess)
    gc()

    if(psl == "psl") slpAnom <- slpAnom/100   # convert MSLP in Pascal to MSLP in hPa
    
    EU <- c(1:which(lon >= lon.max)[1], (which(lon >= 337)[1]:length(lon)))  # restrict area to continental europe only    

    my.brks.var <- c(-20,seq(-3,3,0.5),20)
    my.cols.var <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks.var)-1)    # blue--white--red colors

    # same but for slp:           
    my.brks.var2 <- c(-100,seq(-21,-1,2),0,seq(1,21,2),100)
    my.cols.var2 <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks.var2)-1)    # blue--red colors
    my.cols.var2[floor(length(my.cols.var2)/2)] <- my.cols.var2[floor(length(my.cols.var2)/2)+1] <- "white"

    # save all monthly anomaly maps:
    for(year.test in year.chosen){
        for(month.test in month.chosen){
            #year.test <- 2016; month.test <- 10 # for the debug
            
            pos.year.test <- year.test - year.start + 1

            # wind anomaly for the chosen year and month:
            sfcWindAnomPeriod <- sfcWindAnom[pos.year.test, pos.period(1,month.test),,]
            
            sfcWindAnomPeriodMean <- apply(sfcWindAnomPeriod, c(2,3), mean, na.rm=TRUE)

            # psl anomaly for the chosen year and month:
            slpAnomPeriod <- slpAnom[pos.year.test,pos.period(1,month.test),,]
            
            slpAnomPeriodMean <- apply(slpAnomPeriod, c(2,3), mean, na.rm=TRUE)
            
            fileoutput <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_monthly_anomaly.png")
            
            png(filename=fileoutput,width=2800,height=1000)

            par(fig=c(0, 0.36, 0.08, 0.98), new=TRUE)
            #PlotEquiMap(rescale(sfcWindAnomPeriodMean[,EU],my.brks.var[1],tail(my.brks.var,1)), lon[EU], lat, filled.continents=FALSE, brks=my.brks.var, cols=my.cols.var[2:(length(my.cols.var)-1)], title_scale=1.2, intxlon=10, intylat=10, drawleg=F) #, cex.lab=1.5)
            
            PlotEquiMap(sfcWindAnomPeriodMean[,EU], lon[EU], lat, filled.continents=FALSE, brks=my.brks.var, cols=my.cols.var, title_scale=1.2, intxlon=10, intylat=10, drawleg=F) #, cex.lab=1.5)

            #my.subset2 <- match(values.to.plot2, my.brks.var)
            #legend2.cex <- 1.8
            
            par(fig=c(0.03, 0.36, 0.015, 0.09), new=TRUE)
            #ColorBar(my.brks.var, cols=my.cols.var, vert=FALSE, label_scale=1.8, triangle_ends=c(FALSE,FALSE)) #, subset=my.subset2)
            #ColorBar(my.brks.var, cols=my.cols.var[2:(length(my.cols.var)-1)], vert=FALSE, label_scale=1.8, var_limits=c(-10,10), bar_limits=c(my.brks.var[1],my.brks.var[l(my.brks.var)]), col_inf=my.cols.var[1], col_sup=my.cols.var[length(my.cols.var)])

            ColorBar(brks=my.brks.var[2:(l(my.brks.var)-1)], cols=my.cols.var[2:(l(my.cols.var)-1)], vert=FALSE, label_scale=1.8, bar_limits=c(my.brks.var[2],my.brks.var[l(my.brks.var)-1]), col_inf=my.cols.var[1], col_sup=my.cols.var[l(my.cols.var)], subsample=1)

            par(fig=c(0.34, 0.37, 0, 0.028), new=TRUE)
            mtext("m/s", cex=1.8)

            par(fig=c(0.37, 1, 0.1, 0.98), new=TRUE)

            PlotEquiMap2(slpAnomPeriodMean, lon, lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=t(slpAnomPeriodMean), contours.lty="F1FF1F", cex.lab=1)

            # you could almost use the normal PlotEquiMap funcion below, it only needs to set the black line for anomaly=0 and decrease the size of the contour labels:
            # PlotEquiMap(rescale(slpAnomPeriodMean[,EU], my.brks.var2[1], tail(my.brks.var2,1)), lon[EU], lat, filled.continents=FALSE, brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=(slpAnomPeriodMean[,EU]), contour_lty="F1FF1F", contour_label_scale=10, title_scale=1.2) #, cex.lab=1.5)

            ##my.subset2 <- match(values.to.plot2, my.brks.var)
            #legend2.cex <- 1.8
            
            par(fig=c(0.37, 1, 0, 0.09), new=TRUE)
            #ColorBar2(my.brks.var2, cols=my.cols.var2, vert=FALSE, my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) #, subsampleg=1, label_scale=1.8) #, subset=my.subset2)
            ColorBar(my.brks.var2[2:(l(my.brks.var2)-1)], cols=my.cols.var2[2:(l(my.cols.var2)-1)], vert=FALSE, label_scale=1.8, bar_limits=c(my.brks.var2[2],my.brks.var2[l(my.brks.var2)-1]), col_inf=my.cols.var2[1], col_sup=my.cols.var2[l(my.cols.var2)], subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)

            par(fig=c(0.96, 0.99, 0, 0.027), new=TRUE)
            mtext("hPa", cex=1.8)
            
            #par(fig=c(0.74, 0.76, 0, 0.027), new=TRUE)
            #mtext("0", cex=1.8)

            dev.off()

            # same image formatted for the catalogue:
            fileoutput2 <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_monthly_anomaly_fig2cat.png")            

            system(paste0("~/scripts/fig2catalog.sh -s 0.8 -t '",rean.name," / 10m wind speed and sea level pressure / Monthly anomalies \n",month.name[month.test]," / ",year.test,"' -c 'Region: North Atlantic (27°N-81°N, 85.5°W-45°E)\nReference dataset: ",rean.name," reanalysis' ", fileoutput," ", fileoutput2))

            
            # mean wind speed and slp anomaly only during days of the chosen year:
            load(paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_psl.RData")) # load cluster.sequence

            # arrange the regime sequence in an array, to separate months from years:
            cluster2D <- array(cluster.sequence, c(n.days.in.a.period(month.test,1),n.years))
            #cluster2D <- array(my.cluster$cluster, c(n.days.in.a.period(p,1),n.years)) # only for NCEP

            cluster.test <- cluster2D[,pos.year.test]

            fre1.days <- length(which(cluster.test == 1))
            fre2.days <- length(which(cluster.test == 2))
            fre3.days <- length(which(cluster.test == 3))
            fre4.days <- length(which(cluster.test == 4))

            # wind anomaly for the chosen year and month and cluster:
            sfcWind1 <- sfcWindAnomPeriod[which(cluster.test == 1),,,drop=FALSE]
            sfcWind2 <- sfcWindAnomPeriod[which(cluster.test == 2),,,drop=FALSE]
            sfcWind3 <- sfcWindAnomPeriod[which(cluster.test == 3),,,drop=FALSE]
            sfcWind4 <- sfcWindAnomPeriod[which(cluster.test == 4),,,drop=FALSE]

            # in case a regime has NO days in that month, we must set it to NA:
            if(length(which(cluster.test == 1)) == 0 ) sfcWind1 <- array(NA, c(1,dim(sfcWindAnomPeriod)[2:3]))
            if(length(which(cluster.test == 2)) == 0 ) sfcWind2 <- array(NA, c(1,dim(sfcWindAnomPeriod)[2:3]))
            if(length(which(cluster.test == 3)) == 0 ) sfcWind3 <- array(NA, c(1,dim(sfcWindAnomPeriod)[2:3]))
            if(length(which(cluster.test == 4)) == 0 ) sfcWind4 <- array(NA, c(1,dim(sfcWindAnomPeriod)[2:3]))

            sfcWindMean1 <- apply(sfcWind1, c(2,3), mean, na.rm=FALSE)
            sfcWindMean2 <- apply(sfcWind2, c(2,3), mean, na.rm=FALSE)
            sfcWindMean3 <- apply(sfcWind3, c(2,3), mean, na.rm=FALSE)
            sfcWindMean4 <- apply(sfcWind4, c(2,3), mean, na.rm=FALSE)

            # load regime names:
            load(paste0(rean.dir,"/",rean.name,"_", my.period[p],"_","ClusterNames",".RData"))
            
            ## add strip with daily sequence of WRs:

            mod.name1 <- substr(cluster1.name, nchar(cluster1.name), nchar(cluster1.name))
            mod.name2 <- substr(cluster2.name, nchar(cluster2.name), nchar(cluster2.name))
            mod.name3 <- substr(cluster3.name, nchar(cluster3.name), nchar(cluster3.name))
            mod.name4 <- substr(cluster4.name, nchar(cluster4.name), nchar(cluster4.name))

            cluster1.name.short <- substr(cluster1.name,1,1)
            cluster2.name.short <- substr(cluster2.name,1,1)
            cluster3.name.short <- substr(cluster3.name,1,1)
            cluster4.name.short <- substr(cluster4.name,1,1)

            ## add + or - at the end of the cluster name, if it is a NAO+ or NAO- regime:
            if(mod.name1 == "+" || mod.name1 == "-") cluster1.name.short <- paste0(substr(cluster1.name,1,1), mod.name1)
            if(mod.name2 == "+" || mod.name2 == "-") cluster2.name.short <- paste0(substr(cluster2.name,1,1), mod.name2)
            if(mod.name3 == "+" || mod.name3 == "-") cluster3.name.short <- paste0(substr(cluster3.name,1,1), mod.name3)
            if(mod.name4 == "+" || mod.name4 == "-") cluster4.name.short <- paste0(substr(cluster4.name,1,1), mod.name4)
            
            c1 <- which(cluster.test == 1)
            c2 <- which(cluster.test == 2)
            c3 <- which(cluster.test == 3)
            c4 <- which(cluster.test == 4)

            cluster.test.letters <- cluster.test
            cluster.test.letters[c1] <- cluster1.name.short
            cluster.test.letters[c2] <- cluster2.name.short
            cluster.test.letters[c3] <- cluster3.name.short
            cluster.test.letters[c4] <- cluster4.name.short

            cluster.col <- cluster.test.letters
            cluster.col[which(cluster.test.letters == "N+")] <- "Firebrick1"
            cluster.col[which(cluster.test.letters == "N-")] <- "Dodgerblue1"
            cluster.col[which(cluster.test.letters == "B")] <- "White"
            cluster.col[which(cluster.test.letters == "A")] <- "Darkgoldenrod1"

            orden <- c("NAO+","NAO-","Blocking","Atl.Ridge")
            cluster1 <- which(orden == cluster1.name)
            cluster2 <- which(orden == cluster2.name)
            cluster3 <- which(orden == cluster3.name)
            cluster4 <- which(orden == cluster4.name)

            assign(paste0("fre.days",cluster1), fre1.days)
            assign(paste0("fre.days",cluster2), fre2.days)
            assign(paste0("fre.days",cluster3), fre3.days)
            assign(paste0("fre.days",cluster4), fre4.days)

            assign(paste0("fre",cluster1), wr1y)
            assign(paste0("fre",cluster2), wr2y)
            assign(paste0("fre",cluster3), wr3y)
            assign(paste0("fre",cluster4), wr4y)
            
            assign(paste0("imp.test",cluster1), sfcWindMean1)
            assign(paste0("imp.test",cluster2), sfcWindMean2)
            assign(paste0("imp.test",cluster3), sfcWindMean3)
            assign(paste0("imp.test",cluster4), sfcWindMean4)
            
            # psl anomaly for the chosen year and month and cluster:
            slp1 <- slpAnomPeriod[which(cluster.test == 1),,,drop=FALSE]
            slp2 <- slpAnomPeriod[which(cluster.test == 2),,,drop=FALSE]
            slp3 <- slpAnomPeriod[which(cluster.test == 3),,,drop=FALSE]
            slp4 <- slpAnomPeriod[which(cluster.test == 4),,,drop=FALSE]

             # in case a regime has NO days in that month, we must set it to NA:
            if(length(which(cluster.test == 1)) == 0 ) slp1 <- array(NA, c(1,dim(slpAnomPeriod)[2:3]))
            if(length(which(cluster.test == 2)) == 0 ) slp2 <- array(NA, c(1,dim(slpAnomPeriod)[2:3]))
            if(length(which(cluster.test == 3)) == 0 ) slp3 <- array(NA, c(1,dim(slpAnomPeriod)[2:3]))
            if(length(which(cluster.test == 4)) == 0 ) slp4 <- array(NA, c(1,dim(slpAnomPeriod)[2:3]))
            
            slpMean1 <- apply(slp1, c(2,3), mean, na.rm=FALSE)
            slpMean2 <- apply(slp2, c(2,3), mean, na.rm=FALSE)
            slpMean3 <- apply(slp3, c(2,3), mean, na.rm=FALSE)
            slpMean4 <- apply(slp4, c(2,3), mean, na.rm=FALSE)

            assign(paste0("psl.test",cluster1), slpMean1)
            assign(paste0("psl.test",cluster2), slpMean2)
            assign(paste0("psl.test",cluster3), slpMean3)
            assign(paste0("psl.test",cluster4), slpMean4)

            # save strip with the daily regime series for chosen month and year:
            fileoutput.seq <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_regimes_sequence.png")
            png(filename=fileoutput.seq,width=1500,height=1850)

            plot.new()
                
            sep <- 0.03
            for(day in 1: n.days.in.a.period(p, 2001)){
                sep.cum <- (day-1)*sep
                polygon(c(sep.cum + 0.01, sep.cum + 0.01 + sep, sep.cum + 0.01 + sep, sep.cum + 0.01), c(1.01, 1.01, 1.01+sep, 1.01+sep), border="black", col=cluster.col[day])
                text(sep.cum + 0.01 + sep/2, 0.997 + sep + 0.005, labels=day, cex=1.5)
                text(sep.cum + 0.01 + sep/2, 1.013 + 0.005, labels=cluster.test.letters[day], cex=2)

            }

            dev.off()


            
            # save average impact and sea level pressure only for chosne month and year:
            fileoutput.test <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_monthly_anomaly_regimes.png")
            
            png(filename=fileoutput.test,width=1500,height=2000)
                                
            plot.new()
            
            par(fig=c(0, 0.33, 0.77, 0.97), new=TRUE)
            PlotEquiMap2(imp.test1[,EU], lon[EU], lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var, cols=my.cols.var, cex.lab=1.2, intxlon=10, intylat=10, drawleg=F)
            par(fig=c(0, 0.33, 0.54, 0.74), new=TRUE)
            PlotEquiMap2(imp.test2[,EU], lon[EU], lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var, cols=my.cols.var, cex.lab=1.2, intxlon=10, intylat=10, drawleg=F)
            par(fig=c(0, 0.33, 0.31, 0.51), new=TRUE)
            PlotEquiMap2(imp.test3[,EU], lon[EU], lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var, cols=my.cols.var, cex.lab=1.2, intxlon=10, intylat=10, drawleg=F)
            par(fig=c(0, 0.33, 0.08, 0.28), new=TRUE)
            PlotEquiMap2(imp.test4[,EU], lon[EU], lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var, cols=my.cols.var, cex.lab=1.2, intxlon=10, intylat=10, drawleg=F)
            
            par(fig=c(0,0.33,0.955,0.975), new=TRUE)
            mtext(paste0(orden[1], " wind speed anomaly "), font=2, cex=2)
            par(fig=c(0,0.33,0.725,0.745), new=TRUE)
            mtext(paste0(orden[2], " wind speed anomaly "), font=2, cex=2)
            par(fig=c(0,0.33,0.495,0.515), new=TRUE)
            mtext(paste0(orden[3], " wind speed anomaly "), font=2, cex=2)
            par(fig=c(0,0.33,0.265,0.285), new=TRUE)
            mtext(paste0(orden[4], " wind speed anomaly "), font=2, cex=2)
            
            par(fig=c(0.03, 0.33, 0.015, 0.06), new=TRUE)
            #ColorBar(my.brks.var, cols=my.cols.var, vert=FALSE, label_scale=1.8, triangle_ends=c(FALSE,FALSE)) #, subset=my.subset2)
            ColorBar(my.brks.var[2:(length(my.brks.var)-1)], cols=my.cols.var[2:(length(my.cols.var)-1)], vert=FALSE, label_scale=1.8, bar_limits=c(my.brks.var[2],my.brks.var[l(my.brks.var)-1]), col_inf=my.cols.var[1], col_sup=my.cols.var[l(my.cols.var)], subsample=1) #triangle_ends=c(T,T)) #, subset=my.subset2)

            par(fig=c(0.33, 0.34, 0.01, 0.044), new=TRUE)
            mtext("m/s", cex=1.6)

            # right figures:             
            par(fig=c(0.34, 0.92, 0.77, 0.97), new=TRUE)
            PlotEquiMap2(psl.test1, lon, lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=t(psl.test1), contours.lty="F1FF1F", cex.lab=1, xlabel.dist=.5)
            par(fig=c(0.34, 0.92, 0.54, 0.74), new=TRUE)
            PlotEquiMap2(psl.test2, lon, lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=t(psl.test2), contours.lty="F1FF1F", cex.lab=1, xlabel.dist=.5)
            par(fig=c(0.34, 0.92, 0.31, 0.51), new=TRUE)
            PlotEquiMap2(psl.test3, lon, lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=t(psl.test3), contours.lty="F1FF1F", cex.lab=1, xlabel.dist=.5)
            par(fig=c(0.34, 0.92, 0.08, 0.28), new=TRUE)
            PlotEquiMap2(psl.test4, lon, lat, filled.continents=FALSE, continents.col="black", brks=my.brks.var2, brks2=my.brks.var2, cols=my.cols.var2, intxlon=10, intylat=10, drawleg=F, contours=t(psl.test4), contours.lty="F1FF1F", cex.lab=1, xlabel.dist=.5)

            par(fig=c(0.34,0.92,0.955,0.975), new=TRUE)
            mtext(paste0(orden[1], " sea level pressure anomaly "), font=2, cex=2)
            par(fig=c(0.34,0.92,0.725,0.745), new=TRUE)
            mtext(paste0(orden[2], " sea level pressure anomaly "), font=2, cex=2)
            par(fig=c(0.34,0.92,0.495,0.515), new=TRUE)
            mtext(paste0(orden[3], " sea level pressure anomaly "), font=2, cex=2)
            par(fig=c(0.34,0.92,0.265,0.285), new=TRUE)
            mtext(paste0(orden[4], " sea level pressure anomaly "), font=2, cex=2)
            
            par(fig=c(0.34, 0.93, 0.015, 0.06), new=TRUE)
            #ColorBar2(my.brks.var2, cols=my.cols.var2, vert=FALSE, my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) #, subsampleg=1, label_scale=1.8) #, subset=my.subset2)
            ColorBar(my.brks.var2[2:(length(my.brks.var2)-1)], cols=my.cols.var2[2:(length(my.cols.var2)-1)], vert=FALSE, label_scale=1.8, bar_limits=c(my.brks.var2[2],my.brks.var2[l(my.brks.var2)-1]), col_inf=my.cols.var2[1], col_sup=my.cols.var2[l(my.cols.var2)], subsample=1)

            par(fig=c(0.924, 0.930, 0.01, 0.044), new=TRUE)
            mtext("hPa", cex=1.6)
            
            #par(fig=c(0.627, 0.647, 0, 0.028), new=TRUE)
            #mtext("0", cex=1.8)

            n.days <- floor(n.days.in.a.period(month.test,1))
            
            par(fig=c(0.93, 0.99, 0.77, 0.87), new=TRUE)
            mtext(paste0(fre.days1," days\n(",round(100*fre.days1/n.days,1),"%)"), cex=2.8)
            par(fig=c(0.93, 0.99, 0.54, 0.64), new=TRUE)
            mtext(paste0(fre.days2," days\n(",round(100*fre.days2/n.days,1),"%)"), cex=2.8)
            par(fig=c(0.93, 0.99, 0.31, 0.41), new=TRUE)
            mtext(paste0(fre.days3," days\n(",round(100*fre.days3/n.days,1),"%)"), cex=2.8)
            par(fig=c(0.93, 0.99, 0.08, 0.18), new=TRUE)
            mtext(paste0(fre.days4," days\n(",round(100*fre.days4/n.days,1),"%)"), cex=2.8)


            dev.off()


            ## add the strip with the regime sequence over the average impact composition:
            fileoutput.temp <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_temp.png")
            fileoutput.both <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_temp2.png")

            system(paste0("convert ",fileoutput.seq," -crop +0-1730 +repage ",fileoutput.temp)) # cut the lower part of the strip
            system(paste0("montage ",fileoutput.temp," ",fileoutput.test," -tile 1x2 -geometry +0+0 ",fileoutput.both))


            ## same image formatted for the catalogue:
            fileoutput2.test <- paste0(rean.dir,"/",rean.name,"_",my.period[month.test],"_monthly_anomaly_regimes_fig2cat.png")            

            system(paste0("~/scripts/fig2catalog.sh -s 0.8 -m 70 -r 40 -t '",rean.name," / 10m wind speed and sea level pressure / Monthly regime anomalies \n",month.name[month.test]," / ",year.test,"' -c 'Region: North Atlantic (27°N-81°N, 85.5°W-45°E)\nReference dataset: ",rean.name," reanalysis' ", fileoutput.both," ", fileoutput2.test))

            system(paste0("rm ", fileoutput.temp, " ", fileoutput.both," ", fileoutput.seq," ", fileoutput.test))
                       
    
        } # close for on year.test
    } # close for on month.test

       
} # close for on monthly_anomalies 



           






