##########################################################################################
#                Relationship between WTs and a chosen climate variable                  #
##########################################################################################

library(s2dverification)  # for the function Load()
library(ff)

source('/home/Earth/ncortesi/Downloads/scripts/Rfunctions.R') # for the calendar functions
#Load.path <- '/home/Earth/ncortesi/Downloads/scripts/IC3.conf'      # Load() file path for resampled MSLP

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

ERAint <- list(path = '/esnas/recon/ecmwf/erainterim/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
ERAint2 <- list(path = '/esnas/recon/ecmwf/erainterim/$STORE_FREQ$_mean/$VAR_NAME$_s0-12h/$VAR_NAME$_$YEAR$$MONTH$.nc')  # only for prec
JRA55 <- list(path = '/esnas/recon/jma/jra-55/$STORE_FREQ$_mean/$VAR_NAME$/$VAR_NAME$_$YEAR$$MONTH$.nc')

psl.rean=ERAint   # choose a daily reanalysis dataset for MSLP data
psl.rean.name="Era-Interim"

var.rean=ERAint2 #ERAint   # daily reanalysis dataset used for the var data; it can have a different resolution of the MSLP dataset
var.name='tas'  #'prlr'  #'tas' #'sfcWind'         # any daily variable we want to find if it is WTs-driven
var.name.file='Temperature'  #'Precipitation' #'Surface_Temperature' # '10-m_Wind_Speed'   # variable name used in the ouput map's filename

year.start=1979     # starting year of the MSLP daily data (from the 1st of january)
year.end=2013 #2014       # ending year of the MSLP daily data (up to the 31 of December)

partial.end=FALSE # put TRUE if the last year ('year.end') has not all the yearly data but stop before December the 31th; in this case, must also specify the variable below
n.days.last=334  # number of days available in the last year (used only if partial.end=TRUE) I.e: data for 2015 doesn't have December, so it has 365-31=334 days

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

WTs.type <- c("NE","E","SE","S","SW","W","NW","N","C","C.NE","C.E","C.SE","C.S","C.SW","C.W","C.NW","C.N","A","A.NE","A.E","A.SE","A.S","A.SW","A.W","A.NW","A.N")
WTs.type10<-c("NE","E","SE","S","SW","W","NW","N","C","A")
WTs.type10.name<-c("Northeasterly (NE)","Easterly (E)","Southeasterly (SE)","Southerly (S)","Southwesterly (SW)","Westerly (W)","Northwesterly (NW)",
							"Northerly (N)","Cyclonic (C)","Anticyclonic (A)")
year.tot <- year.end - year.start + 1

periods=c(1:17)       # identify monthly (from 1=Jan to 12=Dec) , seasonal (from 13=winter to 16=Autumn) and yearly=17 values
n.periods <- length(periods)

var <- Load(var.name, NULL, list(var.rean), paste0(y,'0101'), storefreq = 'daily', leadtimemax = n.days, output = 'lonlat') # load var data for the year y only

























# Load just 1 day of var data to detect the number of latitude and longitude points;
#var <- Load(var.name, NULL, var.rean, paste0(year.start,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat')
var <- Load(var = var.name, exp = NULL, obs = list(var.rean), paste0(year.start,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat')
var.lat <- round(var$lat,3)
var.lon <- round(var$lon,3)
var.n.lat <- length(var.lat)  # number of latitude values of var
var.n.lon <- length(var.lon)
var.lon.pos <- ifelse(min(var.lon>=0), TRUE, FALSE)  # set lon.pos to TRUE if the longitude values of the reanalysis grid has no negative values, FALSE if not.
    
### Load just 1 day of MSLP data to detect the number of latitude and longitude points of the WT classifications
### we must exlude points > +80 degrees lat and < -80 deg. because the Lamb grid was created excluding these points:
###MSLP <- Load('psl', NULL, psl.rean, paste0(year.start,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat', configfile = Load.path)
#MSLP <- Load(var = 'psl', exp = NULL, obs = list(psl.rean), paste0(year.start,'0101'), storefreq = 'daily', leadtimemax = 1, output = 'lonlat')

#psl.n.lat <- length(MSLP$lat)  # number of latitude values or MSLP
#psl.n.lon <- length(MSLP$lon)  # number of longitude values of MSLP
#psl.lon.pos <- ifelse(min(MSLP$lon>=0), TRUE, FALSE)  # set lon.pos to TRUE if the longitude values of the reanalysis grid has no negative values, FALSE if not.

#psl.n.lat.unused.poles <- 17 # number of discarded latitude values near each of the two poles (it depends on the spatial resolutions of the MSLP reanalysis)
#psl.n.lat.unused.equat <- 20 # number of unused latitude values near each side of the equator (must exclude 10 degrees N/S for side, so it depends on the spat.res. of the reanalysis)
#psl.pos.lat.eq.north <- tail(which(MSLP$lat >= 0),1) # note than the eventual point at lat=0 is always included
#psl.pos.lat.eq.south <- head(which(MSLP$lat < 0),1) # note than the eventual point at lat=0 is always excluded
#psl.pos.lat.unused.eq.north <- (psl.pos.lat.eq.north - psl.n.lat.unused.poles + 1):psl.pos.lat.eq.north
#psl.pos.lat.unused.eq.south <- psl.pos.lat.eq.south:(psl.pos.lat.eq.south + psl.n.lat.unused.equat - 1)

# final latitude values used as central points:
#psl.lat.used <- MSLP$lat[-c(1:psl.n.lat.unused.poles,(psl.n.lat-psl.n.lat.unused.poles):psl.n.lat, psl.pos.lat.unused.eq.north, psl.pos.lat.unused.eq.south)]  
#psl.lat.used <- round(MSLP$lat[-c(1:psl.n.lat.unused.poles,(psl.n.lat-psl.n.lat.unused.poles):psl.n.lat)],3)  # latitude values used as central points
#psl.lon.used <- round(MSLP$lon,3) # longitude values used as central points

#psl.n.lat.used <- length(psl.lat.used)
#psl.n.lon.used <- length(psl.lon.used)   
#psl.n.grid.points <- psl.n.lat.used * psl.n.lon.used

#if(var.lon.pos && !psl.lon.pos) {ss <- which(psl.lon.used<0); psl.lon.used[ss] <- psl.lon.used[ss] + 360}  # convert the negative long of MSLP to the [0, +360] range
#if(!var.lon.pos && psl.lon.pos) {ss <- which(psl.lon.used>180); psl.lon.used[ss] <- psl.lon.used[ss] - 360}  # convert the positive long of MSLP > 180 to the [-180, +180] range

# exlude var points > +~80 degrees lat and < -80 deg. because the Lamb grid was created excluding these points:
var.n.lat.unused.poles <- 17 # number of discarded latitude values near each of the two poles (it depends on the spatial resolutions of the var reanalysis)
var.n.lat.unused.equat <- 17 # numb. of unused latitude values near each side of the equator (must exclude 10 degrees N/S for side, so it depends on the spat.res. of the reanalysis)

var.pos.lat.eq.north <- tail(which(var$lat >= 0),1) # note than the eventual point at lat=0 is always included
var.pos.lat.eq.south <- head(which(var$lat < 0),1) # note than the eventual point at lat=0 is always excluded
var.pos.lat.unused.eq.north <- (var.pos.lat.eq.north - var.n.lat.unused.equat+1):var.pos.lat.eq.north
var.pos.lat.unused.eq.south <- var.pos.lat.eq.south:(var.pos.lat.eq.south + var.n.lat.unused.equat - 1)
var.pos.lat.unused.eq <- c(var.pos.lat.unused.eq.north, var.pos.lat.unused.eq.south)

#var.lat.used <- var.lat[-c(1:var.n.lat.unused.poles,(var.n.lat-var.n.lat.unused.poles):var.n.lat, var.pos.lat.unused.eq.north, var.pos.lat.unused.eq.south)]  # latitude values used as central points

var.lat.used <- var.lat[-c(1:var.n.lat.unused.poles,(var.n.lat-var.n.lat.unused.poles):var.n.lat)]
var.lon.used <- var.lon # longitude values used as central points
var.n.lat.used <- length(var.lat.used)
var.n.lon.used <- length(var.lon.used)   
var.n.grid.points <- var.n.lat.used * var.n.lon.used
#var.lon.used.bis <- c(var.lon.used[c(257:512)]-360,var.lon.used[c(1:256)]) # to put Europe in the middle of the map
var.lon.used.bis <- c(var.lon.used[c(ceiling(var.n.lon.used/2):var.n.lon.used)]-360,var.lon.used[c(1:(ceiling(var.n.lon.used/2)-1))]) # to put Europe in the middle of the map

#pos.var.lat.unused <- which(is.na( match(var.lat,var.lat.used)))  # position inside var.lat of the latitude values not used (those not in var.lat.used)
pos.var.lat.used <- match(var.lat.used, var.lat) # =which(!is.na( match(var.lat,var.lat.used)))  # position inside var.lat of the latitude values used (those in var.lat.used)
#var.lat[pos.var.lat.unused]


### Map of the central points for Europe:
#void <- array(NA,c(var.n.lat.used, var.n.lon.used))
###void.bis <- void[,c(257:512,1:256)] # when using the var dataset with res 256x512
#void.bis <- void[,c(ceiling(var.n.lon.used/2):var.n.lon.used,1:(ceiling(var.n.lon.used/2)-1))]
###PlotEquiMap(void.bis, var.lon.used.bis, var.lat.used, filled.continents = FALSE, brks=seq(0,360,1), intxlon=5, intylat=5, drawleg=F ) # for the debug
###PlotEquiMap(void.bis[1:215,], var.lon.used.bis, var.lat[1:215], filled.continents = FALSE, brks=seq(0,360,1), intxlon=5, intylat=5, drawleg=F ) # for the debug
#par(oma=c(1,1,1,1))
###PlotEquiMap(void.bis, var.lon.used.bis, var.lat.used, filled.continents = FALSE, brks=seq(0,360,1), intxlon=5, intylat=5, drawleg=F ) # for the debug
#PlotEquiMap(void.bis[25:90, 220:317], var.lon.used.bis[220:317], var.lat[25:90], filled.continents = FALSE, brks=seq(0,360,1), intxlon=5, intylat=5, drawleg=F ) # for the debug
#my.parcelas<-data.frame(name="", lat=rep(var.lat.used,var.n.lon.used), long=rep(var.lon.used.bis, each=var.n.lat.used), pop=0, capital=0, stringsAsFactors=F)
#map.cities(my.parcelas, pch=3, cex=.5,col=c("gray40"))  # add the 4 points of the 4 parcelas
#write.table(cbind(lat=rep(var.lat.used,var.n.lon.used), lon=rep(var.lon.used, each=var.n.lat.used)),file=paste0(workdir,"/list_lat_lon.txt"),row.names=FALSE, col.names=TRUE, quote=FALSE, sep=" ")

# for each var grid point used, find the lat/lon position of the nearby point center of the WT classification:
closer.psl.lat <- closer.psl.lon <- array(NA, c(var.n.lat.used, var.n.lon.used))  # 2 matrices with the lat and lon position of the nearest MSLP grid point

i<-0;vlat.pos <- 0
for (vlat in var.lat.used) {
   vlat.pos<-vlat.pos+1
   
   vlon.pos<-0
   for(vlon in var.lon.used) {
       i<-i+1
       cat(paste0("Point #",i,"/", var.n.grid.points), '\r')
       vlon.pos<-vlon.pos+1
       
       closer.psl.pos <- nearest(vlat, vlon, psl.lat.used, psl.lon.used)
       closer.psl.lat[vlat.pos, vlon.pos] <- psl.lat.used[closer.psl.pos[1]]
       closer.psl.lon[vlat.pos, vlon.pos] <- psl.lon.used[closer.psl.pos[2]]
       
       #print(paste0("vlat=",vlat, " vlat.pos=",vlat.pos, " vlon=",vlon, " vlon.pos=",vlon.pos, " closer.psl.lat.pos=", closer.psl.pos[1], " closer.psl.lon.pos=",closer.psl.pos[2], " closer.psl.lat=", psl.lat.used[closer.psl.pos[1]], " closer.psl.lon=", psl.lon.used[closer.psl.pos[2]] )) # for the debug
       
   }
}

save(closer.psl.lat, closer.psl.lon, file=paste0(workdir,"/closer_psl_",var.name,".RData"))  # save it if it is the first time
load(file=paste0(workdir,"/closer_psl_",var.name,".RData"))  # load it if already saved

#PlotEquiMap(closer.psl.lon, var.lon.used, var.lat.used, filled.continents = FALSE, brks=seq(0,360,1) ) # for the debug
#PlotEquiMap(closer.psl.lat, var.lon.used, var.lat.used, filled.continents = FALSE, brks=seq(-90,90,1) ) # for the debug

# for each var grid point used, load 1 year of daily var and wt data at time, and assign it to the closer mslp central point:
# (you can't open all data at once because it weights too much, but you can open 1 year at time)
chunk <- split.array(dimensions=c(n.days.in.a.yearly.period(year.start,year.end), var.n.lat.used, var.n.lon.used), along=3)
save(chunk, file=paste0(workdir,"/chunk_",var.name,".RData")) # save the chunk to retrieve it later or you run the next loop for 1 year only ('year.end' changes, so 'chunk' changes too)
load(paste0(workdir,"/chunk_",var.name,".RData"))

i=0
for(y in year.start:year.end){
   #y<-1981 # for the debug
   i<-i+1
   print(paste0("Year #",i,"/", year.tot))
   
   n.days <- n.days.in.a.year(y)
   if(partial.end==TRUE && y==year.end) n.days<-n.days.last # the last year can have a lower number of days
 
   var <- Load(var.name, NULL, list(var.rean), paste0(y,'0101'), storefreq = 'daily', leadtimemax = n.days, output = 'lonlat') # load var data for the year y only
   #PlotEquiMap(var$obs[1, 1, 1, 1, , ], var.lon, var.lat, filled.continents = FALSE)

   # open the ff binary files with all the WT classification for that year:
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs1_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs1_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs2_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs2_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs3_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs3_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs4_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs4_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs5_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs5_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs6_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs6_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs7_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs7_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs8_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs8_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs9_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs9_",y)), overwrite=TRUE)
   if(file.exists(paste0(workdir,"/",paste0(y,"_RData/WTs10_",y,".RData")))) ffload(file=paste0(workdir,"/",paste0(y,"_RData/WTs10_",y)), overwrite=TRUE)
    
   var.serie <- wt.serie <- array(NA,c(var.n.lat.used, var.n.lon.used, n.days))  # arrays where to store the var and wt daily data of year y for all points

   j<-0
   vlat.pos <- 0
   for (vlat in var.lat.used) {
       #vlat<-var.lat.used[1]; vlon<-var.lon.used[1]  # for the debug
       j<-j+1
       vlat.pos <- vlat.pos+1       
       cat('Latitude ',j,'/',var.n.lat.used,'\r')
       
       vlon.pos <- 0
       for(vlon in var.lon.used) {
           vlon.pos <- vlon.pos+1
          
           var.serie[vlat.pos, vlon.pos,] <- var$obs[1,1,1,,which(var.lat==vlat),which(var.lon==vlon)]

           #WT <- read.table(file=paste0(workdir,"/","10WTs_",psl.rean,"_",year.start,"-",year.end,"_lat_",closer.psl.lat[vlat.pos,vlon.pos],"_lon_",closer.psl.lon[vlat.pos,vlon.pos],".txt"), header=TRUE, sep=" ", stringsAsFactors=FALSE, row.names=NULL)

           # load WTs classifications (variable WTs) for that year created with WT_vX.R:
           #load(paste0(workdir,"/",y,"_Rdata/","10WTs_",psl.rean,"_year_",y,"_lat_",closer.psl.lat[vlat.pos,vlon.pos],"_lon_",closer.psl.lon[vlat.pos,vlon.pos],".RData"))       
           open(eval(parse(text=paste0("WTs_lat_", closer.psl.lat[vlat.pos,vlon.pos],"_lon_", closer.psl.lon[vlat.pos,vlon.pos]))))
           #open(eval(parse(text=gsub("-","m",paste0("WTs_lat_", closer.psl.lat[vlat.pos,vlon.pos],"_lon_", closer.psl.lon[vlat.pos,vlon.pos])))))
          

           # interval of days belonging to the year y, but starting to count from the year year.begin:
           #seq.days.year <- n.days.in.a.yearly.period(year.start,y) - n.days.in.a.yearly.period(year.start,year.start) + 1:n.days.in.a.year(y)
           WTs <- as.ram(eval(parse(text=paste0("WTs_lat_", closer.psl.lat[vlat.pos,vlon.pos],"_lon_", closer.psl.lon[vlat.pos,vlon.pos]))))
           wt.serie[vlat.pos, vlon.pos,] <- WTs  #[seq.days.year]  #,4]  # extract only the days of the year y
           close(eval(parse(text=paste0("WTs_lat_", closer.psl.lat[vlat.pos,vlon.pos],"_lon_", closer.psl.lon[vlat.pos,vlon.pos]))))
           rm(WTs)
       }
       
   }

   #PlotEquiMap(var.serie[,,1], var.lon, var.lat.used, filled.continents = FALSE) # for the debug

   #save(var.serie, file=paste0(workdir,"/local_",var.name,"_",psl.rean,"_year_",y,".RData"), compress=FALSE)
   #save(wt.serie,  file=paste0(workdir,"/local_","10WTs_",psl.rean,"_year_",y,".RData"), compress=FALSE)

   #year=4000
   #ffload(file=paste0(workdir,"/WTs_",year))
   #latc <- 70
   #lonc <- 0
   #open(eval(parse(text=paste0("WTs_lat_",latc,"_lon_", lonc))))  # do.call doesn't work with open, must 
   #close(eval(parse(text=paste0("WTs_lat_",latc,"_lon_", lonc))))  # do.call doesn't work with open, must use eval(parse(text=...)))

   
   # instead of saving only 1 file for all the spatial domain, save 1 file for each chunk c: (to be able to load them faster in the following step)
   if(!dir.exists(paste0(workdir,"/",var.name,"_chunk"))) dir.create(paste0(workdir,"/",var.name,"_chunk"))
   if(!dir.exists(paste0(workdir,"/wt_chunk"))) dir.create(paste0(workdir,"/wt_chunk"))

   for(c in 1:chunk$n.chunk){
     var.serie.year.chunk<-var.serie[,chunk$int[[c]],]  # format: [lat, lon, day]
     save(var.serie.year.chunk, file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_",psl.rean.name,"_year_",y,"_chunk_",c,".RData"), compress=FALSE)
     
     wt.serie.year.chunk <- wt.serie[,chunk$int[[c]],]
     save(wt.serie.year.chunk, file=paste0(workdir,"/",var.name,"_wt_chunk/local_","10WTs_",psl.rean.name,"_year_",y,"_chunk_",c,".RData"), compress=FALSE)
     # n.wt10<-apply(wt.serie.year.chunk,c(1,2),function(x)length(which(x==18)))  # for the debug
     # edit(n.wt10) # for the debug
   }
    
} # close for on y

#rm(closer.psl.lat, closer.psl.lon, var)


# compute the climatology of var for each period and its daily anomalies for each chunk and period:
var.clim <- array(NA, c(n.periods, var.n.lat.used, var.n.lon.used))

n.days.tot <- n.days.in.a.yearly.period(year.start,year.end)
#n.days.tot <- 365 # for the debug

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

for(c in 1:chunk$n.chunk){
      #c=1 # for the debug
      cat(paste0("Computing chunk n. ", c,"/", chunk$n.chunk),'\r')

      if(c == chunk$n.chunk)  {
           var.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size.last, n.days.tot))
      } else {
           var.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size, n.days.tot))
      }
             
      for(y in year.start:year.end){
           load(file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_",psl.rean.name,"_year_",y,"_chunk_",c,".RData"))  # load var.serie.year.chunk
           int.days.year <- seq.days.in.a.future.year(year.start,y)
               
           var.serie.chunk[,,int.days.year] <- var.serie.year.chunk
           rm(var.serie.year.chunk)
      }

      for(p in periods){ 
          var.serie.chunk.period <- var.serie.chunk[,,days.period[[p]]] # select only the days in the chosen period
          
          var.clim.chunk <- apply(var.serie.chunk.period,c(1,2),mean, na.rm=T)

          var.anom.chunk.period <- var.serie.chunk.period - InsertDim(var.clim.chunk, 3, n.days.period[[p]])
          var.clim[p,,chunk$int[[c]]] <- var.clim.chunk

          # save the var anomaly, one file for each chunk and period:
          assign(paste0("var.anom.chunk.period",p), var.anom.chunk.period)
          
          # save(, file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_anomaly_",psl.rean,"_",year.start,"-",year.end,"_chunk_",c,"_period_",p,".RData"), compress=FALSE)
          # save not working in this case, use do.call below:
          do.call(save, list(paste0("var.anom.chunk.period",p), file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_anomaly_",psl.rean.name,"_",year.start,"-",year.end,"_chunk_",c,"_period_",p,".RData"), compress=FALSE))  # you must use this syntax when saving an Rdata with the variable name given by a string!!!

          rm(var.anom.chunk.period,var.serie.chunk.period)
          do.call(rm, list(paste0("var.anom.chunk.period",p)))
      }
    
      rm(var.serie.chunk)
}

save(var.clim, file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_climatology_",psl.rean.name,"_",year.start,"-",year.end,".RData"), compress=FALSE) # save var.clim
load(paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_climatology_",psl.rean.name,"_",year.start,"-",year.end,".RData"))

# do the same as above, but for the wt:
wts <- c(1:10) # weather type numbers inside wt.serie
n.wts<-length(wts)
n.wt <- var.wt.sum <- var.wt.mean.anom <- var.wt.sd.anom <- array(NA, c(n.periods, n.wts, var.n.lat.used, var.n.lon.used))
#vlat.mat <- vlon.mat <- array(NA, c(var.n.lat.used, var.n.lon.used))  # for the debug

for(c in 1:chunk$n.chunk){
      #s=1;y=year.start # for the debug
      cat(paste0("Computing chunk n. ", c,"/", chunk$n.chunk),'\r')

      if(c == chunk$n.chunk)  {
           var.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size.last, n.days.tot)) 
           wt.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size.last, n.days.tot))
           n.wt.chunk <- var.wt.sum.chunk <- var.wt.mean.anom.chunk <- var.wt.sd.anom.chunk <- array(NA,c(n.periods, n.wts, var.n.lat.used, chunk$chunk.size.last))          
      } else {
           var.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size, n.days.tot))
           wt.serie.chunk <- array(NA, c(var.n.lat.used, chunk$chunk.size, n.days.tot))
           n.wt.chunk <- var.wt.sum.chunk <- var.wt.mean.anom.chunk <- var.wt.sd.anom.chunk <- array(NA,c(n.periods, n.wts, var.n.lat.used, chunk$chunk.size))
      }
    
      for(y in year.start:year.end){
           load(file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_",psl.rean.name,"_year_",y,"_chunk_",c,".RData"))  # load var.serie.year.chunk
           int.days.year <- seq.days.in.a.future.year(year.start,y)    
           var.serie.chunk[,,int.days.year] <- var.serie.year.chunk    
           rm(var.serie.year.chunk)
      }

    
      for(y in year.start:year.end){
           load(file=paste0(workdir,"/",var.name,"_wt_chunk/local_","10WTs_",psl.rean.name,"_year_",y,"_chunk_",c,".RData"))  # load wt.serie.year.chunk

           wt.serie.year.chunk[which(wt.serie.year.chunk==18)]<-10  # rename the A type from number 18 to number 10, to have only the numbers from 1 to 10
           int.days.year <- seq.days.in.a.future.year(year.start,y)
               
           wt.serie.chunk[,,int.days.year] <- wt.serie.year.chunk  #[,chunk$int[[c]],]
           rm(wt.serie.year.chunk)
      }

      for(p in periods){
          # load var.anom.chunk.period
          load(file=paste0(workdir,"/",var.name,"_chunk/local_",var.name,"_anomaly_",psl.rean.name,"_",year.start,"-",year.end,"_chunk_",c,"_period_",p,".RData"))
          var.serie.chunk.period <- var.serie.chunk[,,days.period[[p]]]
          wt.serie.chunk.period <- wt.serie.chunk[,,days.period[[p]]]
          
          vlat.pos  <- 0
          for (vlat in var.lat.used) {
              #vlat<-var.lat.used[1]; vlon<-var.lon.used[1]; wt=1  # for the debug
              vlat.pos <- vlat.pos + 1
              #cat('Latitude ',vlat.pos,'/',var.n.lat.used,'\r')
              
              vlon.pos <- 0 #chunk$int[[c]]-1
              for(vlon in var.lon.used[chunk$int[[c]]]) {
                  vlon.pos <- vlon.pos + 1
                  for(wt in wts){
                      pos.wt <- which(wt.serie.chunk.period[vlat.pos,vlon.pos,]==wt)
                      var.anom.chunk.period <- get(paste0("var.anom.chunk.period",p))
                      
                      n.wt.chunk[p, wt, vlat.pos, vlon.pos] <- length(pos.wt)
                      var.wt.sum.chunk[p, wt, vlat.pos, vlon.pos] <- sum(var.serie.chunk.period[vlat.pos,vlon.pos,][pos.wt])  # only for wind and prec
                      var.wt.mean.anom.chunk[p, wt, vlat.pos, vlon.pos] <- mean(var.anom.chunk.period[vlat.pos,vlon.pos,][pos.wt])
                      var.wt.sd.anom.chunk[p, wt, vlat.pos, vlon.pos] <- sd(var.anom.chunk.period[vlat.pos,vlon.pos,][pos.wt])
                      
                      rm(pos.wt, var.anom.chunk.period)
                  }
              }
          }
          
          n.wt[p,,,chunk$int[[c]]] <- n.wt.chunk[p,,,]
          var.wt.sum[p,,,chunk$int[[c]]] <- var.wt.sum.chunk[p,,,]
          var.wt.mean.anom[p,,,chunk$int[[c]]] <- var.wt.mean.anom.chunk[p,,,]
          var.wt.sd.anom[p,,,chunk$int[[c]]] <- var.wt.sd.anom.chunk[p,,,]
      } # close for on p
}

