
###########################################################################################
#                  Visualize and save the score maps for one score and period             #
###########################################################################################
# run it only after computing and saving all the skill score data:

rm(list=ls())
gc()

##########################################################################################
#                            Load functions and libraries                                #
##########################################################################################

library(SpecsVerification) # for skill scores
library(s2dverification)   # for Load() function
library(abind)             # for merging arrays
source('/esarchive/scratch/ncortesi/scripts/Rfunctions.R')
source('/esarchive/scratch/ncortesi/ESS/general/hatching.R')

##########################################################################################
#                                     User's settings                                    #
##########################################################################################
#if(!mare) {source('/home/Earth/ncortesi/Downloads/scripts/Rfunctions.R')} else {source('/gpfs/projects/bsc32/bsc32842/scripts/Rfunctions.R')}

## working dir where to load .RData input files and to put the output maps and files in the /Data and /Maps subdirs:
## work.dir <- "/scratch/Earth/ncortesi/RESILIENCE/Subestacional"
## work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members/test_with_daily_u_v"
## work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members/test_with_daily_u_v_for_ERAI_too"
## work.dir <- "/esarchive/scratch/ncortesi/Subestacional/S2S_11_members/Thursday/wind_module_test"
## work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members/sfcWind"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_4_members"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_4_members_1996-2013"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_4_members_1996-2013_4_start_dates"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2013"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2013_4_start_dates"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2013_4_start_dates_test"
work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2015_calibrated"
##work.dir <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2015"

var.name <- 'sfcWind'      # forecast variable. It is the name used inside the netCDF files and as suffix in map filenames, i.e: 'sfcWind' or 'psl'

my.pvalue <- 0.05          # in case of computing the EnsCorr, set the p_value level to show in the ensemble mean correlation maps
    
land.only <- FALSE  # set it to true if you want to average only over land, FALSE otherwise [warning: the reliability diagram is computed ONLY over land+sea ]

my.months <- 1:12 ## 8 ##1:12 #9:12  # choose the month(s) to plot and save

## type of bias-correction (if any) applied to the forecasts:
my.bias <- "Calibration method" ## none

# set four.start.dates to TRUE if start dates were restricted to 4 in the computation of the skill scores:
four.start.dates <- TRUE

# choose one or more metrics to visualize between ACC (EnsCorr), FairRPSS, FairCRPSS and/or RelDiagr:
my.score.name <- c('FairRpss') ##,'FairCrpss','EnsCorr') ##,'RelDiagr') ## , 'Rpss')

# metric names to be visualized in the graph titles:
my.score.name.title <-  c('Rpss', 'FairRpss','FairCrpss','Correlation of the ensemble mean') ##,'Reliability Diagram') 

# Choose one or more regions between those defined below for the reliability diagrams and for the summary tables (0=World, 1=Northern europe, etc.)
areas <- 5 ##c(5,0) ## 11 #c(5,7,8,9,0) #c(0,5,6,7,9,1,2,3,4,8)

## Define regions for averaging skill scores and for the reliability diagrams (in the format: lon.min /lon.max / lat.min /lat.max:
## lon.min and lon.max values below MUST be from -180 and +179.999999, while lat.min and lat.max values below MUST be from -90 to 90:
area0.name <- "World"
area0 <- c(-180,179.99,-90,90)
area1.name <- "Northern_Europe"
area1 <- c(-15,45,45,75)
area2.name <- "Southern_Europe"
area2 <- c(-15,45,35,45)
area3.name <- "Southwestern_Europe"
area3 <- c(-15,20,35,45)
area4.name <- "Southeastern_Europe"
area4 <- c(20,45,35,45)
area5.name <- "Europe"  ## used in NEWA
area5 <- c(-10,45,30,75)
area6.name <- "Europe_RESILIENCE"
area6 <- c(-15,45,35,75) ## used in RESILIENCE
area7.name <- "North_America"
area7 <- c(-130,-60,30,50)
area8.name <- "North_Sea"
area8 <- c(-4, 15, 50, 65)
area9.name <- "Iberian_Peninsula"
area9 <- c(-12, 6, 34, 46)
area10.name <- "Canadian_region" ## warning: hatching function does not function for small regions!!!!!
area10 <- c(-114, -111.8, 49.6, 51.7)
area11.name <- "Specific_Point"
area11 <-  c(1, 2, 53, 54) ## point in the North Sea
    
# coordinates of a chosen point in the world to plot the time series over the 52 maps (they must be the same values in objects la y lo)
#my.lat <- 55.3197120 
#my.lon <- 0.5625

#########################################################################################

col <- c("#0C046E", "#1914BE", "#2341F7", "#2F55FB", "#3E64FF", "#528CFF", "#64AAFF", "#82C8FF", "#A0DCFF", "#B4F0FF", "#FFFBAF", "#FFDD9A", "#FFBF87", "#FFA173", "#FF7055", "#FE6346", "#F7403B", "#E92D36", "#C80F1E", "#A50519")

# line color of Reliability Diagram summaries:
col.month <- c('#e41a1c','#e41a1c','#e41a1c','#377eb8','#377eb8','#377eb8','#4daf4a','#4daf4a','#4daf4a','#984ea3','#984ea3','#984ea3')
                
dir.create(path=paste0(work.dir,"/",var.name), showWarnings=FALSE) # if directory already exists, this function doesn't create it again
dir.create(path=paste0(work.dir,"/",var.name,"/formatted"), showWarnings=FALSE) # if directory already exists, this function doesn't create it again

##########################################################################################
#                 visualize worldwide monthly maps of skill scores                       #
##########################################################################################

