rm(list=ls())

library(s2dverification)
library(multiApply)
library(abind)
library(future) # for availableCores() function
source('/esarchive/scratch/ncortesi/scripts/Rfunctions.R')
source('/esarchive/scratch/ncortesi/scripts/Vero/VeroRegimes.R')

#######################################################################################################
##                                                                                                   ##
##   Validation of WR forecasts at subseasonal scale with the assign method (final methodology)      ##
##                                                                                                   ##
#######################################################################################################

## folder with the weekly clustering (one .RData for each week of the year):
work.dir <- "/esarchive/scratch/ncortesi/Regimes/90_WRs_winter_and_summer"

yr1.obs <- 1979 # first year of psl data from the reanalysis
yr2.obs <- 2017 # last year of psl data from the reanalysis 
    
forecast.year <- 2018 # 014             # starting year of the weekly sequence of the forecasts
mes <- 1                                 # starting forecast month (1: january, 2: february, etc.)
day <- 4 #2 #7                              # starting forecast day (usually the first Thursday of January of the forecast year,
                                         # which in case of forecasts of 2014 is the 2 of January and in case of the forecasts of 2016 is the 7 of January)
                                         # (for 2014 forecasts, also Tuesday forecasts were downloaded. They start on the 7th of January)
yr1.hind <- 1998                         # first hindcast year
yr2.hind <- 2016 ##2017                         # last hindcast year (usually the forecast year -1)
n.members <- 11                          # number of ensemble members

## daily data:
forecast.dir <- paste0('/esarchive/exp/ecmwf/monthly_ensforhc/daily_mean/$VAR_NAME$_f6h/', forecast.year,'$MONTH$$DAY$00/$VAR_NAME$_$START_DATE$.nc') ## forecast year: 2014

## 6-hourly data (not used unless you have no daily means data available on /esarchive and you have to average them by yourself):
##forecast.dir <- paste0('/esarchive/exp/ecmwf/monthly_ensforhc/6hourly/$VAR_NAME$/', forecast.year,'$MONTH$$DAY$/$VAR_NAME$_$START_DATE$.nc')  ## forecast year: 2016
##forecast.dir <- paste0('/esarchive/exp/ecmwf/monthly_ensforhc/6hourly/$VAR_NAME$/', forecast.year,'$MONTH$$DAY$00/$VAR_NAME$_$START_DATE$00.nc') ## forecast year: 2014
##forecast.dir.interp <- paste0('/esarchive/scratch/ncortesi/Subestacional/hindcasts_data/6hourly/$VAR_NAME$/$VAR_NAME$_$START_DATE$00.nc') 
##forecast.dir.interp <- paste0('/gpfs/projects/bsc32/bsc32842/6hourly/$VAR_NAME$/$VAR_NAME$_$START_DATE$00.nc') ## forecast year: 2014

n.clusters <- 4

## if TRUE, reorder the WR numbers to use the same order of the observed weekly classification created by us:
reordering <- TRUE

##################################################################################################################################
## Load Subseasonal hindcast data for one start date and lead time (1 week), interpolate it to the same grid of the reanalysis 
## and finally load SLP monthly reanalysis data to assign to each day an observed WR

n.yrs.hind <- yr2.hind - yr1.hind + 1
n.yrs.obs <- yr2.obs - yr1.obs + 1
season.name <- c("winter", "summer")
n.seasons <- length(season.name)

sdates.seq <- weekly.seq(forecast.year, mes, day)  # load the sequence of dates corresponding to all the thursday of the year (for forecast data)
pos.bis <- which(sdates.seq == paste0(forecast.year, "0229")) # find if there is a startdate at the 29th of february and return its position in the vector sdates.seq
if(length(pos.bis) != 0) sdates.seq <- sdates.seq[-pos.bis] # if there is a start date that starts exactly on the 29th of February, remove it
n.sdates <- length(sdates.seq) ## number of start dates

## chunk.num is a number from 1 to the number of start dates sdates (52)
if(length(commandArgs(TRUE)) != 0) chunk.num <- as.integer(commandArgs(TRUE)) else stop("You need to provide an external parameter with the chunk number")

## this script works using start dates as chunks:
sdate <- chunk.num

print("Reading lat and lon") ## same as ERA-Interim:
data <- Load('psl', exp = list(list(path="/esarchive/recon/ecmwf/erainterim/daily_mean/psl_f6h/psl_$YEAR$$MONTH$.nc")), obs = NULL,
             sdates = paste0(yr1.hind, substr(sdates.seq[1],5,6), substr(sdates.seq[1],7,8)),
             storefreq='daily',
             leadtimemin = 1, leadtimemax = 1, 
             latmin = 27, latmax = 81,
             lonmin = 274.5, lonmax = 45, output = 'lonlat', nprocs=1)

lat <- data$lat
lon <- data$lon
n.lat <-  length(lat)
n.lon <-  length(lon)