save(n.wt, var.wt.sum, var.wt.mean.anom, var.wt.sd.anom.chunk, file=paste0(workdir,"/",var.name,"_chunk/output_",psl.rean.name,"_",year.start,"-",year.end,".RData"), compress=FALSE)
load(file=paste0(workdir,"/",var.name,"_chunk/output_",psl.rean.name,"_",year.start,"-",year.end,".RData"))

#if(var.name=='prlr'){ # if it is not in m/s (as in ERA-Interim) but it is in kg/m2/s,  you must multiply for 86400 to convert to mm/day
#    var.clim <- var.clim * 86400
#    var.wt.sum <- var.wt.sum * 86400
#    var.wt.mean.anom <- var.wt.mean.anom * 86400
#    var.wt.sd.anom <- var.wt.sd.anom * 86400
#}

var.wt.mean.contrib <- array(NA, c(n.periods, n.wts, var.n.lat.used, var.n.lon.used))

var.wt.mean <- var.wt.sum/n.wt # average value of var associated to a WT
#n.wt <- 100*n.wt/n.days.tot # convert to yearly frequency in %
for(p in periods) n.wt[p,,,] <- 100*n.wt[p,,,]/n.days.period[[p]] # convert to yearly frequency in %
for(p in periods) var.wt.mean.contrib[p,,,] <- 100*var.wt.sum[p,,,]/(InsertDim(var.clim[p,,],1,n.wts)*n.days.period[[p]])  # convert the sum to a % contribution