for(area.num in areas){
    ## print which area we are evaluating and its coordinates:
    area <- eval(parse(text=paste0("area", area.num)))
    area.name <- eval(parse(text=paste0("area", area.num,".name")))
    print(paste0("Area: ",area.name, "  (Lon: ",area[1],"E to ",area[2],"E; Lat: ",area[3],"N to ",area[4],"N)"))
    
    ## position of max and min long and lat values the chosen region:
    lon.min <- area[1]
    lon.max <- area[2]
    lat.min <- area[3]
    lat.max <- area[4]

    for(month in my.months){
        ## month=1  # for the debug
        print(paste0("Evaluating month of ", my.month[month]))

        ## Load Skill data:
        work.dir.orig <- work.dir
        load(file=paste0(work.dir,'/',var.name,'_',my.month[month],'.RData'))
        if(work.dir != work.dir.orig) work.dir <- work.dir.orig
        
        sdates.seq <- weekly.seq(forecast.year, mes, day)  # sequence of dates corresponding to all the weeks of the year
        my.startdates.days <- as.integer(substr(sdates.seq[which(as.integer(substr(sdates.seq,5,6)) == month)],7,8)) #startdates.monthly[[month]] #c(1:5)
        stringa <- paste0(my.startdates.days,collapse=",")
        if(four.start.dates) stringa <- stringa[1:4]
        
        ## 'lats' and 'lons' are the lists of lat and lon values used in our skill score and loaded before from the .RData.
        ## 'lats' MUST BE in decreasing order from 90 to -90, and 'lons' MUST BE in increasing order from 0 to 360.
        ## 'pos.area.lat' and 'pos.area.lon' defined below select the POSITION of the elements of the region to plot inside vectors lats and lons, respectively.

        ## restrict long values to the chosen ones, instead than the whole world (negative lon.min are considered separately than positive lon.min values):

        ## first, convert eventual negative lon values to positive ones:
        lon.max.posit <- ifelse(lon.max >= 0, lon.max, lon.max + 360)
        lon.min.posit <- ifelse(lon.min >= 0, lon.min, lon.min + 360)

        ## then, get the position of the first and last lon values inside the study area:
        pos.lon.min <- first(which(lons >= lon.min.posit))
        pos.lon.max <- last(which(lons <= lon.max.posit))

        ## concatena the position of the first and last lon values to get all the lon positions of the study area:
        ## (in case lon.min < 0 and lon.max > 0, the positive lon.min is greater than lon.max, so we have to modify the concatenation)
        ## (the concatenating order is important because we use pos.area.lon not only to select points for the Rel.diagram, but also to show them with PlotEquiMap)
        if(pos.lon.min <= pos.lon.max) pos.area.lon <- pos.lon.min:pos.lon.max else pos.area.lon <- c(pos.lon.min:length(lons), 1:pos.lon.max)
        
        ## restrict latitude values to chosen ones (in this case, we don't need to convert lat to positive values:
        pos.lat.max <- first(which(lats <= lat.max))
        pos.lat.min <- last(which(lats >= lat.min))
        pos.area.lat <- pos.lat.max:pos.lat.min
               
        for(my.score.name.map in my.score.name){
            ##my.score.name.map='FairRpss'  # for the debug

            print(paste0("Score: ",my.score.name.map))

            if(my.score.name.map == 'EnsCorr') my.score <- my.EnsCorr.chunk
            ##if(my.score.name.map == 'Rpss') my.score <- my.Rpss.chunk
            if(my.score.name.map == 'FairRpss') my.score <- my.FairRpss.chunk
            if(my.score.name.map == 'FairCrpss') my.score <- my.FairCrpss.chunk

            if(my.score.name.map == 'EnsCorr')  brk.rpss <- seq(-1,1,by=0.1) else brk.rpss <- c(-1000, seq(-0.9,1,by=0.1))
            col.rpss <- colorRampPalette(col)(length(brk.rpss)-1)

            ## at present all breaks and colors are the same, so there is no need to differenciate between indexes, aside the two Fair scores whose range is [-inf,0]
            my.brk <- brk.rpss 
            my.col <- col.rpss
            my.brk.labels <- my.brk
            if(my.score.name.map == 'Rpss' | my.score.name.map == 'FairRpss' | my.score.name.map == 'FairCrpss') my.brk.labels <- c(expression(-infinity), my.brk[-1])
            
            my.RelDiagr <- list()

            for(lead in 1:n.leadtimes){
                print(paste0("Leadtime: ",lead))

                ## in case we are plotting the world skill:
                if(my.score.name.map != "RelDiagr"){
                    
                    my.file <- paste0(work.dir,'/',var.name,'/',var.name,'_', area.name,'_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead],'.png')
                    my.file2 <-  paste0(work.dir,'/',var.name,'/formatted/',var.name,'_',area.name,'_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead],'.png')
                    png(file=my.file, width=1000,height=600)
                    plot.new()

                    ## Map:   
                    par(fig=c(0,0.92,0,0.89), new=TRUE)

                    ## lat.plot are the latitude values to plot of the selected area, in decreasing order from 90 to -90:
                    lat.plot <- lats[pos.area.lat]
                    lon.plot <- lons[pos.area.lon]
                    
                    ## lon.plot are the longitude values to plot of the selected area.
                    ## However, we want to plot America to the left of the map with PlotEquiMap, so we needs to move lon.plot values in increasing order from -180 to 180,
                    ## swapping the values from 180 to 360 degrees at the beginning of the lon.plot vector and converting them to negative values
                    ## (instead of from 0 to 360 as in the 'lons' vector):                    
                    ss <- which(lon.plot >= 180)
                    pp <-  which(lon.plot < 180)
                    lon.plot <- c(lon.plot[ss] - 360, lon.plot[pp])
                    
                    ## new plot for any region:
                    PlotEquiMap(var=my.score[lead, pos.area.lat, pos.area.lon], lon=lon.plot, lat=lat.plot, title_scale=0.8, brks=my.brk, cols=my.col ,axelab=F, filled.continents=FALSE, drawleg=F)
                    
                    ## same but with American to the right:
                    ## PlotEquiMap(var=my.score[lead,,], lon=lons, lat=lats ,title_scale=0.8, brks=my.brk, cols=my.col ,axelab=F, filled.continents=FALSE, drawleg=F)

                    if(my.score.name.map == "EnsCorr") my.PValue.rev <- my.EnsCorr.pvalue < 0.05
                    if(my.score.name.map == "FairRpss") my.PValue.rev <- my.FairRpss.pvalue < 0.05
                    if(my.score.name.map == "FairCrpss") my.PValue.rev <- my.FairCrpss.pvalue < 0.05
                    
                    ## Draw the significance diagonal lines:
                    if(my.score.name.map != "Rpss") {
                        pv <- aperm(my.PValue.rev, c(1,3,2)) ## must swap lon with lat to be able to employ the hatching function:
                        hatching(lats=lat.plot, lons=lon.plot, mask=pv[lead, pos.area.lon, pos.area.lat], dens = 12, ang = 45, col_line = '#252525', lwd_size = 0.5, crosshatching = FALSE)
                    }
                    
                    ## Map title:
                    par(fig=c(0,1,0.82,0.91), new=TRUE)
                    ##my.title <- paste0(my.score.name.map,' of ',cfs.name,'\n ', var.name.map,' for ',my.month[month],'. Forecast time ',leadtime.week[lead],'.')
                    my.title <- paste0("ECMWF-MPS / ", var.name.map, " / ", my.score.name.title[which(my.score.name == my.score.name.map)], "\n", my.month[month], " (",leadtime.week[lead]," days lead time)/ ", yr1.hind, "-", yr2.hind)
                    mtext(my.title, cex=2.4, font=2)

                    ## Color legend:
                    par(fig=c(0.92,1,0,0.9), new=TRUE)
                    ##par(fig=c(0.52,0.82,0,0.9), new=TRUE)

                    ColorBar2(my.brk, cols=my.col, vert=T, my.ticks=-0.5 + 1:length(my.brk), my.labels=my.brk.labels)
                    
                    ## arrange lat/ lon in my.PValue (a second variable in the EnsCorr file) to start from +90 degr. (lat) and from 0 degr (lon):
                    ## my.PValue.rev[lead,,] <- my.PValue[i,n.lat:1,c((n.lonr+1):n.lon,1:n.lonr)]
                    
                    dev.off()
                    
                    system(paste0("/esarchive/scratch/ncortesi/scripts/fig2catalog.sh -s 0.8 -c 'Start dates: ", stringa," of ",my.month[month],"\nLead time: ", leadtime.week[lead], " days\nReference dataset: ERA-Interim\nBias correction: ", my.bias,"\nHatched area: significant from a bootstrapping test (p_value=0.05)' ", my.file," ", my.file2))
                    gc()
                    
                } # close if on !RelDiagr

                
                if(my.score.name.map == "RelDiagr") {
                    ## Note that bins should correspond to the number of hindcast members + 1, for systems with a low number of members (below ~20)
                    ## Warning: if the bootstrap show a gray point instead of a gray bar, decrease the number of bins!
                    ## i.e: in case of computing the Reliabilty Diagram, set the number of intervals of the forecasted frequency (X axis)
                    ## equal to the number of ensemble members + 1 (to include the fist interval of 0%, when no members predict the tercile):
                    n.intervals <- n.members + 1 

                    if(length(which(is.na(ens.chunk.prob[[lead]][int1, pos.area.lat,pos.area.lon]))) > 0)
                        
                    my.RelDiagr[[lead]] <- ReliabilityDiagramHist(ens.chunk.prob[[lead]][int1,pos.area.lat,pos.area.lon], obs.chunk.prob[[lead]][int1,pos.area.lat,pos.area.lon], bins=n.intervals, nboot=500, plot=FALSE, plot.refin=TRUE) # Below normal tercile

                    ## middle tercile is desactivated:
                    ## my.RelDiagr[[n.leadtimes+lead]] <- ReliabilityDiagramHist(ens.chunk.prob[[lead]][int2,,], obs.chunk.prob[[lead]][int2,,], bins=5, nboot=0, plot=FALSE, plot.refin=F)
                    my.RelDiagr[[2*n.leadtimes+lead]] <- ReliabilityDiagramHist(ens.chunk.prob[[lead]][int3,pos.area.lat,pos.area.lon], obs.chunk.prob[[lead]][int3,pos.area.lat,pos.area.lon], bins=n.intervals, nboot=500, plot=FALSE, plot.refin=TRUE) # Above Normale tercile

                    my.file <- paste0(work.dir,'/',var.name,'/',var.name,'_', area.name,'_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead],'.png')
                    my.file2 <-  paste0(work.dir,'/',var.name,'/formatted/',var.name,'_',area.name,'_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead],'.png')
                    
                    png(file=my.file,width=650,height=600)  

                    ##my.title <- paste0('Reliability Diagram of ',cfs.name,'\n ',var.name.map,' for ',my.month[month],'. Forecast time ',leadtime.week[lead],'.')
                    my.title <- paste0("ECMWF-MPS / ", var.name.map, " / Reliability Diagram \n", my.month[month], " (", leadtime.week[lead]," days lead time) / ", yr1.hind, "-", yr2.hind)
                    
                    par(fig=c(0, 0.83, 0, 1))

                    plot(c(0,1),c(0,1),type="l",xlab="Forecast probability (%)",ylab="Observed frequency (%)", xaxt='n', yaxt='n',col='gray30', main=my.title, cex.main=1.6)

                    middle <- 100*round(my.RelDiagr[[lead]]$p.avgs,2)
                    interval <- (middle/2)[2]
                    left <- round(middle - interval,0)
                    left[1] <- 0
                    right <- round(middle + interval,0)
                    right[length(right)] <- 100
                    bins.label <- paste0(paste0(left,"-"),right)
                    
                    axis(1, at=my.RelDiagr[[lead]]$p.avgs, labels=FALSE)
                    text(x=my.RelDiagr[[lead]]$p.avgs, par("usr")[3]-0.03, labels=bins.label, srt=45, pos=1, xpd=TRUE)
                    axis(2, at=(0:10)/10, labels=10*(0:10), cex.axis=1)

                    no_res <- 1/3 #sum(my.RelDiagr[[lead]]$obs.counts)/sum(my.RelDiagr[[lead]]$hist.counts)
                    numb <- c(seq(0,1,by=0.1))
                    no_skill <- (numb+no_res)/2
                    lines(c(0,1), c(no_res,no_res), col="gray", lty=3)
                    lines(c(1/3,1/3), c(0,1), col="gray", lty=3)
                    lines(c(0,1), c(no_skill[1],no_skill[11]), col="black", lty=3)
                    
                    ##lines(my.RelDiagr[[lead]]$p.avgs[!is.na(my.RelDiagr[[lead]]$p.avgs)],my.RelDiagr[[lead]]$cond.prob[!is.na(my.RelDiagr[[lead]]$cond.prob)],type="o", pch=16, col="blue")            
                    for (i in 1:n.intervals){ 
                        lines(rep(my.RelDiagr[[lead]]$p.avgs[i], 2), c(my.RelDiagr[[lead]]$cbar.lo[i],my.RelDiagr[[lead]]$cbar.hi[i]), col="pink", lwd=2) 
                        lines(0.005+rep(my.RelDiagr[[2*n.leadtimes+lead]]$p.avgs[i], 2), c(my.RelDiagr[[2*n.leadtimes+lead]]$cbar.lo[i],my.RelDiagr[[2*n.leadtimes+lead]]$cbar.hi[i]), col="lightblue", lwd=2)
                    }
                    
                    
                    points(my.RelDiagr[[lead]]$p.avgs[!is.na(my.RelDiagr[[lead]]$p.avgs)],my.RelDiagr[[lead]]$cond.prob[!is.na(my.RelDiagr[[lead]]$cond.prob)], type="b", pch=1, col="blue", cex=1.5, lwd=5)

                    ##points(my.RelDiagr[[n.leadtimes+lead]]$p.avgs[!is.na(my.RelDiagr[[n.leadtimes+lead]]$p.avgs)],my.RelDiagr[[n.leadtimes+lead]]$cond.prob[!is.na(my.RelDiagr[[n.leadtimes+lead]]$cond.prob)],type="b", pch=11, col="gold", cex=1.5, lwd=5) # col="darkgreen"
                    
                    points(0.005+my.RelDiagr[[2*n.leadtimes+lead]]$p.avgs[!is.na(my.RelDiagr[[2*n.leadtimes+lead]]$p.avgs)], my.RelDiagr[[2*n.leadtimes+lead]]$cond.prob[!is.na(my.RelDiagr[[2*n.leadtimes+lead]]$cond.prob)],type="b", pch=1, col="red", cex=1.5, lwd=5)
                    
                    legend("bottomright", legend=c('Above Normal','Below Normal'), lty=c(1,1), lwd=c(2.5,2.5), col=c('red','blue'))

                    par(fig=c(0.75, 1, 0.2, 0.55), new=TRUE)
                    perc1 <- 100*my.RelDiagr[[lead]]$hist.counts/sum(my.RelDiagr[[lead]]$hist.counts)
                    barplot(perc1, beside=T,space=c(0,1.2), axes = T, axis.lty=T, axisnames = T, col = "blue", ylim=c(0,max(perc1)+5), main="Below Normal")
                    par(fig=c(0.75, 1, 0.54, 0.56), new=TRUE)
                    mtext("% of forecasts for bin")

                    par(fig=c(0.75, 1, 0.45, 0.8), new=TRUE)
                    perc2 <- 100*my.RelDiagr[[2*n.leadtimes+lead]]$hist.counts/sum(my.RelDiagr[[2*n.leadtimes+lead]]$hist.counts)
                    barplot(perc2, beside=T, space=c(0,1.2), axes = T, axis.lty=T, axisnames = T, col = "red", ylim=c(0,max(perc2)+5), main="Above Normal") 
                    par(fig=c(0.75, 1, 0.79, 0.81), new=TRUE)
                    mtext("% of forecasts for bin")
                    dev.off()

                    system(paste0("/esarchive/scratch/ncortesi/scripts/fig2catalog.sh -c 'Start dates: ", stringa," of ",my.month[month],"\nLead time: ", leadtime.week[lead], " days\nRegion: ", area.name,"\nReference dataset: ERA-Interim \nBias correction: none' ", my.file," ", my.file2))
                }

                gc()
            } # next lead

            gc()
        } # next score

        gc()
    } # next month

} ## next area



#############################################################################
#                Visualize summary tables for a region                      #
#############################################################################
#
# If you have created one .RData for each month of the year, you can plot the summary tables
#

months.avail <- 1:12 ##c(1:7, 10,11) ##1:12 ##1:5  ## months with a file 'sfcWind_<monthname>.RData' already available for the hindcasts
pos.only <- FALSE ## set it to TRUE if you want to exclude from the mean the points with negative skill, FALSE otherwise.
showRPSS <- TRUE ## to show the RPSS instead of the FairCRPSS

print("Summary tables")

for(area.num in areas){
    ## print which area we are evaluating and its coordinates:
    area <- eval(parse(text=paste0("area", area.num)))
    area.name <- eval(parse(text=paste0("area", area.num,".name")))
    print(paste0("Area: ",area.name, "  (Lon: ",area[1],"E to ",area[2],"E; Lat: ",area[3],"N to ",area[4],"N)"))
    
    ## position of max and min long and lat values the chosen region:
    lon.min <- area[1]
    lon.max <- area[2]
    lat.min <- area[3]
    lat.max <- area[4]

    ## If you use a mask, load it to average only over land and check that it has the same lat and lon positions of the score maps:
    if(land.only == TRUE){
        load(paste0('/shared/earth/EarthSystemServices/TOOLS/Topography_bathymetry_and_masks/Seamask.', n.lon, '.', n.lat,'.50m','.RData'))
        mask <- Seamask.512.256.50m
        ##Coordinates repositioning and mask conditioning
        mask <- mask[c((n.lonr+1):n.lon,1:n.lonr),n.lat:1]
        mask <- ifelse(mask == 1, 0, 1) ## assign 1 to land and 0 to sea to plot the mask
        mask <- t(mask)
        myImagePlot(mask)
        myImagePlot(my.FairRpss.chunk[1,,])
        mask <- ifelse(mask == 0, NA, 1) ## assign 1 to land and NA to sea to multiply it for the score values later
    }

    ## plot a box with the chosen area (do it only once):
    ##
    ##png(file=paste0(work.dir,'/',var.name,'/Box_',area.name,'.png'),width=1400,height=800)  
    ##PlotEquiMap(my.FairRpss.chunk[1,,c((n.lonr+1):n.lon,1:n.lonr)], c(lons[c((n.lonr+1):n.lon)]-360, lons[1:n.lonr]), lats, brks=c(-1000,1000), cols="lightblue" , axelab=F, filled.continents=TRUE, drawleg=F, boxlim=c(area[1],area[3],area[2],area[4]), boxcol="black", boxlwd=.7)
    ##dev.off()

    array.EnsCorr <- array.FairRpss <- array.FairCrpss <- array(NA,c(12,4))

    ## Load one file, just to import the lats and lons values:
    work.dir.orig <- work.dir
    load(file=paste0(work.dir,'/',var.name,'_',my.month[1],'.RData'))
    if(work.dir != work.dir.orig) work.dir <- work.dir.orig

    ## ## restrict lon values to chosen one instead than the whole world (negative lon.min are considered separately than positive lon.min values):
    ## if(lon.max < 0) lon.max <- lon.max + 360
    ## pos.lon.max <- which(lons <= lon.max) ## get the position inside the positive lons vector of the longitude values smaller or equal then lon.max value
    ## pos.lon.max.last <- pos.lon.max[length(pos.lon.max)] ## get the position of the last lon value to the east of Greenwich inside the study area

    ## ## concatenate the longitude positions to the west of Greenwich with those to the east of Greenwich in a single vector:
    ## ## (the order is not important because we use pos.area.lon only to select points for the Rel.diagram, not for PlotEquiMap)
    ## if(lon.min < 360) pos.area.lon <- c((which(lons >= lon.min)[1]:length(lons)), 1:pos.lon.max.last)
    
    ## ## if there are no negative lon.values, all long positions are to the east of Greenwich:
    ## if(lon.min > 360) pos.area.lon <- (which(lons >= (lon.min - 360))[1]):pos.lon.max.last

    ## ## restrict latitude values to chosen ones:
    ## pos.lat.min <- which(lats <= lat.min)[1] - 1 ## minimum latitude inside the chosen area
    ## if(area.name != "World") pos.area.lat <- c(which(lats <= lat.max)[1]:pos.lat.min)  # restrict area to chosen one
    ## if(area.name == "World") pos.area.lat <- 1:length(lats) ## correct the lat values in case of the whole world


    
    ## 'lats' and 'lons' are the lists of lat and lon values used in our skill score and loaded before from the .RData.
    ## 'lats' MUST BE in decreasing order from 90 to -90, and 'lons' MUST BE in increasing order from 0 to 360.
    ## 'pos.area.lat' and 'pos.area.lon' defined below select the POSITION of the elements of the region to plot inside vectors lats and lons, respectively.

    ## restrict long values to the chosen ones, instead than the whole world (negative lon.min are considered separately than positive lon.min values):

    ## first, convert eventual negative lon values to positive ones:
    lon.max.posit <- ifelse(lon.max >= 0, lon.max, lon.max + 360)
    lon.min.posit <- ifelse(lon.min >= 0, lon.min, lon.min + 360)

    ## then, get the position of the first and last lon values inside the study area:
    pos.lon.min <- first(which(lons >= lon.min.posit))
    pos.lon.max <- last(which(lons <= lon.max.posit))

    ## concatena the position of the first and last lon values to get all the lon positions of the study area:
    ## (in case lon.min < 0 and lon.max > 0, the positive lon.min is greater than lon.max, so we have to modify the concatenation)
    ## (the concatenating order is important because we use pos.area.lon not only to select points for the Rel.diagram, but also to show them with PlotEquiMap)
    if(pos.lon.min <= pos.lon.max) pos.area.lon <- pos.lon.min:pos.lon.max else pos.area.lon <- c(pos.lon.min:length(lons), 1:pos.lon.max)
    
    ## restrict latitude values to chosen ones (in this case, we don't need to convert lat to positive values:
    pos.lat.max <- first(which(lats <= lat.max))
    pos.lat.min <- last(which(lats >= lat.min))
    pos.area.lat <- pos.lat.max:pos.lat.min
    
    ## Load all data and average it over the chosen region for drawing the Skill figure:
    for(month in months.avail){

        load(file=paste0(work.dir,'/',var.name,'_',my.month[month],'.RData'))
        if(work.dir != work.dir.orig) work.dir <- work.dir.orig

        if(land.only == TRUE){
            ## remove sea values:
            for(i in 1:4) my.EnsCorr.chunk[i,,] <-  my.EnsCorr.chunk[i,,]*mask
            if(showRPSS) { for(i in 1:4) my.Rpss.chunk[i,,] <-  my.Rpss.chunk[i,,]*mask }
            for(i in 1:4) my.FairRpss.chunk[i,,] <-  my.FairRpss.chunk[i,,]*mask
            if(!showRPSS) for(i in 1:4) my.FairCrpss.chunk[i,,] <-  my.FairCrpss.chunk[i,,]*mask
        }
        
        for(l in 0:3){
            ##load(file=paste0(work.dir,"/",fields.name,"_",my.period[p],"_leadmonth_",l,"_","ClusterNames",".RData"))  # load cluster names and summarized data

            if(pos.only == TRUE){
                my.EnsCorr.chunk[which(my.EnsCorr.chunk <= 0)] <- NA
                if(showRPSS) my.Rpss.chunk[which(my.Rpss.chunk <= 0)] <- NA
                my.FairRpss.chunk[which(my.FairRpss.chunk <= 0)] <- NA
                if(!showRPSS) my.FairCrpss.chunk[which(my.FairCrpss.chunk <= 0)] <- NA
            }
            
            array.EnsCorr[month, 1+l] <- mean(my.EnsCorr.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)
            array.FairRpss[month, 1+l] <- mean(my.FairRpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  
            array.FairCrpss[month, 1+l] <- mean(my.FairCrpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)
            if(showRPSS) array.FairCrpss[month, 1+l] <- mean(my.Rpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  ## replace figure of FairCRPSS with RPSS
            
        }
    }

    

    ##
    ## Summary figure of all scores except reliability diagram:
    ##

    if(land.only == TRUE) { mod.filename <- "_land" } else { mod.filename <- "" }
    mod.pos <- ifelse(pos.only, "_positive_only", "")
    mod.RPSS <- ifelse(showRPSS, "_RPSS_instead_of_FairCRPSS", "")
                       
    my.file <- paste0(work.dir,'/',var.name,'/Summary_',var.name,'_', area.name, mod.filename, mod.pos, mod.RPSS, '.png')
    ##my.file2 <- paste0(work.dir,'/',var.name,'/formatted/Summary_',var.name,'_', area.name, mod.filename,'_',mod.pos,'.png')

    png(file=my.file,width=700,height=500)  

    ##my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))
    ##my.cols <- c('#0570b0','#bdc9e1', '#fff7ec', '#fee8c8','#fdd49e','#fdbb84','#fc8d59','#ef6548','#d7301f','#990000')
    ##my.cols <- c('#0570b0','#bdc9e1','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#ef6548','#d7301f','#990000')
    ##my.cols <- c('#74a9cf','#bdc9e1','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000')
    ##my.cols <- c('#bdc9e1','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')

    ##my.seq <- c(-1,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,1)
    ##my.seq <- c(-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6)
    my.seq <- c(-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,1)
    my.seq.legend <- c(my.seq[-length(my.seq)], 0.7) ## legend to show in the map (same as above but with 0.7 as maximum value instead of 1
                       
    ## create an array similar to array.pers but with colors instead of frequencies:                
    plot.new()

    par(mar=c(0,0,2,0), fig=c(0.07, 0.27, 0.9, 1), new=TRUE) 
    mtext("(a)   EnsCorr", cex=1.5)

    par(mar=c(1,4,1,0), fig=c(0.02, 0.27, 0.1, 0.9), new=TRUE)
    plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
    mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
    mtext(side = 2, text = "Start date", line = 3, cex=1.2)
    axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
    axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

    array.colors <- array(my.cols[8],c(12,4))
    array.colors[array.EnsCorr < my.seq[8]] <- my.cols[7]
    array.colors[array.EnsCorr < my.seq[7]] <- my.cols[6]
    array.colors[array.EnsCorr < my.seq[6]] <- my.cols[5]
    array.colors[array.EnsCorr < my.seq[5]] <- my.cols[4]
    array.colors[array.EnsCorr < my.seq[4]] <- my.cols[3]
    array.colors[array.EnsCorr < my.seq[3]] <- my.cols[2]
    array.colors[array.EnsCorr < my.seq[2]] <- my.cols[1]
    array.colors[(1:12)[-months.avail],] <- "gray"

    for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

    par(mar=c(0,0,2,0), fig=c(0.32, 0.57, 0.9, 1), new=TRUE) 
    mtext("(b)  FairRPSS", cex=1.5)

    par(mar=c(1,4,1,0), fig=c(0.32, 0.57, 0.1, 0.9), new=TRUE)
    plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
    mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
    mtext(side = 2, text = "Start date", line = 3, cex=1.2)
    axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
    axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

    array.colors <- array(my.cols[8],c(12,4))
    array.colors[array.FairRpss < my.seq[8]] <- my.cols[7]
    array.colors[array.FairRpss < my.seq[7]] <- my.cols[6]
    array.colors[array.FairRpss < my.seq[6]] <- my.cols[5]
    array.colors[array.FairRpss < my.seq[5]] <- my.cols[4]
    array.colors[array.FairRpss < my.seq[4]] <- my.cols[3]
    array.colors[array.FairRpss < my.seq[3]] <- my.cols[2]
    array.colors[array.FairRpss < my.seq[2]] <- my.cols[1]
    array.colors[(1:12)[-months.avail],] <- "gray"

    for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

    par(mar=c(0,0,2,0), fig=c(0.62, 0.87, 0.9, 1), new=TRUE) 
    if(showRPSS) mtext("(c) RPSS", cex=1.5) else mtext("(c) FairCRPSS", cex=1.5)

    par(mar=c(1,4,1,0), fig=c(0.62, 0.87, 0.1, 0.9), new=TRUE)
    plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
    mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
    mtext(side = 2, text = "Start date", line = 3, cex=1.2)
    axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
    axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

    array.colors <- array(my.cols[8], c(12,4))
    array.colors[array.FairCrpss < my.seq[8]] <- my.cols[7]
    array.colors[array.FairCrpss < my.seq[7]] <- my.cols[6]
    array.colors[array.FairCrpss < my.seq[6]] <- my.cols[5]
    array.colors[array.FairCrpss < my.seq[5]] <- my.cols[4]
    array.colors[array.FairCrpss < my.seq[4]] <- my.cols[3]
    array.colors[array.FairCrpss < my.seq[3]] <- my.cols[2]
    array.colors[array.FairCrpss < my.seq[2]] <- my.cols[1]
    array.colors[(1:12)[-months.avail],] <- "gray"

    for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

    par(fig=c(0.89, 1, 0.1, 0.9), new=TRUE) 
    ##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)
    ColorBar2(brks = my.seq, cols = my.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq.legend)
    gc()

    dev.off()

    ## Not checked but should work:
    ##system(paste0("~/scripts/fig2catalog.sh -r 50 -c 'Region: ", area.name, "\nReference dataset: ERA-Interim' ", my.file," ", my.file2))

    ## if you are not interested in the summary of the Reliability Diagram, you can end the loop here



    
    print("Reliability diagrams")
    
    ## Monthly reliability diagrams, all over the same graph #
    ## ONLY FOR SEA+LAND!!!!

    my.RelDiagr <- list()

    my.file <- paste0(work.dir,'/',var.name,'/Summary_RelDiagr_', area.name,'.png')
    my.file2 <- paste0(work.dir,'/',var.name,'/formatted/','/Summary_RelDiagr_', area.name,'.png')

    ##n.lonr <- n.lon - ceiling(length(lons[lons>0 & lons < 180])) # count the number of longitude values between 180 and 360 degrees longitud

    png(file=my.file, width=3000, height=1600, res=72)
    plot(0, type = "n", axes = FALSE, ann = FALSE)
    mtext(side = 3, text = paste0("ECMWF-MFS / Reliability Diagram\nJanuary to December / ", yr1.hind, "-", yr2.hind), line = -6, cex = 4.4, font=2)
    ## mod.subtitle <- ifelse(tercile == 1, "lower","upper")
    my.xlab <- paste0("Forecast frequency upper tercile (top row) and lower tercile (bottom row)")
    my.ylab <- "Observed frequency"
    mtext(side = 1, text = my.xlab, line = -6, cex = 3)
    mtext(side = 2, text = my.ylab, line = -8, cex = 3)
    
    par(mar=c(0,0,0,0) , fig=c(0, 1, 0, 0.4), new=TRUE)
    legend(0.82,-0.80,0,0, legend=my.month[c(1,4,7,10)], lty=c(1,1), lwd=c(3.5,3.5), col=col.month[c(1,4,7,10)], ncol=4, cex=3.4)

    par(mar=c(4.8,3.8,3.8,3.8) , fig=c(0, 1, 0, 0.4), new=TRUE)

    for(tercile in c(1,3)){
        print(paste0("Tercile: ",tercile))

        my.lead <- (1:n.leadtimes) + (tercile-1) * 4
        
        for(lead2 in my.lead){
            print(paste0("Lead time: ",lead2))

            xmin <- 0.1 + 0.2 * ((lead2-1) %% 4)
            xmax <- xmin + 0.2
            ymin <- 0.10 + 0.38 * (tercile-1)/2
            ymax <- ymin + 0.38

            ##my.title <- paste0('Reliability Diagram of ',cfs.name,'\n ',var.name.map,' for ',my.month[month],'. Forecast time ',leadtime.week[lead],'.')
            my.title <- paste0("Lead time: ", leadtime.week[1+((lead2-1) %% 4)], " days")

            par(fig=c(xmin, xmax, ymin, ymax), new=TRUE)
            
            plot(c(0,1),c(0,1), type="l", xlab="", ylab="", col='gray30', main=my.title, cex.main=2.4, cex.lab=2, cex.axis=2)

            no_res <- 1/3 #sum(my.RelDiagr[[lead]]$obs.counts)/sum(my.RelDiagr[[lead]]$hist.counts)
            numb <- c(seq(0,1,by=0.1))
            no_skill <- (numb+no_res)/2
            lines(c(0,1), c(no_res,no_res), col="gray", lty=3, lwd=3)
            lines(c(1/3,1/3), c(0,1), col="gray", lty=3, lwd=3)
            lines(c(0,1), c(no_skill[1],no_skill[11]), col="black", lty=3, lwd=3)
            
            for(month in c(1,4,7,10)){ #my.months){
                ##month=1  # for the debug
                ##print(paste0("Month=",month))
                
                ## Load data:
                work.dir.orig <- work.dir
                load(file=paste0(work.dir,'/',var.name,'_',my.month[month],'.RData'))   
                if(work.dir != work.dir.orig) work.dir <- work.dir.orig

                cat(paste0('Computing the Reliability Diagram for month ', month,'. Please wait... \n'))

                lead <- 1 + (lead2-1) %% 4
                n.intervals <- n.members + 1 

                if(length(which(is.na(ens.chunk.prob[[lead]][, pos.area.lat, pos.area.lon]))) > 0) print("Warning: NA values were found in the hindcasts")
                                      
                if(tercile == 1) my.RelDiagr[[lead]] <- ReliabilityDiagram(ens.chunk.prob[[lead]][int1, pos.area.lat, pos.area.lon], obs.chunk.prob[[lead]][int1,pos.area.lat,pos.area.lon], bins=n.intervals, nboot=0, plot=FALSE, plot.refin=F, handle.na="use.pairwise.complete") #tercile 1
                
                ##my.RelDiagr[[n.leadtimes+lead]] <- ReliabilityDiagram(ens.chunk.prob[[lead]][int2,pos.area.lat,pos.area.lon], obs.chunk.prob[[lead]][int2,pos.area.lat,pos.area.lon], bins=5, nboot=0, plot=FALSE, plot.refin=F, handle.na="use.pairwise.complete" ) ## tercile 2
                
                if(tercile == 3) my.RelDiagr[[2*n.leadtimes+lead]] <- ReliabilityDiagram(ens.chunk.prob[[lead]][int3,pos.area.lat,pos.area.lon], obs.chunk.prob[[lead]][int3,pos.area.lat,pos.area.lon], bins=n.intervals, nboot=0, plot=FALSE, plot.refin=F, handle.na="use.pairwise.complete") # tercile 3

                
                if(tercile == 1) points(my.RelDiagr[[lead]]$p.avgs[!is.na(my.RelDiagr[[lead]]$p.avgs)],my.RelDiagr[[lead]]$cond.prob[!is.na(my.RelDiagr[[lead]]$cond.prob)],type="b", pch=1, col=col.month[month], cex=1.8, lwd=2)

                if(tercile == 3) points(my.RelDiagr[[2*n.leadtimes+lead]]$p.avgs[!is.na(my.RelDiagr[[2*n.leadtimes+lead]]$p.avgs)], my.RelDiagr[[2*n.leadtimes+lead]]$cond.prob[!is.na(my.RelDiagr[[2*n.leadtimes+lead]]$cond.prob)],type="b", pch=1, col=col.month[month], cex=1.8, lwd=2)

                #rm(obs.chunk.prob, ens.chunk.prob)
                gc()
            } # next month

        } # next lead 
    } # next tercile


    dev.off()


} # close for on area.num


#####################################################################
##   visualize summary table of FairRPSS formatted for NEWA project #
##                for each grid point in the area                   #
#####################################################################

pos.only <- FALSE ## set it to TRUE if you want to exclude from the mean the points with negative skill, FALSE otherwise.
months.avail <- 1:12 ##c(1:4,7:12) ##1:12 ##c(1:7, 10,11) ##1:12 ##1:5  ## months with a file 'sfcWind_<monthname>.RData' already available for the hindcasts

print("Summary table of FairRPSS for NEWA")

## print which area we are evaluating and its coordinates:
area.num <- 5
area <- eval(parse(text=paste0("area", area.num)))
area.name <- eval(parse(text=paste0("area", area.num,".name")))
print(paste0("Area: ",area.name, "  (Lon: ", area[1],"E to ",area[2],"E; Lat: ",area[3],"N to ",area[4],"N)"))

## position of max and min long and lat values the chosen region:
lon.min <- area[1]
lon.max <- area[2]
lat.min <- area[3]
lat.max <- area[4]

## If you use a mask, load it to average only over land and check that it has the same lat and lon positions of the score maps:
if(land.only == TRUE){
    load(paste0('/shared/earth/EarthSystemServices/TOOLS/Topography_bathymetry_and_masks/Seamask.', n.lon, '.', n.lat,'.50m','.RData'))
    mask <- Seamask.512.256.50m
    ##Coordinates repositioning and mask conditioning
    mask <- mask[c((n.lonr+1):n.lon,1:n.lonr),n.lat:1]
    mask <- ifelse(mask == 1, 0, 1) ## assign 1 to land and 0 to sea to plot the mask
    mask <- t(mask)
    myImagePlot(mask)
    myImagePlot(my.FairRpss.chunk[1,,])
    mask <- ifelse(mask == 0, NA, 1) ## assign 1 to land and NA to sea to multiply it for the score values later
}

## plot a box with the chosen area (do it only once):
##
##png(file=paste0(work.dir,'/',var.name,'/Box_',area.name,'.png'),width=1400,height=800)  
##PlotEquiMap(my.FairRpss.chunk[1,,c((n.lonr+1):n.lon,1:n.lonr)], c(lons[c((n.lonr+1):n.lon)]-360, lons[1:n.lonr]), lats, brks=c(-1000,1000), cols="lightblue" , axelab=F, filled.continents=TRUE, drawleg=F, boxlim=c(area[1],area[3],area[2],area[4]), boxcol="black", boxlwd=.7)
##dev.off()

## Load one file, just to import the lats and lons values of the skill scores measured:
work.dir.orig <- work.dir
load(file=paste0(work.dir,'/',var.name,'_', my.month[1],'.RData'))
if(work.dir != work.dir.orig) work.dir <- work.dir.orig

## 'lats' and 'lons' are the lists of lat and lon values used in our skill score and loaded before from the .RData.
## 'lats' MUST BE in decreasing order from 90 to -90, and 'lons' MUST BE in increasing order from 0 to 360.
## 'pos.area.lat' and 'pos.area.lon' defined below select the POSITION of the elements of the region to plot inside vectors lats and lons, respectively.

## restrict long values to the chosen ones, instead than the whole world (negative lon.min are considered separately than positive lon.min values):

## first, convert eventual negative lon values to positive ones:
lon.max.posit <- ifelse(lon.max >= 0, lon.max, lon.max + 360)
lon.min.posit <- ifelse(lon.min >= 0, lon.min, lon.min + 360)

## then, get the position of the first and last lon values inside the study area:
pos.lon.min <- first(which(lons >= lon.min.posit))
pos.lon.max <- last(which(lons <= lon.max.posit))

## concatena the position of the first and last lon values to get all the lon positions of the study area:
## (in case lon.min < 0 and lon.max > 0, the positive lon.min is greater than lon.max, so we have to modify the concatenation)
## (the concatenating order is important because we use pos.area.lon not only to select points for the Rel.diagram, but also to show them with PlotEquiMap)
if(pos.lon.min <= pos.lon.max) pos.area.lon <- pos.lon.min:pos.lon.max else pos.area.lon <- c(pos.lon.min:length(lons), 1:pos.lon.max)

## restrict latitude values to chosen ones (in this case, we don't need to convert lat to positive values:
pos.lat.max <- first(which(lats <= lat.max))
pos.lat.min <- last(which(lats >= lat.min))
pos.area.lat <- pos.lat.max:pos.lat.min

## Load all subseasonal skill data:
my.FairRpss.sub <- array(NA, c(12, 4, length(lats), length(lons))) ## [month, lead time, nlat, nlon]

for(month in months.avail){
    load(file=paste0(work.dir,'/',var.name,'_',my.month[month],'.RData'))
    if(work.dir != work.dir.orig) work.dir <- work.dir.orig
    
    for(l in 0:3) my.FairRpss.sub[month, 1+l,,] <- my.FairRpss.chunk[1+l,,]
}  ## close for on month.avail

## Load all seasonal skill data: (format: [nlon, nlat, lead month, startdate])
my.FairRpss.sea <- readRDS(paste0(work.dir,'/FRPSS_ECMWFSEAS5_sfcWind_Jan-Dec_1996_2015.RDS'))

## load lats and lons associated to seasonal forecasts:
sea.coords <- readRDS(paste0(work.dir,'/lats_lons_NEWA.RDS'))

## reorder dimensions to be similar to the subseasonal forecasts: [month, lead time, nlat, nlon]
my.FairRpss.sea2 <- aperm(my.FairRpss.sea, perm = c(4,3,2,1))
                                               
## the latitude coordinates of Vero are the same used for the subestacional skill
## while the longitude of Vero is ordered in a different manner, with high lon values at the end of the vector, so we have to place them at the beginning:
my.FairRpss.sea3 <- my.FairRpss.sea2[,,,c(66:79, 1:65)]

## insert the array in a bigger array including all the lat and lon values of the world (with NA values):
my.FairRpss.sea4 <- array(NA, c(12, 3, length(lats), length(lons))) ## [month, lead time, nlat, nlon]
my.FairRpss.sea4[,, pos.area.lat, pos.area.lon] <- my.FairRpss.sea3

## average lat and lon values to get the area averaged summary figures:
my.FairRpss.sub.aver <- apply(my.FairRpss.sub, c(1,2), mean, na.rm=T)
my.FairRpss.sea4.aver <- apply(my.FairRpss.sea4, c(1,2), mean, na.rm=T) ## array with the mean values
                              
##
## Draw figures for each point:
##
for(i in pos.area.lat){
    for(j in pos.area.lon){
        print(i)
        
        my.file <- paste0(work.dir,'/',var.name,'/FairRPSS_of_', var.name,'_at_lat_', round(lats[i],2),'_lon_', round(lons[j],2),'.png')
        my.file2 <- paste0(work.dir,'/',var.name,'/formatted/FairRPSS_of_', var.name,'_at_lat_', round(lats[i],2),'_lon_', round(lons[j],2),'.png')

        ##my.file <- paste0(work.dir,'/',var.name,'/FairRPSS_of_', var.name,'_at_point_number_', k,'.png')
        ##my.file2 <- paste0(work.dir,'/',var.name,'/formatted/FairRPSS_of_', var.name,'_at_point_number_', k,'.png')
        
        png(file = my.file, width=530, height=700)  

        my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
        my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)

        plot.new()

        ## Plot figure title:
        par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE) 
        mtext(paste0("FairRPSS of ECMWF 10-m wind speed \nfor 1996-2015 at ", round(lats[i],2), "°N ", round(lons[j],2), "°E"), cex = 1.75, col = "royalblue4")

        ## Draw the left part of the figure with the subseasonal skill:
        par(mar = c(6, 4, 1, 0), fig = c(0.12, 0.47, 0.18, 0.88), new=TRUE)
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 12.04), xlim = c(-0.23, 3.2), ann = F) ## , bty="n") ## to remove black border

        ## Add axes labels:
        mtext(side = 1, text = "                             Forecast time", line = 4.8, cex = 1.2, font = 3)
        mtext(side = 2, text = "Start date", line = 3, cex = 1.2, font = 3)

        ## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
        axis(1, at=seq(0,3), las=3, cex.axis=1, labels=FALSE, mgp=c(3,0.7,0)) 

        ## Plot x-axis labels rotated of 45 degrees at default tick marks:
        labels <- paste0(c("5-11","12-18","19-25","26-32"), " days")
        text(0:3, par("usr")[3] - 0.55, srt = 45, adj = 1, labels = labels, xpd = TRUE)

        ## Draw subtitles:
        mtext(1, text = "                                     Reference dataset: Era-Interim", line = 6)
        mtext(1, text = "                                     Seasonal hindcasts: ECMWF System 5", line = 7)
        mtext(1, text = "                                     Subseasonal hindcasts: ECMWF monthly forecasting system", line = 8)
       
        ## Plot y-axis:
        axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

        ## create an array similar to array.pers but with colors instead of frequencies:                
        n.cols <- length(my.cols)
        array.colors <- array(my.cols[n.cols], c(12,4))
        for(int in (n.cols-1):1) array.colors[my.FairRpss.sub[,,i,j] < my.brks[int+1]] <- my.cols[int]

        ## set to gray the raws when data is not availabe:
        array.colors[(1:12)[-months.avail],] <- "gray"

        ## fill with colors the cells in the figure:
        for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l], border="black") }}
        ## add black border at the bottom:
        polygon(c(-0.5, 3.5, 3.5, -0.5), c(0.54, 0.54, 0.5, 0.5), col="black")

        ## add black border to the right border:
        polygon(c(3.35, 3.35, 3.5, 3.5), c(0.5, 12.5, 12.5, 0.5), col="black")

        ## Draw gray box between the two graphs:
        par(mar = c(0, 0, 0, 0), fig = c(0.472, 0.49, 0.303, 0.860), new=TRUE)
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ann = F, col="red", panel.first={points(0, 0, pch=16, cex=1e6, col="grey80")}, bty="n") ## to remove black border

        ## Plot right part of the figure, with the seasonal skill:
        par(mar = c(6, 4, 1, 0), fig = c(0.386, 0.730, 0.18, 0.88), new=TRUE)
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 12.04), xlim = c(-0.23, 3.2), ann = F, bty="n") ## to remove black border

        ## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
        axis(1, at = seq(0, 2), las = 3, cex.axis = 1, labels = FALSE, mgp = c(3, 0.7, 0)) 

        ## Plot x-axis labels rotated of 45 degrees at default tick marks:
        labels <- paste0(c("1 month", paste0(2:3, " months")))
        text(0:2, par("usr")[3] - 0.55, srt = 45, adj = 1, labels = labels, xpd = TRUE)

        ## create an array similar to array.pers but with colors instead of frequencies:                
        n.cols <- length(my.cols)
        ##array.colors <- array("gray60", c(12,3))
        for(int in (n.cols-1):1) array.colors[my.FairRpss.sea4[,,i,j] < my.brks[int+1]] <- my.cols[int]
        
        ## fill with colors the cells in the figure:
        for(p in 1:12){ for(l in 0:2){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l], border="black") }}

        ## add black border at the bottom:
        polygon(c(-0.5, 2.5, 2.5, -0.5), c(0.54, 0.54, 0.5, 0.5), col="black")

        ## add black border at the top:
        polygon(c(-0.5, 2.5, 2.5, -0.5), c(12.55, 12.55, 12.48, 12.48), col="black")

        ## Draw gray box between the two graphs:
        par(mar = c(0, 0, 0, 0), fig = c(0.492, 0.493, 0.303, 0.860), new=TRUE)
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ann = F, col="red", panel.first={points(0, 0, pch=16, cex=1e6, col="black")}, bty="n") ## to remove black border

        ## add black border to the right border:
        ##polygon(c(3.35, 3.35, 3.5, 3.5), c(0.5, 12.5, 12.5, 0.5), col="black")

        ## Draw color legend:
        par(fig=c(0.70, 0.85, 0.28, 0.88), new=TRUE) 
        ColorBar(my.brks[2:(length(my.brks)-1)], cols=my.cols[2:(length(my.cols)-1)], vert=TRUE, label_scale=1, bar_limits=c(my.brks[2],my.brks[length(my.brks)-1]), col_inf=my.cols[1], col_sup=my.cols[length(my.cols)], subsample=1)
        gc()
        
        dev.off()
        
        ## Add BSC and NEWA logo to the figures in the /formatted subfolder:  
        system(paste0("/esarchive/scratch/ncortesi/scripts/fig2catalog_NEWA.sh -m 80 ", my.file," ", my.file2))

    } ## close for on j
} ## close for on i


