7

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.

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

7

I would add here that both Datasets and DataArrays can have attributes, both called with .attrs e.g.
ds.attrs['global attr'] = 25
ds.variable_2.attrs['variable attr'] = 10
ds.variable_2.attrs['variable attr'] = 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.