Skip to content

Fix k_index failing when temperature and dewpoint use different units - #4231

Open
1915rising wants to merge 1 commit into
Unidata:mainfrom
1915rising:fix/4225-k-index-mixed-units
Open

1915rising wants to merge 1 commit into
Unidata:mainfrom
1915rising:fix/4225-k-index-mixed-units

Conversation

@1915rising

Copy link
Copy Markdown

Fixes #4225.

What broke. k_index raised pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (kelvin, degree_Celsius) when temperature was given in kelvin and dewpoint in degC — a normal mix, since dewpoint routines and model output do not all land on the same unit. The final expression adds the absolute dewpoint at 850 hPa to the 850–500 hPa temperature difference; with the inputs in different offset units, pint rejects that addition. The workaround was converting everything to kelvin by hand before calling.

Reproduction. Using the profile from the existing test_k_index (which includes exact 850/700/500 hPa levels): all-degC and all-kelvin inputs return 33.5 degC, temperature in degC with dewpoint in kelvin returns 33.5 degC, but temperature in kelvin with dewpoint in degC raised the OffsetUnitCalculusError above. After this fix all four combinations return 33.5 degC.

Fix. k_index in src/metpy/calc/thermo.py converts temperature and dewpoint to degC before interpolating to 850/700/500 hPa, so the arithmetic happens in a single unit. Interpolation is linear and the conversion is exact, so results for same-unit inputs are unchanged. +6 lines including the comment.

Tests. New regression test test_k_index_mixed_temperature_units in tests/calc/test_thermo.py: it fails on unmodified code with the OffsetUnitCalculusError and passes with the fix. tests/calc/test_thermo.py: 227 passed before, 228 passed after, no other changes.

@1915rising
1915rising requested a review from a team as a code owner November 27, 2026 17:10
@1915rising
1915rising requested review from dopplershift and removed request for a team November 27, 2026 17:10

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.

k_index fails with OffsetUnitCalculusError

1 participant