# load the ncdf4 and ggplot2 packages
library(ncdf4)
library(ggplot2)

#---------------make lists of file names-----------------------
#daily files of 2012 simulation data
#Variables that will be extracted:
#1) od550duco <- DOD 550 nm coarse 
#2) emidu <- [kg/m²]
#3) ustar <- u* [m/s]
#4) ustt <- u* threshold [m/s]
#5) roughcor <- roughness correction
#6) smoiscor <- moisture correction


i <- 0
ndays <- 366
oddate <- vector(length = ndays)
emidate <- vector(length = ndays)
ustardate <- vector(length = ndays)
usttdate <- vector(length = ndays)
roughcordate <- vector(length = ndays)
smoiscordate <- vector(length = ndays)

for (m in 1:12)
{
  if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
  {	
    for (d in 1:31)
    {
      i <- i+1
      date <- as.character(2012000000+d*100+m*10000)
      oddate[i] <- paste("od550duco_",date, sep ='')
      emidate[i] <- paste("emidu_",date, sep ='')
      ustardate[i] <- paste("ustar_",date, sep ='')
      usttdate[i] <- paste("ustt_",date, sep ='')
      roughcordate[i] <- paste("roughcor_",date, sep ='')
      smoiscordate[i] <- paste("smoiscor_",date, sep ='')
    }
  }
  else if (m==2)
  {
    for (d in 1:29)
    {
      i <- i+1
      date <- as.character(2012020000+d*100)
      oddate[i] <- paste("od550duco_",date, sep ='')
      emidate[i] <- paste("emidu_",date, sep ='')
      ustardate[i] <- paste("ustar_",date, sep ='')
      usttdate[i] <- paste("ustt_",date, sep ='')
      roughcordate[i] <- paste("roughcor_",date, sep ='')
      smoiscordate[i] <- paste("smoiscor_",date, sep ='')
    }
  }
  else
  {
    for (d in 1:30)
    {
      i <- i+1
      date <- as.character(2012000000+d*100+m*10000)
      oddate[i] <- paste("od550duco_",date, sep ='')
      emidate[i] <- paste("emidu_",date, sep ='')
      ustardate[i] <- paste("ustar_",date, sep ='')
      usttdate[i] <- paste("ustt_",date, sep ='')
      roughcordate[i] <- paste("roughcor_",date, sep ='')
      smoiscordate[i] <- paste("smoiscor_",date, sep ='')
    }
  }
}


#---------------file for mask the sea---------------------------------
ncpathsm <- "/esarchive/exp/nmmb-bsc-ctm/b051/"
ncnamesm <- "seamask"  
ncseamask <- paste(ncpathsm, ncnamesm, ".nc", sep="")
nsm <- nc_open(ncseamask)
# get seamask
seamask_array <- ncvar_get(nsm,"seamask")
fillvalue <- ncatt_get(nsm,"seamask","_FillValue")
print("dimensiones seamask")
dim(seamask_array)





#---------------Set boundaries of different regions of study ---------------------
#the simulation output has the following resolution:
#lon: -180º -> +182.812º / [0 -> 258] / (1.406248º)
#lat: -90º -> +92º / [0 -> 182] / (1º)

matrixlenlat <- matrix(0,0,4)
#North Africa
matrixlenlat <- rbind(matrixlenlat,c(108,154,91,131))  # c(minlon,maxlon,minlat,maxlat)
                                                       # in this case: (28.125W,36.56E,1N,41N)
#South Africa
matrixlenlat <- rbind(matrixlenlat,c(132,168,55,89))
#Middle East
matrixlenlat <- rbind(matrixlenlat,c(153,175,89,131))
#Northwest Asia
matrixlenlat <- rbind(matrixlenlat,c(164,182,126,141))
#Southwest Asia
matrixlenlat <- rbind(matrixlenlat,c(175,197,95,126))
#Northeast Asia
matrixlenlat <- rbind(matrixlenlat,c(178,236,121,151))
#Australia
matrixlenlat <- rbind(matrixlenlat,c(200,257,40,89))
#South America
matrixlenlat <- rbind(matrixlenlat,c(67,107,30,105))
#North America
matrixlenlat <- rbind(matrixlenlat,c(14,93,100,161))
#Europe 
matrixlenlat <- rbind(matrixlenlat,c(114,164,126,167))

