# load the ncdf4 and ggplot2 packages

library(ncdf4)
library(ggplot2)
library(reticulate)
library(grid)
library(gridExtra)



load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")
load("NorthAfrica_ustar.RData")
dim(NorthAfrica_od)
# # #_______________________________Annual_____________________________________________________________________
vectorod <- c((as.vector(NorthAfrica_od[seq(4,2924,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,2924,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(4,2924,by=8),])))

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

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999

      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
   }
 }
R2_wind <- R2
slope_wind <- slope
save(R2_wind, file = "R2_wind.RData")
save(slope_wind, file = "slope_wind.RData")
RMSE_wind <- RMSE
save(RMSE_wind, file = "RMSE_wind.RData")
intercept_wind <- intercept
save(intercept_wind, file = "intercept_wind.RData")

#_______________________________DJF_____________________________________________________________________

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)

vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

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)

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_DJF <- R2
stderrint_wind_DJF <- stderrint
stderrslp_wind_DJF <- stderrslp
slope_wind_DJF <- slope
intercept_wind_DJF <- intercept
save(intercept_wind_DJF, file = "intercept_wind_DJF.RData")
save(R2_wind_DJF, file = "R2_wind_DJF.RData")
save(slope_wind_DJF, file = "slope_wind_DJF.RData")
save(stderrint_wind_DJF, file = "stderrint_wind_DJF.RData")
save(stderrslp_wind_DJF, file = "stderrslp_wind_DJF.RData")
RMSE_wind_DJF <- RMSE
save(RMSE_wind_DJF, file = "RMSE_wind_DJF.RData")

#_______________________________MAM_____________________________________________________________________


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),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

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)

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_MAM <- R2
stderrint_wind_MAM <- R2
stderrslp_wind_MAM <- R2
slope_wind_MAM <- slope
intercept_wind_MAM <- intercept

save(slope_wind_MAM, file = "slope_wind_MAM.RData")
save(intercept_wind_MAM, file = "intercept_wind_MAM.RData")
save(R2_wind_MAM, file = "R2_wind_MAM.RData")
save(stderrint_wind_MAM, file = "stderrint_wind_MAM.RData")
save(stderrslp_wind_MAM, file = "stderrslp_wind_MAM.RData")
RMSE_wind_MAM <- RMSE
save(RMSE_wind_MAM, file = "RMSE_wind_MAM.RData")


# #_______________________________JJA_____________________________________________________________________

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),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

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)

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_JJA <- R2
stderrint_wind_JJA <- R2
stderrslp_wind_JJA <- R2
slope_wind_JJA <- slope
intercept_wind_JJA <- intercept
save(slope_wind_JJA, file = "slope_wind_JJA.RData")
save(intercept_wind_JJA, file = "intercept_wind_JJA.RData")
save(R2_wind_JJA, file = "R2_wind_JJA.RData")
save(stderrint_wind_JJA, file = "stderrint_wind_JJA.RData")
save(stderrslp_wind_JJA, file = "stderrslp_wind_JJA.RData")
RMSE_wind_JJA <- RMSE
save(RMSE_wind_JJA, file = "RMSE_wind_JJA.RData")

# #_______________________________SON_____________________________________________________________________

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),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

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)

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_SON <- R2
stderrint_wind_SON <- R2
stderrslp_wind_SON <- R2
slope_wind_SON <- slope
intercept_wind_SON <- intercept
save(slope_wind_SON, file = "slope_wind_SON.RData")
save(intercept_wind_SON, file = "intercept_wind_SON.RData")
save(R2_wind_SON, file = "R2_wind_SON.RData")
save(stderrint_wind_SON, file = "stderrint_wind_SON.RData")
save(stderrslp_wind_SON, file = "stderrslp_wind_SON.RData")
RMSE_wind_SON <- RMSE
save(RMSE_wind_SON, file = "RMSE_wind_SON.RData")

#---------------------------------January---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(4,244,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,244,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(4,244,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_January <- R2
stderrint_wind_January <- R2
stderrslp_wind_January <- R2
slope_wind_January <- slope
intercept_wind_January <- intercept
save(slope_wind_January, file = "slope_wind_January.RData")
save(intercept_wind_January, file = "intercept_wind_January.RData")
save(R2_wind_January, file = "R2_wind_January.RData")
save(stderrint_wind_January, file = "stderrint_wind_January.RData")
save(stderrslp_wind_January, file = "stderrslp_wind_January.RData")
RMSE_wind_January <- RMSE
save(RMSE_wind_January, file = "RMSE_wind_January.RData")


