rfor-loopncdf4

How to populate data frame from thousands of ncdf files using a for-loop?


I'm trying to open thousands of netcdf files and pull the data into a dataframe that I can save as a csv file.

So after putting all the ncdf files names in a csv file as a list, I ended up using this code to create a dataframe populated with the data from the ncdf files.

vec0 <- vector()
time <- c(vec0,1:28413)

vec1 <- vector()
temp <- c(vec1,1:28413)

vec2 <- vector()
sphum <- c(vec2,1:28413)

vec3 <- vector() 
rain <- c(vec3,1:28413)

vec4 <- vector()
totprcp <- c(vec4,1:28413)

for (i in 1:length(filenames))
  {ncdata=nc_open(filenames[i]) 

 nctime=ncvar_get(ncdata,"time") 
  time[i] = nctime[1]

nctemp=ncvar_get(ncdata,"Tair_f_inst") 
  temp[i] = nctemp[1]

 nchum=ncvar_get(ncdata,"Qair_f_inst")
  sphum[i] = nchum[1]

  ncrain=ncvar_get(ncdata,"Rainf_tavg") 
  rain[i] = ncrain[1]

  ncprcp=ncvar_get(ncdata,"Rainf_f_tavg")
  totprcp[i] = ncprcp[1]
  nc_close(filenames[i])}

  cbind(time,temp,sphum,rain,totprcp)

But I'm only getting accurate data for the first row and its filling in a sequence of numbers (1 through 28412) for the rest of the rows. I think my mistake lies in the way I wrote the nc_close component of the code. Any ideas?


Solution

  • nc_close() should be applied to the nc object itself, not to the filename. I.e. to "An object of class ncdf4 (as returned by either function nc_open or function nc_create."

    So, you should use

    nc_close(ncdata)