##
## Draw figure for area average:
##
my.file <- paste0(work.dir,'/',var.name,'/FairRPSS_of_', var.name,'_area_average.png')
my.file2 <- paste0(work.dir,'/',var.name,'/formatted/FairRPSS_of_', var.name,'_area_average.png')
        
png(file = my.file, width=530, height=700)  

my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
my.brks <- c(-1, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
my.brks <- c(-1, 0, 0.01, 0.02, 0.03, 0.04, 0.5, 0.6, 1)

plot.new()

## Plot figure title:
par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE) 
mtext(paste0("FairRPSS of ECMWF 10-m wind speed \nfor 1996-2015 averaged over Europe "), cex = 1.75, col = "royalblue4")

## Draw the left part of the figure with the subseasonal skill:
par(mar = c(6, 4, 1, 0), fig = c(0.12, 0.47, 0.18, 0.88), new=TRUE)
plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 12.04), xlim = c(-0.23, 3.2), ann = F) ## , bty="n") ## to remove black border

## Add axes labels:
mtext(side = 1, text = "                             Forecast time", line = 5, cex = 1.2, font = 3)
mtext(side = 2, text = "Start date", line = 3, cex = 1.2, font = 3)

## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
axis(1, at=seq(0,3), las=3, cex.axis=1, labels=FALSE, mgp=c(3,0.7,0)) 

