Repository navigation
Raise GridsMismatchError when open_dataset data is not on the grid - #1900
lucas-santos-f wants to merge 1 commit into
Conversation
open_dataset and open_mfdataset accepted data from another grid without complaint: dimensions that did not match the grid stayed unmapped (e.g. ncol), and a grid dimension with the wrong size went through as well. Check the mapped dataset against the grid and raise GridsMismatchError when a face, node or edge dimension has a different size, or when no data variable is on any of them. Part of UXARRAY#1853
|
Hi @lucas-santos-f, thank you for opening this PR! I wanted to send a quick followup to let you know that this is on the uxarray team's to-do list, but reviews might not happen for a while (possibly a few weeks or more). That is partly because the issue was originally triaged to be addressed a few weeks from now rather than as a high priority for right now. Another thing that could help significantly would be, if you get the chance, to update this PR such that it fully addresses the original issue (including the expanded scope in #1853 (comment)). I am guessing the expanded scope could be solved fully and concisely just by comparing inputs' uxgrids during Two other minor sidenotes: (1) a quick heads up, some of this may end up having merge conflicts with #1887 (which is a relatively high priority currently and will hopefully merge soon) so it might make sense to delay until after that merges, and (2) just a minor comment on the proposed changes here, for any new small helper functions it would be greatly preferred to introduce them somewhere other than api.py, such as in uxarray/core/utils.py instead. If you have any questions please don't hesitate to ask, and thank you again for working on this! |
Fixes part of #1853 but does not fully close it.
Overview
ux.open_dataset(grid, data)andux.open_mfdataset(grid, paths)never compared the data with the grid. Dimensions that_map_dataset_to_ugridcan't map were left as they were (thencol=5400in the issue's example), and a face, node or edge dimension with the wrong size went through as well.Both functions now call
_check_data_on_grid()after the dimension mapping. It raisesGridsMismatchErrorwhenn_face,n_nodeorn_edgehas a different size in the data than in the grid, or when the dataset has data variables but none of them is on any of those dimensions. For the issue's example:Behavior change: lat/lon data opened with a UGRID grid (e.g.
outRLL1deg_vortex.ncwithoutRLL1deg.ug) now raises too; it used to open with the data left onlat/lon.uxgrid.n_edgeis only read when the data has ann_edgedimension, so edge connectivity is built only in that case (also for an existingGridpassed in, as allowed since #1827). Both docstrings get aRaisessection, which covers the conformity note left to this issue in #1827.Not covered, from the follow-up comment on the issue:
ux.UxDataset({...}, uxgrid=...)with arrays from different grids still raisesxr.AlignmentErrorinstead ofGridsMismatchError.iseldown to the same number of faces) still goes through, here and inUxDataset(...). Catching it means comparing grid contents, not just sizes.Both go beyond checking sizes on open, and the comment's examples are all
UxDataset(...)calls, close to #1886, so I kept them out to keep this one small. Hence "part of"; if you'd rather track those in a new issue, I'll switch it to "Closes".Tests in
test/core/test_api.pycover the issue's example through both functions and ann_face/n_node/n_edgevariable smaller and larger than the grid's. All 7 cases fail onmainand pass here.pytest test: 1291 passed, 1 skipped (Python 3.13 on Windows; 1284 onmain). pre-commit is clean.#1887 changes the same lines in
api.py, so whichever goes in second needs a small rebase. I can do it on this side.Happy to adjust anything if needed.
PR Checklist
General
Testing & Benchmarking
Documentation and Examples
docs/api.rst; internal (private) function names start with an underscore (_)AI Disclosure
AI Usage: Claude Code (Claude Opus 5.5) traced the cause, wrote the fix and the tests, and ran the checks.
AI disclosure: this change was written with the help of Claude Code (Claude Opus 5.5).