Skip to content

Fix concat_<side>_guards coords assignment - #332

Merged
dschwoerer merged 2 commits into
masterfrom
fix-concat-coords
Feb 11, 2026
Merged

dschwoerer merged 2 commits into
masterfrom
fix-concat-coords

Conversation

@mikekryjak

Copy link
Copy Markdown
Collaborator

Fixes #331.

In short, _concat_upper_guards and its 3 friends for lower, outer and inner guards had coord assignments of this type:

xBOUT/xbout/region.py

Lines 1345 to 1353 in 9c634a4

# can't use commented out version, uncommented one works around xarray bug
# removing attrs
# https://github.com/pydata/xarray/issues/4415
# https://github.com/pydata/xarray/issues/4393
# da_inner = da_inner.assign_coords(**{xcoord: new_xcoord, ycoord: new_ycoord})
da_inner[xcoord].data[...] = new_xcoord.data
da_inner = da_inner.reset_index(xcoord).set_xindex(xcoord)
da_inner[ycoord].data[...] = new_ycoord.data
da_inner = da_inner.reset_index(ycoord).set_xindex(ycoord)

According to the comment, this approach was chosen due to a bug. I'm not sure if I've followed the Xarray issue chain properly, but it now seems fixed: pydata/xarray#4393

This approach doesn't work anymore because coordinates are read only by default.... and I am very confused why it worked for me not that long ago! I have replaced it with the proper Xarray assignment.

I based this PR off a commit before #317 because that PR breaks loading any simulations for me (#329). Let's merge this one after #329 is resolved.

Coords are read only by default.
This meant the existing assignment method raises a ValueError.
The fix ensures that coords are assigned properly for each of the four concat functions.
@mikekryjak mikekryjak added the bugfix Fix for a bug label Jan 26, 2026
@johnomotani

Copy link
Copy Markdown
Collaborator

Just for historical interest: the mentioned issues were closed as duplicates, not fixed. The original issue is still open (pydata/xarray#2180) but has no update since 2018, so guess it won't ever be fixed. This PR looks like a different workaround for the same issue, but that's not a problem as long as it works!

@dschwoerer
dschwoerer merged commit 1585bba into master Feb 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fix for a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bugs due to coordinates being read-only? Dimension attrs lost when creating new variable with that dimension

3 participants