var.clim.NA <-  array(NA, c(n.periods, var.n.lat, var.n.lon.used))
n.wt.NA <- var.wt.mean.contrib.NA <- var.wt.sum.NA <- var.wt.mean.anom.NA <- var.wt.mean.NA <- var.wt.sd.anom.NA <- array(NA, c(n.periods, n.wts, var.n.lat, var.n.lon.used))

var.clim.NA[,pos.var.lat.used,] <- var.clim
n.wt.NA[,,pos.var.lat.used,] <- n.wt
var.wt.mean.NA[,,pos.var.lat.used,] <- var.wt.mean
var.wt.sum.NA[,,pos.var.lat.used,] <- var.wt.sum
var.wt.mean.anom.NA[,,pos.var.lat.used,] <- var.wt.mean.anom
var.wt.mean.contrib.NA[,,pos.var.lat.used,] <- var.wt.mean.contrib
var.wt.sd.anom.NA[,,pos.var.lat.used,] <- var.wt.sd.anom

# remove the equatorial area from visualization:
n.wt.NA[,,var.pos.lat.unused.eq,]<- var.wt.mean.NA[,,var.pos.lat.unused.eq,] <- var.wt.sum.NA[,,var.pos.lat.unused.eq,] <- var.wt.mean.contrib.NA[,,var.pos.lat.unused.eq,] <- NA
var.wt.mean.anom.NA[,,var.pos.lat.unused.eq,] <- var.wt.sd.anom.NA[,,var.pos.lat.unused.eq,] <- NA