## loading and preformatting of the chunk (6hourly to daily + anomalies with loess filter). Do it only once, then set preformatting = FALSE:
## You can also run the preformatting on "WRs_subseasonal_chunks.R", it generates exactly the same outputs, and it is updated to include daily data too (instead of 6-hourly only)
preformatting <- FALSE
## if there is no .RData available in gpfs, load 6hourly data once from esarchive and convert them to daily anomalies:
if(preformatting){
    sdates <- paste0(yr1.hind:yr2.hind, substr(sdates.seq[sdate],5,6), substr(sdates.seq[sdate],7,8)) ## same start date as 'sdate' but for all hindcast years
    
    ## load forecasts for all the years of start date 'sdate' and ALL FORECAST DAYS associated to it (32) already interpolated to the same resolution of the reanalysis:
    ## note that the first forecast day is not equal to startdate+1 but to startdate!!!    
    system.time(data <- Load('psl', exp = list(list(path=forecast.dir.interp)),
                             obs = NULL,
                             sdates = sdates,
                             storefreq='daily',
                             leadtimemin = 1, leadtimemax = 129, ## replace 129 with 32 when you'll load daily data directly
                             latmin = 27, latmax = 81,
                             lonmin = 274.5, lonmax = 45, output = 'lonlat', nprocs=1)) ##max(1, availableCores()))) ## it's faster with more cores, even without interpolation
    

    ## In case you are loading 6-hourly data, you have to convert them to daily means before:
    data.daily <- array(NA, c(1, n.members, n.yrs.hind, 32, n.lat, n.lon)) ## [dataset x members x sdate x time x lat x lon]
    my.names <- names(dim(data$mod))

    ## above loop is twice faster with .Internal(mean) used below, but there must be no NA values:
    for(dd in 1:32) { print(paste0("Day ", dd, " of 32")); data.daily[1, 1:n.members, 1:n.yrs.hind, dd, 1:n.lat, 1:n.lon] <-  apply(data$mod, c(1,2,3,5,6), function(x){.Internal(mean(x[(dd-1)*4 + 1:4]))})}

    data$mod <- data.daily
    names(dim(data$mod)) <- my.names ## re-assign the dimension names because they were deleted by the previous command

    rm(data.daily)

    ## remove the eventual forecasts of the 29th of February: (they are present for the forecasts of 2016, but not of 2014):
    ## if(leap.year(forecast.year)) data.daily <- data.daily[1,,,-,,] ##sistema!!!

    ## measure forecast climatology for the 32 days loaded:
    clim <- Clim(var_exp = data$mod, var_obs = data$mod, memb=F)

    ## apply the LOESS filter to the 32-days climatology:
    clim_smoothed_for <- aperm(apply(clim$clim_exp, c(1,3,4), Loess, loess_span=1), c(2,1,3,4))

    ## measure forecast anomalies:
    anom_for <- Ano(data$mod, clim_smoothed_for)

    ## convert forecast anomalies from Pascal to hPa;
    anom_for <- anom_for / 100
    
    ## save SLP anomalies in the gpfs. Subsequent calls will read this file instead of loading data fron esarchive or gpfs:
    save(anom_for, file=paste0("/gpfs/projects/bsc32/bsc32842/daily/forecasts_",forecast.year,"/anom_for_chunk_", sdate, ".RData"))

} ## close if on preformatting


