Repository navigation
Fix k_index failing when temperature and dewpoint use different units - #4231
Open
1915rising wants to merge 1 commit into
Open
1915rising wants to merge 1 commit into
1915rising wants to merge 1 commit into
Conversation
1915rising
requested review from
dopplershift
and removed request for
a team
November 27, 2026 17:10
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.
Fixes #4225.
What broke.
k_indexraisedpint.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_indexinsrc/metpy/calc/thermo.pyconverts 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_unitsintests/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.