## Plot x-axis labels rotated of 45 degrees at default tick marks:
labels <- paste0(c("5-11","12-18","19-25","26-32"), " days")
text(0:3, par("usr")[3] - 0.55, srt = 45, adj = 1, labels = labels, xpd = TRUE)
y

## Draw subtitle:
mtext(1, text = "                                     Reference dataset: Era-Interim", line = 7)

## Plot y-axis:
axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

## create an array similar to array.pers but with colors instead of frequencies:                
n.cols <- length(my.cols)
array.colors <- array(my.cols[n.cols], c(12,4))
for(int in (n.cols-1):1) array.colors[my.FairRpss.sub.aver < my.brks[int+1]] <- my.cols[int]

## set to gray the raws when data is not availabe:
array.colors[(1:12)[-months.avail],] <- "gray"

## fill with colors the cells in the figure:
for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l], border="black") }}

## add black border at the bottom:
polygon(c(-0.5, 3.5, 3.5, -0.5), c(0.54, 0.54, 0.5, 0.5), col="black")

## add black border to the right border:
polygon(c(3.35, 3.35, 3.5, 3.5), c(0.5, 12.5, 12.5, 0.5), col="black")

## Draw gray box between the two graphs:
par(mar = c(0, 0, 0, 0), fig = c(0.472, 0.49, 0.303, 0.860), new=TRUE)
plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ann = F, col="red", panel.first={points(0, 0, pch=16, cex=1e6, col="grey80")}, bty="n") ## to remove black border

