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

rm(list=ls())
library(s2dverification)  # for the function Load()
library(abind) # for funcion abind()
source('/home/Earth/ncortesi/scripts/Rfunctions.R') # for the calendar functions

# directory where to find the WT classification
workdir <- "/scratch/Earth/ncortesi/RESILIENCE/WT"

# reanalysis used for daily var data (must be the same as the mslp data):
var.rean <- list(path = '/esnas/recon/ecmwf/erainterim/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
#var.rean <- list(path = '/esnas/recon/noaa/ncep-reanalysis/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$.nc')
rean.name <- 'erai' #'ncep'  #'erai'
    
# any daily variable:
var.name='sfcWind'   #'tas' #'sfcWind' #'prlr'        
var.name.file='10-m Wind Speed'  #'Precipitation' #'Surface_Temperature' # '10-m_Wind_Speed'   # variable name used in the ouput map's title

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

LOESS <- FALSE  # LOESS climatology filter on/off (if off, a 5-days mobile windows is usedto measure daily anomalies)

year.start <- 1985
year.end <- 2016

##########################################################################################
n.periods <- length(periods)
n.years <- year.end - year.start + 1

my.brks <- my.cols <- my.labels <- my.unit <- list()
my.brks.freq <- c(0,0.05,seq(0.1,0.7,0.1),1) # Frequency of a WT or WD
my.cols.freq <-  colorRampPalette(c('#f7fbff','#deebf7','#c6dbef','#9ecae1','#6baed6','#4292c6','#2171b5','#08519c','#08306b'))(length(my.brks.freq)-1)
my.unit.freq <- "%"

if(var.name == 'tas') var.num <- 1
if(var.name == 'sfcWind') var.num <- 2
if(var.name == 'prlr') var.num <- 3

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)")
wd.dir <- c("N","NE","E","SE","S","SW","W","NW")
n.wd <- length(wd.dir)

#year.tot <- year.end - year.start + 1

# load WT metadata to get the info on year.start and year.end
#WTs_files <- list.files(paste0(workdir,"/txt/ERAint/all_years"))
#WTs_file1 <- read.table(file=paste0(workdir,"/txt/ERAint/all_years/",WTs_files[1]), header=TRUE)
#load(paste0(workdir,"/txt/ERAint/metadata.RData")) 

# load daily var data for all years:
var <- Load(var.name, NULL, list(var.rean), paste0(year.start:year.end,'0101'), storefreq = 'daily', leadtimemax=366, output = 'lonlat', nprocs=8)
#var <- Load(var.name, NULL, list(var.rean), paste0(2001,'0101'), storefreq = 'daily', leadtimemax=1, output = 'lonlat', nprocs=8)

var.lat <- var$lat # var lat and lon MUST be the same of WT classification, even if the latter can have NA for certain lat values.
var.lon <- var$lon

n.lat.var <- length(var.lat)
n.lon.var <- length(var.lon)
                 
# remove bisestile days from var to have all arrays with the same dimensions:
cat("Removing bisestile days. Please wait...\n")
var365 <- var$obs[,,,1:365,,,drop=FALSE]

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

rm(var)
gc()

# convert var to daily anomalies:
cat("Calculating anomalies. Please wait...\n")

if(LOESS == TRUE){
        var365ClimDaily <- apply(var365, c(1,2,4,5,6), mean, na.rm=T)
        var365ClimLoess <- var365ClimDaily
        
        for(i in 1:n.lat.var){
            for(j in 1:n.lon.var){
                my.data <- data.frame(ens.mean=var365ClimDaily[1,1,,i,j], day=1:365)
                my.loess <- loess(ens.mean ~ day, my.data, span=0.35)
                var365ClimLoess[1,1,,i,j] <- predict(my.loess)
            }
        }

        rm(var365ClimDaily, my.data, my.loess)
        gc()

        var365Clim <- InsertDim(var365ClimLoess, 3, year.end-year.start+1)

        rm(var365ClimLoess)
        gc()
        
} else { # apply a 5-days mobile window:

        #var365Clim  <- InsertDim(var365ClimDaily, 3, n.years)
        var365Clim <- array(NA,c(365,dim(var365)[5:6]))
        var365x3 <- abind(var365,var365,var365,along=4)

        for(d in 1:365){
           window <- var365x3[1,1,,365+d+seq(-2,2),,drop=FALSE]
           var365Clim[d,,] <- apply(window,c(5,6),mean,na.rm=TRUE)
        }
           
        rm(var365x3)
        gc()

        var365Clim <- InsertDim(var365Clim, 1, n.years)
        var365Clim <- InsertDim(InsertDim(var365Clim,1,1),1,1)
}
    
        
var365Anom <- var365 - var365Clim

