library(reshape2)
library(ggplot2)


load("R2_wind.RData")
load("R2_wind_MAM.RData")
load("R2_wind_JJA.RData")
load("R2_wind_SON.RData")
load("R2_wind_DJF.RData")
load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")
load("NorthAfrica_ustar.RData")

# 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^(-(15:0)))
wind_steps <- seq(from = 0, to = 1.5, by = 0.025)

length(od_steps)
length(em_steps)
wind_steps

# R2[90:99,]
# em_steps[104]
# od_steps[139]
# em_steps[97]
# od_steps[102]
#R2_JJA <- replace(R2_JJA,which(R2_JJA==1),-999)
# which(R2_JJA == max(R2_JJA), arr.ind = TRUE)
# em_steps[103]
# od_steps[128]
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(4,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,476,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[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),])))
vectorwind1 <- c((as.vector(NorthAfrica_ustar[seq(2684,2924,by=8),])))
vectorod <- c(vectorod1,vectorod)
vectorem <- c(vectorem1,vectorem)
vectorwind <- c(vectorwind1,vectorwind)

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc <- vector(length=(length(wind_steps)))
percb <- vector(length=(length(wind_steps)))
NorthAfrica_od5<- vectorod[vectorem>(10^(-3))]
NorthAfrica_em5<- vectorem[vectorem>(10^(-3))]
for (n in 1:length(wind_steps))
{
  vectoref <- vectorod[(vectorwind>wind_steps[n])]
  perc[n] <- length(vectoref)/length(vectorod)
  
  vectoref2 <- vectorod[(vectorem>(10^(-3)))&(vectorwind>wind_steps[n])]
  percb[n] <- length(vectoref2)/length(NorthAfrica_od5)
}
perc
percb
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(484,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,1212,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(484,1212,by=8),])))

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc2 <- vector(length=(length(wind_steps)))
percb2 <- vector(length=(length(wind_steps)))
NorthAfrica_od5<- vectorod[vectorem>(10^(-3))]
NorthAfrica_em5<- vectorem[vectorem>(10^(-3))]
for (n in 1:length(wind_steps))
{
  vectoref <- vectorod[(vectorwind>wind_steps[n])&(vectorem>0)]
  perc2[n] <- length(vectoref)/length(NorthAfrica_od0)
  
  vectoref2 <- vectorod[(vectorem>(10^(-3)))&(vectorwind>wind_steps[n])]
  percb2[n] <- length(vectoref2)/length(NorthAfrica_od5)
}
perc2
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1948,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1220,1948,by=8),])))

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc3 <- vector(length=(length(wind_steps)))
percb3 <- vector(length=(length(wind_steps)))
NorthAfrica_od5<- vectorod[vectorem>(10^(-3))]
NorthAfrica_em5<- vectorem[vectorem>(10^(-3))]
for (n in 1:length(wind_steps))
{
  vectoref <- vectorod[(vectorwind>wind_steps[n])&(vectorem>0)]
  perc3[n] <- length(vectoref)/length(NorthAfrica_od0)
  
  vectoref2 <- vectorod[(vectorem>(10^(-3)))&(vectorwind>wind_steps[n])]
  percb3[n] <- length(vectoref2)/length(NorthAfrica_od5)
}
perc3
#---------------------------------------------------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2676,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1956,2676,by=8),])))

NorthAfrica_od0<- vectorod[vectorem!=0]
NorthAfrica_em0<- vectorem[vectorem!=0]
perc4 <- vector(length=(length(wind_steps)))

percb4 <- vector(length=(length(wind_steps)))
NorthAfrica_od5<- vectorod[vectorem>(10^(-3))]
NorthAfrica_em5<- vectorem[vectorem>(10^(-3))]
for (n in 1:length(wind_steps))
{
  vectoref <- vectorod[(vectorwind>wind_steps[n])&(vectorem>0)]
  perc4[n] <- length(vectoref)/length(NorthAfrica_od0)
  
  vectoref2 <- vectorod[(vectorem>(10^(-3)))&(vectorwind>wind_steps[n])]
  percb4[n] <- length(vectoref2)/length(NorthAfrica_od5)
}
perc4
#---------------------------------------------------------------