matrixnames <- c("NorthAfrica", "SouthAfrica", "MiddleEast", "NorthwestAsia", "SouthwestAsia", "NortheastAsia", "Australia", "SouthAmerica", "NorthAmerica", "Europe")

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

for (r in 1:10)
{ 
  print("r:")
  print(r)
  for (d in 1:length(oddate))
  {
    print(d)
    #file for DOD
    ncpathod <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/od550duco/"
    ncnameod <- oddate[d] 
    ncfod <- paste(ncpathod, ncnameod, ".nc", sep="")
    nod <- nc_open(ncfod)
    #array for DOD
    od_array <- ncvar_get(nod,"od550duco")
    dlname <- ncatt_get(nod,"od550duco","long_name")
    dunits <- ncatt_get(nod,"od550duco","units")
    fillvalue <- ncatt_get(nod,"od550duco","_FillValue")
    
    
    
    # get longitude and latitude
    lon <- ncvar_get(nod,"lon")
    nlon <- dim(lon)-2 #257
    lat <- ncvar_get(nod,"lat")
    nlat <- dim(lat)-2 #181

    # get time
    time <- ncvar_get(nod,"time")
    nt <- dim(time)-1

    nc_close(nod)
    od_array <- od_array[,,2:9]

   
    
    #file for emissions
    ncpathem <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/emidu/"
    ncnameem <- emidate[d]  
    ncfem <- paste(ncpathem, ncnameem, ".nc", sep="")
    nem <- nc_open(ncfem)
    emidu_array <- ncvar_get(nem,"emidu")
    dlname <- ncatt_get(nem,"emidu","long_name")
    dunits <- ncatt_get(nem,"emidu","units")
    fillvalue <- ncatt_get(nem,"emidu","_FillValue")
    nc_close(nem)
    emidu_array <- emidu_array[,,,2:9] #8 time steps 2:9
    
    ncpathustar <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/ustar/"
    ncnameustar <- ustardate[d]  
    ncfustar <- paste(ncpathustar, ncnameustar, ".nc", sep="")
    nustar <- nc_open(ncfustar)
    ustar_array <- ncvar_get(nustar,"ustar")
    dlname <- ncatt_get(nustar,"ustar","long_name")
    dunits <- ncatt_get(nustar,"ustar","units")
    fillvalue <- ncatt_get(nustar,"ustar","_FillValue")
    nc_close(nustar)
    ustar_array <- ustar_array[,,2:9]
    
    ncpathustt <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/ustt/"
    ncnameustt <- usttdate[d]  
    ncfustt <- paste(ncpathustt, ncnameustt, ".nc", sep="")
    nustt <- nc_open(ncfustt)
    ustt_array <- ncvar_get(nustt,"ustt")
    dlname <- ncatt_get(nustt,"ustt","long_name")
    dunits <- ncatt_get(nustt,"ustt","units")
    fillvalue <- ncatt_get(nustt,"ustt","_FillValue")
    nc_close(nustt)
    ustt_array <- ustt_array[,,1:4,2:9] #4 bins 1:4
    
    
    ncpathroughcor <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/roughcor/"
    ncnameroughcor <- roughcordate[d]  
    ncfroughcor <- paste(ncpathroughcor, ncnameroughcor, ".nc", sep="")
    nroughcor <- nc_open(ncfroughcor)
    roughcor_array <- ncvar_get(nroughcor,"roughcor")
    dlname <- ncatt_get(nroughcor,"roughcor","long_name")
    dunits <- ncatt_get(nroughcor,"roughcor","units")
    fillvalue <- ncatt_get(nroughcor,"roughcor","_FillValue")
    nc_close(nroughcor)
    roughcor_array <- roughcor_array[,,2:9]

    ncpathsmoiscor <- "/esarchive/exp/monarch/a1yo/Elisa/global/3hourly/smoiscor/"
    ncnamesmoiscor <- smoiscordate[d]  
    ncfsmoiscor <- paste(ncpathsmoiscor, ncnamesmoiscor, ".nc", sep="")
    nsmoiscor <- nc_open(ncfsmoiscor)
    smoiscor_array <- ncvar_get(nsmoiscor,"smoiscor")
    dlname <- ncatt_get(nsmoiscor,"smoiscor","long_name")
    dunits <- ncatt_get(nsmoiscor,"smoiscor","units")
    fillvalue <- ncatt_get(nsmoiscor,"smoiscor","_FillValue")
    nc_close(nsmoiscor)
    smoiscor_array <- smoiscor_array[,,2:9]


    emidu2_array <- array(0,dim=c(nlon,nlat,nt))
    ustt1_array <- array(0,dim=c(nlon,nlat,nt))
    ustt2_array <- array(0,dim=c(nlon,nlat,nt))
    ustt3_array <- array(0,dim=c(nlon,nlat,nt))
    ustt4_array <- array(0,dim=c(nlon,nlat,nt))
    
    
    # sums values of the 8 emission bins for every time step, lat and lon
    # as the emission in the model output is an accumulation of previous hour of the day, here
    # here we convert accumulation from previous hours of the day to 3-hourly accumulation
    for (n in 1:nlon)
    {
      for(i in 1:nlat)
      {
        for(t in 1:nt)
        {
          if (t==1)
           {
             emidu2_array[n,i,t] <- sum(emidu_array[n,i,1:8,t])
           }
           else
           {
             emidu2_array[n,i,t] <- sum(emidu_array[n,i,1:8,t])-sum(emidu_array[n,i,1:8,t-1])
           }
          ustt1_array[n,i,t] <- ustt_array[n,i,1,t]
          ustt2_array[n,i,t] <- ustt_array[n,i,2,t]
          ustt3_array[n,i,t] <- ustt_array[n,i,3,t]
          ustt4_array[n,i,t] <- ustt_array[n,i,4,t]
        }
      }
    }


    #applies a values of -999 to all grid cells corresponding to sea surface
    for (t in 1:nt)
    {
      for(n in 2:nlon)
      {
        for(i in 1:(nlat-1))
        {
              if (seamask_array[(n-1),(i+1)]==1)
              {
                emidu2_array[n,i,t] <- -999
                ustt1_array[n,i,t] <- -999
                ustt2_array[n,i,t] <- -999
                ustt3_array[n,i,t] <- -999
                ustt4_array[n,i,t] <- -999
                od_array[n,i,t] <- -999
                ustar_array[n,i,t] <- -999
                roughcor_array[n,i,t] <- -999
                smoiscor_array[n,i,t] <- -999
              }
        }
      }
    }

    
   # transform arrays (lon,lat,time) to matrices (lonlat, time) 
   # decomposition of the 4 bins for ustt (ustt1,ustt2,ustt3,ustt4)
    nlon <- ((matrixlenlat[r,2]-matrixlenlat[r,1])+1) #lon 47 NorthAfrica
    nlat <- ((matrixlenlat[r,4]-matrixlenlat[r,3])+1) #lat 41 NorthAfrica

    od_matrix <- matrix(0,nt,nlon*nlat)
    emidu_matrix <- matrix(0,nt,nlon*nlat)
    ustar_matrix <- matrix(0,nt,nlon*nlat)
    ustt1_matrix <- matrix(0,nt,nlon*nlat)
    ustt2_matrix <- matrix(0,nt,nlon*nlat)
    ustt3_matrix <- matrix(0,nt,nlon*nlat)
    ustt4_matrix <- matrix(0,nt,nlon*nlat)
    roughcor_matrix <- matrix(0,nt,nlon*nlat)
    smoiscor_matrix <- matrix(0,nt,nlon*nlat)

    k <- 0
    lati <- matrix(0,nt,nlon*nlat)
    long <- matrix(0,nt,nlon*nlat)

    for (t in 1:nt)
    {
      for(n in 1:nlon)
      {
        for(i in 1:nlat)
        {
          od_matrix[t,(i+(nlat*(n-1)))] <- od_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          emidu_matrix[t,(i+(nlat*(n-1)))] <- (emidu2_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t])

          ustar_matrix[t,(i+(nlat*(n-1)))] <- ustar_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          ustt1_matrix[t,(i+(nlat*(n-1)))] <- ustt1_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          ustt2_matrix[t,(i+(nlat*(n-1)))] <- ustt2_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          ustt3_matrix[t,(i+(nlat*(n-1)))] <- ustt3_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          ustt4_matrix[t,(i+(nlat*(n-1)))] <- ustt4_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          roughcor_matrix[t,(i+(nlat*(n-1)))] <- roughcor_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]
          smoiscor_matrix[t,(i+(nlat*(n-1)))] <- smoiscor_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t]


          lati[t,(i+(nlat*(n-1)))] <- i
          long[t,(i+(nlat*(n-1)))] <- n
          if (od_array[(n-1+matrixlenlat[r,1]),(i-1+matrixlenlat[r,3]),t] != -999)
          {
            k <- k+1
          }
        }
      }
    }
    od_matrix2 <- matrix(0,0,(k/8))
    emidu_matrix2 <- matrix(0,0,(k/8))
    ustar_matrix2 <- matrix(0,0,(k/8))
    ustt1_matrix2 <- matrix(0,0,(k/8))
    ustt2_matrix2 <- matrix(0,0,(k/8))
    ustt3_matrix2 <- matrix(0,0,(k/8))
    ustt4_matrix2 <- matrix(0,0,(k/8))
    roughcor_matrix2 <- matrix(0,0,(k/8))
    smoiscor_matrix2 <- matrix(0,0,(k/8))

    lati2 <- matrix(0,0,(k/8))
    long2 <- matrix(0,0,(k/8))
    for (t in 1:(nt))
    {
      vector <- as.vector(od_matrix[t,])
      vector <- vector[vector != -999]
      od_matrix2 <- rbind(od_matrix2,vector)

      vector2 <- as.vector(emidu_matrix[t,])
      vector2 <- vector2[vector2 != -999]
      emidu_matrix2 <- rbind(emidu_matrix2,vector2)

      vector3 <- as.vector(lati[t,])
      vector3 <- vector3[vector3 != -999]
      lati2 <- rbind(lati2,vector3)

      vector4 <- as.vector(long[t,])
      vector4 <- vector4[vector4 != -999]
      long2 <- rbind(long2,vector4)

      vector5 <- as.vector(ustar_matrix[t,])
      vector5 <- vector5[vector5 != -999]
      ustar_matrix2 <- rbind(ustar_matrix2,vector5)

      vector6 <- as.vector(ustt1_matrix[t,])
      vector6 <- vector6[vector6 != -999]
      ustt1_matrix2 <- rbind(ustt1_matrix2,vector6)

      vector7 <- as.vector(ustt2_matrix[t,])
      vector7 <- vector7[vector7 != -999]
      ustt2_matrix2 <- rbind(ustt2_matrix2,vector7)

      vector8 <- as.vector(ustt3_matrix[t,])
      vector8 <- vector8[vector8 != -999]
      ustt3_matrix2 <- rbind(ustt3_matrix2,vector8)

      vector9 <- as.vector(ustt4_matrix[t,])
      vector9 <- vector9[vector9 != -999]
      ustt4_matrix2 <- rbind(ustt4_matrix2,vector9)

      vector10 <- as.vector(roughcor_matrix[t,])
      vector10 <- vector10[vector10 != -999]
      roughcor_matrix2 <- rbind(roughcor_matrix2,vector10)

      vector11 <- as.vector(smoiscor_matrix[t,])
      vector11 <- vector11[vector11 != -999]
      smoiscor_matrix2 <- rbind(smoiscor_matrix2,vector11)
    }

    if (d==1)
    {
      stod_matrix2 <- od_matrix2
      stemidu_matrix2 <- emidu_matrix2

      stustar_matrix2 <- ustar_matrix2
      stustt1_matrix2 <- ustt1_matrix2
      stustt2_matrix2 <- ustt2_matrix2
      stustt3_matrix2 <- ustt3_matrix2
      stustt4_matrix2 <- ustt4_matrix2
      stroughcor_matrix2 <- roughcor_matrix2
      stsmoiscor_matrix2 <- smoiscor_matrix2

      stlong <- long2
      stlati <- lati2
    }
    else if (d==2)
    {
      matrixod <- rbind(stod_matrix2,od_matrix2)
      matrixem <- rbind(stemidu_matrix2,emidu_matrix2)
      matrixustar <-  rbind(stustar_matrix2,ustar_matrix2)
      matrixustt1 <-  rbind(stustt1_matrix2,ustt1_matrix2)
      matrixustt2 <-  rbind(stustt2_matrix2,ustt2_matrix2)
      matrixustt3 <-  rbind(stustt3_matrix2,ustt3_matrix2)
      matrixustt4 <-  rbind(stustt4_matrix2,ustt4_matrix2)
      matrixroughcor <- rbind(stroughcor_matrix2,roughcor_matrix2)
      matrixsmoiscor <- rbind(stsmoiscor_matrix2,smoiscor_matrix2)
      matrixlong <- rbind(stlong,long2)
      matrixlati <- rbind(stlati,lati2)
    }
    else
    {
      matrixod <- rbind(matrixod,od_matrix2)
      matrixem <- rbind(matrixem,emidu_matrix2)
      matrixustar <-  rbind(matrixustar,ustar_matrix2)
      matrixustt1 <-  rbind(matrixustt1,ustt1_matrix2)
      matrixustt2 <-  rbind(matrixustt2,ustt2_matrix2)
      matrixustt3 <-  rbind(matrixustt3,ustt3_matrix2)
      matrixustt4 <-  rbind(matrixustt4,ustt4_matrix2)
      matrixroughcor <- rbind(matrixroughcor,roughcor_matrix2)
      matrixsmoiscor <- rbind(matrixsmoiscor,smoiscor_matrix2)
      matrixlong <- rbind(matrixlong,long2)
      matrixlati <- rbind(matrixlati,lati2)
    }
    
  }
  if (r==1)
  {
    NorthAfrica_od <- matrixod
    NorthAfrica_em <- matrixem
    NorthAfrica_lat <- matrixlati
    NorthAfrica_lon <- matrixlong
    NorthAfrica_ustar <- matrixustar
    NorthAfrica_ustt1 <- matrixustt1
    NorthAfrica_ustt2 <- matrixustt2
    NorthAfrica_ustt3 <- matrixustt3
    NorthAfrica_ustt4 <- matrixustt4
    NorthAfrica_rc <- matrixroughcor
    NorthAfrica_mc <- matrixsmoiscor
    
    save(NorthAfrica_od, file = "NorthAfrica_od.RData")
    save(NorthAfrica_em, file = "NorthAfrica_em.RData")
    save(NorthAfrica_ustar, file = "NorthAfrica_ustar.RData")
    save(NorthAfrica_ustt1, file = "NorthAfrica_ustt1.RData")
    save(NorthAfrica_ustt2, file = "NorthAfrica_ustt2.RData")
    save(NorthAfrica_ustt3, file = "NorthAfrica_ustt3.RData")
    save(NorthAfrica_ustt4, file = "NorthAfrica_ustt4.RData")
    save(NorthAfrica_rc, file = "NorthAfrica_rc.RData")
    save(NorthAfrica_mc, file = "NorthAfrica_mc.RData")
    save(NorthAfrica_lat, file = "NorthAfrica_lat.RData")
    save(NorthAfrica_lon, file = "NorthAfrica_lon.RData")
  }
  if (r==2)
  {
    SouthAfrica_od <- matrixod
    SouthAfrica_em <- matrixem
    SouthAfrica_lat <- matrixlati
    SouthAfrica_lon <- matrixlong
    SouthAfrica_ustar <- matrixustar
    SouthAfrica_ustt1 <- matrixustt1
    SouthAfrica_ustt2 <- matrixustt2
    SouthAfrica_ustt3 <- matrixustt3
    SouthAfrica_ustt4 <- matrixustt4
    SouthAfrica_rc <- matrixroughcor
    SouthAfrica_mc <- matrixsmoiscor
    
    save(SouthAfrica_od, file = "SouthAfrica_od.RData")
    save(SouthAfrica_em, file = "SouthAfrica_em.RData")
    save(SouthAfrica_ustar, file = "SouthAfrica_ustar.RData")
    save(SouthAfrica_ustt1, file = "SouthAfrica_ustt1.RData")
    save(SouthAfrica_ustt2, file = "SouthAfrica_ustt2.RData")
    save(SouthAfrica_ustt3, file = "SouthAfrica_ustt3.RData")
    save(SouthAfrica_ustt4, file = "SouthAfrica_ustt4.RData")
    save(SouthAfrica_rc, file = "SouthAfrica_rc.RData")
    save(SouthAfrica_mc, file = "SouthAfrica_mc.RData")
    save(SouthAfrica_lat, file = "SouthAfrica_lat.RData")
    save(SouthAfrica_lon, file = "SouthAfrica_lon.RData")
  }
  if (r==3)
  {
    MiddleEast_od <- matrixod
    MiddleEast_em <- matrixem
    MiddleEast_lat <- matrixlati
    MiddleEast_lon <- matrixlong
    MiddleEast_ustar <- matrixustar
    MiddleEast_ustt1 <- matrixustt1
    MiddleEast_ustt2 <- matrixustt2
    MiddleEast_ustt3 <- matrixustt3
    MiddleEast_ustt4 <- matrixustt4
    MiddleEast_rc <- matrixroughcor
    MiddleEast_mc <- matrixsmoiscor
    
    save(MiddleEast_od, file = "MiddleEast_od.RData")
    save(MiddleEast_em, file = "MiddleEast_em.RData")
    save(MiddleEast_ustar, file = "MiddleEast_ustar.RData")
    save(MiddleEast_ustt1, file = "MiddleEast_ustt1.RData")
    save(MiddleEast_ustt2, file = "MiddleEast_ustt2.RData")
    save(MiddleEast_ustt3, file = "MiddleEast_ustt3.RData")
    save(MiddleEast_ustt4, file = "MiddleEast_ustt4.RData")
    save(MiddleEast_rc, file = "MiddleEast_rc.RData")
    save(MiddleEast_mc, file = "MiddleEast_mc.RData")
    save(MiddleEast_lat, file = "MiddleEast_lat.RData")
    save(MiddleEast_lon, file = "MiddleEast_lon.RData")
  }
  if (r==4)
  {
    NorthwestAsia_od <- matrixod
    NorthwestAsia_em <- matrixem
    NorthwestAsia_lat <- matrixlati
    NorthwestAsia_lon <- matrixlong
    NorthwestAsia_ustar <- matrixustar
    NorthwestAsia_ustt1 <- matrixustt1
    NorthwestAsia_ustt2 <- matrixustt2
    NorthwestAsia_ustt3 <- matrixustt3
    NorthwestAsia_ustt4 <- matrixustt4
    NorthwestAsia_rc <- matrixroughcor
    NorthwestAsia_mc <- matrixsmoiscor
    
    save(NorthwestAsia_od, file = "NorthwestAsia_od.RData")
    save(NorthwestAsia_em, file = "NorthwestAsia_em.RData")
    save(NorthwestAsia_ustar, file = "NorthwestAsia_ustar.RData")
    save(NorthwestAsia_ustt1, file = "NorthwestAsia_ustt1.RData")
    save(NorthwestAsia_ustt2, file = "NorthwestAsia_ustt2.RData")
    save(NorthwestAsia_ustt3, file = "NorthwestAsia_ustt3.RData")
    save(NorthwestAsia_ustt4, file = "NorthwestAsia_ustt4.RData")
    save(NorthwestAsia_rc, file = "NorthwestAsia_rc.RData")
    save(NorthwestAsia_mc, file = "NorthwestAsia_mc.RData")
    save(NorthwestAsia_lat, file = "NorthwestAsia_lat.RData")
    save(NorthwestAsia_lon, file = "NorthwestAsia_lon.RData")
  }
  if (r==5)
  {
    SouthwestAsia_od <- matrixod
    SouthwestAsia_em <- matrixem
    SouthwestAsia_lat <- matrixlati
    SouthwestAsia_lon <- matrixlong
    SouthwestAsia_ustar <- matrixustar
    SouthwestAsia_ustt1 <- matrixustt1
    SouthwestAsia_ustt2 <- matrixustt2
    SouthwestAsia_ustt3 <- matrixustt3
    SouthwestAsia_ustt4 <- matrixustt4
    SouthwestAsia_rc <- matrixroughcor
    SouthwestAsia_mc <- matrixsmoiscor
    
    save(SouthwestAsia_od, file = "SouthwestAsia_od.RData")
    save(SouthwestAsia_em, file = "SouthwestAsia_em.RData")
    save(SouthwestAsia_ustar, file = "SouthwestAsia_ustar.RData")
    save(SouthwestAsia_ustt1, file = "SouthwestAsia_ustt1.RData")
    save(SouthwestAsia_ustt2, file = "SouthwestAsia_ustt2.RData")
    save(SouthwestAsia_ustt3, file = "SouthwestAsia_ustt3.RData")
    save(SouthwestAsia_ustt4, file = "SouthwestAsia_ustt4.RData")
    save(SouthwestAsia_rc, file = "SouthwestAsia_rc.RData")
    save(SouthwestAsia_mc, file = "SouthwestAsia_mc.RData")
    save(SouthwestAsia_lat, file = "SouthwestAsia_lat.RData")
    save(SouthwestAsia_lon, file = "SouthwestAsia_lon.RData")
  }
  if (r==6)
  {
    NortheastAsia_od <- matrixod
    NortheastAsia_em <- matrixem
    NortheastAsia_lat <- matrixlati
    NortheastAsia_lon <- matrixlong
    NortheastAsia_ustar <- matrixustar
    NortheastAsia_ustt1 <- matrixustt1
    NortheastAsia_ustt2 <- matrixustt2
    NortheastAsia_ustt3 <- matrixustt3
    NortheastAsia_ustt4 <- matrixustt4
    NortheastAsia_rc <- matrixroughcor
    NortheastAsia_mc <- matrixsmoiscor
    
    save(NortheastAsia_od, file = "NortheastAsia_od.RData")
    save(NortheastAsia_em, file = "NortheastAsia_em.RData")
    save(NortheastAsia_ustar, file = "NortheastAsia_ustar.RData")
    save(NortheastAsia_ustt1, file = "NortheastAsia_ustt1.RData")
    save(NortheastAsia_ustt2, file = "NortheastAsia_ustt2.RData")
    save(NortheastAsia_ustt3, file = "NortheastAsia_ustt3.RData")
    save(NortheastAsia_ustt4, file = "NortheastAsia_ustt4.RData")
    save(NortheastAsia_rc, file = "NortheastAsia_rc.RData")
    save(NortheastAsia_mc, file = "NortheastAsia_mc.RData")
    save(NortheastAsia_lat, file = "NortheastAsia_lat.RData")
    save(NortheastAsia_lon, file = "NortheastAsia_lon.RData")
  }
  if (r==7)
  {
    Australia_od <- matrixod
    Australia_em <- matrixem
    Australia_lat <- matrixlati
    Australia_lon <- matrixlong
    Australia_ustar <- matrixustar
    Australia_ustt1 <- matrixustt1
    Australia_ustt2 <- matrixustt2
    Australia_ustt3 <- matrixustt3
    Australia_ustt4 <- matrixustt4
    Australia_rc <- matrixroughcor
    Australia_mc <- matrixsmoiscor
    
    save(Australia_od, file = "Australia_od.RData")
    save(Australia_em, file = "Australia_em.RData")
    save(Australia_ustar, file = "Australia_ustar.RData")
    save(Australia_ustt1, file = "Australia_ustt1.RData")
    save(Australia_ustt2, file = "Australia_ustt2.RData")
    save(Australia_ustt3, file = "Australia_ustt3.RData")
    save(Australia_ustt4, file = "Australia_ustt4.RData")
    save(Australia_rc, file = "Australia_rc.RData")
    save(Australia_mc, file = "Australia_mc.RData")
    save(Australia_lat, file = "Australia_lat.RData")
    save(Australia_lon, file = "Australia_lon.RData")
  }
  if (r==8)
  {
    SouthAmerica_od <- matrixod
    SouthAmerica_em <- matrixem
    SouthAmerica_lat <- matrixlati
    SouthAmerica_lon <- matrixlong
    SouthAmerica_ustar <- matrixustar
    SouthAmerica_ustt1 <- matrixustt1
    SouthAmerica_ustt2 <- matrixustt2
    SouthAmerica_ustt3 <- matrixustt3
    SouthAmerica_ustt4 <- matrixustt4
    SouthAmerica_rc <- matrixroughcor
    SouthAmerica_mc <- matrixsmoiscor
    
    save(SouthAmerica_od, file = "SouthAmerica_od.RData")
    save(SouthAmerica_em, file = "SouthAmerica_em.RData")
    save(SouthAmerica_ustar, file = "SouthAmerica_ustar.RData")
    save(SouthAmerica_ustt1, file = "SouthAmerica_ustt1.RData")
    save(SouthAmerica_ustt2, file = "SouthAmerica_ustt2.RData")
    save(SouthAmerica_ustt3, file = "SouthAmerica_ustt3.RData")
    save(SouthAmerica_ustt4, file = "SouthAmerica_ustt4.RData")
    save(SouthAmerica_rc, file = "SouthAmerica_rc.RData")
    save(SouthAmerica_mc, file = "SouthAmerica_mc.RData")
    save(SouthAmerica_lat, file = "SouthAmerica_lat.RData")
    save(SouthAmerica_lon, file = "SouthAmerica_lon.RData")
  }
  if (r==9)
  {
    NorthAmerica_od <- matrixod
    NorthAmerica_em <- matrixem
    NorthAmerica_lat <- matrixlati
    NorthAmerica_lon <- matrixlong
    NorthAmerica_ustar <- matrixustar
    NorthAmerica_ustt1 <- matrixustt1
    NorthAmerica_ustt2 <- matrixustt2
    NorthAmerica_ustt3 <- matrixustt3
    NorthAmerica_ustt4 <- matrixustt4
    NorthAmerica_rc <- matrixroughcor
    NorthAmerica_mc <- matrixsmoiscor
    
    save(NorthAmerica_od, file = "NorthAmerica_od.RData")
    save(NorthAmerica_em, file = "NorthAmerica_em.RData")
    save(NorthAmerica_ustar, file = "NorthAmerica_ustar.RData")
    save(NorthAmerica_ustt1, file = "NorthAmerica_ustt1.RData")
    save(NorthAmerica_ustt2, file = "NorthAmerica_ustt2.RData")
    save(NorthAmerica_ustt3, file = "NorthAmerica_ustt3.RData")
    save(NorthAmerica_ustt4, file = "NorthAmerica_ustt4.RData")
    save(NorthAmerica_rc, file = "NorthAmerica_rc.RData")
    save(NorthAmerica_mc, file = "NorthAmerica_mc.RData")
    save(NorthAmerica_lat, file = "NorthAmerica_lat.RData")
    save(NorthAmerica_lon, file = "NorthAmerica_lon.RData")
  }
  if (r==10)
  {
    Europe_od <- matrixod
    Europe_em <- matrixem
    Europe_lat <- matrixlati
    Europe_lon <- matrixlong
    Europe_ustar <- matrixustar
    Europe_ustt1 <- matrixustt1
    Europe_ustt2 <- matrixustt2
    Europe_ustt3 <- matrixustt3
    Europe_ustt4 <- matrixustt4
    Europe_rc <- matrixroughcor
    Europe_mc <- matrixsmoiscor
    
    save(Europe_od, file = "Europe_od.RData")
    save(Europe_em, file = "Europe_em.RData")
    save(Europe_ustar, file = "Europe_ustar.RData")
    save(Europe_ustt1, file = "Europe_ustt1.RData")
    save(Europe_ustt2, file = "Europe_ustt2.RData")
    save(Europe_ustt3, file = "Europe_ustt3.RData")
    save(Europe_ustt4, file = "Europe_ustt4.RData")
    save(Europe_rc, file = "Europe_rc.RData")
    save(Europe_mc, file = "Europe_mc.RData")
    save(Europe_lat, file = "Europe_lat.RData")
    save(Europe_lon, file = "Europe_lon.RData")
  }
}

