Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit x, y coordinates, x & y bounds from conversion #330

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_ugrid_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def test_from_bounds(self):
}
},
)
assert set(uds.data_vars) == {"a", "b", "c", "grid_x", "grid_y"}
assert set(uds.data_vars) == {"a", "b", "c"}
assert uds["a"].dims == ("layer", "mesh2d_nFaces")
assert uds["b"].dims == ("x",)
assert uds["c"].dims == ()
Expand Down
7 changes: 5 additions & 2 deletions xugrid/core/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ def from_structured2d(

grids = []
dss = []
xy_vars = set() # store x, y, x_bounds, y_bounds to drop.
for name, args in topology.items():
x_bounds = None
y_bounds = None
Expand All @@ -594,6 +595,7 @@ def from_structured2d(
raise ValueError("x and y must be provided for bounds")
x_bounds = dataset[args["x_bounds"]]
y_bounds = dataset[args["y_bounds"]]
xy_vars.update((args["x_bounds"], args["y_bounds"]))
elif isinstance(args, tuple):
x, y = args
else:
Expand All @@ -619,10 +621,11 @@ def from_structured2d(
# Use subset to check that ALL dims of stackdims are present in the
# variable.
checkdims = set(stackdims)
xy_vars.update(checkdims)
ugrid_vars = [
name
for name, var in dataset.data_vars.items()
if checkdims.issubset(var.dims)
if checkdims.issubset(var.dims) and name not in xy_vars
]
dss.append(
dataset[ugrid_vars] # noqa: PD013
Expand All @@ -633,7 +636,7 @@ def from_structured2d(
grids.append(grid)

# Add the original dataset to include all non-UGRID variables.
dss.append(dataset)
dss.append(dataset.drop_vars(xy_vars, errors="ignore"))
# Then merge with compat="override". This'll pick the first available
# variable: i.e. it will prioritize the UGRID form.
merged = xr.merge(dss, compat="override")
Expand Down
Loading