# move Europe to the center of the maps:
p1 <- ceiling(var.n.lon.used/2)-1
p2 <- ceiling(var.n.lon.used/2)
p3 <- var.n.lon.used

var.clim.NA.bis <- var.clim.NA[,,c(p2:p3,1:p1)]
n.wt.NA.bis <- n.wt.NA[,,,c(p2:p3,1:p1)]
var.wt.mean.NA.bis <- var.wt.mean.NA[,,,c(p2:p3,1:p1)]
var.wt.mean.contrib.NA.bis <- var.wt.mean.contrib.NA[,,,c(p2:p3,1:p1)]
var.wt.mean.anom.NA.bis <- var.wt.mean.anom.NA[,,,c(p2:p3,1:p1)]
var.wt.sd.anom.NA.bis <- var.wt.sd.anom.NA[,,,c(p2:p3,1:p1)]
gc()

# Map intervals and colors:
my.brks <- list()

#my.brks[[1]] <- c(0,seq(1.5,9,0.5),15)  # Wind speed Climatology
my.brks[[1]] <- c(0,seq(1.05,10,0.05),15)  # Wind speed Climatology
my.brks[[1]] <- c(-50,-40,-30,seq(-20,30,0.05),40,50)  # Surface Temperature Climatology
my.brks[[1]] <- c(0,seq(0.05,10,0.05),15)  # Precipitation Climatology

