library(reshape2)
library(ggplot2)


load("R2_em.RData")
load("R2_em_MAM.RData")
load("R2_em_JJA.RData")
load("R2_em_SON.RData")
load("R2_em_DJF.RData")
load("R2_em_January.RData")
load("R2_em_February.RData")
load("R2_em_March.RData")
load("R2_em_April.RData")
load("R2_em_May.RData")
load("R2_em_June.RData")
load("R2_em_July.RData")
load("R2_em_August.RData")
load("R2_em_Setember.RData")
load("R2_em_October.RData")
load("R2_em_November.RData")
load("R2_em_December.RData")

load("slope_em.RData")
load("slope_em_MAM.RData")
load("slope_em_JJA.RData")
load("slope_em_SON.RData")
load("slope_em_DJF.RData")
load("slope_em_January.RData")
load("slope_em_February.RData")
load("slope_em_March.RData")
load("slope_em_April.RData")
load("slope_em_May.RData")
load("slope_em_June.RData")
load("slope_em_July.RData")
load("slope_em_August.RData")
load("slope_em_Setember.RData")
load("slope_em_October.RData")
load("slope_em_November.RData")
load("slope_em_December.RData")


load("intercept_em.RData")
load("intercept_em_MAM.RData")
load("intercept_em_JJA.RData")
load("intercept_em_SON.RData")
load("intercept_em_DJF.RData")
load("intercept_em_January.RData")
load("intercept_em_February.RData")
load("intercept_em_March.RData")
load("intercept_em_April.RData")
load("intercept_em_May.RData")
load("intercept_em_June.RData")
load("intercept_em_July.RData")
load("intercept_em_August.RData")
load("intercept_em_Setember.RData")
load("intercept_em_October.RData")
load("intercept_em_November.RData")
load("intercept_em_December.RData")




load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")
load("NorthAfrica_ustar.RData")


dim(R2_em_MAM)
dim(R2_em_DJF)
dim(R2_em_JJA)
dim(R2_em_SON)

# R2 <- replace(R2,which(R2==1),-999)
# R2[104,139] <- -999
# 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)))

# R2[90:99,]
# em_steps[104]
# od_steps[139]
# em_steps[97]
# od_steps[102]

#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(4,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,476,by=8),])))
vectorod1 <- c((as.vector(NorthAfrica_od[seq(2684,2924,by=8),])))
vectorem1 <- c((as.vector(NorthAfrica_em[seq(2684,2924,by=8),])))
vectorod <- c(vectorod1,vectorod)
vectorem <- c(vectorem1,vectorem)


NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc <- vector(length=(length(em_steps)))
for (n in 1:length(em_steps))
{
  vectoref <- vectorod[(vectorem>em_steps[n])]
  perc[n] <- length(vectoref)/length(NorthAfrica_od0)
}
perc
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(484,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,1212,by=8),])))


NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc2 <- vector(length=(length(em_steps)))
for (n in 1:length(em_steps))
{
  vectoref <- vectorod[(vectorem>em_steps[n])]
  perc2[n] <- length(vectoref)/length(NorthAfrica_od0)
}
perc2
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_ustar[seq(1220,1948,by=8),])))

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc3 <- vector(length=(length(em_steps)))
for (n in 1:length(em_steps))
{
  vectoref <- vectorod[(vectorem>em_steps[n])]
  perc3[n] <- length(vectoref)/length(NorthAfrica_od0)
}
perc3
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_ustar[seq(1956,2676,by=8),])))

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc4 <- vector(length=(length(em_steps)))
for (n in 1:length(em_steps))
{
  vectoref <- vectorod[(vectorem>em_steps[n])]
  perc4[n] <- length(vectoref)/length(NorthAfrica_od0)
}
perc4
#---------------------------------------------------------------







R2_em_JJA <- replace(R2_em_JJA,which(R2_em_JJA==1),-999)
which(R2_em_JJA == max(R2_em_JJA), arr.ind = TRUE)

R2_em_year <- as.vector(R2_em[,1])
head(R2_em_year)
data <- data.frame(R2_em_year,R2_em_DJF[,1],R2_em_MAM[,1],R2_em_JJA[,1],R2_em_SON[,1],
                   # R2_em_January[,1],R2_em_February[,1],
                   # R2_em_December[,1],R2_em_March[,1],R2_em_April[,1],R2_em_May[,1],R2_em_June[,1],R2_em_July[,1],R2_em_August[,1],
                   # R2_em_Setember[,1], R2_em_October[,1],R2_em_November[,1],
                   em_steps,perc,perc2,perc3,perc4)
