Skip to content

Commit 2e6fe60

Browse files
committed
Update thickness_hydrostatic to have an option to return full profile
1 parent b3aff08 commit 2e6fe60

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

src/metpy/calc/thermo.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,16 +3338,9 @@ def moist_static_energy(height, temperature, specific_humidity):
33383338
)
33393339
def thickness_hydrostatic(pressure, temperature, mixing_ratio=None,
33403340
molecular_weight_ratio=mpconsts.nounit.epsilon, bottom=None,
3341-
depth=None):
3341+
depth=None, full_profile=False):
33423342
r"""Calculate the thickness of a layer via the hypsometric equation.
33433343
3344-
This thickness calculation uses the pressure and temperature profiles (and optionally
3345-
mixing ratio) via the hypsometric equation with virtual temperature adjustment.
3346-
3347-
.. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p,
3348-
3349-
Which is based off of Equation 3.24 in [Hobbs2006]_.
3350-
33513344
This assumes a hydrostatic atmosphere. Layer bottom and depth specified in pressure.
33523345
33533346
Parameters
@@ -3374,6 +3367,12 @@ def thickness_hydrostatic(pressure, temperature, mixing_ratio=None,
33743367
The depth of the layer in hPa. Defaults to the full profile if bottom is not given,
33753368
and 100 hPa if bottom is given.
33763369
3370+
full_profile : `bool`, optional
3371+
Controls whether or not to return the full thickness profile, i.e., the thickness
3372+
between individual layers as a `Pint.Quantity` array or the thickness of
3373+
the whole atmospheric profile as a `Pint.Quantity`. Defaults to False, which returns
3374+
the thickness of the atmosphere as a single value.
3375+
33773376
Returns
33783377
-------
33793378
`pint.Quantity`
@@ -3427,6 +3426,13 @@ def thickness_hydrostatic(pressure, temperature, mixing_ratio=None,
34273426
Since this function returns scalar values when given a profile, this will return Pint
34283427
Quantities even when given xarray DataArray profiles.
34293428
3429+
This thickness calculation uses the pressure and temperature profiles (and optionally
3430+
mixing ratio) via the hypsometric equation with virtual temperature adjustment.
3431+
3432+
.. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p,
3433+
3434+
Which is based off of Equation 3.24 in [Hobbs2006]_.
3435+
34303436
.. versionchanged:: 1.0
34313437
Renamed ``mixing`` parameter to ``mixing_ratio``
34323438
@@ -3470,31 +3476,20 @@ def thickness_hydrostatic(pressure, temperature, mixing_ratio=None,
34703476
)
34713477

34723478
# Take the integral
3473-
return (
3474-
-mpconsts.nounit.Rd / mpconsts.nounit.g
3475-
* np.trapz(layer_virttemp, np.log(layer_p))
3476-
)
3479+
diff_logp = np.diff(np.log(layer_p))
3480+
thickness = (-mpconsts.nounit.Rd / mpconsts.nounit.g *
3481+
diff_logp * (layer_virttemp[1:] + layer_virttemp[:-1]) / 2.)
3482+
return thickness if full_profile else np.sum(thickness)
34773483

34783484

34793485
@exporter.export
34803486
@preprocess_and_wrap()
34813487
@check_units('[pressure]', '[temperature]')
34823488
def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative_humidity,
3483-
bottom=None, depth=None):
3489+
bottom=None, depth=None, full_profile=False):
34843490
r"""Calculate the thickness of a layer given pressure, temperature and relative humidity.
34853491
3486-
Similar to ``thickness_hydrostatic``, this thickness calculation uses the pressure,
3487-
temperature, and relative humidity profiles via the hypsometric equation with virtual
3488-
temperature adjustment
3489-
3490-
.. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p,
3491-
3492-
which is based off of Equation 3.24 in [Hobbs2006]_. Virtual temperature is calculated
3493-
from the profiles of temperature and relative humidity.
3494-
3495-
This assumes a hydrostatic atmosphere.
3496-
3497-
Layer bottom and depth specified in pressure.
3492+
This assumes a hydrostatic atmosphere. Layer bottom and depth specified in pressure.
34983493
34993494
Parameters
35003495
----------
@@ -3516,6 +3511,12 @@ def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative
35163511
The depth of the layer in hPa. Defaults to the full profile if bottom is not given,
35173512
and 100 hPa if bottom is given.
35183513
3514+
full_profile : `bool`, optional
3515+
Controls whether or not to return the full thickness profile, i.e., the thickness
3516+
between individual layers as a `Pint.Quantity` array or the thickness of
3517+
the whole atmospheric profile as a `Pint.Quantity`. Defaults to False, which returns
3518+
the thickness of the atmosphere as a single value.
3519+
35193520
Returns
35203521
-------
35213522
`pint.Quantity`
@@ -3557,11 +3558,19 @@ def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative
35573558
Since this function returns scalar values when given a profile, this will return Pint
35583559
Quantities even when given xarray DataArray profiles.
35593560
3561+
Similar to ``thickness_hydrostatic``, this thickness calculation uses the pressure,
3562+
temperature, and relative humidity profiles via the hypsometric equation with virtual
3563+
temperature adjustment
3564+
3565+
.. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p,
3566+
3567+
which is based off of Equation 3.24 in [Hobbs2006]_. Virtual temperature is calculated
3568+
from the profiles of temperature and relative humidity.
35603569
"""
35613570
mixing = mixing_ratio_from_relative_humidity(pressure, temperature, relative_humidity)
35623571

35633572
return thickness_hydrostatic(pressure, temperature, mixing_ratio=mixing, bottom=bottom,
3564-
depth=depth)
3573+
depth=depth, full_profile=full_profile)
35653574

35663575

35673576
@exporter.export

tests/calc/test_thermo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,17 @@ def test_thickness_hydrostatic_isothermal_subset():
16071607
assert_almost_equal(thickness, 4242.527 * units.m, 2)
16081608

16091609

1610+
def test_thickness_hydrostatic_full_profile():
1611+
"""Test the thickness calculation for a moist layer."""
1612+
pressure = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.hPa
1613+
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.degC
1614+
mixing = np.array([0.01458, 0.00209, 0.00224, 0.00240, 0.00256, 0.00010])
1615+
thickness = thickness_hydrostatic(pressure, temperature, mixing_ratio=mixing,
1616+
full_profile=True)
1617+
result = [1780.778, 306.126, 304.513, 281.455, 7218.833] * units.m
1618+
assert_almost_equal(thickness, result, 2)
1619+
1620+
16101621
def test_thickness_hydrostatic_from_relative_humidity():
16111622
"""Test the thickness calculation for a moist layer using RH data."""
16121623
pressure = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.hPa

0 commit comments

Comments
 (0)