Fix ESMF read casting elementConn before checking for padding - #1746
Draft
cmdupuis3 wants to merge 1 commit into
Draft
Fix ESMF read casting elementConn before checking for padding#1746cmdupuis3 wants to merge 1 commit into
elementConn before checking for padding#1746cmdupuis3 wants to merge 1 commit into
Conversation
`_read_esmf` cast `elementConn` to INT_DTYPE and only then compared against
INT_FILL_VALUE, so the padding was never recognized:
- decoded (`mask_and_scale=True`), xarray turns ESMF's -1 into NaN and the
cast is platform-dependent -- arm64 gives 0, so the padding decodes to
-1, a negative index that silently wraps to the last node
- undecoded, the raw -1 survives the cast and decodes to -2
Locate the padding before the cast, from `numElementConn`, which states each
face's size directly; also treat NaN as padding whatever that count claims.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Closes #1747
Overview
_read_esmfcastselementConntoINT_DTYPEand only then compares againstINT_FILL_VALUE, so the padding is recognized in neither of the two ways xarray can hand it over. With CF decoding on (mask_and_scale=True, the default), xarray replaces ESMF's -1 with NaN and promotes the array to float; casting NaN to an integer dtype is platform-dependent, and on arm64 it yields 0, so the padding decodes to -1. With decoding off, the raw -1 survives the cast and decodes to -2. Both are negative indices that silently wrap to the end of the coordinate arrays instead of being treated as fill.The fix locates the padding before the cast, from
numElementConn— the format's own statement of each face's node count — and additionally treats NaN as padding whatever that count claims.The test builds the ESMF dataset by hand instead of going through
_encode_esmf, so it exercises the reader in isolation and covers third-party ESMF files rather than only our own output. It is parametrized overmask_and_scaleto pin both decoding paths.PR Checklist
General
Testing & Benchmarking
Documentation and Examples
docs/api.rst; internal (private) function names start with an underscore (_)AI Disclosure
AI Usage: Claude Opus 5