## Plot right part of the figure, with the seasonal skill:
par(mar = c(6, 4, 1, 0), fig = c(0.386, 0.730, 0.18, 0.88), new=TRUE)
plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 12.04), xlim = c(-0.23, 3.2), ann = F, bty="n") ## to remove black border

## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
axis(1, at = seq(0, 2), las = 3, cex.axis = 1, labels = FALSE, mgp = c(3, 0.7, 0)) 

## Plot x-axis labels rotated of 45 degrees at default tick marks:
labels <- paste0(c("1 month", paste0(2:3, " months")))
text(0:2, par("usr")[3] - 0.55, srt = 45, adj = 1, labels = labels, xpd = TRUE)

## create an array similar to array.pers but with colors instead of frequencies:                
n.cols <- length(my.cols)
##array.colors <- array("gray60", c(12,3))
for(int in (n.cols-1):1) array.colors[my.FairRpss.sea4.aver < my.brks[int+1]] <- my.cols[int]

## fill with colors the cells in the figure:
for(p in 1:12){ for(l in 0:2){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l], border="black") }}

## add black border at the bottom:
polygon(c(-0.5, 2.5, 2.5, -0.5), c(0.54, 0.54, 0.5, 0.5), col="black")

## add black border at the top:
polygon(c(-0.5, 2.5, 2.5, -0.5), c(12.55, 12.55, 12.48, 12.48), col="black")