#my.brks[[2]] <- c(-10,seq(-4.95,5,0.05),10) # Wind Speed Anomaly associated to a WT
my.brks[[2]] <- c(seq(0,10,0.1),100) # Mean wind speed in m/s associated to a WT
my.brks[[3]] <- c(seq(0,20,0.1),100) # Frequency associated to a WT
my.brks[[4]] <- c(seq(0,30,0.1),100) # % Contribution of a WT to total var
my.brks[[5]] <- c(seq(-10,-3,1),seq(-2.3,2.3,0.1),seq(3,10,1)) # % Mean anomaly of a WT for wind speed
my.brks[[5]] <- c(-50,seq(-10,-3,1),seq(-2.9,2.9,0.1),seq(3,10,1),50) # % Mean anomaly of a WT for temperature
my.brks[[5]] <- c(-100000,seq(-100,0,1),seq(1,100,1),100000) # % Mean anomaly of a WT for precipitation


my.brks[[6]] <- c(seq(0,3,0.1),10) # Standard deviation of the anomalies pf a WT

my.cols <- list()
#my.cols[[index]] <- colorRampPalette(my.palette[[index]])(length(my.brks[[index]])-1)
#my.cols[[1]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[1]])-1)  # blue-green-white-yellow-red colors
#my.cols[[1]] <-  colorRampPalette(c("white","yellow","orange","red","darkred"))(length(my.brks[[1]])-1)  # blue-yellow-red colors
#my.cols[[1]] <-  colorRampPalette(c("white","white","green","yellow","orange","red","darkred"))(length(my.brks[[1]])-1)  # blue-yellow-red colors
#my.cols[[1]] <-  colorRampPalette(c("blue","white","darkred"))(length(my.brks[[1]])-1)  # blue-yellow-red colors
#my.cols[[1]] <-  colorRampPalette(c("blue","green","yellow","red","brown","violetred4"))(length(my.brks[[1]])-1)  # blue-yellow-red colors
#my.cols[[1]] <-  colorRampPalette(c("white","cyan2","gold","orange","red","brown","brown4","deeppink4"))(length(my.brks[[1]])-1)  # blue-yellow-red colors
my.cols[[1]] <-  colorRampPalette(c("white","gray","cyan2","blue","deeppink4"))(length(my.brks[[1]])-1)  # white-gray-blue-purple colores for wind speed
my.cols[[1]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[1]])-1)   # blue-yellow-red colors