## after preformatting is complete and one .RData file with the SLP hindcast anomalies 'anom_for' for each chunk has been saved in gpfs/project/..., you can run the validation of the chunk data:
validation <- TRUE
if(validation){
    print("Validating chunk. Please wait...")
          
    ## reshape the daily sequence of observed WRs in an array [years, days]:
    ## remember that for sea=1 season is winter so the time series of WRs goes from Jan to Mar and from Oct do Dec for each year,
    ## while sea=2 is summer and the WR time series goes from Apr to Sep for each year
    pos.last.day.march <- 90 ## (31 + 28 + 31)
    pos.first.day.october <- 274 ## (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 1)

    ## WR time series for all days of the year starting from the 1st of January and for all observed years:
    WR.seq.obs.array.year <- array(NA, c(n.yrs.obs, 365*2)) 
    for(seas in 1:2){
        if(seas == 1) dd <- c(1:pos.last.day.march, pos.first.day.october:365) else dd <- (pos.last.day.march + 1):(pos.first.day.october - 1)
        load(paste0(work.dir, "/", season.name[seas],"_patterns/observed_regimes_", season.name[seas],".RData"))
        n.days.season <- length(WR_obs$cluster) / n.yrs.obs

        ##if(seas == 1) mod <- 0 else mod <- 4
        WR.seq.obs.array <- t(array(WR_obs$cluster, c(n.days.season, n.yrs.obs)))
        WR.seq.obs.array.year[,dd] <- WR.seq.obs.array
        rm(WR_obs, WR.seq.obs.array)
    }

    ## add the WR time series of the following year in case the forecast days enter in the following year:
    WR.temp <- WR.seq.obs.array.year[-1,1:365]
    WR.seq.obs.array.year[1:(n.yrs.obs-1), 366:(365*2)] <- WR.temp
    
    ## select only the reanalysis years within the hindcasts years to be able to compare the observed WRs with the forecasted ones:
    yr1 <- yr1.hind - yr1.obs + 1
    yr2 <- yr2.hind - yr1.obs + 1
    WR.seq.obs.array.year <- WR.seq.obs.array.year[yr1:yr2,] ## load one year more to be able to assign obs WRs for forecast days beyond the 31th of December (see below)

    print("Loading forecasts. Please wait...")
    
    ## load forecast anomalies:
    load(paste0("/gpfs/projects/bsc32/bsc32842/daily/forecasts_", forecast.year,"/anom_for_chunk_", sdate, ".RData"))

    ## in case of the forecasts of 2018 we hae to remove tha SLP hindcast data for year 2017 which was nor removed in the .RData:
    if(forecast.year == 2018) anom_for <- anom_for[,,1:19,,,, drop=FALSE]
    
    ## For each forecast day of the start date 'sdate', identify to which week of the year (starting from the 1st of January) it belongs:

    ## position of the 32 forecast days on the SAME MONTH of the hindcast start date
    forecast.daily.seq <- as.numeric(substr(sdates.seq[sdate],7,8)) + 1:32 - 1 

    ## month of the currently selected start date:
    month.sdate <- as.numeric(substr(sdates.seq[sdate],5,6))

    ## get the total number of days BEFORE the month of the start date (for a no leap year):
    if(month.sdate > 1) n.days.previous <- n.days.in.a.monthly.period(1, month.sdate - 1, 2001) else n.days.previous <- 0

    ## get the sequence of forecast days counting from the START of the YEAR instead than from the start of the month of the start date:
    days.seq <- n.days.previous + forecast.daily.seq

    ## get the sequence of monthly numbers corresponding to the sequence of forecast days:
    monthly.seq <- seq.months.in.a.year(2001)[days.seq]

    ## get the sequence of seasonal numbers corresponding to the sequence of forecast days:
    season.seq <- seq.seasons.in.a.year(2001)[days.seq]
    
    ## if the forecast days extend to the following year, assign them to January:
    if(length(which(is.na(monthly.seq))) > 0) monthly.seq[which(is.na(monthly.seq))] <- 1 

    ## sequence of weekly numbers STARTING FROM THE FIRST Of JANUARY, each one corresponding to one of the 32 forecast days:
    ##weekly.seq <- seq.weeks.in.a.year(2001)[days.seq]

    ##The last week of the year is the number 53 and it is made up by 1 day ony, so it isn't used in the WR classification and it is included in week 52:
    ##weekly.seq[which(weekly.seq == 53)] <- 52

    ## if the forecast days extend to the following year, assign them to winter season
    if(length(which(is.na(season.seq))) > 0) season.seq[which(is.na(season.seq))] <- 1

    ## check all the sequences (for the debug):
    ## cbind(forecast.days=1:length(days.seq), days.seq, monthly.seq, season.seq) 

    ## identify which month(s) to load:
    ## months2load <- unique(monthly.seq)

    ## identify which week(s) to load:
    seasons2load <- unique(season.seq)
    
    ## array which will store the cluster sequence of the observed WRs for all the 32 forecast days of the selected chunk (start date):
    WR.seq.rean <- array(NA, c(n.yrs.hind, 32)) 

    ## array which will store the cluster sequence of the predicted WRs for all the 32 forecast days of the selected chunk (start date):
    WR.seq.for <- array(NA, c(1, n.members, n.yrs.hind, 32))

    ## vector which will store the % of coincident WRs for all the 32 forecast days of the selected chunk (start date):
    WR.seq.perc <- array(0, 32)

    ## array which for each WR will store the spatial correlation value of between the observed and predicted WR patterns, for each forecast day
    WR.sp.cor <- array(NA, c(n.clusters, 32))

    ## array which for each WR will store the spatial correlation value of between the observed and predicted WR patterns, for each forecast week (5-11 days, etc)
    WR.sp.cor.week <- array(NA, c(n.clusters, 4))
    
    print("Loading seasonal WR classification. Please wait...")
    for(sea in seasons2load){
        ##print(paste0("Season number ", sea))

        ## extract only forecast anomalies of days belonging to that season:
        pos.sea <- which(season.seq == sea) ## pos.season is the position of the numbers of the forecast days which belongs to the selected season 'sea'
        anom_for_same_season <- anom_for[,,, pos.sea,,,drop=FALSE] ## 'anom_for' are the forecasted anomalies for the 32 forecast days 
        
        ## load 'WR_obs' variable with the cluster data of the same extended season of the forecast days considered:
        ## for sea = 1 is winter and goes from Jan to Mar and from Oct do Dec; for sea = 2 is summer and goes from Apr to Sep
        ## WR_obs$composite is a 3D array with the spatial patterns of the 4 WRs of the season
        ## WR_obs$cluster is a vector with obs daily sequence for all years of the loaded period
        load(paste0(work.dir, "/", season.name[sea], "_patterns/observed_regimes_", season.name[sea],".RData"))
        
        ## measure distance between the SLP anomalies of each forecast day and those of the obs. WRs of the reference month(s), and assign to each forecast day the closer WR:
        names(dim(WR_obs$composite)) <- c("lat", "lon", "nclust") ## add names to the dimensions as they are needed by RegimeAssign() function below

        ## Unfortunately function RegimesAssign() doesn't work in chunks because it depends on Apply() which automatically tries to employ all cores of the Power9 node.
        ## For this reason, I had to replace Apply() with apply() below:
        ## WR.seq.for[,,, pos.week] <- RegimesAssign(var_ano = anom_for_same_week, ref_maps = WR_obs$composite, lats = data$lat, method = 'distance')$cluster
        posdim <- which(names(dim(WR_obs$composite)) == 'nclust')
        poslat <- which(names(dim(WR_obs$composite)) == 'lat')
        poslon <- which(names(dim(WR_obs$composite)) == 'lon')
        poslat_ano <- which(names(dim(anom_for_same_season)) == 'lat')
        poslon_ano <- which(names(dim(anom_for_same_season)) == 'lon')
        nclust <- dim(WR_obs$composite)[posdim]
        nlat <- dim(WR_obs$composite)[poslat]
        nlon <- dim(WR_obs$composite)[poslon]
        
        ## WR.seq.for[,,, pos.week] <- apply(X = anom_for_same_week, MARGIN = c((1:length(dim(anom_for_same_week)))[-c(poslat_ano, poslon_ano)]),
        ##                                  FUN = 'anom2regime', ref = WR_obs$composite, lat = data$lat, method = 'distance')        
        latWeights <- InsertDim(sqrt(cos(data$lat * pi / 180)), 2, dim(WR_obs$composite)[poslon]) ## rm sqrt?

        assign.method <- "RMSE" #"cor" ## the two methods differ for less than 2% of the WRs assigned
        if(assign.method == "RMSE"){
            ## measure RMSE between observed and forecasted WR anomalies an take the lowest values:
            temp1 <- apply(X = anom_for_same_season, MARGIN = 1:4, FUN = function(x){mean((as.vector(x * latWeights) - as.vector(WR_obs$composite[,,1] * latWeights))^2)})
            temp2 <- apply(X = anom_for_same_season, MARGIN = 1:4, FUN = function(x){mean((as.vector(x * latWeights) - as.vector(WR_obs$composite[,,2] * latWeights))^2)})
            temp3 <- apply(X = anom_for_same_season, MARGIN = 1:4, FUN = function(x){mean((as.vector(x * latWeights) - as.vector(WR_obs$composite[,,3] * latWeights))^2)})
            temp4 <- apply(X = anom_for_same_season, MARGIN = 1:4, FUN = function(x){mean((as.vector(x * latWeights) - as.vector(WR_obs$composite[,,4] * latWeights))^2)})
            temp <- abind(temp1, temp2, temp3, temp4, along = 5)
            WR.seq.for[1,,, pos.sea] <- apply(temp, 1:4, function(x){ which(x == min(x)) })         
            rm(temp, temp1, temp2, temp3, temp4)
        } else {
            ## Alterantively, measure the spatial correlation instead of the RMSE and take the highest values:
            temp1 <- apply(X = anom_for_same_week, MARGIN = 1:4, FUN = function(x){cor(as.vector(x * latWeights), as.vector(WR_obs$composite[,,1] * latWeights))})
            temp2 <- apply(X = anom_for_same_week, MARGIN = 1:4, FUN = function(x){cor(as.vector(x * latWeights), as.vector(WR_obs$composite[,,2] * latWeights))})
            temp3 <- apply(X = anom_for_same_week, MARGIN = 1:4, FUN = function(x){cor(as.vector(x * latWeights), as.vector(WR_obs$composite[,,3] * latWeights))})
            temp4 <- apply(X = anom_for_same_week, MARGIN = 1:4, FUN = function(x){cor(as.vector(x * latWeights), as.vector(WR_obs$composite[,,4] * latWeights))})

            temp <- abind(temp1, temp2, temp3, temp4, along = 5)
            WR.seq.for[1,,, pos.sea] <- apply(temp, 1:4, function(x){ which(x == max(x)) })         
            rm(temp, temp1, temp2, temp3, temp4)
        }
        
        ## add 4 to the WR number in case of summer:
        ##if(sea == 1) mod <- 0 else mod <- 4
        ## WR.seq.for[1,,, pos.sea] <- WR.seq.for[1,,, pos.sea] ## + mod
        
        ## ## check observed seasonal regime patterns (for the debug):
        ## PlotEquiMap(WR_obs$composite[,,1], lon, lat, filled.continents=FALSE, brks=seq(-16,16,2))
        ## PlotEquiMap(WR_obs$composite[,,2], lon, lat, filled.continents=FALSE, brks=seq(-16,16,2))
        ## PlotEquiMap(WR_obs$composite[,,3], lon, lat, filled.continents=FALSE, brks=seq(-16,16,2))
        ## PlotEquiMap(WR_obs$composite[,,4], lon, lat, filled.continents=FALSE, brks=seq(-16,16,2))
               
        ## Select only the part of WR.seq.obs.array inside the forecast days of the selected season.
        ## Note that WR.seq.rean[,X] indicates the observed WRs corresponding to the forecast days, while WR.seq.obs.array indicates the observed WRs
        ## corresponding to the calendar days, so you have to convert the second to the first with the command below:             
        WR.seq.rean <- WR.seq.obs.array.year[1:n.yrs.hind, days.seq] 
        
        ## for the debug:
        ##cbind(forecast.days=1:length(days.seq), days.seq, monthly.seq, season.seq, WR.obs.yr1=WR.seq.rean[1,],
        ##      WR.for.yr1.mbr1=WR.seq.for[1,1,1,], WR.for.yr1.mbr2=WR.seq.for[1,2,1,], WR.for.yr1.mbr3=WR.seq.for[1,3,1,], WR.for.yr1.mbr4=WR.seq.for[1,4,1,])

        ## compare the forecasted WR sequence with the observed one:
        for(d in pos.sea){
            for(y in 1:n.yrs.hind){
                WR.seq.perc[d] <- WR.seq.perc[d] + length(which(WR.seq.for[1,,y,d] == WR.seq.rean[y,d]))
            }
        }
        
        ## for each WR and forecasted day, measure the spatial correlations between predicted and observed WR patterns:
        for(wr in 1:n.clusters) {
            for(d in pos.sea){
                ## hindcast years and members with the same assigned WR:
                n.dd <- length(which(WR.seq.for[1,,,d] == wr))

                if(n.dd > 0){
                    ## print(WR.seq.for[1,,,d])
                    same.wr <- which(WR.seq.for[1,,,d] == wr, arr.ind = TRUE)
                    
                    anom_for_same_day_and_wr <- 0
                    for(dd in 1:n.dd) {
                        anom_for_same_day_and_wr <- anom_for_same_day_and_wr + anom_for_same_season[1,same.wr[dd, 1], same.wr[dd, 2], which(pos.sea == d),,]
                        ## PlotEquiMap(anom_for_same_week[same.wr[dd, 1], same.wr[dd, 2], i,,], lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Hindcasted")
                    }

                    ## ensemble mean of SLP hindcast anomalies during days and members of predicted regime wr
                    anom_for_same_day_and_wr <- anom_for_same_day_and_wr / n.dd 
                    
                    WR.sp.cor[wr, d] <- cor(as.vector(WR_obs$composite[,, wr]), as.vector(anom_for_same_day_and_wr))

                    ## ## for debug:
                    ## my.brks <- c(-100,seq(-21,-1,2),0,seq(1,21,2),100) ## 23 intervals
                    ## PlotEquiMap(WR_obs$composite[,, wr], lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Observed")
                    ## x11()
                    ## PlotEquiMap(anom_for_same_day_and_wr, lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Hindcasted")

                } else {
                    WR.sp.cor[wr, d] <- NA
                }
            }

        } ## close for on wr
        
        ## for each WR and forecasted WEEK, measure the spatial correlations between predicted and observed WR patterns:
        week.days <- list(5:11, 12:18, 19:25, 26:32)
        for(wr in 1:n.clusters) {
            for(week.pos in 1:4){        
                ## number of hindcast years and members with the same assigned WR:
                n.dd <- length(which(WR.seq.for[1,,, week.days[[week.pos]]] == wr))
                
                if(n.dd > 0){
                    ## print(WR.seq.for[1,,, week.days[[week.pos]]])
                    same.wr <- which(WR.seq.for[1,,, week.days[[week.pos]]] == wr, arr.ind = TRUE)
                    
                    anom_for_same_week_and_wr <- 0
                    for(dd in 1:n.dd) {
                        anom_for_same_week_and_wr <- anom_for_same_week_and_wr + anom_for_same_season[1, same.wr[dd, 1], same.wr[dd, 2], week.days[[week.pos]][same.wr[dd, 3]],,]
                        ## PlotEquiMap(anom_for_same_week[same.wr[dd, 1], same.wr[dd, 2], i,,], lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Hindcasted")   
                    }
                    
                    ## ensemble mean of SLP hindcast anomalies during days and members of predicted regime wr
                    anom_for_same_week_and_wr <- anom_for_same_week_and_wr / n.dd 
                    
                    WR.sp.cor.week[wr, week.pos] <- cor(as.vector(WR_obs$composite[,, wr]), as.vector(anom_for_same_week_and_wr))
                    
                    ## ## for debug:
                    ## my.brks <- c(-100,seq(-21,-1,2),0,seq(1,21,2),100) ## 23 intervals
                    ## PlotEquiMap(WR_obs$composite[,, wr], lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Observed")
                    ## x11()
                    ## PlotEquiMap(anom_for_same_day_and_wr, lon=data$lon, lat=data$lat, filled.continents=FALSE, brks=my.brks, toptitle="Hindcasted")
                    
                } else {
                    WR.sp.cor.week[wr, week.pos] <- NA
                }
            }
            
        } ## close for on wr
            
    } ## close for on sea
    
    ## for the debug:
    cbind(forecast.days=1:length(days.seq), days.seq, monthly.seq, season.seq, n.coinc.days=WR.seq.perc, perc.coinc=round(WR.seq.perc/(n.yrs.hind * n.members),2), WR.obs.yr1=WR.seq.rean[1,], WR.for.yr1.mbr1=WR.seq.for[1,1,1,], WR.for.yr1.mbr2=WR.seq.for[1,2,1,], WR.for.yr1.mbr3=WR.seq.for[1,3,1,], WR.for.yr1.mbr4=WR.seq.for[1,4,1,])

    ## convert it to a %:
    WR.seq.perc <- 100 * WR.seq.perc / (n.yrs.hind * n.members)

    print("Saving results")
    save(sdate, WR.seq.perc, WR.seq.for, WR.seq.rean, WR.sp.cor, WR.sp.cor.week,  yr1, yr2, n.yrs.hind, n.members, work.dir, file=paste0(work.dir, "/forecasts_", forecast.year,"/Assign_table_startdate_",sdate,".Rdata"))

} ## close if on validation

