JuliaGeo / NetCDF.jl

NetCDF support for the julia programming language
http://juliageo.org/NetCDF.jl/
MIT License
115 stars 28 forks source link

Is there a way to return list of variables in netcdf? #181

Open alex-s-gardner opened 1 year ago

alex-s-gardner commented 1 year ago

I have a netcdf file and I want to programmatically determine the list of variables contained in the file. The only utility I could identify is NetCDF.info but it just prints a bunch of info to screen.

meggart commented 1 year ago

The NcFile type returned by NetCDF.open contains a dict holding paris of variable names and NcVar diskarrays. You can extract the variable names as follow:

nc = NetCDF.open(p)
varnames = keys(nc.vars)

This will include all variables including dimension variables. Also, if you want to make sure the NetCDF file gets immediately closed after the query and not only when gc is triggered you can use the do-syntax. So the following would return all variable names that are not a dimension name at the same time and make sure the file is immediately closed again.

NetCDF.open(p) do nc
    setdiff(keys(nc.vars),keys(nc.dim))
end