#my.cols[[2]] <-  colorRampPalette(c("deeppink4","darkblue","blue","white","red","darkred","brown4"))(length(my.brks[[2]])-1)  # blue-white-red colors
my.cols[[2]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[2]])-1)  # blue--yellow-red colors
my.cols[[3]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[3]])-1)  # blue-yellow-red colors
#my.cols[[4]] <-  colorRampPalette(c("white","cyan2","blue","deeppink4"))(length(my.brks[[4]])-1)  # blue-yellow-red colors
my.cols[[4]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[4]])-1)  # blue--yellow-red colors

my.cols[[5]] <-  c(colorRampPalette(c("dodgerblue3","white"))(length(my.brks[[5]])-1), colorRampPalette(c("white","darkorchid4"))(length(my.brks[[5]])-1))
my.cols[[5]] <- my.cols[[5]][seq(1, length(my.cols[[5]]),2)] 
my.cols[[5]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[5]])-1)  # blue--yellow-red colors for temper
my.cols[[5]] <-  c(colorRampPalette(c("blue","white"))(length(my.brks[[5]])-1), colorRampPalette(c("white","darkred"))(length(my.brks[[5]])-1))  # blue-white-red for prec
my.cols[[5]] <- my.cols[[5]][seq(1, length(my.cols[[5]]),2)] 
#my.cols[[5]] <-  colorRampPalette(c("white","blue","purple"))(length(my.brks[[5]])-1) 