rm(var365Clim)
gc()

rm(var365)
gc()

# save var anomalies for retreiving them when necessary:
save(var365Anom,var.lat, var.lon, n.lat.var, n.lon.var, file=paste0(workdir,"/",rean.name,"_",var.name,"365Anom.RData"))









# Compute wind directions:
uas <- Load(var = 'uas', exp = NULL, obs = list(var.rean), paste0(year.start:year.end,'0101'), storefreq = 'daily', leadtimemax=366, output = 'lonlat', nprocs=8)$obs
vas <- Load(var = 'vas', exp = NULL, obs = list(var.rean), paste0(year.start:year.end,'0101'), storefreq = 'daily', leadtimemax=366, output = 'lonlat', nprocs=8)$obs

windir <- (180/pi)*atan2(uas,vas) + 180 # wind direction in degrees; 90 is necessary to shift from trigonometric system to cardinal system and +180 to shift
                                            # from the direction wind is blowing to the direction wind is coming from
rm(uas, vas)
gc()

# remove bisestile days from windir, to compare it with var, which has no bisestiles:
cat("Removing bisestile days. Please wait...\n")
windir365 <- windir[,,,1:365,,,drop=FALSE]

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


rm(windir)
gc()

dir2 <- which(windir365 > 22.5)
dir3 <- which(windir365 > 22.5 + 45)
dir4 <- which(windir365 > 22.5 + 90)
dir5 <- which(windir365 > 22.5 + 135)
dir6 <- which(windir365 > 22.5 + 180)
dir7 <- which(windir365 > 22.5 + 225)
dir8 <- which(windir365 > 22.5 + 270)
dir1 <- which(windir365 > 22.5 + 315)

windirClass <- array(1, dim(windir365)) # N
windirClass[dir2] <- 2 # NE
windirClass[dir3] <- 3 # E
windirClass[dir4] <- 4 # SE
windirClass[dir5] <- 5 # S
windirClass[dir6] <- 6 # SW
windirClass[dir7] <- 7 # W
windirClass[dir8] <- 8 # NW
windirClass[dir1] <- 1 # N

rm(windir365)
rm(dir1,dir2,dir3,dir4,dir5,dir6,dir7,dir8)
gc()

# save it once to retreive it later:
save(windirClass, file=paste0(workdir,"/",rean.name,"_windirClass.RData"))

n.lonr <- n.lon.var - ceiling(length(var.lon[var.lon>0 & var.lon < 180])) # count the num of lon values between 180 and 360 degrees
lon.swapped <- c((n.lonr+1):n.lon.var,1:n.lonr) # lon is always positive but rearranged
var.lon2 <-  c(var.lon[c((n.lonr+1):n.lon.var)]-360,var.lon[1:n.lonr]) # to have lon values from -180 to 180

windirFreqInter <- array(NA, c(n.wd, length(periods), n.years, n.lat.var, n.lon.var)) # array where to store the interannual frequencies

