library(CSTools)

# Winter temperature:
mth = '11'
clim_var = 'tas'
ini <- 1992
fin <- 2012
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
grid <- "256x128"
obs <- "erainterim"

require(zeallot)
glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'
c(exp, obs) %<-% 
   CST_Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5), 
                                  list(name = 'ecmwf/system5c3s'),
                                  list(name = 'meteofrance/system6_m1'),
                                  list(name = 'dwd/system2_m1')),
                  obs = obs, sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                  lonmin = 230, lonmax = 310, latmin = 15, latmax = 70, 
                  storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
                  output = "lonlat", method = "bilinear", 
                  grid = paste("r", grid, sep = ""))
c(ano_exp, ano_obs) %<-% CST_Anomaly(exp = exp, obs = obs, cross = TRUE, memb = TRUE)
AnomDJF <- CST_MultiMetric(exp = ano_exp, obs = ano_obs, metric = 'correlation', 
                          multimodel = TRUE)
corre <- AnomDJF$data[ , , 2, , ]
names(dim(corre)) <- c("maps", "lat", "lon")
Lat <- exp$lat
Lon <- exp$lon
PlotCombinedMap(corre, lon = Lon, lat = Lat, map_select_fun = max,
                display_range = c(0, 1), map_dim = 'maps',
                legend_scale = 0.5, brks = 11,
                cols = list(c('white', 'darkblue'),
                            c('white', 'darkred'),
                            c('white', 'darkorange'),
                            c('white', 'darkgreen'),
                            c('white', 'black')),
                bar_titles = c(names(AnomDJF$Datasets)[-5], "MMM"),
            fileout = "/esarchive/scratch/nperez/MultiModelSkill_cor_tas_1992-2012.png",
                width = 14, height = 8)    

rm(list=ls())


# Winter precipitation
library(CSTools)
mth = '11'
clim_var = 'prlr'
ini <- 1992
fin <- 2012
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
grid <- "256x128"
obs <- "erainterim"

require(zeallot)
glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'
c(exp, obs) %<-%
   CST_Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5),
                                  list(name = 'ecmwf/system5c3s'),
                                  list(name = 'meteofrance/system6_m1'),
                                  list(name = 'dwd/system2_m1')),
                  obs = obs, sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                  lonmin = 230, lonmax = 310, latmin = 15, latmax = 70,
                  storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
                  output = "lonlat", method = "bilinear", 
                  grid = paste("r", grid, sep = ""))
c(ano_exp, ano_obs) %<-% CST_Anomaly(exp = exp, obs = obs, cross = TRUE, memb = TRUE)
AnomDJF <- CST_MultiMetric(exp = ano_exp, obs = ano_obs, metric = 'correlation',
                          multimodel = TRUE)
corre <- AnomDJF$data[ , , 2, , ]
names(dim(corre)) <- c("maps", "lat", "lon")
Lat <- exp$lat
Lon <- exp$lon
PlotCombinedMap(corre, lon = Lon, lat = Lat, map_select_fun = max,
                display_range = c(0, 1), map_dim = 'maps',
                legend_scale = 0.5, brks = 11,
                cols = list(c('white', 'darkblue'),
                            c('white', 'darkred'),
                            c('white', 'darkorange'),
                            c('white', 'darkgreen'),
                            c('white', 'black')),
                bar_titles = c(names(AnomDJF$Datasets)[-5], "MMM"),
            fileout = "/esarchive/scratch/nperez/MultiModelSkill_cor_precip_1992-2012.png",
                width = 14, height = 8)    





# Summer temperature:

mth = '05'
clim_var = 'tas'
ini <- 1992
fin <- 2012
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
grid <- "256x128"
obs <- "erainterim"

glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'
c(exp, obs) %<-% 
   CST_Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5), 
                                  list(name = 'ecmwf/system5c3s'),
                                  list(name = 'meteofrance/system6_m1'),
                                  list(name = 'dwd/system2_m1')),
                  obs = obs, sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                  lonmin = 230, lonmax = 310, latmin = 15, latmax = 70, 
                  storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
                  output = "lonlat", method = "bilinear", 
                  grid = paste("r", grid, sep = ""))

c(ano_exp, ano_obs) %<-% CST_Anomaly(exp = exp, obs = obs, cross = TRUE, memb = TRUE)
AnomJJA <- CST_MultiMetric(exp = ano_exp, obs = ano_obs, metric = 'correlation',
                          multimodel = TRUE)
corre <- AnomJJA$data[ , , 2, , ]
names(dim(corre)) <- c("maps", "lat", "lon")
Lat <- exp$lat
Lon <- exp$lon
PlotCombinedMap(corre, lon = Lon, lat = Lat, map_select_fun = max,
                display_range = c(0, 1), map_dim = 'maps',
                legend_scale = 0.5, brks = 11,
                cols = list(c('white', 'darkblue'),
                            c('white', 'darkred'),
                            c('white', 'darkorange'),
                            c('white', 'darkgreen'),
                            c('white', 'black')),
                bar_titles = c(names(AnomJJA$Datasets)[-5], "MMM"),
            fileout = "/esarchive/scratch/nperez/MultiModelSkill_JJA_cor_tas_1992-2012.png",
                width = 14, height = 8)

rm(list=ls())



# Summer precipitation:

mth = '05'
clim_var = 'prlr'
ini <- 1992
fin <- 2012
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
grid <- "256x128"
obs <- "erainterim"

glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'
c(exp, obs) %<-%
   CST_Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5),
                                  list(name = 'ecmwf/system5c3s'),
                                  list(name = 'meteofrance/system6_m1'),
                                  list(name = 'dwd/system2_m1')),
                  obs = obs, sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                  lonmin = 230, lonmax = 310, latmin = 15, latmax = 70,
                  storefreq = "monthly", sampleperiod = 1,  nmember = 9,
                  output = "lonlat", method = "bilinear",
                  grid = paste("r", grid, sep = ""))

c(ano_exp, ano_obs) %<-% CST_Anomaly(exp = exp, obs = obs, cross = TRUE, memb = TRUE)
AnomJJA <- CST_MultiMetric(exp = ano_exp, obs = ano_obs, metric = 'correlation',
                          multimodel = TRUE)
corre <- AnomJJA$data[ , , 2, , ]
names(dim(corre)) <- c("maps", "lat", "lon")
Lat <- exp$lat
Lon <- exp$lon
PlotCombinedMap(corre, lon = Lon, lat = Lat, map_select_fun = max,
                display_range = c(0, 1), map_dim = 'maps',
                legend_scale = 0.5, brks = 11,
                cols = list(c('white', 'darkblue'),
                            c('white', 'darkred'),
                            c('white', 'darkorange'),
                            c('white', 'darkgreen'),
                            c('white', 'black')),
                bar_titles = c(names(AnomJJA$Datasets)[-5], "MMM"),
            fileout = "/esarchive/scratch/nperez/MultiModelSkill_JJA_cor_precip_1992-2012.png",
                width = 14, height = 8)