##
## Plotting
##

## activate it only once the validation is complete and all 'Assign_table_startdate_X.Rdata' files have been saved:
plotting <- FALSE 
if(plotting){
    ## Load the .Rdata with the results for each chunk:

    ## load the first startdate just to get the dimensions of the arrays to load:
    ## (in the .RData there are the following variables: sdate, WR.seq.perc, WR.seq.for, WR.seq.rean, yr1, yr2, n.yrs.hind, n.members, work.dir)
    load(paste0(work.dir, "/forecasts_", forecast.year,"/Assign_table_startdate_1.Rdata"))

    ## arrays where we want to store the full results of all chunks:
    WR.seq.perc.full <- array(NA, c(n.sdates, dim(WR.seq.perc)))
    WR.seq.rean.full <- array(NA, c(n.sdates, dim(WR.seq.rean)))
    WR.seq.for.full <- array(NA, c(n.sdates, dim(WR.seq.for)))
    WR.sp.cor.full <- array(NA, c(n.sdates, dim(WR.sp.cor)))
    if(exists("WR.sp.cor.week")) WR.sp.cor.week.full <- array(NA, c(n.sdates, dim(WR.sp.cor.week)))

    for(sdate in 1:n.sdates){
        load(paste0(work.dir, "/forecasts_", forecast.year,"/Assign_table_startdate_", sdate,".Rdata"))
        
        WR.seq.perc.full[sdate,] <- WR.seq.perc
        WR.seq.rean.full[sdate,,] <- WR.seq.rean
        WR.seq.for.full[sdate,,,,] <- WR.seq.for
        WR.sp.cor.full[sdate,,] <- WR.sp.cor
        WR.sp.cor.week.full[sdate,,] <- WR.sp.cor.week
    }

    ## measure WR frequencies:

    ## for each WR and forecast WEEK, measure the frequencies of occurrence of predicted and observed WR patterns:
    days.we <- list(5:11, 12:18, 19:25, 26:32)  ## define lead weeks
    n.for.weeks <- length(days.we) ## number of forecast weeks
    
    ## frequency of each observed WR (in %) for each year:
    fr.wr.obs <- array(NA, c(n.clusters, n.sdates, n.yrs.hind, n.for.weeks))

    ## frequency of each forecasted WR (in %) for each year:
    fr.wr.for <- array(NA, c(n.clusters, n.sdates, n.yrs.hind, n.for.weeks))

    ## frequency of each forecasted WR (in %) for each year and member:
    fr.wr.for.memb <- array(NA, c(n.clusters, n.sdates, n.yrs.hind, n.for.weeks, n.members))

    ## frequency of each forecasted WR (in %) for each year and concatenated fo each member:
    fr.wr.for.memb.conc <- array(NA, c(n.clusters, n.sdates, n.yrs.hind * n.members, n.for.weeks))

    for(wr in 1:n.clusters) {
        for(sd in 1:n.sdates){
            for(yr in 1:n.yrs.hind){
                for(we in 1:n.for.weeks){
                    fr.wr.obs[wr, sd, yr, we] <- length(which(WR.seq.rean.full[sd, yr, days.we[[we]]] == wr)) / length(days.we[[we]]) 
                    fr.wr.for[wr, sd, yr, we] <- length(which(WR.seq.for.full[sd, 1,, yr, days.we[[we]]] == wr)) / ( n.members * length(days.we[[we]]) )
                    for(memb in 1:n.members) fr.wr.for.memb[wr, sd, yr, we, memb] <- length(which(WR.seq.for.full[sd, 1, memb, yr, days.we[[we]]] == wr)) / ( n.members * length(days.we[[we]]) )
                }
            }
        }
    }


    ## measure temporary correlations between frequency series:
    WR.temp.cor <- array(NA, c(n.clusters, n.sdates, n.for.weeks))
    WR.temp.cor.conc <- array(NA, c(n.clusters, n.sdates, n.for.weeks)) ## measure temp correlations between frequency series by concatenating the time series of each member in a single one
    
    for(wr in 1:n.clusters) {
        for(sd in 1:n.sdates){
            for(we in 1:n.for.weeks){
                WR.temp.cor[wr, sd, we] <- cor(fr.wr.obs[wr, sd,, we], fr.wr.for[wr, sd,, we])
                
                fr.wr.for.memb.conc.temp <- c()
                for(memb in 1:n.members) fr.wr.for.memb.conc.temp <- c(fr.wr.for.memb.conc.temp, fr.wr.for.memb[wr, sd,, we, memb])
                fr.wr.for.memb.conc[wr, sd,, we] <- fr.wr.for.memb.conc.temp
                WR.temp.cor.conc[wr, sd, we] <- cor(rep(fr.wr.obs[wr, sd,, we], n.members), fr.wr.for.memb.conc[wr, sd,, we])
            }
        }
    }

    
    ## measure frequency bias:
    WR.fr.bias <- array(NA, c(n.clusters, n.sdates, n.for.weeks))

    for(wr in 1:n.clusters) {
        for(sd in 1:n.sdates){
            for(we in 1:length(days.we)){
                WR.fr.bias[wr, sd, we] <- mean(fr.wr.for[wr, sd,, we]) - mean(fr.wr.obs[wr, sd,, we])
            }
        }
    }

    ## measure weekly spatial correlations:
    WR.weekly.sp.cor <-  array(NA, c(n.clusters, n.sdates, n.for.weeks))
    for(wr in 1:n.clusters) WR.weekly.sp.cor[wr,,1] <- rowMeans(WR.sp.cor.full[,wr, days.we[[1]]])
    for(wr in 1:n.clusters) WR.weekly.sp.cor[wr,,2] <- rowMeans(WR.sp.cor.full[,wr, days.we[[2]]])
    for(wr in 1:n.clusters) WR.weekly.sp.cor[wr,,3] <- rowMeans(WR.sp.cor.full[,wr, days.we[[3]]])
    for(wr in 1:n.clusters) WR.weekly.sp.cor[wr,,4] <- rowMeans(WR.sp.cor.full[,wr, days.we[[4]]])

    ## measure all indicators over all start dates of the same season:
    ##
    ## note that for forecasts of 2014, the start dates begins from the 2 of January, so winter start dates (from Oct to March) are numbered from 1 to 13 and from 40 to 52,
    ## while summer start dates (from apr to sep) are numbered from 14 to 39.
    winter.sdates <- c(1:13, 40:52)
    summer.sdates <- c(14:39)
        
    ## measure temporary correlations between frequency series all winter or summer start dates together:
    WR.temp.cor.season <- WR.temp.cor.conc.season <- array(NA, c(n.clusters, 2, n.for.weeks))

    mean.fr.winter.obs <- mean.fr.summer.obs <- mean.fr.winter.for <- mean.fr.summer.for <- array(0, c(n.clusters, n.yrs.hind, n.for.weeks))
    mean.fr.winter.obs.conc <- mean.fr.summer.obs.conc <- mean.fr.winter.for.conc <- mean.fr.summer.for.conc <- array(0, c(n.clusters, n.yrs.hind * n.members, n.for.weeks))

    for(wr in 1:n.clusters) {
        for(we in 1:n.for.weeks){
            for(sd in winter.sdates)  mean.fr.winter.obs[wr,,we] <- mean.fr.winter.obs[wr,,we] + fr.wr.obs[wr, sd,, we]
            for(sd in summer.sdates)  mean.fr.summer.obs[wr,,we] <- mean.fr.summer.obs[wr,,we] + fr.wr.obs[wr, sd,, we]
            
            for(sd in winter.sdates)  mean.fr.winter.for[wr,,we] <- mean.fr.winter.for[wr,,we] + fr.wr.for[wr, sd,, we]
            for(sd in summer.sdates)  mean.fr.summer.for[wr,,we] <- mean.fr.summer.for[wr,,we] + fr.wr.for[wr, sd,, we]
            
            ## same as above but for concatenated time series:
            for(sd in winter.sdates)  mean.fr.winter.obs.conc[wr,,we] <- mean.fr.winter.obs.conc[wr,,we] + rep(fr.wr.obs[wr, sd,, we], n.members)
            for(sd in summer.sdates)  mean.fr.summer.obs.conc[wr,,we] <- mean.fr.summer.obs.conc[wr,,we] + rep(fr.wr.obs[wr, sd,, we], n.members)
            
            for(sd in winter.sdates)  mean.fr.winter.for.conc[wr,,we] <- mean.fr.winter.for.conc[wr,,we] + fr.wr.for.memb.conc[wr, sd,, we]
            for(sd in summer.sdates)  mean.fr.summer.for.conc[wr,,we] <- mean.fr.summer.for.conc[wr,,we] + fr.wr.for.memb.conc[wr, sd,, we]
        }
    }

    mean.fr.winter.obs <- mean.fr.winter.obs / length(winter.sdates)
    mean.fr.summer.obs <- mean.fr.summer.obs / length(summer.sdates)
    mean.fr.winter.obs.conc <- mean.fr.winter.obs.conc / length(winter.sdates)
    mean.fr.summer.obs.conc <- mean.fr.summer.obs.conc / length(summer.sdates)
    
    cor.fr.season <- cor.fr.season.conc <- array(NA, c(n.clusters, 2, n.for.weeks))

    for(wr in 1:n.clusters) {
        for(we in 1:n.for.weeks){
            cor.fr.season[wr,1,we] <- cor(mean.fr.winter.obs[wr,,we], mean.fr.winter.for[wr,,we])
            cor.fr.season[wr,2,we] <- cor(mean.fr.summer.obs[wr,,we], mean.fr.summer.for[wr,,we])
            
            ## same but for concatenated time series:
            cor.fr.season.conc[wr,1,we] <- cor(mean.fr.winter.obs.conc[wr,,we], mean.fr.winter.for.conc[wr,,we])
            cor.fr.season.conc[wr,2,we] <- cor(mean.fr.summer.obs.conc[wr,,we], mean.fr.summer.for.conc[wr,,we])
        }
    }

    ## measure seasonal frequency bias:
    WR.fr.bias.season <- array(NA, c(n.clusters, 2, n.for.weeks))
    WR.fr.bias.season[,1,] <- apply(WR.fr.bias[, winter.sdates,], c(1,3), mean, na.rm=TRUE)
    WR.fr.bias.season[,2,] <- apply(WR.fr.bias[, summer.sdates,], c(1,3), mean, na.rm=TRUE)

    ## measure daily coincidents regimes for each season:
    WR.seq.perc.full.season <- array(NA, c(2,32))
    WR.seq.perc.full.season[1,] <- apply(WR.seq.perc.full[winter.sdates,], 2, mean, na.rm=T)
    WR.seq.perc.full.season[2,] <- apply(WR.seq.perc.full[summer.sdates,], 2, mean, na.rm=T)


    ##
    ## Plot Figure 1 with % of coincident regimes
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    my.brks <- c(0,30,40,50,60,70,80,90,100)

    ## ColorTable <- function(matrix, cols, brks, fileout = NULL, wifth = 8, height = 5, size_units = "in", res = 100){
    ##     if (!is.null(fileout)) {
    ##         saveToFile(fileout)
    ##     } else {
    ##         dev.new(units = size_units, res = res, width = width, height = height)
    ##     }
    ## }

    my.file <- paste0(work.dir, "/Figure1.png")
    png(file=my.file, width=530, height=300)  
    plot.new()

    ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
    par(fig = c(0, 0.88, 0.2, 0.8))
    plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 2.04), xlim = c(-0.23, 30.2), ann = F, bty="n") ## to remove black border

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

    ## Add figure title:
    my.title = "Coincident regimes (in %) between ECMWF-MPS and ERA-Interim\nwith the assign method (1979-2017)"
    mtext(side = 3, text = my.title, cex = 1.3, line = 3)

    ## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
    axis(1, at=seq(0,31), 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(" ",1:31)
    text(0:31, par("usr")[3] - 0.55, srt = 0, adj = 1, labels = 1:32, xpd = TRUE, cex=.8) 

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

    ## Plot y-axis:
    axis(2, at=2:1, las=2, labels=1:2, cex.axis=.8)

    ## 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(2,32))
    for(int in (n.cols-1):1) array.colors[WR.seq.perc.full.season < my.brks[int+1]] <- my.cols[int]

    ##set to gray the last column:
    ##array.colors[,32] <- "#ece7f2"

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

    ## add color legend:
    par(fig = c(0.83, 1, 0.30, 0.75), new=TRUE)
    ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
    
    dev.off()    


    ##
    ## Plot Figure 2 with daily Spatial correlations:
    ##

    ## my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ## my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ## my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#fcfbfd','#efedf5','#dadaeb','#bcbddc','#9e9ac8','#807dba','#6a51a3','#4a1486')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#034e7b')
    my.cols <- c('#fff5f0','#fee0d2','#fcbba1','#fc9272','#fb6a4a','#ef3b2c','#cb181d','#99000d')

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(0, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure2_regime_", wr,".png")
        png(file=my.file, width=530, height=700)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0, 0.88, 0, 1))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 52.04), xlim = c(-0.23, 30.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = paste0("Spatial correlation between ECMWF-MPS regime ",wr,"\nand ERA-Interim's (1979-2017)")
        mtext(side = 3, text = my.title, cex = 1.3)

        ## old x-axis that cannot be rotated of 45 degrees; only used to draw ticks:
        axis(1, at=seq(0,31), 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(" ",1:31)
        text(0:31, par("usr")[3] - 1.55, srt = 0, adj = 1, labels = 1:32, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=52:1, las=2, labels=1:52, cex.axis=.8)

        ## 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(52,32))
        for(int in (n.cols-1):1) array.colors[WR.sp.cor.full[, wr,] < my.brks[int+1]] <- my.cols[int]

        ##set to black the NAs:
        ss <- which(is.na(WR.sp.cor.full[,wr,]))
        if(length(ss) > 0)  array.colors[ss] <- "black"

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

        ## add color legend:
        par(fig = c(0.83, 1, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }




    ##
    ## Plot Figure 3 with Temporal freq.correlations:
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure3_regime_", wr,".png")
        png(file=my.file, width=530, height=300)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0, 1))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 2.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = "Temporal correlation between ECMWF-MPS regimes\nand ERA-Interim's (1979-2017)"
        mtext(side = 3, text = my.title, cex = 1.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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 1.55, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=2:1, las=2, labels=1:2, cex.axis=.8)

        ## 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(2, 4))
        for(int in (n.cols-1):1) array.colors[cor.fr.season[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.57, 0.74, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }


    ##
    ## Plot Figure 4 with Frequency bias for each forecast week:
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(-0.25, -0.15, -0.10, -0.05, 0, 0.05, 0.10, 0.15, 0.25)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure4_regime_", wr,".png")
        png(file=my.file, width=530, height=300)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0.2, 0.8))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 2.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = "Frequency bias between ECMWF-MPS regimes\nand ERA-Interim's (1979-2017)"
        mtext(side = 3, text = my.title, cex = 1.3, line = 4)

        ## 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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 0.5, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=1) 

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

        ## Plot y-axis:
        axis(2, at=2:1, las=2, labels=c("Winter","Summer"), cex.axis=1.5)

        ## 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(2, 4))
        for(int in (n.cols-1):1) array.colors[WR.fr.bias.season[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.67, 0.84, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }



    ##
    ## Plot Figure 5 with daily spatial correlations averaged for each forecast week (51, 12 days, etc):
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(0, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure5_regime_", wr,".png")
        png(file=my.file, width=530, height=700)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0, 1))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 52.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = paste0("Temporal correlation between ECMWF-MPS regime ",wr,"\nand ERA-Interim's (1979-2017)")
        mtext(side = 3, text = my.title, cex = 1.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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 1.55, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=52:1, las=2, labels=1:52, cex.axis=.8)

        ## 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(52, 4))
        for(int in (n.cols-1):1) array.colors[WR.weekly.sp.cor[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.57, 0.74, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }

    ##
    ## Plot Figure 6 with weekly spatial correlations
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(0, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure6_regime_", wr,".png")
        png(file=my.file, width=530, height=700)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0, 1))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 52.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = paste0("Spatial correlation between ECMWF-MPS regime ",wr,"\nand ERA-Interim's (1979-2017)")
        mtext(side = 3, text = my.title, cex = 1.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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 1.55, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=52:1, las=2, labels=1:52, cex.axis=.8)

        ## 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(52, 4))
        for(int in (n.cols-1):1) array.colors[WR.sp.cor.week.full[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.57, 0.74, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }

    
    ##
    ## Plot Figure 7 with Temporal freq.correlations concatenated for each start date:
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure7_regime_", wr,"_temp_corr_concatenated.png")
        png(file=my.file, width=530, height=700)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0, 1))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 52.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = paste0("Temporal correlation between ECMWF-MPS Regime ", wr, "\nand ERA-Interim's (1979-2017)")
        mtext(side = 3, text = my.title, cex = 1.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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 1.55, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=52:1, las=2, labels=1:52, cex.axis=.8)

        ## 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(52, 4))
        for(int in (n.cols-1):1) array.colors[WR.temp.cor.conc[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.57, 0.74, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()
    }



    ##
    ## Plot Figure 8 with Temporal freq.correlations for concatenated time series and for season:
    ##

    ##my.cols <- c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59','#e34a33','#b30000', '#7f0000')
    ##my.cols <- c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    ##my.cols <- c('#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858')
    my.cols <- rev(c('#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac'))

    ##my.brks <- c(-0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 1)
    ##my.brks <- c(0,20,30,40,50,60,70,80,90,100)
    ##my.brks <- c(0,30,40,50,60,70,80,90,100)
    my.brks <- c(-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1)

    for(wr in 1:n.clusters){
        my.file <- paste0(work.dir, "/Figure8_regime_", wr,"_temp_cor_season_concatenated.png")
        png(file=my.file, width=530, height=300)  
        plot.new()

        ##par(mar = c(0, 0, 2, 0), fig = c(0.32, 0.57, 0.8, 0.94), new=TRUE)
        par(fig = c(0.3, 0.63, 0.2, 0.8))
        plot(1, 1, type = "n", xaxt = "n", yaxt = "n", ylim = c(1, 2.04), xlim = c(-0.23, 4.2), ann = F, bty="n") ## to remove black border

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

        ## Add figure title:
        my.title = paste0("Temporal correlation between ECMWF-MPS Regime ", wr, "\nand ERA-Interim's (1979-2017)")
        mtext(side = 3, text = my.title, cex = 1.3, line = 4)

        ## 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(" ",1:31)
        xlabel <- c("5-11", "12-18","19-25","26-32")
        text(0:3, par("usr")[3] - 1.55, srt = 45, adj = 1, labels = xlabel, xpd = TRUE, cex=.8) 

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

        ## Plot y-axis:
        axis(2, at=2:1, las=2, labels=1:2, cex.axis=.8)

        ## 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(2, 4))
        for(int in (n.cols-1):1) array.colors[cor.fr.season.conc[wr,,] < my.brks[int+1]] <- my.cols[int]

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

        ## add color legend:
        par(fig = c(0.57, 0.74, 0.30, 0.75), new=TRUE)
        ColorBar(brks=my.brks, cols=my.cols, vert=TRUE, label_scale=1.3, subsample=1) #my.ticks=-0.5 + 1:length(my.brks.var2), my.labels=my.brks.var2) subset=my.subset2)
        
        dev.off()    

    }


    
}


    
## for(season in 1:2){
##     ## determine which of the 52 start dates have to be loaded (depending on the chosen season, winter goes from October to March and summer from April to September)
##     if(season == 1) sdates.season <- c(40:52, 1:13) else sdates.season <- 14:39