# save wind direction mean frequency maps:
for(p in periods){
    # select wind direction data only inside period p:
    windirPeriod <- windirClass[1,1,,pos.period(1,p),,]

    for(wd in 1:8){
  
      # remove from windPeriod the days not belonging to that wd (setting its value to NA):
      windirPeriod.wt <- windirPeriod
        
      ss <- which(windirPeriod == wd)
      pp <- which(windirPeriod != wd)
        
      windirPeriod.wt[ss] <- 1   # set to 1 the days belonging to that wt
      windirPeriod.wt[pp] <- NA  # set to NA the days not belonging to that wt
      rm(ss,pp)
      gc()
          
      # mean frequency maps:
      windirDays <- apply(windirPeriod.wt, c(3,4), sum, na.rm=TRUE)
      windirFreq <- windirDays / (n.years * n.days.in.a.period(p,1))

      my.fileout <- paste0(workdir,"/windir/",rean.name,"_frequency_",period.name[p],"_direction_",wd.dir[wd],".png")
        
      png(filename=my.fileout,width=900,height=600)
 
      layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
      PlotEquiMap(windirFreq[,lon.swapped], var.lon2, var.lat, filled.continents = FALSE, brks=my.brks.freq, cols=my.cols.freq, colNA="gray", drawleg=F)
      ColorBar2(my.brks.freq, cols=my.cols.freq, vert=FALSE, cex=1, my.ticks=-0.5 + 1:length(my.brks.freq), my.labels=100*my.brks.freq, label.dist=.5)
      dev.off()

      # mean frequency maps for North Pole:
      my.fileout <- paste0(workdir,"/windir/",rean.name,"_frequency_polar_",period.name[p],"_direction_",wd.dir[wd],".png")
      PlotStereoMap(windirFreq, var.lon, var.lat, latlims = c(60,90), brks=my.brks.freq, cols=my.cols.freq, subsampleg=1, units=my.unit.freq, colNA="gray", fileout=my.fileout)
      # measure the interannual frequency series of that wind directions (for each grid point):
      windirFreqInter[wd,p,,,] <- apply(windirPeriod.wt,c(1,3,4), sum, na.rm=TRUE) /  n.days.in.a.period(p,1)
        
    } # close for on wd
    
} # close for on p

save(windirFreqInter, file=paste0(workdir,"/",rean.name,"_windirFreqInter.RData"))








# save impact map of wind direction on var (average of var only during the days belonging to a particular wind direction):

# load var anomalies and wind direction array:
load(file=paste0(workdir,"/",rean.name,"_",var.name,"365Anom.RData"))
load(paste0(workdir,"/",rean.name,"_windirClass.RData"))

#my.brks[[1]] <- c(-100,seq(-8,8,1),100) # % Mean anomaly of a WT for temperature
my.brks[[1]] <- seq(-10,10,1) # % Mean anomaly of a WT for temperature
my.labels[[1]] <- my.brks[[1]] #c(c("-10",my.brks[[1]][-1])[-length(my.brks[[1]])],"10")
my.cols[[1]] <-  colorRampPalette(as.character(read.csv("~/scripts/palettes/rgbhex.csv",header=F)[,1]))(length(my.brks[[1]])-1)  # blue--yellow-red colors for temperature
my.unit[[1]] <- "degrees"

n.lonr <- n.lon.var - ceiling(length(var.lon[var.lon>0 & var.lon < 180])) # count the num of lon values between 180 and 360 degrees
lon.swapped <- c((n.lonr+1):n.lon.var,1:n.lonr) # lon is always positive but rearranged
var.lon2 <-  c(var.lon[c((n.lonr+1):n.lon.var)]-360,var.lon[1:n.lonr]) # to have lon values from -180 to 180

impact.var <- array(NA, c(n.wd, length(periods), n.lat.var, n.lon.var)) # array where to save the impact of each wd on var

