correlation 6 varibiles in netcdf

Hi all,

Today I am trying to calculate a partial correlation between 6 variables in a single NetCDF and write the output (estimate) to one single file. Please help in improving the loop :slight_smile:

 library(ppcor)
 mods <- nc_open("Obs_model_1981-2018.nc")
 lon <- ncvar_get(mods, varid = "lon")
 lat <- ncvar_get(mods, varid = "lat")
nx <- length(lon)
ny <- length(lat)
      obss <- ncvar_get(mods, "obs")
      mods1 <- ncvar_get(mods, "mod1")
     mods2 <- ncvar_get(mods, "mod2")
     mods3 <- ncvar_get(mods, "mod3")
    mods4 <- ncvar_get(mods, "mod4")
     mods5 <- ncvar_get(mods, "mod5")

rl=array(0, c(nx,ny))
err=array(0, c(nx,ny))
for (x in 1:nx) {
      print(x)
  for (y in 1:ny) {
    pp =data.frame(obss,mods1,mods2,mods3,mods4,mods5)
    rl[x,y] = pcor(pp, "pearson")

   }
}

dump to netcdf

 time <- ncdim_def("Time","months", 1, unlim=TRUE)
lons <- ncdim_def("longitude", "degrees_east", lon)
lats <- ncdim_def("latitude", "degrees_north", lat)

var_temp <- ncvar_def("PC", "years", list(lons, lats, time), longname="PCorrelation", mv) 
ncnew <- nc_create("PCorrelation.nc", list(var_temp))
ncvar_put(ncnew, var_temp, rl, start=c(1,1,1), count=c(nx,ny,1))
nc_close(ncnew)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.