I am loading multiple NetCDF files in python 3 (on Windows) using MFDataset, and am wanting to loop through the variables and look at the attributes. I am able to load and read the data fine, however I am wanting to access the attributes associated with the variables.
import netCDF4
file = r"C:\netcdf_files\data*.nc" #wildcarded NetCDF files
with netCDF4.MFDataset(file) as src:
for name, variable in src.variables.items():
for attrname in variable.ncattrs():
print("{} -- {}".format(attrname, variable.getncattr(attrname)))
When the above snippet runs I am given a the following exception:
Traceback (most recent call last):
File "netCDF4\_netCDF4.pyx", line 5026, in netCDF4._netCDF4._Variable.__getattr__ (netCDF4\_netCDF4.c:57060)
KeyError: 'getncattr'
For a single file NetCDF file, loaded with Dataset:
import netCDF4
file = r"C:\netcdf_files\data1.nc" #single, explicit file
with netCDF4.Dataset(file) as src:
for name, variable in src.variables.items():
for attrname in variable.ncattrs():
print("{} -- {}".format(attrname, variable.getncattr(attrname)))
The above runs just fine, printing out the variable attributes as desired.
I am still very new to working with NetCDF files - is there a way to access the variable attributes when loading data using MFDataset? Thanks!