How to extract variable names from netCDF file in R?

I am writing a function in R to extract some air quality simulation data from netCDF files. I have installed the package "ncdf".

To let other users or me choose which variables are extracted from the netCDF file, I would like to extract the names of all the variables in the file so that I can list them in a simple list, not just the print.ncdf() file, to provide too much information. Is there any way to do this?

I tried unlist() in the var field of the ncdf object, but it seemed like it also returned the contents ...

I googled and looked for a stack overflow *, but didn't seem to find an answer, so your help is much appreciated.

Thank you very much in advance.

+8
r netcdf
source share
2 answers

If your ncdf object is called nc , then pretty simple:

 names(nc$var) 

For example, using the dataset downloaded here , for example (since you did not specify one):

 nc <- open.ncdf("20130128-ABOM-L4HRfnd-AUS-v01-fv01_0-RAMSSA_09km.nc") names(nc$var) [1] "analysed_sst" "analysis_error" "sea_ice_fraction" "mask" 
+12
source share

Now is the year 2016. The ncdf package is deprecated. The same code as the plannapus user request is now:

 library(ncdf4) netcdf.file <- "flux.nc" nc = ncdf4::nc_open(netcdf.file) variables = names(nc[['var']]) #print(nc) 

Note from the documentation:

 Package: ncdf Title: Interface to Unidata netCDF Data Files Maintainer: Brian Ripley <ripley@stats.ox.ac.uk> Version: 1.6.9 Author: David Pierce <dpierce@ucsd.edu> Description: This is deprecated and will be removed from CRAN in early 2016: use 'RNetCDF' or 'ncdf4' instead. Newer package "ncdf4" is designed to work with the netcdf library version 4, and supports features such as compression and chunking.Unfortunately, for various reasons the ncdf4 package must have a different API than the ncdf package. 

Note from the maintainer's homepage:

 Package ncdf4 -- use this for new code The "ncdf4" package is designed to work with the netcdf library, version 4. It includes the ability to use compression and chunking, which seem to be some of the most anticipated benefits of the version 4 library. Note that the API of ncdf4 has to be different from the API of ncdf, unfortunately. New code should use ncdf4, not ncdf. 

http://cirrus.ucsd.edu/~pierce/ncdf/

+5
source share

All Articles