Repository navigation
Conversation
SpatialAccessor.get_weights() indexes the lower and upper bounds by position (e.g. domain_bounds[:, 0]), which assumes the bounds are stored as (lat, bnds). Bounds stored as (bnds, lat), which is valid CF and what a transpose produces, made get_weights() return meaningless weights along the bounds dimension. average() then failed with a misleading KeyError, or with an AlignmentError when a longitude cell spans the prime meridian. Identify the bounds dimension with the existing _get_bounds_dim() helper and move it last before computing the weights. Bounds with extra dimensions, such as time-varying bounds, now raise a clear ValueError instead of returning wrong weights. Fixes xCDAT#750 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SpatialAccessor.get_weights()computes the axis weights from positional slices of the bounds (domain_bounds[:, 0],domain_bounds[:, 1]), so it only works when the bounds are stored as(lat, bnds)/(lon, bnds). With bounds stored as(bnds, lat)/(bnds, lon)(valid CF, and what atranspose()produces),get_weights()silently returns weights along thebndsdimension andaverage()fails:Without a longitude cell that spans the prime meridian, the same data fails with
KeyError: 'The weights DataArray does not include an X axis, or the dimension names are not the same.'instead.As discussed in #750, the bounds dimension is now identified by name instead of by position:
get_weights()finds it with the existingbounds._get_bounds_dim()helper (already used for the temporal weights) and moves it last before the per-axis weight methods run. The weight methods themselves are unchanged, so bounds already stored as(lat, bnds)give the same results as before.Bounds with extra dimensions, such as time-varying
(time, lat, bnds)bounds (see #797), are still not supported. They now raise the helper'sValueError("Bounds variable must have exactly one more dimension than the coordinate variable ...") instead ofget_weights()returning weights on the wrong dimensions andaverage()failing with theKeyErrorabove.Tests:
TestGetWeights::test_dataset_weights_for_bounds_with_bounds_dim_first,TestGetWeights::test_raises_error_if_bounds_have_extra_dimsandTestAverage::test_spatial_average_for_bounds_with_bounds_dim_first. All three fail onmainand pass with this change.tests/test_regrid.pyfail the same way onmain, because ESMF/esmpy has no Windows build (esmpy was stubbed locally so that the test modules import).tests/test_spatial.pyandtests/test_bounds.py97 passed (94 onmain, where the 3 new tests fail).(lat, bnds), annvbounds dimension, non-indexlat/loncoordinates ony/xdimensions, Dask-backed bounds anddata_var=are the same as onmain; the transposed variants of the Dask-backed anddata_var=cases now work too.pre-commit run --all-files(ruff, ruff-format, mypy) passes.AI assistance: the fix, the tests and this description were prepared with Claude Code.
Checklist
Any dependent changes have been merged and published in downstream modules(N/A, no dependent changes)If applicable:
I have made corresponding changes to the documentation(N/A, no API or documentation change)I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected)(N/A, not a breaking change)🤖 Generated with Claude Code