#---------------------------------February---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(252,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(252,476,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(252,476,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_February <- R2
stderrint_wind_February <- R2
stderrslp_wind_February <- R2
slope_wind_February <- slope
intercept_wind_February <- intercept
save(slope_wind_February, file = "slope_wind_February.RData")
save(intercept_wind_February, file = "intercept_wind_February.RData")
save(R2_wind_February, file = "R2_wind_February.RData")
save(stderrint_wind_February, file = "stderrint_wind_February.RData")
save(stderrslp_wind_February, file = "stderrslp_wind_February.RData")
RMSE_wind_February <- RMSE
save(RMSE_wind_February, file = "RMSE_wind_February.RData")


#---------------------------------March---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(484,724,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,724,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(484,724,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_March <- R2
stderrint_wind_March <- R2
stderrslp_wind_March <- R2
slope_wind_March <- slope
intercept_wind_March <- intercept
save(slope_wind_March, file = "slope_wind_March.RData")
save(intercept_wind_March, file = "intercept_wind_March.RData")
save(R2_wind_March, file = "R2_wind_March.RData")
save(stderrint_wind_March, file = "stderrint_wind_March.RData")
save(stderrslp_wind_March, file = "stderrslp_wind_March.RData")
RMSE_wind_March <- RMSE
save(RMSE_wind_March, file = "RMSE_wind_March.RData")

#---------------------------------April---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(732,964,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(732,964,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(732,964,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_April <- R2
stderrint_wind_April <- R2
stderrslp_wind_April <- R2
slope_wind_April <- slope
intercept_wind_April <- intercept
save(slope_wind_April, file = "slope_wind_April.RData")
save(intercept_wind_April, file = "intercept_wind_April.RData")
save(R2_wind_April, file = "R2_wind_April.RData")
save(stderrint_wind_April, file = "stderrint_wind_April.RData")
save(stderrslp_wind_April, file = "stderrslp_wind_April.RData")
RMSE_wind_April <- RMSE
save(RMSE_wind_April, file = "RMSE_wind_April.RData")


#---------------------------------May---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(972,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(972,1212,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(972,1212,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_May <- R2
stderrint_wind_May <- R2
stderrslp_wind_May <- R2
slope_wind_May <- slope
intercept_wind_May <- intercept
save(slope_wind_May, file = "slope_wind_May.RData")
save(intercept_wind_May, file = "intercept_wind_May.RData")
save(R2_wind_May, file = "R2_wind_May.RData")
save(stderrint_wind_May, file = "stderrint_wind_May.RData")
save(stderrslp_wind_May, file = "stderrslp_wind_May.RData")
RMSE_wind_May <- RMSE
save(RMSE_wind_May, file = "RMSE_wind_May.RData")

#---------------------------------June---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1452,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1452,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1220,1452,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
pvalue <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_June <- R2
stderrint_wind_June <- R2
stderrslp_wind_June <- R2
slope_wind_June <- slope
intercept_wind_June <- intercept
save(slope_wind_June, file = "slope_wind_June.RData")
save(intercept_wind_June, file = "intercept_wind_June.RData")
save(R2_wind_June, file = "R2_wind_June.RData")
save(stderrint_wind_June, file = "stderrint_wind_June.RData")
save(stderrslp_wind_June, file = "stderrslp_wind_June.RData")
RMSE_wind_June <- RMSE
save(RMSE_wind_June, file = "RMSE_wind_June.RData")


#---------------------------------July---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1460,1700,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1460,1700,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1460,1700,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
pvalue <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_July <- R2
stderrint_wind_July <- R2
stderrslp_wind_July <- R2
slope_wind_July <- slope
intercept_wind_July <- intercept
save(slope_wind_July, file = "slope_wind_July.RData")
save(intercept_wind_July, file = "intercept_wind_July.RData")
save(R2_wind_July, file = "R2_wind_July.RData")
save(stderrint_wind_July, file = "stderrint_wind_July.RData")
save(stderrslp_wind_July, file = "stderrslp_wind_July.RData")
RMSE_wind_July <- RMSE
save(RMSE_wind_July, file = "RMSE_wind_July.RData")


#---------------------------------August---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1708,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1708,1948,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1708,1948,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
pvalue <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_August <- R2
stderrint_wind_August <- R2
stderrslp_wind_August <- R2
slope_wind_August <- slope
intercept_wind_August <- intercept
save(slope_wind_August, file = "slope_wind_August.RData")
save(intercept_wind_August, file = "intercept_wind_August.RData")
save(R2_wind_August, file = "R2_wind_August.RData")
save(stderrint_wind_August, file = "stderrint_wind_August.RData")
save(stderrslp_wind_August, file = "stderrslp_wind_August.RData")
RMSE_wind_August <- RMSE
save(RMSE_wind_August, file = "RMSE_wind_August.RData")


