adding attributes and metadata to a dataset using xarray code example

Example 1: xarray: create 2d dataset

In [1]: data = np.random.rand(4, 3)

In [2]: locs = ['IA', 'IL', 'IN']

In [3]: times = pd.date_range('2000-01-01', periods=4)

In [4]: foo = xr.DataArray(data, coords=[times, locs], dims=['time', 'space'])

In [5]: foo
Out[5]: 
<xarray.DataArray (time: 4, space: 3)>
array([[0.127, 0.967, 0.26 ],
       [0.897, 0.377, 0.336],
       [0.451, 0.84 , 0.123],
       [0.543, 0.373, 0.448]])
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA' 'IL' 'IN'

Example 2: adding attributes and metadata to a dataset using xarray

#import xarray
import xarray as xr

#open dataset
ds = xr.open_dataset(file_name.nc)

#add attributes and metadata in different ways
ds.attrs = {attribute name : attribute value}
ds.attrs[attribute name] = attribute value