Skip to content

DataArray attributes not present in DataSet. Coherency problem between DataSet and NetCDF file #5208

@oliviermarti

Description

@oliviermarti

When I create a DataSet from DataArrays, attributes are lost.

When are create attributes in a DataSet, they are know shown by print (DataSet), but are written in the NetCDF file.

Below is python code showing the xarray behaviour in details.

My requests :

  • When creating a DataSet from DataArrays, DataArrays attributes should be incorporated in the DataSet. (maybe optional)
  • Attributes present in a DataSet should appear with a print (DataSet). Like for DataArrays.

Thanks,

Olivier

#!/usr/bin/env python
# coding: utf-8
import numpy  as np
import xarray as xr

# Creates DataArrays
nt = 4
time = np.arange (nt) * 86400.0
time = xr.DataArray (time, coords=[time,], dims=["time",])
aa   = time * 2.0

# Adding attributes to DataArrays
time.attrs['units'] = "second"
aa.attrs['units']   = "whatever"

# Attributes are visible in the DataArrays
print ('----------> time DataArray: ')
print (time)
print ('----------> aa DataArray : ' )
print (aa)
print ('----------> aa attributes : ')
print (aa.attrs )

# Creating a Dataset
ds = xr.Dataset(
    { "aa": (["time",], aa),  },
    coords={"time": (["time",], time), },   ) 

# Attributes are not visible in the Dataset
print ('----------> DataSet before setting attributes')
print (ds)
# My request #1 : attributes of the DataArrays should be added to the DataSet (may be optional)
print ('----------> Attributes of aa in DataSet : none')
print ( ds['aa'].attrs )
print ('----------> Attributes of aa outside DataSet : still here')
print ( aa.attrs )

print ('----------> Attributes are not written to the NetCDF file')
ds.to_netcdf ('sample1.nc')

# Adding attributes directly to the Dataset
ds['time'].attrs['units'] = "second"
ds['aa'].attrs['units']   = "whatever"

# Attributes are still not visible in the Dataset
print ('----------> DataSet after setting attributes : attributes not shown' )
print (ds)
# My request #2 : attributes added to the DataSet should be printed

print ('----------> But they are written in the NetCDF file')
ds.to_netcdf ('sample2.nc')
# MyRequest : coherency between the DataSet and the NetCDF file

# What if I read a NetCDF file
dt = xr.open_dataset ( 'sample2.nc')

print ('----------> DataSet read in a NetCDF file : Attributes are not shown')
print (dt)

print  ('----------> Attributes of aa in DataSet : present')
print ( dt['aa'].attrs )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions