juseg / hyoga

Paleoglacier modelling framework
https://hyoga.io
GNU General Public License v3.0
8 stars 0 forks source link

Documentation examples fail on cf_xarray 0.8. #73

Closed juseg closed 1 month ago

juseg commented 1 year ago

Documentation examples fail on cf_xarray==0.8.0. Assigning new variables on the example data raises a MergeError:

Traceback (most recent call last):
  File "/home/julien/git/code/hyoga/examples/datasets/./plot_bedrock_isostasy.py", line 25, in <module>
    ds = ds.hyoga.assign_isostasy(hyoga.open.example('pism.alps.in.boot.nc'))
  File "/home/julien/git/code/hyoga/hyoga/core/accessor.py", line 240, in assign_isostasy
    return self.assign(
  File "/home/julien/git/code/hyoga/hyoga/core/accessor.py", line 186, in assign
    return self._ds.assign(variables)
  File "/usr/lib/python3.10/site-packages/xarray/core/dataset.py", line 6080, in assign
    data.update(results)
  File "/usr/lib/python3.10/site-packages/xarray/core/dataset.py", line 4946, in update
    merge_result = dataset_update_method(self, other)
  File "/usr/lib/python3.10/site-packages/xarray/core/merge.py", line 1104, in dataset_update_method
    return merge_core(
  File "/usr/lib/python3.10/site-packages/xarray/core/merge.py", line 772, in merge_core
    raise MergeError(
xarray.core.merge.MergeError: unable to determine if these variables should be coordinates or not in the merged result: {'mapping'}

This issue can be worked around by downgrading to cf_xarray<0.8.0.

juseg commented 1 month ago

The bug remains on later versions of cf-xarray and is caused by code such as:

with hyoga.open.example('pism.alps.out.2d.nc') as ds:
    with hyoga.open.example('pism.alps.in.boot.nc') as boot:
        ds = ds.hyoga.assign_isostasy(boot)

Internally ds.hyoga.getvar() uses the cf dataset accessor. Since cf_xarray == 0.8.0 this adds any grid mapping data-variable as a new coordinate, raising the MergeError in subsequent calls to ds.assign(). More at https://github.com/xarray-contrib/cf-xarray/issues/513.