
library(s2dverification)  # for the function Load()
library(abind)
source('/home/Earth/ncortesi/Downloads/scripts/Rfunctions.R') # for the calendar functions

# available reanalysis for the geopotential (g500) or the geopotential height (z500 = g500/9.81):
ERAint <- list(path = '/esnas/reconstructions/ecmwf/eraint/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')

ERAint.name <- "ERA-Interim"
JRA55 <- list(path = '/esnas/reconstructions/jma/jra-55/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
JRA55.name <- "JRA-55"

workdir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes"    # working dir with the input files with the WT classifications for each MSLP grid point
mapdir <- "/scratch/Earth/ncortesi/RESILIENCE/Regimes_maps"  # output dir with seasonal and yearly maps

rean <- ERAint  # choose one of the two above reanalysis from where to load the input psl and var data
rean.name <- ERAint.name

psl <- "g500"                 # pressure variable to use from the chosen reanalysis

var.name <- "tas" #"sfcWind" #"tas"                              # name of the 'predictand' variable of the chosen reanalysis
var.name.full <- "Temperature" #"Wind Speed" #"Temperature"         # full name of the 'predictand' variable to put in the title of the graphs
var.unit <- "ºC" #"m/s"                                  # unit of measure (for drawing color scales)

year.start <- 1979
year.end <- 2013

variance.explained <- 0.8  # the user can set here the minimum % of variance explained by the PCs (typically 0.8 which is equal to 80%)

# Coordinates of the box enclosing the study region (the Northern Atlantic in this case):
lat.max <- 70
lat.min <- 30
lon.max <- 40
lon.min <- 280 # put a positive number here because the geopotential has only positive values of longitud!

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

# load only 1 day of geopot to detect the minimum and maximum lat and lon values to identify only the North Atlantic
domain <- Load(var = psl, exp = NULL, obs = list(rean), paste0(year.start,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat', nprocs=1)
pos.lat.max <- which(abs(domain$lat-lat.max)==min(abs(domain$lat-lat.max)))
pos.lat.min <- which(abs(domain$lat-lat.min)==min(abs(domain$lat-lat.min)))
pos.lon.max <- which(abs(domain$lon-lon.max)==min(abs(domain$lon-lon.max)))
pos.lon.min <- which(abs(domain$lon-lon.min)==min(abs(domain$lon-lon.min)))
pos.lat <- if(pos.lat.min < pos.lat.max) {pos.lat.min:pos.lat.max} else { pos.lat.max:pos.lat.min}
pos.lon <- c(1:pos.lon.max,pos.lon.min:length(domain$lon)) # beware that it only consider the case where the study region cross the meridian of Greenwhich!
n.pos.lat <- length(pos.lat)
n.pos.lon <- length(pos.lon)
lat <- domain$lat[pos.lat] # lat of chosen area only (it is smaller than the whole spatial domain loaded by the data)
lon <- domain$lon[pos.lon] # lon of chosen area only
lat.min.area <- domain$lat[pos.lat.min] # min and max lat/lon of the chosen area only (same as lat.max, but its values correspond to actual values in the lat vector)
lat.max.area <- domain$lat[pos.lat.max]
lon.min.area <- domain$lon[pos.lon.min]
lon.max.area <- domain$lon[pos.lon.max]
if(lat.min >= lat.max) stop("lat.min cannot be greater than lat.max")

# Load psl data:
psleuFull <-array(NA,c(n.days.in.a.yearly.period(year.start,year.end), n.pos.lat, n.pos.lon))

for (y in year.start:year.end){
    var <- Load(var = psl, exp = NULL, obs = list(rean), paste0(y,'0101'), storefreq = 'daily', leadtimemax = n.days.in.a.year(y), output = 'lonlat',
                latmin = lat.min.area, latmax = lat.max.area, lonmin = lon.min.area, lonmax = lon.max.area)
    psleuFull[seq.days.in.a.future.year(year.start, y),,] <- var$obs
    rm(var)
    gc()
}

if(psl=="g500") psleuFull <- psleuFull/9.81   # convert geopotential to geopotential height
    
# compute the PCs:
my.PCA <- list()
my.cluster <- list()
tot.variance <- list()
n.pcs <- my.cluster <- c()

for(period in 13:16){ # 1-12: Jan-Dec; 13-16: Winter-Spring-Summer-Autumn; 17: Year

    days.period <- NA
    for(y in year.start:year.end) days.period <- c(days.period, n.days.in.a.future.year(year.start, y) + pos.period(y,period))
    days.period <- days.period[-1] # remove the NA at the begin that was introduced only to be able to execure the above command
    n.days.period <- length(days.period)

    pslPeriod <- psleuFull[days.period,,] # select only days in the chosen period (i.e: winter)

    # weight the pressure fields based on latitude:
    lat.weighted <- cos(lat*pi/180)^0.5
    lat.weighted.array <- InsertDim(InsertDim(lat.weighted,2,n.pos.lon),1,n.days.period)
    pslPeriod.weighted <- pslPeriod * lat.weighted.array
    rm(lat.weighted.array)
    gc()
    
    pslmat <- pslPeriod.weighted
    dim(pslmat) <- c(n.days.period, n.pos.lat*n.pos.lon) # convert array in a matrix!
    rm(pslPeriod, pslPeriod.weighted)
    gc()

    my.seq <- seq(1, n.pos.lat*n.pos.lon, 9) # select only 1 point of 9
    pslcut <- pslmat[,my.seq]

    my.PCA[[period]] <- princomp(pslcut,cor=FALSE)
    tot.variance[[period]] <- head(cumsum(my.PCA[[period]]$sdev^2/sum(my.PCA[[period]]$sdev^2)),50)  # check how many PCAs to keep basing on the sum of their expl. variance
    n.pcs[period] <- head(as.numeric(which(tot.variance[[period]] > variance.explained)),1) # select only the pcs that explains at least 80% of variance

    my.cluster[[period]] <- kmeans(my.PCA[[period]]$scores[,1:n.pcs[period]], 4)  # 4 is the number of clusters, 7 the number of EOFs which explains ~80% of variance

    rm(pslcut, pslmat)
    gc()
}

# Load var data:
vareuFull <-array(NA,c(n.days.in.a.yearly.period(year.start,year.end),n.pos.lat,n.pos.lon))

for (y in year.start:year.end){
    var <- Load(var = var.name, exp = NULL, obs = list(rean), paste0(y,'0101'), storefreq = 'daily', leadtimemax = n.days.in.a.year(y), output = 'lonlat',
           latmin = domain$lat[pos.lat.min], latmax = domain$lat[pos.lat.max], lonmin = domain$lon[pos.lon.min], lonmax = domain$lon[pos.lon.max])
    vareuFull[seq.days.in.a.future.year(year.start, y),,] <- var$obs
    rm(var)
    gc()
}

save.image(file=paste0(workdir,"/weather_regimes_",rean.name,"_",var.name,"_",year.start,"-",year.end,".RData"))
load(file=paste0(workdir,"/weather_regimes_",rean.name,"_",var.name,"_",year.start,"-",year.end,".RData"))

for(period in 13:16){ # 1-12: Jan-Dec; 13-16: Winter-Spring-Summer-Autumn; 17: Year
    days.period <- NA
    for(y in year.start:year.end) days.period <- c(days.period, n.days.in.a.future.year(year.start, y) + pos.period(y,period))
    days.period <- days.period[-1] # remove the NA at the begin that was introduced only to be able to execure the above command
    n.days.period <- length(days.period)

    varPeriod <- vareuFull[days.period,,] # select only var data during the chosen period

    varPeriodClim <- apply(varPeriod, c(2,3), mean, na.rm=T)
    varPeriodClim2 <- InsertDim(varPeriodClim, 1, n.days.period)

    varPeriodAnom <- varPeriod - varPeriodClim2
    rm(varPeriod, varPeriodClim2)
    gc()

    # for each of the 4 clusters, remove the days not belonging to sequences of at least 5 days:
    # warning: first 4 days and last 4 days are not take into account y the algorithm and are treated as not belonging to any sequence (sequ=FALSE)    
    wrdiff <- diff(my.cluster[[period]]$cluster)

    sequ <- rep(FALSE, n.days.period)
    for(i in 5:(n.days.period-5)){
        sequ[i] <- TRUE
        if(wrdiff[i] != 0 & wrdiff[i+1] != 0) sequ[i] = FALSE
        if(wrdiff[i] != 0 & wrdiff[i+2] != 0) sequ[i] = FALSE
        if(wrdiff[i] != 0 & wrdiff[i+3] != 0) sequ[i] = FALSE
        if(wrdiff[i] != 0 & wrdiff[i+4] != 0) sequ[i] = FALSE
        if(wrdiff[i-1] != 0 & wrdiff[i] != 0) sequ[i] = FALSE
        if(wrdiff[i-1] != 0 & wrdiff[i+1] != 0) sequ[i] = FALSE
        if(wrdiff[i-1] != 0 & wrdiff[i+2] != 0) sequ[i] = FALSE
        if(wrdiff[i-1] != 0 & wrdiff[i+3] != 0) sequ[i] = FALSE
        if(wrdiff[i-2] != 0 & wrdiff[i] != 0) sequ[i] = FALSE
        if(wrdiff[i-2] != 0 & wrdiff[i+1] != 0) sequ[i] = FALSE
        if(wrdiff[i-2] != 0 & wrdiff[i+2] != 0) sequ[i] = FALSE
        if(wrdiff[i-3] != 0 & wrdiff[i] != 0) sequ[i] = FALSE
        if(wrdiff[i-3] != 0 & wrdiff[i+1] != 0) sequ[i] = FALSE
        if(wrdiff[i-4] != 0 & wrdiff[i] != 0) sequ[i] = FALSE
    } # close for loop on i
    
    # sequence of daily cluster numbers without the days that doesn't belong to sequences of 5 or more days:
    cluster.sequence <- my.cluster[[period]]$cluster * sequ

    rm(wrdiff, sequ)
    gc()

    #wr1 <- which(my.cluster[[period]]$cluster==1)
    #wr2 <- which(my.cluster[[period]]$cluster==2)
    #wr3 <- which(my.cluster[[period]]$cluster==3)
    #wr4 <- which(my.cluster[[period]]$cluster==4)

    # Replace the days belonging to a regime with the days belonging to a sequence of at least 5 days of that regime:
    wr1 <- which(cluster.sequence == 1)
    wr2 <- which(cluster.sequence == 2)
    wr3 <- which(cluster.sequence == 3)
    wr4 <- which(cluster.sequence == 4)
    
    period.length <- n.days.in.a.period(period,1999)  # using year 1999 introduce a small error in winter season because of bisestile years

    # yearly frequency of each regime:
    wr1y <- wr2y <- wr3y <-wr4y <- c()
    for(y in year.start:year.end){
        #wr1y[y-year.start+1] <- length(which(my.cluster[[period]]$cluster[(y-year.start)*period.length+(1:period.length)] == 1))
        #wr2y[y-year.start+1] <- length(which(my.cluster[[period]]$cluster[(y-year.start)*period.length+(1:period.length)] == 2))
        #wr3y[y-year.start+1] <- length(which(my.cluster[[period]]$cluster[(y-year.start)*period.length+(1:period.length)] == 3))
        #wr4y[y-year.start+1] <- length(which(my.cluster[[period]]$cluster[(y-year.start)*period.length+(1:period.length)] == 4))
        wr1y[y-year.start+1] <- length(which(cluster.sequence[(y-year.start)*period.length+(1:period.length)] == 1))
        wr2y[y-year.start+1] <- length(which(cluster.sequence[(y-year.start)*period.length+(1:period.length)] == 2))
        wr3y[y-year.start+1] <- length(which(cluster.sequence[(y-year.start)*period.length+(1:period.length)] == 3))
        wr4y[y-year.start+1] <- length(which(cluster.sequence[(y-year.start)*period.length+(1:period.length)] == 4))
    }

    # convert to frequencies in %:
    wr1y <- wr1y/period.length
    wr2y <- wr2y/period.length
    wr3y <- wr3y/period.length
    wr4y <- wr4y/period.length

    varPeriodAnom1 <- varPeriodAnom[wr1,,]
    varPeriodAnom2 <- varPeriodAnom[wr2,,]
    varPeriodAnom3 <- varPeriodAnom[wr3,,]
    varPeriodAnom4 <- varPeriodAnom[wr4,,]

    varPeriodAnom1mean <- apply(varPeriodAnom1,c(2,3),mean,na.rm=T)
    varPeriodAnom2mean <- apply(varPeriodAnom2,c(2,3),mean,na.rm=T)
    varPeriodAnom3mean <- apply(varPeriodAnom3,c(2,3),mean,na.rm=T)
    varPeriodAnom4mean <- apply(varPeriodAnom4,c(2,3),mean,na.rm=T)

    varPeriodAnomBoth1 <- abind(varPeriodAnom, varPeriodAnom1, along = 1)
    varPeriodAnomBoth2 <- abind(varPeriodAnom, varPeriodAnom2, along = 1)
    varPeriodAnomBoth3 <- abind(varPeriodAnom, varPeriodAnom3, along = 1)
    varPeriodAnomBoth4 <- abind(varPeriodAnom, varPeriodAnom4, along = 1)

    pvalue1 <- apply(varPeriodAnomBoth1, c(2,3), function(x) t.test(x[1:n.days.period],x[(n.days.period+1):length(x)])$p.value)
    pvalue2 <- apply(varPeriodAnomBoth2, c(2,3), function(x) t.test(x[1:n.days.period],x[(n.days.period+1):length(x)])$p.value)
    pvalue3 <- apply(varPeriodAnomBoth3, c(2,3), function(x) t.test(x[1:n.days.period],x[(n.days.period+1):length(x)])$p.value)
    pvalue4 <- apply(varPeriodAnomBoth4, c(2,3), function(x) t.test(x[1:n.days.period],x[(n.days.period+1):length(x)])$p.value)
    rm(varPeriodAnom, varPeriodAnomBoth1, varPeriodAnomBoth2, varPeriodAnomBoth3, varPeriodAnomBoth4)
    gc()

    pslPeriod <- psleuFull[days.period,,]
    pslPeriodClim <- apply(pslPeriod, c(2,3), mean, na.rm=T)
    pslPeriodClim2 <- InsertDim(pslPeriodClim, 1, n.days.period)

    pslPeriodAnom <- pslPeriod - pslPeriodClim2
    rm(pslPeriod, pslPeriodClim, pslPeriodClim2)
    gc()

    pslwr1 <- pslPeriodAnom[wr1,,]
    pslwr2 <- pslPeriodAnom[wr2,,]
    pslwr3 <- pslPeriodAnom[wr3,,]
    pslwr4 <- pslPeriodAnom[wr4,,]
    rm(pslPeriodAnom)
    gc()
    
    pslwr1mean <- apply(pslwr1,c(2,3),mean,na.rm=T)
    pslwr2mean <- apply(pslwr2,c(2,3),mean,na.rm=T)
    pslwr3mean <- apply(pslwr3,c(2,3),mean,na.rm=T)
    pslwr4mean <- apply(pslwr4,c(2,3),mean,na.rm=T)

    rm(pslwr1,pslwr2,pslwr3,pslwr4)
    
    # breaks and colors of the geopotential fields:
    #my.brks <- c(48000, seq(48501,57100,1), 60000) # % Mean anomaly of a WR
    #my.brks2 <- c(48000, seq(48500,58000,500)) # % Mean anomaly of a WR
    my.brks <- c(-300,-199:200,300) # % Mean anomaly of a WR
    my.brks2 <- c(-300,seq(-200,200,10),300) # % Mean anomaly of a WR
    #my.cols <- colorRampPalette((brewer.pal(9,"PuBu")))(length(my.brks)-1)  
    my.cols <- colorRampPalette(rev(brewer.pal(11,"RdBu")))(length(my.brks)-1)    # blue--white--red colors

    # breaks and colors of the impact maps:
    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,seq(-10,-3,1),seq(-2.8,2.8,0.2),seq(3,10,1),20) # % Mean anomaly of a WR

    #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

    #regime1.name <- "Cluster #1"
    #regime2.name <- "Cluster #2"
    #regime3.name <- "Cluster #3"
    #regime4.name <- "Cluster #4"

    # save all the data necessary to redraw the graphs when we know the right regime:
    save(workdir,rean.name,var.name,var.name.full,var.unit,year.start,year.end,period,my.period,lon,lat,my.brks,my.brks2,my.brks.var,my.cols,my.cols.var,pslwr1mean,pslwr2mean,pslwr3mean,pslwr4mean, varPeriodAnom1mean, varPeriodAnom2mean, varPeriodAnom3mean,varPeriodAnom4mean,wr1y,wr2y,wr3y,wr4y,pvalue1,pvalue2,pvalue3,pvalue4,file=paste0(workdir,"/",rean.name,"_",var.name,"_",my.period[period],"_mapdata.RData"))
    
    rm(pvalue1,pvalue2,pvalue3,pvalue4)
    rm(pslwr1mean,pslwr2mean,pslwr3mean,pslwr4mean)
    rm(varPeriodAnom1mean,varPeriodAnom2mean,varPeriodAnom3mean,varPeriodAnom4mean)
    gc()    

} # close the for loop on 'period' 

# run it twice: once, with ordering <- FALSE to look only at the cartography and find visually the right regime names of the four clusters;
# and the second time, to save the ordered cartography:
ordering <- TRUE

# choose an order to give to the cartography, from top to bottom:
orden <- c("NAO+","NAO-","Blocking","Atlantic Ridge")
    
regime1.name <- orden[1]
regime2.name <- orden[2]
regime3.name <- orden[3]
regime4.name <- orden[4]

for(period in 13:16){    
    load(file=paste0(workdir,"/",rean.name,"_",var.name,"_",my.period[period],"_mapdata.RData"))

        if(period == 13){ # Winter
            cluster3.name="NAO-"
            cluster4.name="Atlantic Ridge"
            cluster2.name="NAO+"
            cluster1.name="Blocking"
        }
        if(period == 14){
            cluster4.name="Atlantic Ridge"
            cluster2.name="NAO-"
            cluster1.name="Blocking"
            cluster3.name="NAO+"
        }
        if(period == 15){
            cluster1.name="NAO+"
            cluster4.name="NAO-"
            cluster2.name="Blocking"
            cluster3.name="Atlantic Ridge"
        }
        if(period == 16){
            cluster3.name="Atlantic Ridge"
            cluster1.name="Blocking"
            cluster4.name="NAO-"
            cluster2.name="NAO+"  
        }

    # assign to each cluster its regime:
    if(ordering == FALSE){
       cluster1 <- 1; cluster2 <- 2; cluster3 <- 3; cluster4 <- 4
    } else {
       cluster1 <- which(orden == cluster1.name)
       cluster2 <- which(orden == cluster2.name)
       cluster3 <- which(orden == cluster3.name)
       cluster4 <- which(orden == cluster4.name)
    }
    
    assign(paste0("map",cluster1), pslwr1mean)
    assign(paste0("map",cluster2), pslwr2mean)
    assign(paste0("map",cluster3), pslwr3mean)
    assign(paste0("map",cluster4), pslwr4mean)
       
    assign(paste0("imp",cluster1), varPeriodAnom1mean)
    assign(paste0("imp",cluster2), varPeriodAnom2mean)
    assign(paste0("imp",cluster3), varPeriodAnom3mean)
    assign(paste0("imp",cluster4), varPeriodAnom4mean)

    assign(paste0("sig",cluster1), pvalue1)
    assign(paste0("sig",cluster2), pvalue2)
    assign(paste0("sig",cluster3), pvalue3)
    assign(paste0("sig",cluster4), pvalue4)

    assign(paste0("fre",cluster1), wr1y)
    assign(paste0("fre",cluster2), wr2y)
    assign(paste0("fre",cluster3), wr3y)
    assign(paste0("fre",cluster4), wr4y)
       
    # Visualize the composition with the 3 graphs for all the 4 regimes: its pressure fields, its impact on var and its frequency series:
    png(filename=paste0(mapdir,"/",rean.name,"_",var.name,"_",my.period[period],".png"),width=3000,height=3700)
    
    #layout(matrix(c(1,1,1,1,2,4,6,7,rep(c(2,4,6,8),8),3,5,6,9,
    #                10,12,6,14,rep(c(10,12,6,15),8),11,13,6,16,
    #                17,19,6,21,rep(c(17,19,6,22),8),18,20,6,23,
    #                24,26,6,28,rep(c(24,26,6,29),8),25,27,6,30),41,4,byrow=TRUE),widths=c(2,2,0.2,2))
    #layout.show(30)
    layout(matrix(c(1,1,1,1,1,1,1,1,2,4,6,7,rep(c(2,4,6,8),8),3,5,6,9,31,32,6,33,
                    10,12,6,14,rep(c(10,12,6,15),8),11,13,6,16,34,35,6,36,
                    17,19,6,21,rep(c(17,19,6,22),8),18,20,6,23,37,38,6,39,
                    24,26,6,28,rep(c(24,26,6,29),8),25,27,6,30),45,4,byrow=TRUE),widths=c(2,2,0.2,2), heights=c(rep(0.2,12),0.2,rep(0.2,10),0.2,rep(0.2,10),0.2,rep(0.2,11)))
           
    layout.show(39)
    
    plot(0,0, axes=F, xlab="", ylab="")
    title(paste("Weather Regimes for",my.period[period],"season (1979-2013). Source:",rean.name), cex.main=9, line=-2)
    EU <- c(1:54,130:161) # position of long values of Europe only (without the Atlantic Sea and America)

    #title1 <- paste(regime1.name, "geopotential height")
    title(paste(regime1.name, "geopotential height"), cex.main=2)
    PlotEquiMap(map1[,EU], lon[EU], lat, filled.continents = FALSE, brks=my.brks, cols=my.cols, toptitle="", sizetit=1.2, contours=t(map1[,EU]),brks2=my.brks2, drawleg=F)


    ColorBar(my.brks, cols=my.cols, vert=FALSE, subsampleg=100, cex=1.5)
    mtext(side=4," m", cex=2)
    title1 <- paste(regime1.name, "impact on", var.name.full)
    PlotEquiMap(imp1[,EU], lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, toptitle=title1, sizetit=1.2, drawleg=F, dots=t(sig1[,EU] < 0.05))
    ColorBar2(my.brks.var, cols=my.cols.var, vert=FALSE, subsampleg=10, cex=1.5, my.ticks=c(0,10,20,30,40,50,60,70,80), my.labels=c(-20,-10,-6,-3,0,3,6,10,20))
    mtext(side=4,paste0(" ",var.unit), cex=2)
    plot(0, type="n", axes=F, xlab="", ylab="")
    plot(0, type="n", axes=F, xlab="", ylab="")  # plot 2 empty graphs
    title(paste0(regime1.name, " Frequency (", round(100*mean(fre1),1), "%)"), cex.main=2, line=-1)
    barplot.freq(100*fre1, year.start, year.end, cex.y=1.5, cex.x=1.5, freq.max=60)    
    plot(0, type="n", axes=F,  xlab="", ylab="")
    title("Year", cex.main=1.5, line=-1)
    
    title2 <- paste(regime2.name, "geopotential height")
    PlotEquiMap(map2[,EU], lon[EU], lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, toptitle=title2, sizetit=1.2, contours=t(map2[,EU]), brks2=my.brks2, drawleg=F)
    ColorBar(my.brks, cols=my.cols, vert=FALSE, subsampleg=100, cex=1.5)
    mtext(side=4," m", cex=2)
    title2 <- paste(regime2.name, "impact on", var.name.full)
    PlotEquiMap(imp2[,EU], lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, toptitle=title2, sizetit=1.2, drawleg=F, dots=t(sig2[,EU] < 0.05))
    ColorBar2(my.brks.var, cols=my.cols.var, vert=FALSE, subsampleg=10, cex=1.5, my.ticks=c(0,10,20,30,40,50,60,70,80), my.labels=c(-20,-10,-6,-3,0,3,6,10,20))
    mtext(side=4,paste0(" ",var.unit), cex=2)
    plot(0, type="n", axes=F, xlab="", ylab="")
    title(paste0(regime2.name, " Frequency (", round(100*mean(fre2),1), "%)"), cex.main=2, line=-1)
    barplot.freq(100*fre2, year.start, year.end, cex.y=1.5, cex.x=1.5, freq.max=60)    
    plot(0, type="n", axes=F,  xlab="", ylab="")
    title("Year", cex.main=1.5, line=-1)
    
    title3 <- paste(regime3.name, "geopotential height")
    PlotEquiMap(map3[,EU], lon[EU], lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, toptitle=title3, sizetit=1.2, contours=t(map3[,EU]), brks2=my.brks2, drawleg=F)
    ColorBar(my.brks, cols=my.cols, vert=FALSE, subsampleg=100, cex=1.5)
    mtext(side=4," m", cex=2)
    title3 <- paste(regime3.name, "impact on", var.name.full)
    PlotEquiMap(imp3[,EU], lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, toptitle=title3, sizetit=1.2, drawleg=F, dots=t(sig3[,EU] < 0.05))
    ColorBar2(my.brks.var, cols=my.cols.var, vert=FALSE, subsampleg=10, cex=1.5, my.ticks=c(0,10,20,30,40,50,60,70,80), my.labels=c(-20,-10,-6,-3,0,3,6,10,20))
    mtext(side=4,paste0(" ",var.unit), cex=2)
    plot(0, type="n", axes=F, xlab="", ylab="")
    title(paste0(regime3.name, " Frequency (", round(100*mean(fre3),1), "%)"), cex.main=2, line=-1)
    barplot.freq(100*fre3, year.start, year.end, cex.y=1.5, cex.x=1.5, freq.max=60)
    plot(0, type="n", axes=F,  xlab="", ylab="")
    title("Year", cex.main=1.5, line=-1)
    
    title4 <- paste(regime4.name, "geopotential height")
    PlotEquiMap(map4[,EU], lon[EU], lat, filled.continents=FALSE, brks=my.brks, cols=my.cols, toptitle=title4, sizetit=1.2, contours=t(map4[,EU]), brks2=my.brks2, drawleg=F)
    ColorBar(my.brks, cols=my.cols, vert=FALSE, subsampleg=100, cex=1.5)
    mtext(side=4," m", cex=2)       
    title4 <- paste(regime4.name, "impact on", var.name.full)
    PlotEquiMap(imp4[,EU], lon[EU], lat, filled.continents = FALSE, brks=my.brks.var, cols=my.cols.var, toptitle=title4, sizetit=1.2, drawleg=F, dots=t(sig4[,EU] < 0.05))
    ColorBar2(my.brks.var, cols=my.cols.var, vert=FALSE, subsampleg=10, cex=1.5, my.ticks=c(0,10,20,30,40,50,60,70,80), my.labels=c(-20,-10,-6,-3,0,3,6,10,20))
    mtext(side=4,paste0(" ",var.unit), cex=2)
    plot(0, type="n", axes=F, xlab="", ylab="")
    title(paste0(regime4.name, " Frequency (", round(100*mean(fre4),1), "%)"), cex.main=2, line=-1)
    barplot.freq(100*fre4, year.start, year.end, cex.y=1.5, cex.x=1.5, freq.max=60)
    plot(0, type="n", axes=F,  xlab="", ylab="")
    title("Year", cex.main=1.5, line=-1)
    dev.off()
               
} # close for loop on 'period'


