# load the ncdf4 and ggplot2 packages
library(ncdf4)
library(ggplot2)
library(reticulate)
library(grid)
library(gridExtra)
library(car)

#load emission and dod data 
load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")

#create empty vectors to fill
r_anual <- vector(length=8)
r_DJF <- vector(length=8)
r_MAM <- vector(length=8)
r_JJA <- vector(length=8)
r_SON <- vector(length=8)

r_anual_timelag <- vector(length=8)
r_DJF_timelag <- vector(length=8)
r_MAM_timelag <- vector(length=8)
r_JJA_timelag <- vector(length=8)
r_SON_timelag <- vector(length=8)


#In this loop R coefficient for every time step and season is computed
#Also the R coeff for em and dod with timlag is computed
for (t in 1:8)
{
   print(t)
    #_____________________________________Annual_______________________________
    vectorem <- as.vector(NorthAfrica_em[seq(1+(t-1),2921+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1+(t-1),2921+(t-1),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_anual[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #with timelag
    vectorem <- as.vector(NorthAfrica_em[seq(1+(t-1),2920+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1+(t),2920+(t),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_anual_timelag[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))

    #_______________________________DJF_________________________________________
    
    vectorem <- as.vector(NorthAfrica_em[seq(2681+(t-1),2921+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(2681+(t-1),2921+(t-1),by=8),])
    vectorod <- c(vectorod,(as.vector(NorthAfrica_od[seq(1+(t-1),473+(t-1),by=8),])))
    vectorem <- c(vectorem,(as.vector(NorthAfrica_em[seq(1+(t-1),473+(t-1),by=8),])))
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_DJF[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #with timelag
    vectorem <- as.vector(NorthAfrica_em[seq(2681+(t-1),2920+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(2681+(t),2920+(t),by=8),])
    vectorod <- c(vectorod,(as.vector(NorthAfrica_od[seq(1+(t),473+(t),by=8),])))
    vectorem <- c(vectorem,(as.vector(NorthAfrica_em[seq(1+(t-1),473+(t-1),by=8),])))
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_DJF_timelag[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    # #___________________________________MAM__________________________________
    
    vectorem <- as.vector(NorthAfrica_em[seq(481+(t-1),1209+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(481+(t-1),1209+(t-1),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_MAM[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #with timelag
    vectorem <- as.vector(NorthAfrica_em[seq(481+(t-1),1209+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(481+(t),1209+(t),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_MAM_timelag[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #___________________________________JJA_____________________________________
    
    vectorem <- as.vector(NorthAfrica_em[seq(1217+(t-1),1945+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1217+(t-1),1945+(t-1),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_JJA[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #with timelag
    vectorem <- as.vector(NorthAfrica_em[seq(1217+(t-1),1945+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1217+(t),1945+(t),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_JJA_timelag[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    # #___________________________________SON__________________________________

    vectorem <- as.vector(NorthAfrica_em[seq(1953+(t-1),2673+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1953+(t-1),2673+(t-1),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_SON[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
    
    #with timelag
    vectorem <- as.vector(NorthAfrica_em[seq(1953+(t-1),2673+(t-1),by=8),])
    vectorod <- as.vector(NorthAfrica_od[seq(1953+(t),2673+(t),by=8),])
    NorthAfrica_od0 <- vectorod
    NorthAfrica_em0<- vectorem
    r_SON_timelag[t] <- ((cor(NorthAfrica_od0, NorthAfrica_em0)))
}


#preparation of the data for plotting
vectorhours <- c(3,6,9,12,15,18,21,24)
r_anual_timelag
r_anual_timelag[c(8, 1)]
r_anual_timelag <- replace(r_anual_timelag, c(1,2,3,4,5,6,7,8), r_anual_timelag[c(8,1,2,3,4,5,6,7)])
r_anual_timelag
r_DJF_timelag <- replace(r_DJF_timelag,c(1,2,3,4,5,6,7,8), r_DJF_timelag[c(8,1,2,3,4,5,6,7)])
r_MAM_timelag <- replace(r_MAM_timelag, c(1,2,3,4,5,6,7,8), r_MAM_timelag[c(8,1,2,3,4,5,6,7)])
r_SON_timelag <- replace(r_SON_timelag,c(1,2,3,4,5,6,7,8), r_SON_timelag[c(8,1,2,3,4,5,6,7)])
r_JJA_timelag <- replace(r_JJA_timelag,c(1,2,3,4,5,6,7,8), r_JJA_timelag[c(8,1,2,3,4,5,6,7)])

data <- data.frame(r_anual, r_DJF, r_MAM, r_JJA, r_SON, r_anual_timelag,
                   r_DJF_timelag, r_MAM_timelag, r_JJA_timelag, r_SON_timelag,vectorhours)
length(vectorhours)


#PLOT

png("r_correlations.png")
myplot1 <- ggplot(data, aes(x = vectorhours, y =r_anual^(2), colour="Annual")) + geom_line(size=0.5) + 
  geom_line(aes(x=vectorhours,y=r_DJF^(2),colour="DJF"),linetype = "solid",size=0.5) +
  geom_line(aes(x=vectorhours,y=r_MAM^(2),colour="MAM"),linetype = "solid",size=0.5)+
  geom_line(aes(x=vectorhours,y=r_JJA^(2),colour="JJA"),linetype = "solid",size=0.5)+
  geom_line(aes(x=vectorhours,y=r_SON^(2),colour="SON"),linetype = "solid",size=0.5)+
  geom_line(aes(x=vectorhours,y=r_DJF_timelag^(2),colour="DJF"),linetype = "dotted",size=1)+
  geom_line(aes(x=vectorhours,y=r_MAM_timelag^(2),colour="MAM"),linetype = "dotted",size=1)+
  geom_line(aes(x=vectorhours,y=r_JJA_timelag^(2),colour="JJA"),linetype = "dotted",size=1)+
  geom_line(aes(x=vectorhours,y=r_SON_timelag^(2),colour="SON"),linetype = "dotted",size=1)+
  geom_line(aes(x=vectorhours,y=r_anual_timelag^(2),colour="Annual"),linetype = "dotted",size=1)+
  scale_x_continuous(limits=c(3,24), breaks=seq(3, 24, 3))+
  scale_y_continuous(limits=c(0,0.7))+
  scale_colour_manual("", 
                      breaks = c("Annual", "DJF","MAM","JJA","SON"),
                      values = c("#000000", "#E69F00","#009E73","#56B4E9","#F0E442")) +
  labs(x="Time of the day [UTC]", y="R²") +  
  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))
print(myplot1)
dev.off()