R-Opening multiple netcdf4 at once

47 Views Asked by At

I am trying to open approximately 10 ncdf4 files at once and extract the data contained within.

After setting the correct wd, I have tried:

temp = list.files(pattern='*.nc')

myfiles = lapply(temp,nc_open)

nav_lat_test <- ncvar_get(myfiles[1],"nav_lat")

but this returns an error

Error in ncvar_get(myfiles[1], "nav_lat") : 
  first argument (nc) is not of class ncdf4!

When I check the class of myfiles[1] it is a list, which is presumably why I cannot use ncvar_get on it. However, I do not know why this is a list. I understand why myfiles would be, but not myfiles[1].

1

There are 1 best solutions below

0
On BEST ANSWER
nav_lat_test <- ncvar_get(myfiles[[1]],"nav_lat")

Use [[]] to select a single element, and [] to return a list of elements. In this case, you want a single element.