data <- data.frame(R2_wind[,1],R2_wind_DJF[,1],R2_wind_MAM[,1],
                   R2_wind_JJA[,1],R2_wind_SON[,1],wind_steps,perc,perc2,perc3,perc4,
                   percb,percb2,percb3,percb4)
length(wind_steps)
length(R2_wind[,1])

png("R2_ẁind_dist.png")
myplot1 <- ggplot(data, aes(x = wind_steps, y =R2_wind_DJF[,1], colour="DJF")) + geom_line(size=0.5) + 
  geom_line(aes(x=wind_steps,y=R2_wind_DJF[,1],colour="DJF"),linetype = "solid",size=0.5) +
  # geom_line(aes(x=wind_steps,y=R2_wind_MAM[,1],colour="MAM"),linetype = "solid",size=0.5)+
  # geom_line(aes(x=wind_steps,y=R2_wind_JJA[,1],colour="JJA"),linetype = "solid",size=0.5)+
  # geom_line(aes(x=wind_steps,y=R2_wind_SON[,1],colour="SON"),linetype = "solid",size=0.5)+
  geom_line(aes(x=wind_steps,y=perc,colour="DJF"),linetype = "twodash",size=1)+
  # geom_line(aes(x=wind_steps,y=perc2,colour="MAM"),linetype = "dotted",size=1)+
  # geom_line(aes(x=wind_steps,y=perc3,colour="JJA"),linetype = "dotted",size=1)+
  # geom_line(aes(x=wind_steps,y=perc4,colour="SON"),linetype = "dotted",size=1)+
  # geom_line(aes(x=wind_steps,y=percb,colour="DJF"),linetype = "twodash",size=1)+
  # geom_line(aes(x=wind_steps,y=percb2,colour="MAM"),linetype = "twodash",size=1)+
  # geom_line(aes(x=wind_steps,y=percb3,colour="JJA"),linetype = "twodash",size=1)+
  # geom_line(aes(x=wind_steps,y=percb4,colour="SON"),linetype = "twodash",size=1)+
  scale_x_continuous(limits=c(0,1.5))+
  scale_y_continuous(limits=c(0,1))+
  scale_colour_manual("", 
                      breaks = c("Annual", "DJF","MAM","JJA","SON"),
                      values = c("#E69F00", "#E69F00","#009E73","#56B4E9","#F0E442")) +
  labs(x="Wind Friction Threshold [m/s]", y="R²") +  #annotation_logticks(sides = "b") +
  scale_y_continuous(limits=c(0,1), sec.axis = sec_axis(~.*1, name = "Remaining Data [%]",
                                                        labels = function(b) { paste0(round(b * 100, 0), "%")}))+
  #scale_x_discrete(breaks = c("0","1e-14","1e-13","1") ) + 
  theme_bw() + theme(legend.position = "none") + theme(axis.text.x=element_text(size=16, angle=0, vjust=0.3),
                     axis.text.y=element_text(size=16),
                     axis.title=element_text(size=17),
                     legend.text=element_text(size=17))
print(myplot1)
dev.off()

R2_wind_MAM[match(max(R2_wind_MAM[,1]),R2_wind_MAM[,1]),1] <- 0
R2_wind[match(max(R2_wind[,1]),R2_wind[,1]),1] <- 0

cat("Year","&",max(R2_wind[,1]),"&",wind_steps[match(max(R2_wind[,1]),R2_wind[,1])],"\\","\n")
cat("DJF","&",max(R2_wind_DJF[,1]),"&",wind_steps[match(max(R2_wind_DJF[,1]),R2_wind_DJF[,1])],"\\","\n")
cat("MAM","&",max(R2_wind_MAM[,1]),"&",wind_steps[match(max(R2_wind_MAM[,1]),R2_wind_MAM[,1])],"\\","\n")
cat("JJA","&",max(R2_wind_JJA[,1]),"&",wind_steps[match(max(R2_wind_JJA[,1]),R2_wind_JJA[,1])],"\\","\n")
cat("SON","&",max(R2_wind_SON[,1]),"&",wind_steps[match(max(R2_wind_SON[,1]),R2_wind_SON[,1])],"\\","\n")
