Is there some way to add a global attribute to a netCDF
file using xarray
? When I do something like hndl_nc['global_attribute'] = 25
, it just adds a new variable.
Xarray is an open source project and Python package that introduces labels in the form of dimensions, coordinates, and attributes on top of raw NumPy-like arrays, which allows for more intuitive, more concise, and less error-prone user experience.
In Xarray, directly indexing a Dataset
like hndl_nc['variable_name']
pulls out a DataArray
object. To get or set attributes, index .attrs
like hndl_nc.attrs['global_attribute']
or hndl_nc.attrs['global_attribute'] = 25
.
You can access both variables and attributes using Python's attribute syntax like hndl_nc.variable_or_attribute_name
, but this is a convenience feature that only works when the variable or attribute name does not conflict with a preexisting method or property, and cannot be used for setting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With