library(reshape2)
library(ggplot2)

load("R2_em_DJF.RData")
load("R2_em_MAM.RData")
load("R2_em_JJA.RData")
load("R2_em_SON.RData")
load("R2_em.RData")

load("slope_em_DJF.RData")
load("slope_em_MAM.RData")
load("slope_em_JJA.RData")
load("slope_em_SON.RData")
load("slope_em.RData")

load("RMSE_em_DJF.RData")
load("RMSE_em_MAM.RData")
load("RMSE_em_JJA.RData")
load("RMSE_em_SON.RData")
load("RMSE_em.RData")


head(R2_em_DJF)
slope <- slope_em_SON
R2 <- R2_em_SON
RMSE <- RMSE_em_SON
dim(R2)
max(R2)
#which(R2 == max(R2), arr.ind = TRUE)


od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))
#wind_steps <- seq(from = 0, to = 1.5, by = 0.025)
wind_steps <- seq(from = 0, to = 9, by = 0.05)
length(wind_steps)
length(od_steps)

rownames(R2) <- as.numeric(em_steps)
rownames(slope) <- as.numeric(em_steps)
rownames(RMSE) <- as.numeric(em_steps)
rownames(R2)
colnames(R2) <- as.numeric(od_steps)
colnames(slope) <- as.numeric(od_steps)
colnames(RMSE) <- as.numeric(od_steps)
colnames(R2)
#----------------------------------------------------
# em_steps2 <- em_steps[1:55]
# for (n in 1:128)
# {
#   em_steps2 <- c(em_steps2,em_steps[1:55])
# }
# length(em_steps2)
# #----------------------------------------------------
# od_steps2 <- vector(length=7095)
# for (n in 1:129)
# {
#   for (s in 1:55)
#   {
#     od_steps2[s+(n-1)*55] <- od_steps[n]
#   }
# }
#----------------------------------------------------
longData<-melt(R2[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData$Var1=as.numeric(longData$Var1)
longData$Var2=as.numeric(longData$Var2)
#head(longData$Var1)
dim(longData)
head(longData)
#longData[,1]
#longData[,2] <- od_steps2
#longData[,2]
# 
# heatplot(R2)

hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
hex <- c("#FF0000", "#FF6600", "#FFFF33", "#00FF00","#00FFFF", "#0000FF", "#9900CC")

#hex <- c("#EF0515","#EE800A","#D8ED0E","#59ED12","#17EC4E","#1BECCA","#1F96EB","#2323EB")
#hex <- c("#A00842", "#AE3055", "#BC5968", "#CA827B", "#D8AA8E", "#E6D3A1", "#F5FCB5")
png(filename="R2_em_SON.png", width = 400, height = 300)
myplot1 <- ggplot(longData, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,1), name="R²") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green", midpoint = 0.5,
  #   limit = c(0,1), space = "Lab",
  #   breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1),
  #   name="R²")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - SON") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


longData2<-melt(slope[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="slope_em_SON.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,400), name="Slope [m²/kg]") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - SON") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


longData2<-melt(RMSE[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="RMSE_em_SON.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,0.01), name="RMSE") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - SON") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()

#------------------------------------------------------------------

slope <- slope_em_DJF
R2 <- R2_em_DJF
RMSE <- RMSE_em_DJF
dim(R2)
max(R2)
#which(R2 == max(R2), arr.ind = TRUE)

length(wind_steps)
length(od_steps)

rownames(R2) <- as.numeric(em_steps)
rownames(slope) <- as.numeric(em_steps)
rownames(RMSE) <- as.numeric(em_steps)
rownames(R2)
colnames(R2) <- as.numeric(od_steps)
colnames(slope) <- as.numeric(od_steps)
colnames(RMSE) <- as.numeric(od_steps)
colnames(R2)
#----------------------------------------------------
# em_steps2 <- em_steps[1:55]
# for (n in 1:128)
# {
#   em_steps2 <- c(em_steps2,em_steps[1:55])
# }
# length(em_steps2)
# #----------------------------------------------------
# od_steps2 <- vector(length=7095)
# for (n in 1:129)
# {
#   for (s in 1:55)
#   {
#     od_steps2[s+(n-1)*55] <- od_steps[n]
#   }
# }
#----------------------------------------------------
longData<-melt(R2[38:87,24:45])
#longData<-longData[longData$value!=0,]
longData$Var1=as.numeric(longData$Var1)
longData$Var2=as.numeric(longData$Var2)
#head(longData$Var1)
dim(longData)
head(longData)
#longData[,1]
#longData[,2] <- od_steps2
#longData[,2]
# 
# heatplot(R2)

hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
hex <- c("#FF0000", "#FF6600", "#FFFF33", "#00FF00","#00FFFF", "#0000FF", "#9900CC")

#hex <- c("#EF0515","#EE800A","#D8ED0E","#59ED12","#17EC4E","#1BECCA","#1F96EB","#2323EB")
#hex <- c("#A00842", "#AE3055", "#BC5968", "#CA827B", "#D8AA8E", "#E6D3A1", "#F5FCB5")
png(filename="R2_em_DJF.png", width = 400, height = 300)
myplot1 <- ggplot(longData, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,1), name="R²") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green", midpoint = 0.5,
  #   limit = c(0,1), space = "Lab",
  #   breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1),
  #   name="R²")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - DJF") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


longData2<-melt(slope[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="slope_em_DJF.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,400), name="Slope [m²/kg]") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - DJF") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()

longData2<-melt(RMSE[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="RMSE_em_DJF.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,0.01), name="RMSE") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - DJF") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()




#-----------------------------------------------------------------------------------------
slope <- slope_em_MAM
R2 <- R2_em_MAM
dim(R2)
max(R2)
#which(R2 == max(R2), arr.ind = TRUE)




rownames(R2) <- as.numeric(em_steps)
rownames(slope) <- as.numeric(em_steps)
rownames(R2)
colnames(R2) <- as.numeric(od_steps)
colnames(slope) <- as.numeric(od_steps)
colnames(R2)
#----------------------------------------------------
# em_steps2 <- em_steps[1:55]
# for (n in 1:128)
# {
#   em_steps2 <- c(em_steps2,em_steps[1:55])
# }
# length(em_steps2)
# #----------------------------------------------------
# od_steps2 <- vector(length=7095)
# for (n in 1:129)
# {
#   for (s in 1:55)
#   {
#     od_steps2[s+(n-1)*55] <- od_steps[n]
#   }
# }
#----------------------------------------------------
longData<-melt(R2[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData$Var1=as.numeric(longData$Var1)
longData$Var2=as.numeric(longData$Var2)
#head(longData$Var1)
dim(longData)
head(longData)
#longData[,1]
#longData[,2] <- od_steps2
#longData[,2]
# 
# heatplot(R2)

hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
hex <- c("#FF0000", "#FF6600", "#FFFF33", "#00FF00","#00FFFF", "#0000FF", "#9900CC")

#hex <- c("#EF0515","#EE800A","#D8ED0E","#59ED12","#17EC4E","#1BECCA","#1F96EB","#2323EB")
#hex <- c("#A00842", "#AE3055", "#BC5968", "#CA827B", "#D8AA8E", "#E6D3A1", "#F5FCB5")
png(filename="R2_em_MAM.png", width = 400, height = 300)
myplot1 <- ggplot(longData, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,1), name="R²") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green", midpoint = 0.5,
  #   limit = c(0,1), space = "Lab",
  #   breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1),
  #   name="R²")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - MAM") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


longData2<-melt(slope[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="slope_em_MAM.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,400), name="Slope [m²/kg]") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - MAM") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


#------------------------------------------------------------------------------

slope <- slope_em_JJA
R2 <- R2_em_JJA
dim(R2)
max(R2)
#which(R2 == max(R2), arr.ind = TRUE)




rownames(R2) <- as.numeric(em_steps)
rownames(slope) <- as.numeric(em_steps)
rownames(R2)
colnames(R2) <- as.numeric(od_steps)
colnames(slope) <- as.numeric(od_steps)
colnames(R2)
#----------------------------------------------------
# em_steps2 <- em_steps[1:55]
# for (n in 1:128)
# {
#   em_steps2 <- c(em_steps2,em_steps[1:55])
# }
# length(em_steps2)
# #----------------------------------------------------
# od_steps2 <- vector(length=7095)
# for (n in 1:129)
# {
#   for (s in 1:55)
#   {
#     od_steps2[s+(n-1)*55] <- od_steps[n]
#   }
# }
#----------------------------------------------------
longData<-melt(R2[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData$Var1=as.numeric(longData$Var1)
longData$Var2=as.numeric(longData$Var2)
#head(longData$Var1)
dim(longData)
head(longData)
#longData[,1]
#longData[,2] <- od_steps2
#longData[,2]
# 
# heatplot(R2)

hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
hex <- c("#FF0000", "#FF6600", "#FFFF33", "#00FF00","#00FFFF", "#0000FF", "#9900CC")

#hex <- c("#EF0515","#EE800A","#D8ED0E","#59ED12","#17EC4E","#1BECCA","#1F96EB","#2323EB")
#hex <- c("#A00842", "#AE3055", "#BC5968", "#CA827B", "#D8AA8E", "#E6D3A1", "#F5FCB5")
png(filename="R2_em_JJA.png",  width = 400, height = 300)
myplot1 <- ggplot(longData, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,1), name="R²") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green", midpoint = 0.5,
  #   limit = c(0,1), space = "Lab",
  #   breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1),
  #   name="R²")  +
  scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - JJA") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


longData2<-melt(slope[1:92,1:47])
#longData<-longData[longData$value!=0,]
longData2$Var1=as.numeric(longData2$Var1)
longData2$Var2=as.numeric(longData2$Var2)

png(filename="slope_em_JJA.png", width = 400, height = 300)
myplot1 <- ggplot(longData2, aes(x = as.factor(Var1), y = as.factor(Var2))) +
  geom_raster(aes(fill=value)) + coord_equal(expand = F) +
  scale_fill_gradientn(colours = rev(hex),limit = c(0,400), name="Slope [m²/kg]") +
  # scale_fill_gradient2(
  #   low = "red", mid = "yellow", high = "green",  midpoint = 250,
  #   limit = c(0,400),
  #   space = "Lab",
  #   name="Slope [m²/kg]")  +
   scale_x_discrete(breaks = c("0", "1e-10","1e-09","1e-08","1e-07","1e-06","1e-05","1e-04","0.001","0.01","0.1","1") )+
  scale_y_discrete(breaks = c("1e-04", "0.001","0.01","0.1", "1","10") )+
  labs(y="DOD threshold", x="emission threshold [kg/m²]", title="North Africa - JJA") +
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) +
  theme_bw() + theme(axis.text.x=element_text(size=15, angle=90, vjust=0.3),
                     axis.text.y=element_text(size=15),
                     plot.title=element_text(size=17),
                     legend.text = element_text(size=15),
                     axis.title=element_text(size=16),
                     legend.title=element_text(size=16))
print(myplot1)
dev.off()


# length(longData$Var1)
# length(longData$Var2)
# length(longData$value)
# raster <- ggplot(longData, aes(x = as.factor(Var2), y = as.factor(Var1), z=value)) +
#   geom_raster(aes(fill = value), alpha = 1) + coord_equal(expand = F) +
#   scale_fill_gradient(low = "red", high = "green")+
#   #geom_contour(colour = "white", binwidth = 0.2) +
#   scale_x_discrete(breaks = c("0","1e-14","1e-13","1e-12","1e-11","1e-10","1e-9","1e-8","1") )+
#   scale_y_discrete(breaks = c("0","1e-04","0.001","1e-2","1e-1","1e-0") )+
#   labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')
# raster


warning()