Skip to content

Commit a8c9ff1

Browse files
committed
Fix UxDataset constructor and to_xarray()
1 parent c4e2758 commit a8c9ff1

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

uxarray/core/dataset.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,19 @@ def __init__(
9090
else:
9191
self._uxgrid = uxgrid
9292

93+
# As of xarray's 2026.4.0, `xr.Dataset(xr.Dataset)` is prohibited;
94+
# hence this check, i.e. If we get `xr.Dataset` as input, use its `data_vars`
95+
# as `dict` and
96+
if args and isinstance(args[0], xr.Dataset):
97+
ds = args[0]
98+
args = (dict(ds.data_vars),) + args[1:]
99+
kwargs.setdefault("coords", dict(ds.coords))
100+
kwargs.setdefault("attrs", ds.attrs)
101+
93102
super().__init__(*args, **kwargs)
94103

104+
# super().__init__(*args, **kwargs)
105+
95106
# declare plotting accessor
96107
plot = UncachedAccessor(UxDatasetPlotAccessor)
97108
remap = UncachedAccessor(RemapAccessor)
@@ -627,9 +638,9 @@ def to_xarray(self, grid_format: str = "UGRID") -> xr.Dataset:
627638
"""
628639
if grid_format == "HEALPix":
629640
ds = self.rename_dims({"n_face": "cell"})
630-
return xr.Dataset(ds)
641+
return xr.Dataset(ds.data_vars, coords=ds.coords, attrs=ds.attrs)
631642

632-
return xr.Dataset(self)
643+
return xr.Dataset(self.data_vars, coords=self.coords, attrs=self.attrs)
633644

634645
def get_dual(self):
635646
"""Compute the dual mesh for a dataset, returns a new dataset object.

0 commit comments

Comments
 (0)