## Draw gray box between the two graphs:
par(mar = c(0, 0, 0, 0), fig = c(0.492, 0.493, 0.303, 0.860), new=TRUE)
plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ann = F, col="red", panel.first={points(0, 0, pch=16, cex=1e6, col="black")}, bty="n") ## to remove black border

## add black border to the right border:
##polygon(c(3.35, 3.35, 3.5, 3.5), c(0.5, 12.5, 12.5, 0.5), col="black")

## Draw color legend:
par(fig=c(0.70, 0.85, 0.28, 0.88), new=TRUE) 
ColorBar(my.brks[2:(length(my.brks)-1)], cols=my.cols[2:(length(my.cols)-1)], vert=TRUE, label_scale=1, bar_limits=c(my.brks[2],my.brks[length(my.brks)-1]), col_inf=my.cols[1], col_sup=my.cols[length(my.cols)], subsample=1)
gc()

dev.off()

## Add BSC and NEWA logo to the figures in the /formatted subfolder:  
system(paste0("/esarchive/scratch/ncortesi/scripts/fig2catalog_NEWA.sh -m 80 ", my.file," ", my.file2))


##############################################################################################
#   visualize difference between monthly skill scores maps of two different forecast systems #
##############################################################################################
#
# both systems must have been validated previously with the same resolution
# (e.g: with the lower resolution of the common reanalysis or system to whom they were interpolated)
#