##     ## load only lat, lon and n.members
##     system.time(data <- Load('psl', exp = list(list(path=forecast.dir.interp)),
##                              obs = NULL,
##                              sdates = paste0(yr1.hind, substr(sdates.seq[sdates.season[1]],5,6), substr(sdates.seq[sdates.season[1]],7,8)) ,
##                              storefreq='daily',
##                              leadtimemin = 1, leadtimemax = 1,
##                              latmin = 27, latmax = 81,
##                              lonmin = 274.5, lonmax = 45, output = 'lonlat', nprocs=1))

##     ## create an empty array with will store all hindcasted SLP daily anomalies of all start dates of the season:
##     anom.for.season <- array(NA, c(1, n.members, length(sdates.season) * n.yrs.hind, 32, length(data$lat), length(data$lon)))

##     i <- 0
##     for(sdate in sdates.season){
##         ## note that sdate is the hindcast start date, not the observed start date of the reference WR classification that starts on the 1st of January, so they differ!!!
##         print(paste0("Loading Start Date ", i+1, "/", length(sdates.season)))

##         load(paste0("/gpfs/projects/bsc32/bsc32842/daily/anom_for_chunk_", sdate, ".RData"))

##         anom.for.season[,, (1:n.yrs.hind) + n.yrs.hind * i,,,] <- anom_for
##         rm(anom_for)
##         gc()
##         i <- i + 1
##     }


## } ## close for on season

## ## load only lat, lon and n.members
## system.time(data <- Load('psl', exp = list(list(path=forecast.dir.interp)),
##                          obs = NULL,
##                          sdates = paste0(yr1.hind, substr(sdates.seq[1],5,6), substr(sdates.seq[1],7,8)) ,
##                          storefreq='daily',
##                          leadtimemin = 1, leadtimemax = 1,
##                          latmin = 27, latmax = 81,
##                          lonmin = 274.5, lonmax = 45, output = 'lonlat', nprocs=1))