my.cols[[6]] <-  colorRampPalette(as.character(read.csv("/home/Earth/vtorralb/rgbhex.csv",header=F)[,1]))(length(my.brks[[6]])-1)  # blue--yellow-red colors



# Create and save maps:

index=1
#p=17 # for the debug

for(p in periods){
    png(filename=paste0(mapdir,"/",var.name.file,"_Climatology_",period.name[p],".png"),width=1000,height=700)

    layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
    #layout.show(2)
    #PlotEquiMap2(var.clim.NA, var.lon.used, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F)
    # change longitude in format -180,180 to displey europe in the middle of the map:
    #PlotEquiMap(var.clim[p,,], var.lon.used, var.lat.used, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
    
    # for temperature, you must remove -273 after var.clim.NA.bis[p,,]:
    PlotEquiMap(var.clim.NA.bis[p,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
    ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=20, cex=1)

    dev.off()
}


index=2
#p=17 # for the debug
wt=6

for(p in periods){
   png(filename=paste0(mapdir,"/Average_10-m_Wind_Speed_WT_", WTs.type10[wt],period.name[p],".png"),width=1000,height=700)

   layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
   #PlotEquiMap(var.wt.mean.NA[wt,,], var.lon.used, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
   #PlotEquiMap(var.wt.mean.NA[wt,,100:200], var.lon.used[100:200], var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
   PlotEquiMap(var.wt.mean.NA.bis[p,wt,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
   #PlotEquiMap(var.wt.mean.NA.bis[wt,,1:50], head(var.lon.used.bis,50), var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
   ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=10, cex=1)

   dev.off()
}


index=3
p=17 # for the debug
wt=6

png(filename=paste0(mapdir,"/Frequency_WT_", WTs.type10[wt],".png"),width=1000,height=700)

layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
#PlotEquiMap(n.wt.NA[wt,,], var.lon.used, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F)
#PlotEquiMap(n.wt.NA[wt,,1:18], tail(var.lon.used,18), var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F)
#n.wt.NA[wt,50:55,490:512]
#ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=5, cex=1)
#n.wt10<-apply(wt.serie.,c(1,2),function(x)length(which(x==18)))  # for the debug
#edit(n.wt.NA[10,,]) # for the debug
#layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
PlotEquiMap(n.wt.NA.bis[p,wt,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]], drawleg=F, colNA="gray")
ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=5, cex=1)