for(p in periods){
    # select var data only inside period p:
    varPeriod <- var365Anom[1,1,,pos.period(1,p),,]

    # select wind direction data only inside period p:
    windirPeriod <- windirClass[1,1,,pos.period(1,p),,]

    for(wd in 1:8){
  
      # remove from varPeriod the days not belonging to that wd (setting its value to NA):
      windirPeriod.wt <- windirPeriod
        
      ss <- which(windirPeriod == wd)
      pp <- which(windirPeriod != wd)
        
      windirPeriod.wt[ss] <- 1   # set to 1 the days belonging to that wt
      windirPeriod.wt[pp] <- NA  # set to NA the days not belonging to that wt
      rm(ss,pp)
      gc()
        
      varPeriod.wt <- varPeriod * windirPeriod.wt
    
      var.mean.wt <- apply(varPeriod.wt, c(3,4), mean, na.rm=TRUE)

      impact.var[wd,p,,] <- var.mean.wt
        
      # visualize impact map of the wd on var:
      my.fileout <- paste0(workdir,"/windir/",rean.name,"_impact_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png")
        
      png(filename=my.fileout,width=900,height=600)
        
      layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
      #PlotEquiMap(var.mean.wt, var.lon, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], drawleg=F, colNA="gray")
      PlotEquiMap(rescale(var.mean.wt[,lon.swapped],-10,10), var.lon2, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], colNA="gray", drawleg=F)
      ColorBar2(my.brks[[var.num]], cols=my.cols[[var.num]], vert=FALSE, cex=1, my.ticks=-0.5 + 1:length(my.brks[[var.num]]), my.labels=my.labels[[var.num]], label.dist=.5)
      dev.off()

      # North Pole map:
      #png(filename=paste0(workdir,"/windir/impact_polar_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png"))
      my.fileout <- paste0(workdir,"/windir/",rean.name,"_impact_polar_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png")
      PlotStereoMap(rescale(var.mean.wt,-10,10), var.lon, var.lat, latlims = c(60,90), brks=my.brks[[var.num]], cols=my.cols[[var.num]], subsampleg=1, units=my.unit[[var.num]], colNA="gray", fileout=my.fileout)

      # 
      # same maps as above but removing points with frequency < 3%:
      #
        
      windirFreq.wt <- apply(windirPeriod.wt, c(3,4), sum, na.rm=TRUE) / (n.years * n.days.in.a.period(p,1))

      ss <- which(windirFreq.wt < 0.03)
      pp <- which(windirFreq.wt >= 0.03)
        
      windirFreq.wt[ss] <- NA
      windirFreq.wt[pp] <- 1
  
      var.mean.wt2 <- var.mean.wt * windirFreq.wt  
      rm(ss,windirFreq.wt)
      gc()
        
      # visualize impact map of the wd on var:
      my.fileout <- paste0(workdir,"/windir/",rean.name,"_impact_min3percent_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png")
        
      png(filename=my.fileout,width=900,height=600)
        
      layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
      #PlotEquiMap(var.mean.wt, var.lon, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], drawleg=F, colNA="gray")
      PlotEquiMap(rescale(var.mean.wt2[,lon.swapped],-10,10), var.lon2, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], colNA="gray", drawleg=F)
      ColorBar2(my.brks[[var.num]], cols=my.cols[[var.num]], vert=FALSE, cex=1, my.ticks=-0.5 + 1:length(my.brks[[var.num]]), my.labels=my.labels[[var.num]], label.dist=.5)
      dev.off()

      # North Pole map:
      #png(filename=paste0(workdir,"/windir/impact_polar_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png"))
      my.fileout <- paste0(workdir,"/windir/",rean.name,"_impact_min3percent_polar_",var.name,"_",period.name[p],"_direction_",wd.dir[wd],".png")
      PlotStereoMap(rescale(var.mean.wt2,-10,10), var.lon, var.lat, latlims = c(60,90), brks=my.brks[[var.num]], cols=my.cols[[var.num]], subsampleg=1, units=my.unit[[var.num]], colNA="gray", fileout=my.fileout)

      rm(var.mean.wt, var.mean.wt2)
    
    } # close for on wd
    
} # close for on p

save(impact.var, file=paste0(workdir,"/",rean.name,"_",var.name,"_impact.RData"))





# choose an year and a month to plot the simulated impact maps with the WDs:
my.year <- 2014
my.month <- 9

load(paste0(workdir,"/",rean.name,"_windirFreqInter.RData"))
load(paste0(workdir,"/",rean.name,"_",var.name,"_impact.RData"))


impact.var2 <- InsertDim(impact.var, 2, n.years)

impact.weighted <- impact.var2 * windirFreqInter

impact.total <- apply(impact.weighted, c(2,3,4), sum, na.rm=FALSE)









# Create and save monthly/seasonal/yearly climatology maps of var:

#p=17 # for the debug

