Skip to content

Commit

Permalink
Add K Index calculate function. (#1990)
Browse files Browse the repository at this point in the history
* Add K Index calculation function.
  • Loading branch information
C2oWisComing authored Aug 9, 2021
1 parent 5c39de6 commit 79abf12
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/_templates/overrides/metpy.calc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Soundings
cape_cin
critical_angle
el
k_index
lcl
lfc
lifted_index
Expand Down
3 changes: 3 additions & 0 deletions docs/api/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ References
.. [Garratt1994] Garratt, J.R., 1994: *The Atmospheric Boundary Layer*. Cambridge
University Press, 316 pp.
.. [George1960] George, J.J., 1960: *Weather Forecasting for Aeronautics*. New York and
London Academic Press, 673 pp.
.. [Hobbs1977] Hobbs, P. V., and J. M. Wallace, 1977: *Atmospheric Science: An
Introductory Survey*. Academic Press, 350 pp.
Expand Down
49 changes: 49 additions & 0 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,55 @@ def lifted_index(pressure, temperature, parcel_profile):
return lifted_index


@exporter.export
@preprocess_and_wrap()
@check_units('[pressure]', '[temperature]', '[temperature]')
def k_index(pressure, temperature, dewpoint):
"""Calculate K Index from the pressure temperature and dewpoint.
K Index formula derived from [George1960]_:
K = (T850 - T500) + Td850 - (T700 - Td700)
where:
T850 is the temperature at 850 hPa
T700 is the temperature at 700 hPa
T500 is the temperature at 500 hPa
Td850 is the dewpoint at 850 hPa
Td700 is the dewpoint at 700 hPa
Calculation of the K Index is defined as the temperature difference between
the static instability between 850 hPa and 500 hPa, add with the moisture
at 850hPa, then subtract from the dryness of the airmass at 700 hPa.
Parameters
----------
pressure : `pint.Quantity`
Pressure level(s), in order from highest to lowest pressure
temperature : `pint.Quantity`
Temperature corresponding to pressure
dewpoint : `pint.Quantity`
Dewpoint temperature corresponding to pressure
Returns
-------
`pint.Quantity`
K Index
"""
# Find temperature and dewpoint at 850, 700 and 500 hPa
(t850, t700, t500), (td850, td700, _) = interpolate_1d(units.Quantity([850, 700, 500],
'hPa'), pressure, temperature,
dewpoint)

# Calculate k index.
k_index = ((t850 - t500) + td850 - (t700 - td700)).to(units.degC)

return k_index


@exporter.export
@add_vertical_dim_from_xarray
@preprocess_and_wrap(
Expand Down
30 changes: 27 additions & 3 deletions tests/calc/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
dewpoint_from_relative_humidity, dewpoint_from_specific_humidity,
dry_lapse, dry_static_energy, el, equivalent_potential_temperature,
exner_function, gradient_richardson_number, InvalidSoundingError,
isentropic_interpolation, isentropic_interpolation_as_dataset, lcl,
lfc, lifted_index, mixed_layer, mixed_layer_cape_cin, mixed_parcel,
mixing_ratio, mixing_ratio_from_relative_humidity,
isentropic_interpolation, isentropic_interpolation_as_dataset, k_index,
lcl, lfc, lifted_index, mixed_layer, mixed_layer_cape_cin,
mixed_parcel, mixing_ratio, mixing_ratio_from_relative_humidity,
mixing_ratio_from_specific_humidity, moist_lapse, moist_static_energy,
most_unstable_cape_cin, most_unstable_parcel, parcel_profile,
parcel_profile_with_lcl, parcel_profile_with_lcl_as_dataset,
Expand Down Expand Up @@ -1871,6 +1871,30 @@ def test_lifted_index():
assert_almost_equal(li, -7.9176350 * units.delta_degree_Celsius, 2)


def test_k_index():
"""Test the K Index calculation."""
pressure = np.array([1014., 1000., 997., 981.2, 947.4, 925., 914.9, 911.,
902., 883., 850., 822.3, 816., 807., 793.2, 770.,
765.1, 753., 737.5, 737., 713., 700., 688., 685.,
680., 666., 659.8, 653., 643., 634., 615., 611.8,
566.2, 516., 500., 487., 484.2, 481., 475., 460.,
400.]) * units.hPa
temperature = np.array([24.2, 24.2, 24., 23.1, 21., 19.6, 18.7, 18.4,
19.2, 19.4, 17.2, 15.3, 14.8, 14.4, 13.4, 11.6,
11.1, 10., 8.8, 8.8, 8.2, 7., 5.6, 5.6,
5.6, 4.4, 3.8, 3.2, 3., 3.2, 1.8, 1.5,
-3.4, -9.3, -11.3, -13.1, -13.1, -13.1, -13.7, -15.1,
-23.5]) * units.degC
dewpoint = np.array([23.2, 23.1, 22.8, 22., 20.2, 19., 17.6, 17.,
16.8, 15.5, 14., 11.7, 11.2, 8.4, 7., 4.6,
5., 6., 4.2, 4.1, -1.8, -2., -1.4, -0.4,
-3.4, -5.6, -4.3, -2.8, -7., -25.8, -31.2, -31.4,
-34.1, -37.3, -32.3, -34.1, -37.3, -41.1, -37.7, -58.1,
-57.5]) * units.degC
ki = k_index(pressure, temperature, dewpoint)
assert_almost_equal(ki, 33.5 * units.degC, 2)


def test_gradient_richardson_number():
"""Test gradient Richardson number calculation."""
theta = units('K') * np.asarray([254.5, 258.3, 262.2])
Expand Down

0 comments on commit 79abf12

Please sign in to comment.