length(R2_em[,1])
length(em_steps)
png("R2_em_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =R2_em_year, colour="Annual")) + geom_line() + 
  geom_line(aes(x=em_steps,y=R2_em_DJF[,1],colour="DJF"),linetype = "solid",size=1) +
  geom_line(aes(x=em_steps,y=R2_em_MAM[,1],colour="MAM"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_JJA[,1],colour="JJA"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_SON[,1],colour="SON"),linetype = "solid",size=1)+
  # geom_line(aes(x=em_steps,y=perc,colour="DJF"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc2,colour="MAM"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc3,colour="JJA"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc4,colour="SON"),linetype = "dotted",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("Annual", "DJF","MAM","JJA","SON"),
                      values = c("#000000", "#E69F00","#009E73","#56B4E9","#F0E442")) +
  labs(x="Emission Threshold [kg/m²]", y="R²") +  
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  #scale_y_continuous(limits=c(0,1), sec.axis = sec_axis(~.*1, name = "Emission Events Considered [%]",
  # labels = function(b) { paste0(round(b * 100, 0), "%")}))+
  
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("R2_em_DJF_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =R2_em_DJF[,1], colour="DJF")) + geom_line() +
  geom_line(aes(x=em_steps,y=R2_em_January[,1],colour="January"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_February[,1],colour="February"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_December[,1],colour="December"),linetype = "twodash",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("DJF", "January","February","December"),
                      values = c("#56B4E9","#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="R²") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("R2_em_MAM_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =R2_em_MAM[,1], colour="MAM")) + geom_line() +
  geom_line(aes(x=em_steps,y=R2_em_March[,1],colour="March"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_April[,1],colour="April"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_May[,1],colour="May"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("MAM", "March","April","May"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="R²") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("R2_em_JJA_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =R2_em_JJA[,1], colour="JJA")) + geom_line() +
  geom_line(aes(x=em_steps,y=R2_em_June[,1],colour="June"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_July[,1],colour="July"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_August[,1],colour="August"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("JJA", "June","July","August"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="R²") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("R2_em_SON_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =R2_em_SON[,1], colour="SON")) + geom_line() +
  geom_line(aes(x=em_steps,y=R2_em_Setember[,1],color="September"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_October[,1],color="October"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=R2_em_November[,1],color="November"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("SON", "September","October","November"),
                      values = c("#56B4E9", "#E69F00","#009E73","#000000")) +
  
  labs(x="Emission Threshold [kg/m²]", y="R²") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

#----------------------------------------------------------------
R2_em_JJA <- replace(R2_em_JJA,which(R2_em_JJA==1),-999)
which(R2_em_JJA == max(slope_em_JJA), arr.ind = TRUE)

slope_em_year <- as.vector(slope_em[,1])
head(slope_em_year)
data <- data.frame(slope_em_year,slope_em_DJF[,1],slope_em_MAM[,1],slope_em_JJA[,1],slope_em_SON[,1],
                   # slope_em_January[,1],slope_em_February[,1],
                   # slope_em_December[,1],slope_em_March[,1],slope_em_April[,1],slope_em_May[,1],slope_em_June[,1],slope_em_July[,1],slope_em_August[,1],
                   # slope_em_Setember[,1], slope_em_October[,1],slope_em_November[,1],
                   em_steps,perc,perc2,perc3,perc4)
length(slope_em[,1])
length(em_steps)
png("slope_em_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =slope_em_year, colour="Annual")) + geom_line() + 
  geom_line(aes(x=em_steps,y=slope_em_DJF[,1],colour="DJF"),linetype = "solid",size=1) +
  geom_line(aes(x=em_steps,y=slope_em_MAM[,1],colour="MAM"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_JJA[,1],colour="JJA"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_SON[,1],colour="SON"),linetype = "solid",size=1)+
  # geom_line(aes(x=em_steps,y=perc,colour="DJF"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc2,colour="MAM"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc3,colour="JJA"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc4,colour="SON"),linetype = "dotted",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,500))+
  scale_colour_manual("", 
                      breaks = c("Annual", "DJF","MAM","JJA","SON"),
                      values = c("#000000", "#E69F00","#009E73","#56B4E9","#F0E442")) +
  labs(x="Emission Threshold [kg/m²]", y="Slope [m²/kg]") +  
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  #scale_y_continuous(limits=c(0,1), sec.axis = sec_axis(~.*1, name = "Emission Events Considered [%]",
  # labels = function(b) { paste0(round(b * 100, 0), "%")}))+
  
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("slope_em_DJF_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =slope_em_DJF[,1], colour="DJF")) + geom_line() +
  geom_line(aes(x=em_steps,y=slope_em_January[,1],colour="January"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_February[,1],colour="February"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_December[,1],colour="December"),linetype = "twodash",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,500))+
  scale_colour_manual("", 
                      breaks = c("DJF", "January","February","December"),
                      values = c("#56B4E9","#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Slope [m²/kg]") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("slope_em_MAM_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =slope_em_MAM[,1], colour="MAM")) + geom_line() +
  geom_line(aes(x=em_steps,y=slope_em_March[,1],colour="March"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_April[,1],colour="April"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_May[,1],colour="May"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,500))+
  scale_colour_manual("", 
                      breaks = c("MAM", "March","April","May"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Slope [m²/kg]") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("slope_em_JJA_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =slope_em_JJA[,1], colour="JJA")) + geom_line() +
  geom_line(aes(x=em_steps,y=slope_em_June[,1],colour="June"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_July[,1],colour="July"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_August[,1],colour="August"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,500))+
  scale_colour_manual("", 
                      breaks = c("JJA", "June","July","August"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Slope [m²/kg]") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("slope_em_SON_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =slope_em_SON[,1], colour="SON")) + geom_line() +
  geom_line(aes(x=em_steps,y=slope_em_Setember[,1],color="September"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_October[,1],color="October"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=slope_em_November[,1],color="November"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,500))+
  scale_colour_manual("", 
                      breaks = c("SON", "September","October","November"),
                      values = c("#56B4E9", "#E69F00","#009E73","#000000")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Slope [m²/kg]") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

#----------------------------------------------------------------
R2_em_JJA <- replace(R2_em_JJA,which(R2_em_JJA==1),-999)
which(R2_em_JJA == max(slope_em_JJA), arr.ind = TRUE)

intercept_em_year <- as.vector(intercept_em[,1])
head(intercept_em_year)
data <- data.frame(intercept_em_year,intercept_em_DJF[,1],intercept_em_MAM[,1],intercept_em_JJA[,1],intercept_em_SON[,1],
                   # intercept_em_January[,1],intercept_em_February[,1],
                   # intercept_em_December[,1],intercept_em_March[,1],intercept_em_April[,1],intercept_em_May[,1],intercept_em_June[,1],intercept_em_July[,1],intercept_em_August[,1],
                   # intercept_em_Setember[,1], intercept_em_October[,1],intercept_em_November[,1],
                   em_steps,perc,perc2,perc3,perc4)
length(intercept_em[,1])
length(em_steps)
png("intercept_em_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =intercept_em_year, colour="Annual")) + geom_line() + 
  geom_line(aes(x=em_steps,y=intercept_em_DJF[,1],colour="DJF"),linetype = "solid",size=1) +
  geom_line(aes(x=em_steps,y=intercept_em_MAM[,1],colour="MAM"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_JJA[,1],colour="JJA"),linetype = "solid",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_SON[,1],colour="SON"),linetype = "solid",size=1)+
  # geom_line(aes(x=em_steps,y=perc,colour="DJF"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc2,colour="MAM"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc3,colour="JJA"),linetype = "dotted",size=1)+
  # geom_line(aes(x=em_steps,y=perc4,colour="SON"),linetype = "dotted",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("Annual", "DJF","MAM","JJA","SON"),
                      values = c("#000000", "#E69F00","#009E73","#56B4E9","#F0E442")) +
  labs(x="Emission Threshold [kg/m²]", y="Intercept") +  
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  #scale_y_continuous(limits=c(0,1), sec.axis = sec_axis(~.*1, name = "Emission Events Considered [%]",
  # labels = function(b) { paste0(round(b * 100, 0), "%")}))+
  
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("intercept_em_DJF_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =intercept_em_DJF[,1], colour="DJF")) + geom_line() +
  geom_line(aes(x=em_steps,y=intercept_em_January[,1],colour="January"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_February[,1],colour="February"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_December[,1],colour="December"),linetype = "twodash",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("DJF", "January","February","December"),
                      values = c("#56B4E9","#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Intercept") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("intercept_em_MAM_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =intercept_em_MAM[,1], colour="MAM")) + geom_line() +
  geom_line(aes(x=em_steps,y=intercept_em_March[,1],colour="March"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_April[,1],colour="April"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_May[,1],colour="May"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("MAM", "March","April","May"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Intercept") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("intercept_em_JJA_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =intercept_em_JJA[,1], colour="JJA")) + geom_line() +
  geom_line(aes(x=em_steps,y=intercept_em_June[,1],colour="June"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_July[,1],colour="July"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_August[,1],colour="August"),linetype = "twodash",size=1)+
  
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("JJA", "June","July","August"),
                      values = c("#56B4E9", "#000000", "#E69F00","#009E73")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Intercept") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()

png("intercept_em_SON_dist.png")
myplot1 <- ggplot(data, aes(x = em_steps, y =intercept_em_SON[,1], colour="SON")) + geom_line() +
  geom_line(aes(x=em_steps,y=intercept_em_Setember[,1],color="September"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_October[,1],color="October"),linetype = "twodash",size=1)+
  geom_line(aes(x=em_steps,y=intercept_em_November[,1],color="November"),linetype = "twodash",size=1)+
  scale_x_log10(limits=c(10^(-6),10^(-1)))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("SON", "September","October","November"),
                      values = c("#56B4E9", "#E69F00","#009E73","#000000")) +
  
  labs(x="Emission Threshold [kg/m²]", y="Intercept") + 
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(axis.text.x=element_text(size=14, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=14),
                     axis.title=element_text(size=15),
                     legend.text=element_text(size=15)) + annotation_logticks(sides = "b") 
print(myplot1)
dev.off()



#-----------------------------------------------------------------
load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")
mean_em <- vector(length=12)
mean_od <- vector(length=12)
sd_em <- vector(length=12)
sd_od <- vector(length=12)
vectorod <- c((as.vector(NorthAfrica_od[seq(4,244,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,244,by=8),])))
mean_em[1] <- mean(vectorem)
mean_od[1] <- mean(vectorod)
sd_em[1] <- sd(vectorem)
sd_od[1] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(252,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(252,476,by=8),])))
mean_em[2] <- mean(vectorem)
mean_od[2] <- mean(vectorod)
sd_em[2] <- sd(vectorem)
sd_od[2] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(484,724,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,724,by=8),])))
mean_em[3] <- mean(vectorem)
mean_od[3] <- mean(vectorod)
sd_em[3] <- sd(vectorem)
sd_od[3] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(732,964,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(732,964,by=8),])))
mean_em[4] <- mean(vectorem)
mean_od[4] <- mean(vectorod)
sd_em[4] <- sd(vectorem)
sd_od[4] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(972,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(972,1212,by=8),])))
mean_em[5] <- mean(vectorem)
mean_od[5] <- mean(vectorod)
sd_em[5] <- sd(vectorem)
sd_od[5] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1452,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1452,by=8),])))
mean_em[6] <- mean(vectorem)
mean_od[6] <- mean(vectorod)
sd_em[6] <- sd(vectorem)
sd_od[6] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(1460,1700,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1460,1700,by=8),])))
mean_em[7] <- mean(vectorem)
mean_od[7] <- mean(vectorod)
sd_em[7] <- sd(vectorem)
sd_od[7] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(1708,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1708,1948,by=8),])))
mean_em[8] <- mean(vectorem)
mean_od[8] <- mean(vectorod)
sd_em[8] <- sd(vectorem)
sd_od[8] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2188,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2188,by=8),])))
mean_em[9] <- mean(vectorem)
mean_od[9] <- mean(vectorod)
sd_em[9] <- sd(vectorem)
sd_od[9] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(2196,2436,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2196,2436,by=8),])))
mean_em[10] <- mean(vectorem)
mean_od[10] <- mean(vectorod)
sd_em[10] <- sd(vectorem)
sd_od[10] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(2444,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2444,2676,by=8),])))
mean_em[11] <- mean(vectorem)
mean_od[11] <- mean(vectorod)
sd_em[11] <- sd(vectorem)
sd_od[11] <- sd(vectorod)
vectorod <- c((as.vector(NorthAfrica_od[seq(2684,2924,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2684,2924,by=8),])))
mean_em[12] <- mean(vectorem)
mean_od[12] <- mean(vectorod)
sd_em[12] <- sd(vectorem)
sd_od[12] <- sd(vectorod)