comparison <- FALSE
if(comparison){

    months.avail <- 1:12 ##c(1:7, 10:12) ##1:12 ##1:5  ## months with a file 'sfcWind_<monthname>.RData' already available for the forecast systems

    ## First forecast system to compare:
    work.dir1 <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_4_members_1996-2013_4_start_dates"

    ## second forecast system to compare:
    work.dir2 <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2013_4_start_dates"

    work.dir1.orig <- work.dir1
    work.dir2.orig <- work.dir2
    
    for(area.num in areas){
        ## print which area we are evaluating and its coordinates:
        area <- eval(parse(text=paste0("area", area.num)))
        area.name <- eval(parse(text=paste0("area", area.num,".name")))
        print(paste0("Area: ",area.name, "  (Lon: ",area[1],"E to ",area[2],"E; Lat: ",area[3],"N to ",area[4],"N)"))
        
        ## position of max and min long and lat values the chosen region:
        lon.min <- area[1]
        lon.max <- area[2]
        lat.min <- area[3]
        lat.max <- area[4]

        for(month in months.avail){
            ## month=1  # for the debug
            print(paste0("Evaluating month of ", my.month[month]))

            ## Load Skill data of the first forecast system just to load forecast.year, lats and lons:
            work.dir.orig <- work.dir1
            load(file=paste0(work.dir1,'/',var.name,'_',my.month[month],'.RData'))
            if(work.dir1 != work.dir1.orig) work.dir1 <- work.dir1.orig
            
            sdates.seq <- weekly.seq(forecast.year,mes,day)  # sequence of dates corresponding to all the thursday of the year
            my.startdates.days <- as.integer(substr(sdates.seq[which(as.integer(substr(sdates.seq,5,6)) == month)],7,8)) #startdates.monthly[[month]] #c(1:5)
            stringa <- paste0(my.startdates.days,collapse=",")

            ## 'lats' and 'lons' are the lists of lat and lon values used in our skill score and loaded before from the .RData.
            ## 'lats' MUST BE in decreasing order from 90 to -90, and 'lons' MUST BE in increasing order from 0 to 360.
            ## 'pos.area.lat' and 'pos.area.lon' defined below select the POSITION of the elements of the region to plot inside vectors lats and lons, respectively.

            ## restrict long values to the chosen ones, instead than the whole world (negative lon.min are considered separately than positive lon.min values):

            ## first, convert eventual negative lon values to positive ones:
            lon.max.posit <- ifelse(lon.max >= 0, lon.max, lon.max + 360)
            lon.min.posit <- ifelse(lon.min >= 0, lon.min, lon.min + 360)

            ## then, get the position of the first and last lon values inside the study area:
            pos.lon.min <- first(which(lons >= lon.min.posit))
            pos.lon.max <- last(which(lons <= lon.max.posit))

            ## concatena the position of the first and last lon values to get all the lon positions of the study area:
            ## (in case lon.min < 0 and lon.max > 0, the positive lon.min is greater than lon.max, so we have to modify the concatenation)
            ## (the concatenating order is important because we use pos.area.lon not only to select points for the Rel.diagram, but also to show them with PlotEquiMap)
            if(pos.lon.min < pos.lon.max) pos.area.lon <- pos.lon.min:pos.lon.max else pos.area.lon <- c(pos.lon.min:length(lons), 1:pos.lon.max)
            
            ## restrict latitude values to chosen ones (in this case, we don't need to convert lat to positive values:
            pos.lat.max <- first(which(lats <= lat.max))
            pos.lat.min <- last(which(lats >= lat.min))
            pos.area.lat <- pos.lat.max:pos.lat.min
            
            for(my.score.name.map in my.score.name){
                ##my.score.name.map='FairRpss'  # for the debug

                print(paste0("Score: ",my.score.name.map))

                ## Load Skill data of the first forecast system:
                work.dir1.orig <- work.dir1
                load(file=paste0(work.dir1,'/',var.name,'_',my.month[month],'.RData'))
                if(work.dir1 != work.dir1.orig) work.dir1 <- work.dir1.orig
                
                if(my.score.name.map == 'EnsCorr') my.score1 <- my.EnsCorr.chunk
                if(my.score.name.map == 'Rpss') my.score1 <- my.Rpss.chunk
                if(my.score.name.map == 'FairRpss') my.score1 <- my.FairRpss.chunk
                if(my.score.name.map == 'FairCrpss') my.score1 <- my.FairCrpss.chunk

                ## Load Skill data of the second forecast system:
                load(file=paste0(work.dir2,'/',var.name,'_',my.month[month],'.RData'))
                if(work.dir2 != work.dir2.orig) work.dir2 <- work.dir2.orig
                
                if(my.score.name.map == 'EnsCorr') my.score2 <- my.EnsCorr.chunk
                if(my.score.name.map == 'Rpss') my.score2 <- my.Rpss.chunk
                if(my.score.name.map == 'FairRpss') my.score2 <- my.FairRpss.chunk
                if(my.score.name.map == 'FairCrpss') my.score2 <- my.FairCrpss.chunk
                
                if(my.score.name.map == 'EnsCorr')  brk.rpss <- seq(-1,1,by=0.1) else brk.rpss <- c(-1000, seq(-0.9,1,by=0.1))
                col.rpss <- colorRampPalette(col)(length(brk.rpss)-1)

                ## at present all breaks and colors are the same, so there is no need to differenciate between indexes, aside the two Fair scores whose range is [-inf,0]
                my.brk <- brk.rpss 
                my.col <- col.rpss
                my.brk.labels <- my.brk
                if(my.score.name.map == 'FairRpss' | my.score.name.map=='FairCrpss') my.brk.labels <- c(expression(-infinity), my.brk[-1])
                
                for(lead in 1:n.leadtimes){
                    print(paste0("Leadtime: ",lead))

                    ## in case we are plotting the world skill:
                    if(my.score.name.map != "RelDiagr"){

                        my.file <- paste0(work.dir,'/',var.name,'/',var.name,'_', area.name,'_comparison_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead],'.png')
                        my.file2 <-  paste0(work.dir,'/',var.name,'/formatted/',var.name,'_',area.name,'_comparison_', my.score.name.map, '_', my.month[month],'_leadtime_',leadtime.week[lead], '.png')
                        
                        png(file=my.file, width=1000,height=600)

                        ## Map:   
                        par(fig=c(0,0.92,0,0.89), new=TRUE)

                        ## lat.plot are the latitude values to plot of the selected area, in decreasing order from 90 to -90:
                        lat.plot <- lats[pos.area.lat]
                        lon.plot <- lons[pos.area.lon]
                        
                        ## lon.plot are the longitude values to plot of the selected area.
                        ## However, we want to plot America to the left of the map with PlotEquiMap, so we needs to move lon.plot values in increasing order from -180 to 180,
                        ## swapping the values from 180 to 360 degrees at the beginning of the lon.plot vector and converting them to negative values
                        ## (instead of from 0 to 360 as in the 'lons' vector):                    
                        ss <- which(lon.plot >= 180)
                        pp <-  which(lon.plot < 180)
                        lon.plot <- c(lon.plot[ss] - 360, lon.plot[pp])
                        
                        ## new plot for any region:
                        my.score.diff <- my.score2 - my.score1
                        PlotEquiMap(var=my.score.diff[lead, pos.area.lat, pos.area.lon], lon=lon.plot, lat=lat.plot ,title_scale=0.8, brks=my.brk, cols=my.col ,axelab=F, filled.continents=FALSE, drawleg=F)
                        
                        ## same but with America to the right:
                        ## PlotEquiMap(var=my.score[lead,,], lon=lons, lat=lats ,title_scale=0.8, brks=my.brk, cols=my.col ,axelab=F, filled.continents=FALSE, drawleg=F)

                        ##if(my.score.name.map == "EnsCorr") my.PValue.rev <- my.EnsCorr.pvalue < 0.05
                        ##if(my.score.name.map == "FairRpss") my.PValue.rev <- my.FairRpss.pvalue < 0.05
                        ##if(my.score.name.map == "FairCrpss") my.PValue.rev <- my.FairCrpss.pvalue < 0.05
                        
                        ## Draw the significance diagonal lines:
                        ##pv <- aperm(my.PValue.rev, c(1,3,2)) ## must swap lon with lat to be able to employ the hatching function:

                        ##hatching(lats=lat.plot, lons=lon.plot, mask=pv[lead, pos.area.lon, pos.area.lat], dens = 12, ang = 45, col_line = '#252525', lwd_size = 0.5, crosshatching = FALSE)

                        ## Map title:
                        par(fig=c(0,1,0.82,0.91), new=TRUE)
                        ##my.title <- paste0(my.score.name.map,' of ',cfs.name,'\n ', var.name.map,' for ',my.month[month],'. Forecast time ',leadtime.week[lead],'.')
                        my.title <- paste0("ECMWF-MPS / ", var.name.map, " / ", my.score.name.title[which(my.score.name == my.score.name.map)], "\n", my.month[month], " (",leadtime.week[lead]," days lead time)/ ", yr1.hind, "-", yr2.hind)
                        mtext(my.title, cex=2.4, font=2)

                        ## Color legend:
                        par(fig=c(0.92,1,0,0.9), new=TRUE)
                        ##par(fig=c(0.52,0.82,0,0.9), new=TRUE)

                        ColorBar2(my.brk, cols=my.col, vert=T, my.ticks=-0.5 + 1:length(my.brk), my.labels=my.brk.labels)
                        
                        ## arrange lat/ lon in my.PValue (a second variable in the EnsCorr file) to start from +90 degr. (lat) and from 0 degr (lon):
                        ## my.PValue.rev[lead,,] <- my.PValue[i,n.lat:1,c((n.lonr+1):n.lon,1:n.lonr)]
                        
                        dev.off()
                        
                        system(paste0("~/scripts/fig2catalog.sh -s 0.8 -c 'Start dates: ", stringa," of ",my.month[month],"\nLead time: ", leadtime.week[lead], " days\nReference dataset: ERA-Interim\nBias correction: none\nHatched area: significant from a bootstrapping test (p_value=0.05)' ", my.file," ", my.file2))
                        gc()
                        
                    } # close if on !RelDiagr
                    
                    gc()
                } # next lead

                gc()
            } # next score

            gc()
        } # next month

    } ## next area

} ## close if on comparison

#############################################################################################
#   Visualize differences between the summary tables of two forecast systems for a region   #
#############################################################################################
#
# both systems must have been validated previously with the same resolution
# (e.g: with the lower resolution of the common reanalysis or system to whom they were interpolated)
#

showRPSS <- TRUE ## to show the RPSS instead of the FairCRPSS

comparison <- FALSE
if(comparison){

    pos.only <- FALSE ## set it to TRUE if you want to exclude from the mean the points with negative skill, FALSE otherwise.
    months.avail <- 1:12 ##c(1:7, 10, 11) ##1:5 ## 1:12  ## months with a file 'sfcWind_<monthname>.RData' already available for both forecast systems

    ## First forecast system to compare:
    work.dir1 <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_4_members_1996-2013_4_start_dates"

    ## second forecast system to compare:
    work.dir2 <- "/esarchive/scratch/ncortesi/Subestacional/ECMWF_11_members_1996-2013_4_start_dates"

    work.dir1.orig <- work.dir1
    work.dir2.orig <- work.dir2
    
    print("Comparison tables")    
    for(area.num in areas){

        ## print which area we are evaluating and its coordinates:
        area <- eval(parse(text=paste0("area", area.num)))
        area.name <- eval(parse(text=paste0("area", area.num,".name")))
        print(paste0("Area: ",area.name, "  (Lon: ",area[1],"E to ",area[2],"E; Lat: ",area[3],"N to ",area[4],"N)"))
        
        ## position of max and min long and lat values the chosen region:
        lon.min <- area[1]
        lon.max <- area[2]
        lat.min <- area[3]
        lat.max <- area[4]

        ## If you use a mask, load it to average only over land and check that it has the same lat and lon positions of the score maps:
        if(land.only == TRUE){
            load(paste0('/shared/earth/EarthSystemServices/TOOLS/Topography_bathymetry_and_masks/Seamask.', n.lon, '.', n.lat,'.50m','.RData'))
            mask <- Seamask.512.256.50m
            ##Coordinates repositioning and mask conditioning
            mask <- mask[c((n.lonr+1):n.lon,1:n.lonr),n.lat:1]
            mask <- ifelse(mask == 1, 0, 1) ## assign 1 to land and 0 to sea to plot the mask
            mask <- t(mask)
            myImagePlot(mask)
            myImagePlot(my.FairRpss.chunk[1,,])
            mask <- ifelse(mask == 0, NA, 1) ## assign 1 to land and NA to sea to multiply it for the score values later
        }

        ## plot a box with the chosen area (do it only once):
        ##
        ##png(file=paste0(work.dir,'/',var.name,'/Box_',area.name,'.png'),width=1400,height=800)  
        ##PlotEquiMap(my.FairRpss.chunk[1,,c((n.lonr+1):n.lon,1:n.lonr)], c(lons[c((n.lonr+1):n.lon)]-360, lons[1:n.lonr]), lats, brks=c(-1000,1000), cols="lightblue" , axelab=F, filled.continents=TRUE, drawleg=F, boxlim=c(area[1],area[3],area[2],area[4]), boxcol="black", boxlwd=.7)
        ##dev.off()

        array.EnsCorr1 <- array.FairRpss1 <- array.FairCrpss1 <- array(NA,c(12,4))
        array.EnsCorr2 <- array.FairRpss2 <- array.FairCrpss2 <- array(NA,c(12,4))

        ## Load one file, just to import the lats and lons values:
        load(file=paste0(work.dir1,'/',var.name,'_',my.month[months.avail[1]],'.RData'))
        if(work.dir1 != work.dir1.orig) work.dir1 <- work.dir1.orig

        ## restrict lon values to chosen one instead than the whole world (negative lon.min are considered separately than positive lon.min values):
        if(lon.max < 0) lon.max <- lon.max + 360
        pos.lon.max <- which(lons <= lon.max) ## get the position inside the positive lons vector of the longitude values smaller or equal then lon.max value
        pos.lon.max.last <- pos.lon.max[length(pos.lon.max)] ## get the position of the last lon value to the east of Greenwich inside the study area

        ## concatenate the longitude positions to the west of Greenwich with those to the east of Greenwich in a single vector:
        ## (the order is not important because we use pos.area.lon only to select points for the Rel.diagram, not for PlotEquiMap)
        if(lon.min < 360) pos.area.lon <- c((which(lons >= lon.min)[1]:length(lons)), 1:pos.lon.max.last)
        
        ## if there are no negative lon.values, all long positions are to the east of Greenwich:
        if(lon.min > 360) pos.area.lon <- (which(lons >= (lon.min - 360))[1]):pos.lon.max.last

        ## restrict latitude values to chosen ones:
        pos.lat.min <- which(lats <= lat.min)[1] - 1 ## minimum latitude inside the chosen area
        if(area.name != "World") pos.area.lat <- c(which(lats <= lat.max)[1]:pos.lat.min)  # restrict area to chosen one
        if(area.name == "World") pos.area.lat <- 1:length(lats) ## correct the lat values in case of the whole world

        ## Load all data of the validation of the first forecast system and average it over the chosen region:
        for(month in months.avail){
            load(file = paste0(work.dir1, '/', var.name, '_', my.month[month], '.RData'))
            if(work.dir1 != work.dir1.orig) work.dir1 <- work.dir1.orig

            if(land.only == TRUE){
                ## remove sea values:
                for(i in 1:4) my.EnsCorr.chunk[i,,] <- my.EnsCorr.chunk[i,,] * mask
                if(showRPSS) for(i in 1:4) my.Rpss.chunk[i,,] <- my.Rpss.chunk[i,,] * mask
                for(i in 1:4) my.FairRpss.chunk[i,,] <- my.FairRpss.chunk[i,,] * mask
                if(!showRPSS) for(i in 1:4) my.FairCrpss.chunk[i,,] <- my.FairCrpss.chunk[i,,] * mask
            }
            
            for(l in 0:3){
                if(pos.only == TRUE){
                    my.EnsCorr.chunk[which(my.EnsCorr.chunk <= 0)] <- NA
                    if(showRPSS) my.FairRpss.chunk[which(my.FairRpss.chunk <= 0)] <- NA
                    my.FairRpss.chunk[which(my.FairRpss.chunk <= 0)] <- NA
                    if(!showRPSS) my.FairCrpss.chunk[which(my.FairCrpss.chunk <= 0)] <- NA
                }
            
                array.EnsCorr1[month, 1+l] <- mean(my.EnsCorr.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)
                array.FairRpss1[month, 1+l] <- mean(my.FairRpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  
                if(!showRPSS) array.FairCrpss1[month, 1+l] <- mean(my.FairCrpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  
                if(showRPSS) array.FairCrpss1[month, 1+l] <- mean(my.Rpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  

            }
        } ## close for on month

        ## Load all data of the validation of the second forecast system and average it over the chosen region:
        for(month in months.avail){
            load(file=paste0(work.dir2, '/', var.name, '_', my.month[month], '.RData'))
            if(work.dir2 != work.dir2.orig) work.dir2 <- work.dir2.orig

            if(land.only == TRUE){
                ## remove sea values:
                for(i in 1:4) my.EnsCorr.chunk[i,,] <-  my.EnsCorr.chunk[i,,]*mask
                if(showRPSS) for(i in 1:4) my.Rpss.chunk[i,,] <-  my.Rpss.chunk[i,,]*mask
                for(i in 1:4) my.FairRpss.chunk[i,,] <-  my.FairRpss.chunk[i,,]*mask
                if(!showRPSS) for(i in 1:4) my.FairCrpss.chunk[i,,] <-  my.FairCrpss.chunk[i,,]*mask
            }
            
            for(l in 0:3){ 
                if(pos.only == TRUE){
                    my.EnsCorr.chunk[which(my.EnsCorr.chunk <= 0)] <- NA
                    if(showRPSS) my.Rpss.chunk[which(my.Rpss.chunk <= 0)] <- NA
                    my.FairRpss.chunk[which(my.FairRpss.chunk <= 0)] <- NA
                    if(!showRPSS) my.FairCrpss.chunk[which(my.FairCrpss.chunk <= 0)] <- NA
                }
            
                array.EnsCorr2[month, 1+l] <- mean(my.EnsCorr.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)
                array.FairRpss2[month, 1+l] <- mean(my.FairRpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  
                if(!showRPSS) array.FairCrpss2[month, 1+l] <- mean(my.FairCrpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)
                if(showRPSS) array.FairCrpss2[month, 1+l] <- mean(my.Rpss.chunk[1+l, pos.area.lat, pos.area.lon], na.rm=T)  

            }
            
        } ## close for on month

        array.EnsCorr.diff <- array.EnsCorr2 - array.EnsCorr1
        array.FairCrpss.diff <- array.FairCrpss2 - array.FairCrpss1
        array.FairRpss.diff <- array.FairRpss2 - array.FairRpss1

        ##my.cols <- rev(c('#a50026','#d73027','#f46d43','#fdae61','#fee090','#e0f3f8','#abd9e9','#74add1','#4575b4','#313695'))
        my.cols <- c(rev(c('#eff3ff','#c6dbef','#9ecae1','#6baed6','#3182bd','#08519c')),'#ffffb2','#fed976','#feb24c','#fd8d3c','#f03b20','#bd0026')
        ##my.seq <- c(-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4)
        ##my.seq <- c(-0.15, -0.12, -0.09, -0.06, -0.03, 0, 0.03, 0.06, 0.09, 0.12, 0.15)
        my.seq <- c(-0.12,-0.10, -0.08, -0.06, -0.04, -0.02, 0, 0.02, 0.04, 0.06, 0.08, 0.10, 0.12)

        if(land.only == TRUE) { mod.filename <- "_land" } else { mod.filename <- "" }

        mod.pos <- ifelse(pos.only, "_positive_only", "")
        mod.RPSS <- ifelse(showRPSS, "_RPSS_instead_of_FairCRPSS", "")
        
        my.file <- paste0(work.dir2,'/',var.name,'/Summary_comparison_of_',var.name,'_', area.name, mod.filename, mod.pos, mod.RPSS,'.png')
        ##my.file2 <- paste0(work.dir2,'/',var.name,'/formatted/Summary_Comparison_',var.name,'_', area.name, mod.filename,'_with_letras.png')

        png(file=my.file,width=700,height=500)  

        ## create an array similar to array.pers but with colors instead of frequencies:                
        plot.new()

        par(mar=c(0,0,2,0), fig=c(0.07, 0.27, 0.9, 1), new=TRUE) 
        mtext("(a)   EnsCorr", cex=1.5)

        par(mar=c(1,4,1,0), fig=c(0.02, 0.27, 0.1, 0.9), new=TRUE)
        plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
        mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
        mtext(side = 2, text = "Start date", line = 3, cex=1.2)
        axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
        axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)
        
        array.colors <- array(my.cols[12],c(12,4))
        array.colors[array.EnsCorr.diff < my.seq[13]] <- my.cols[12]
        array.colors[array.EnsCorr.diff < my.seq[12]] <- my.cols[11]
        array.colors[array.EnsCorr.diff < my.seq[11]] <- my.cols[10]
        array.colors[array.EnsCorr.diff < my.seq[10]] <- my.cols[9]
        array.colors[array.EnsCorr.diff < my.seq[9]] <- my.cols[8]
        array.colors[array.EnsCorr.diff < my.seq[8]] <- my.cols[7]
        array.colors[array.EnsCorr.diff < my.seq[7]] <- my.cols[6]
        array.colors[array.EnsCorr.diff < my.seq[6]] <- my.cols[5]
        array.colors[array.EnsCorr.diff < my.seq[5]] <- my.cols[4]
        array.colors[array.EnsCorr.diff < my.seq[4]] <- my.cols[3]
        array.colors[array.EnsCorr.diff < my.seq[3]] <- my.cols[2]
        array.colors[array.EnsCorr.diff < my.seq[2]] <- my.cols[1]
        array.colors[(1:12)[-months.avail],] <- "gray"

        for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

        par(mar=c(0,0,2,0), fig=c(0.32, 0.57, 0.9, 1), new=TRUE) 
        mtext("(b)  FairRPSS", cex=1.5)

        par(mar=c(1,4,1,0), fig=c(0.32, 0.57, 0.1, 0.9), new=TRUE)
        plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
        mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
        mtext(side = 2, text = "Start date", line = 3, cex=1.2)
        axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
        axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

        array.colors <- array(my.cols[12],c(12,4))
        array.colors[array.FairRpss.diff < my.seq[13]] <- my.cols[12]
        array.colors[array.FairRpss.diff < my.seq[12]] <- my.cols[11]
        array.colors[array.FairRpss.diff < my.seq[11]] <- my.cols[10]
        array.colors[array.FairRpss.diff < my.seq[10]] <- my.cols[9]
        array.colors[array.FairRpss.diff < my.seq[9]] <- my.cols[8]
        array.colors[array.FairRpss.diff < my.seq[8]] <- my.cols[7]
        array.colors[array.FairRpss.diff < my.seq[7]] <- my.cols[6]
        array.colors[array.FairRpss.diff < my.seq[6]] <- my.cols[5]
        array.colors[array.FairRpss.diff < my.seq[5]] <- my.cols[4]
        array.colors[array.FairRpss.diff < my.seq[4]] <- my.cols[3]
        array.colors[array.FairRpss.diff < my.seq[3]] <- my.cols[2]
        array.colors[array.FairRpss.diff < my.seq[2]] <- my.cols[1]
        array.colors[(1:12)[-months.avail],] <- "gray"
        
        for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

        par(mar=c(0,0,2,0), fig=c(0.62, 0.87, 0.9, 1), new=TRUE) 
        if(showRPSS) mtext("(c) RPSS", cex=1.5) else mtext("(c) FairCRPSS", cex=1.5)

        par(mar=c(1,4,1,0), fig=c(0.62, 0.87, 0.1, 0.9), new=TRUE)
        plot(1,1, type="n", xaxt="n", yaxt="n", ylim=c(1,12.04), xlim=c(-0.23,3.2), ann=F)
        mtext(side = 1, text = "Forecast time (days)", line = 3.5, cex=1.2)
        mtext(side = 2, text = "Start date", line = 3, cex=1.2)
        axis(1, at=seq(0,3), las=3, cex.axis=1, labels=c("5-11","12-18","19-25","26-32"), mgp=c(3,0.7,0))
        axis(2, at=seq(1,12), las=2, labels=rev(my.month.short), cex.axis=1)

        array.colors <- array(my.cols[12],c(12,4))
        array.colors[array.FairCrpss.diff < my.seq[13]] <- my.cols[12]
        array.colors[array.FairCrpss.diff < my.seq[12]] <- my.cols[11]
        array.colors[array.FairCrpss.diff < my.seq[11]] <- my.cols[10]
        array.colors[array.FairCrpss.diff < my.seq[10]] <- my.cols[9]
        array.colors[array.FairCrpss.diff < my.seq[9]] <- my.cols[8]
        array.colors[array.FairCrpss.diff < my.seq[8]] <- my.cols[7]
        array.colors[array.FairCrpss.diff < my.seq[7]] <- my.cols[6]
        array.colors[array.FairCrpss.diff < my.seq[6]] <- my.cols[5]
        array.colors[array.FairCrpss.diff < my.seq[5]] <- my.cols[4]
        array.colors[array.FairCrpss.diff < my.seq[4]] <- my.cols[3]
        array.colors[array.FairCrpss.diff < my.seq[3]] <- my.cols[2]
        array.colors[array.FairCrpss.diff < my.seq[2]] <- my.cols[1]
        array.colors[(1:12)[-months.avail],] <- "gray"

        for(p in 1:12){ for(l in 0:3){ polygon(c(0.5+l-1, 0.5+l-1, 1.5+l-1, 1.5+l-1), c(-0.5+13-p, 0.5+13-p, 0.5+13-p, -0.5+13-p), col=array.colors[p,1+l]) }}

        par(fig=c(0.89, 1, 0.1, 0.9), new=TRUE) 
        ##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)
        ColorBar2(brks = my.seq, cols = my.cols, vert=T, draw.ticks=F, label.dist=0.3, my.ticks=-0.5 + 1:length(my.seq), my.labels=my.seq)
        gc()

        dev.off()

        ## Not checked but whould work:
        ##system(paste0("~/scripts/fig2catalog.sh -r 50 -c 'Region: ", area.name, "\nReference dataset: ERA-Interim' ", my.file," ", my.file2))

    } ## close for on area.num
    
} #close if on comparison