dev.off()


index=4
p=17
wt=6 # for the debug

for(wt in 1:n.wts){
    png(filename=paste0(mapdir,"/Daily_contribution_of_WT_", WTs.type10[wt],".png"),width=1000,height=700)
    
    layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
    PlotEquiMap(var.wt.mean.contrib.NA.bis[p,wt,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], cols=my.cols[[index]],
                drawleg=F, colNA="gray", toptitle=WTs.type10.name[wt] , sizetit=1)
    ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=10, cex=1)
    
    dev.off()
}


# Average anomalies associated to a WT:
# WARNING: for temerature, you must remove -273 after var.wt.mean.anom.NA.bis[p,wt,,] to convert to anomalies in degrees
#          for precipitation, you must multiply it for 86400
index=5
#p=17 # for the debug
#wt=6 # for the debug
slide=FALSE  # put TRUE if you wan to print maps for a slide, with bigger titles and legends, false otherwise
europe=TRUE # put TRUE to show only a zoom over Europe; false otherwise
map.only=FALSE # put TRUE if you want to print only the image with no legend or title (for posters)

domain <- ifelse(europe, "European", "World")

for(p in periods){
  for(wt in 1:n.wts){
    if(!slide) {n.box <- 10; col.cex=1; my.title <- paste(WTs.type10.name[wt],period.name[p])} else {n.box <- 5; col.cex=3; my.title <- ""}
    if(map.only) my.title <- ""
    
    png(filename=paste0(mapdir,"/",var.name.file,"_Mean_",domain,"_Anomalies_of_WT_", WTs.type10[wt],"_",period.name[p],".png"),width=1000,height=700)
    
    layout(matrix(c(rep(1,n.box),2), n.box+1, 1, byrow = TRUE))
    if(!europe) PlotEquiMap(var.wt.mean.anom.NA.bis[p,wt,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], 
                cols=my.cols[[index]], drawleg=F, colNA="gray", toptitle=my.title, sizetit=1.5)
    if(europe) PlotEquiMap(var.wt.mean.anom.NA.bis[p,wt,25:90,220:310], var.lon.used.bis[220:310], var.lat[25:90], filled.continents = FALSE, brks=my.brks[[index]], 
                cols=my.cols[[index]], drawleg=F, colNA="gray", toptitle=my.title, sizetit=1.5)
    ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=2, cex=col.cex)
    if(slide && map.only) title(paste0(WTs.type10.name[wt],period.name[p]),cex.main=3.5,outer=T)
    
    dev.off()
  }
  
}




index=6
wt=6 # for the debug
slide=FALSE  # put TRUE if you wan to print maps for a slide, with bigger titles and legends, false otherwise
europe=FALSE # put TRUE to show only a zoom over Europe; false otherwise

if(!slide) {n.box <- 10; col.cex=1; my.title <- WTs.type10.name[wt]} else {n.box <- 5; col.cex=3; my.title <- ""}
domain <- ifelse(europe, "European", "World")

for(wt in 1:n.wts){
    png(filename=paste0(mapdir,"/StDev_",domain,"_Anomalies_of_WT_", WTs.type10[wt],".png"),width=1000,height=700)
    
    layout(matrix(c(rep(1,n.box),2), n.box+1, 1, byrow = TRUE))
    
    if(!europe) PlotEquiMap(var.wt.sd.anom.NA.bis[p,wt,,], var.lon.used.bis, var.lat, filled.continents = FALSE, brks=my.brks[[index]], 
                cols=my.cols[[index]], drawleg=F, colNA="gray", toptitle=my.title, sizetit=1.5)
    if(europe) PlotEquiMap(var.wt.sd.anom.NA.bis[p,wt,25:90,220:310], var.lon.used.bis[220:310], var.lat[25:90], filled.continents = FALSE, brks=my.brks[[index]], 
                cols=my.cols[[index]], drawleg=F, colNA="gray", toptitle=my.title, sizetit=1.5)
    ColorBar(my.brks[[index]], cols=my.cols[[index]], vert=FALSE, subsampleg=2, cex=col.cex)
    if(slide) title(WTs.type10.name[wt],cex.main=3.5,outer=T)
    
    dev.off()
}

#WTs.type<-c("NE","E","SE","S","SW","W","NW","N","C","C.NE","C.E","C.SE","C.S","C.SW","C.W","C.NW","C.N","A","A.NE","A.E","A.SE","A.S","A.SW","A.W","A.NW","A.N")

save.image(file=paste0(workdir,"/Weather_types",var.name,"_",psl.rean.name,"_",year.start,"-",year.end,".RData"), compress=FALSE)
