ai2cm / fv3net

explore the FV3 data for parameterization
MIT License
16 stars 3 forks source link

xtorch_harmonics: work around `xarray.merge` attribute promotion issue #2398

Closed spencerkclark closed 2 months ago

spencerkclark commented 2 months ago

When merging DataArrays, a known issue (pydata/xarray#5436) is that xarray will promote DataArray-level attributes to Dataset-level ones:

>>> a = xr.DataArray(0, name="a", attrs={"x": "y"})
>>> b = xr.DataArray(0, name="b", attrs={"m": "n"})
>>> xr.merge([a, b])
<xarray.Dataset>
Dimensions:  ()
Data variables:
    a        int64 0
    b        int64 0
Attributes:
    x:        y

This is generally not correct, and leads to distracting Dataset-level attributes on the results of xtorch_harmonics.roundtrip_filter. A suggested workaround is to convert DataArrays to Datasets prior to passing to merge: https://github.com/pydata/xarray/issues/5436#issuecomment-1255120923. This PR implements this suggestion and tests that DataArray-level attributes are no longer added at the Dataset level.

Coverage reports (updated automatically):