months <- c(1,2,3,4,5,6,7,8,9,10,11,12)

png("emission_dist.png")
df2 <- data.frame(mean_em,mean_od,sd_em,sd_od,months)
p<- ggplot(df2, aes(x=months, y=mean_em)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=mean_em-sd_em, ymax=mean_em+sd_em), width=.2,
                position=position_dodge(0.05))
print(p)
dev.off()

R2_em_SON[match(max(R2_em_SON[,1]),R2_em_SON[,1]),1] <- 0
R2_em_Setember[match(max(R2_em_Setember[,1]),R2_em_Setember[,1]),1] <- 0
R2_em_October[match(max(R2_em_October[,1]),R2_em_October[,1]),1] <- 0
R2_em_November[match(max(R2_em_November[,1]),R2_em_November[,1]),1] <- 0
R2_em_December[match(max(R2_em_December[,1]),R2_em_December[,1]),1] <- 0
R2_em_December[match(max(R2_em_December[,1]),R2_em_December[,1]),1] <- 0
R2_em_January[match(max(R2_em_January[,1]),R2_em_January[,1]),1] <- 0



cat("Year","&",max(R2_em_year),"&",em_steps[match(max(R2_em_year),R2_em_year)],"\\","\n")
cat("DJF","&",max(R2_em_DJF[,1]),"&",em_steps[match(max(R2_em_DJF[,1]),R2_em_DJF[,1])],"\\","\n")
cat("MAM","&",max(R2_em_MAM[,1]),"&",em_steps[match(max(R2_em_MAM[,1]),R2_em_MAM[,1])],"\\","\n")
cat("JJA","&",max(R2_em_JJA[,1]),"&",em_steps[match(max(R2_em_JJA[,1]),R2_em_JJA[,1])],"\\","\n")
cat("SON","&",max(R2_em_SON[,1]),"&",em_steps[match(max(R2_em_SON[,1]),R2_em_SON[,1])],"\\","\n")
cat("January","&",max(R2_em_January[,1]),"&",em_steps[match(max(R2_em_January[,1]),R2_em_January[,1])],"\\","\n")
cat("February","&",max(R2_em_February[,1]),"&",em_steps[match(max(R2_em_February[,1]),R2_em_February[,1])],"\\","\n")
cat("March","&",max(R2_em_March[,1]),"&",em_steps[match(max(R2_em_March[,1]),R2_em_March[,1])],"\\","\n")
cat("April","&",max(R2_em_April[,1]),"&",em_steps[match(max(R2_em_April[,1]),R2_em_April[,1])],"\\","\n")
cat("May","&",max(R2_em_May[,1]),"&",em_steps[match(max(R2_em_May[,1]),R2_em_May[,1])],"\\","\n")
cat("June","&",max(R2_em_June[,1]),"&",em_steps[match(max(R2_em_June[,1]),R2_em_June[,1])],"\\","\n")
cat("July","&",max(R2_em_July[,1]),"&",em_steps[match(max(R2_em_July[,1]),R2_em_July[,1])],"\\","\n")
cat("August","&",max(R2_em_August[,1]),"&",em_steps[match(max(R2_em_August[,1]),R2_em_August[,1])],"\\","\n")
cat("September","&",max(R2_em_Setember[,1]),"&",em_steps[match(max(R2_em_Setember[,1]),R2_em_Setember[,1])],"\\","\n")
cat("October","&",max(R2_em_October[,1]),"&",em_steps[match(max(R2_em_October[,1]),R2_em_October[,1])],"\\","\n")
cat("November","&",max(R2_em_November[,1]),"&",em_steps[match(max(R2_em_November[,1]),R2_em_November[,1])],"\\","\n")
cat("December","&",max(R2_em_December[,1]),"&",em_steps[match(max(R2_em_December[,1]),R2_em_December[,1])],"\\","\n")