# Map intervals and colors:
my.brks[[1]] <- c(-50,-40,-30,seq(-20,30,0.05),40,50)  # Surface Temperature Climatology
my.brks[[2]] <- c(0,seq(0.05,10,0.05),15)  # Precipitation Climatology
my.brks[[3]] <- c(0,seq(1.05,10,0.05),15)  # Wind speed Climatology

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("white","gray","cyan2","blue","deeppink4"))(length(my.brks[[1]])-1)  # white-gray-blue-purple colores for wind speed

n.lonr <- n.lon.var - ceiling(length(var.lon[var.lon>0 & var.lon < 180])) # count the num of lon values between 180 and 360 degrees
lon.swapped <- c((n.lonr+1):n.lon.var,1:n.lonr) # lon is always positive but rearranged
var.lon2 <-  c(var.lon[c((n.lonr+1):n.lon.var)]-360,var.lon[1:n.lonr]) # to have lon values from -180 to 180

mod.var <- 0
if(var.num == 1) mod.var <- -273.5     # for temperature, you must remove -273 after var.clim.NA.bis[p,,]:

for(p in periods){
    # Select only days of the chosen month/season:
    varPeriod <- var365[,,,pos.period(1,p),,,drop=FALSE]
    varPeriodMean <- apply(varPeriod,c(1,2,5,6),mean,na.rm=TRUE) 

    png(filename=paste0(workdir,"/",var.name.file,"_Climatology_",period.name[p],".png"),width=1000,height=700)

    layout(matrix(c(rep(1,10),2), 11, 1, byrow = TRUE))
       
    #PlotEquiMap(varPeriodMean[1,1,,]+mod.var, var.lon, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], drawleg=F, colNA="gray")
    PlotEquiMap(varPeriodMean[1,1,,lon.swapped]+mod.num, var.lon2, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], drawleg=F)
    ColorBar(my.brks[[var.num]], cols=my.cols[[var.num]], vert=FALSE, subsampleg=20, cex=1)

    dev.off()
}






# Impact of a WT on var (average of var only during the days belonging to a particular WT):

# load lat and lon from SLP grid:
load(paste0(workdir,"/txt/",mslp.rean.name,"/metadata.RData"))  # load lat.used and lon.used

# only for compatibility with older versions (it should be already loaded):
lat <- round(MSLP$lat,3)
lon <- round(MSLP$lon,3)

# load daily WT classification for all years and grid points and with the same format of var365Anom:
load(paste0(workdir,"/txt/",mslp.rean.name,"/WTs.RData"))  # load lat.used and lon.used

n.WTs <- 10 # length(unique(WTs))
wt.codes <- unique(WTs[1,1,,])

# load var anomalies:
load(file=paste0(workdir,"/",rean.name,"_",var.name,"365Anom.RData"))

my.brks[[1]] <- c(-100,seq(-8,8,1),100) # % Mean anomaly of a WT for temperature
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 for temperature

for(p in periods){
    # select var data only inside period p:
    varPeriod <- var365Anom[,pos.period(1,p),,]

    # select WT data only inside period p:
    WTsPeriod <- WTs[,pos.period(1,p),,]

    for(wt in 1:n.WTs){

      wt.code <- wt.codes[wt] # i.e: for 10 WTs, their codes are. 1 2 3 4 5 6 7 8 9 18
  
      # remove from varPeriod the days not belonging to that wt (setting its value to NA):
      WTsPeriod.wt <- WTsPeriod
      ss <- which(WTsPeriod == wt.code)
      pp <- which(WTPeriod != wt.code)
        
      WTsPeriod.wt[ss] <- 1   # set to 1 the days belonging to that wt
      WTsPeriod.wt[pp] <- NA  # set to NA the days not belonging to that wt
        
      varPeriod.wt <- varPeriod * WTsPeriod.wt
    
      var.mean.wt <- apply(varPeriod.wt, c(3,4), mean, na.rm=TRUE)
          
      # visualize impact map of the wt on var:
      png(filename=paste0(workdir,"/",var.name.file,"/impact/",var.name,"_"period.name[p],".png"),width=1000,height=700)

      PlotEquiMap(var.mean.wt, var.lon, var.lat, filled.continents = FALSE, brks=my.brks[[var.num]], cols=my.cols[[var.num]], drawleg=F, colNA="gray")


        
    }
}





















    


#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


