Skip to content

Handle repeated values in xp in interpolate_1d - #4165

Open
bhaskargurram-ai wants to merge 1 commit into
Unidata:mainfrom
bhaskargurram-ai:fix/interpolate-duplicate-points
Open

bhaskargurram-ai wants to merge 1 commit into
Unidata:mainfrom
bhaskargurram-ai:fix/interpolate-duplicate-points

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown

Description Of Changes

When the smallest value of xp is repeated, interpolate_1d (and log_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_water is NaN for an observed sounding with repeated pressure levels, and works once the duplicates are dropped.

>>> p = [1000, 950, 900, 850, 800, 700, 600, 500, 500] * units.hPa
>>> td = [20, 18, 15, 12, 9, 3, -5, -15, -15] * units.degC
>>> precipitable_water(p, td)
<Quantity(nan, 'millimeter')>

Changes:

  • When the points below and above have the same coordinate, the interpolation weight is 0 (the value of the point below is used) instead of 0/0.
  • xp is 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 xp already gave finite results. This makes the bottom end match. dopplershift suggested comparing with np.interp: like np.interp, there's no NaN now. At exactly a repeated value, np.interp returns 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 of xp, increasing and decreasing input, and log_interpolate_1d to a repeated top pressure level
  • tests/calc/test_indices.py: precipitable_water with a repeated top level matches the result without the duplicate

Those that hit the bottom-end case fail on main and all pass with this change. tests/interpolate and tests/calc pass. Ruff and flake8 are clean.

Checklist

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
bhaskargurram-ai requested a review from a team as a code owner October 18, 2026 03:57
@bhaskargurram-ai
bhaskargurram-ai requested review from dcamron and removed request for a team October 18, 2026 03:57
@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Precipitable Water is Nan

2 participants