Repository navigation
Handle repeated values in xp in interpolate_1d - #4165
Open
bhaskargurram-ai wants to merge 1 commit into
Open
bhaskargurram-ai wants to merge 1 commit into
bhaskargurram-ai wants to merge 1 commit into
Conversation
When the lowest value of xp was repeated (for instance the top pressure level of a sounding reported twice), interpolating at that value divided zero by zero and returned NaN. This made precipitable_water and other calculations that interpolate to the top of the profile return NaN for such soundings. Use the value of the point below when two neighbouring points have the same coordinate, and sort xp with a stable sort so that the first of the repeated points in the input is the one used. Fixes Unidata#1387
bhaskargurram-ai
requested review from
dcamron
and removed request for
a team
October 18, 2026 03:57
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 Of Changes
When the smallest value of
xpis repeated,interpolate_1d(andlog_interpolate_1d) returns NaN for a point at that value, with a "invalid value encountered in divide" warning. The search for the first point above x gives index 0, which is then bumped to 1, so the interpolation is between two points with the same coordinate and divides 0 by 0. For a sounding this happens when the top pressure level is reported twice, and anything that interpolates to the top of the profile then returns NaN. That's what happens in #1387:precipitable_wateris NaN for an observed sounding with repeated pressure levels, and works once the duplicates are dropped.Changes:
xpis now sorted with a stable sort, so with repeated values the first of the repeated points in the input is the one used, whatever the input order. The Notes section of the docstring says so.As far as I can tell, repeated values in the interior and at the top end of
xpalready gave finite results. This makes the bottom end match. dopplershift suggested comparing withnp.interp: likenp.interp, there's no NaN now. At exactly a repeated value,np.interpreturns the last of the repeated points and this returns the first, which is what MetPy already did for interior repeats.Tests:
tests/interpolate/test_one_dimension.py: repeated values at the start and in the middle ofxp, increasing and decreasing input, andlog_interpolate_1dto a repeated top pressure leveltests/calc/test_indices.py:precipitable_waterwith a repeated top level matches the result without the duplicateThose that hit the bottom-end case fail on main and all pass with this change.
tests/interpolateandtests/calcpass. Ruff and flake8 are clean.Checklist