#---------------------------------Setember---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2188,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2188,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(1956,2188,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_Setember <- R2
stderrint_wind_Setember <- R2
stderrslp_wind_Setember <- R2
slope_wind_Setember <- slope
intercept_wind_Setember <- intercept
save(slope_wind_Setember, file = "slope_wind_Setember.RData")
save(intercept_wind_Setember, file = "intercept_wind_Setember.RData")
save(R2_wind_Setember, file = "R2_wind_Setember.RData")
save(stderrint_wind_Setember, file = "stderrint_wind_Setember.RData")
save(stderrslp_wind_Setember, file = "stderrslp_wind_Setember.RData")
RMSE_wind_Setember <- RMSE
save(RMSE_wind_Setember, file = "RMSE_wind_Setember.RData")


#---------------------------------October---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2196,2436,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2196,2436,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(2196,2436,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_October <- R2
stderrint_wind_October <- R2
stderrslp_wind_October <- R2
slope_wind_October <- slope
intercept_wind_October <- intercept
save(slope_wind_October, file = "slope_wind_October.RData")
save(intercept_wind_October, file = "intercept_wind_October.RData")
save(R2_wind_October, file = "R2_wind_October.RData")
save(stderrint_wind_October, file = "stderrint_wind_October.RData")
save(stderrslp_wind_October, file = "stderrslp_wind_October.RData")
RMSE_wind_October <- RMSE
save(RMSE_wind_October, file = "RMSE_wind_October.RData")

#---------------------------------November---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2444,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2444,2676,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(2444,2676,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
pvalue <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_November <- R2
stderrint_wind_November <- R2
stderrslp_wind_November <- R2
slope_wind_November <- slope
intercept_wind_November <- intercept
save(slope_wind_November, file = "slope_wind_November.RData")
save(intercept_wind_November, file = "intercept_wind_November.RData")
save(R2_wind_November, file = "R2_wind_November.RData")
save(stderrint_wind_November, file = "stderrint_wind_November.RData")
save(stderrslp_wind_November, file = "stderrslp_wind_November.RData")
RMSE_wind_November <- RMSE
save(RMSE_wind_November, file = "RMSE_wind_November.RData")


#---------------------------------December---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2684,2924,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2684,2924,by=8),])))
vectorwind <- c((as.vector(NorthAfrica_ustar[seq(2684,2924,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(wind_steps),length(od_steps))
intercept <- matrix(0,length(wind_steps),length(od_steps))
R2 <- matrix(0,length(wind_steps),length(od_steps))
r <- matrix(0,length(wind_steps),length(od_steps))
stderrint <- matrix(0,length(wind_steps),length(od_steps))
stderrslp <- matrix(0,length(wind_steps),length(od_steps))
RMSE <- matrix(0,length(wind_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(wind_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorwind>=wind_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_wind_December <- R2
stderrint_wind_December <- R2
stderrslp_wind_December <- R2
slope_wind_December <- slope
intercept_wind_December <- intercept
save(slope_wind_December, file = "slope_wind_December.RData")
save(intercept_wind_December, file = "intercept_wind_December.RData")
save(R2_wind_December, file = "R2_wind_December.RData")
save(stderrint_wind_December, file = "stderrint_wind_December.RData")
save(stderrslp_wind_December, file = "stderrslp_wind_December.RData")
RMSE_wind_December <- RMSE
save(RMSE_wind_December, file = "RMSE_wind_December.RData")

NorthAfrica_od0 <- vectorod[vectorem != 0]
NorthAfrica_em0<- vectorem[vectorem != 0]
vectorod <- vectorod[vectorem==0]
vectorem <- vectorem[vectorem==0]

length(vectorod)
length(vectorem)
length(NorthAfrica_od0)
length(NorthAfrica_em0)
datos <- data.frame(vectorem,vectorod)

r[1] <- cor(NorthAfrica_em0, NorthAfrica_od0)
linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
summary(linearMod)
R2[1] <- summary(linearMod)$r.squared
pvalue[1] <- summary(linearMod)$coefficients[,4] 
intercept[1] <- summary(linearMod)$coefficients[1,1] 
slope[1] <- summary(linearMod)$coefficients[2,1] 
stderrint[1] <- summary(linearMod)$coefficients[1,2]
stderrslp[1] <- summary(linearMod)$coefficients[2,2]