-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add downdraft CAPE calculation #823
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7daaf37
Add downdraft CAPE calculation.
jrleeman 5d46493
refactor
jrleeman 0fe88bd
dcape
jrleeman 1f4a5fe
Add tests for DCAPE
jrleeman e61befc
Refactor layer calculation
jrleeman 8f186ab
Modify test for custom parcel
jrleeman 09b8628
working on it
jrleeman 4cd18dc
refactor again
jrleeman dd7e098
update testing
jrleeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2105,3 +2105,103 @@ def dewpoint_from_specific_humidity(specific_humidity, temperature, pressure): | |
return dewpoint_rh(temperature, relative_humidity_from_specific_humidity(specific_humidity, | ||
temperature, | ||
pressure)) | ||
|
||
|
||
@exporter.export | ||
@check_units('[pressure]', '[temperature]', '[temperature]') | ||
def downdraft_cape(pressure, temperature, dewpoint, parcel=None, bottom=None, | ||
heights=None, depth=400 * units.hPa): | ||
r"""Calculate downdraft CAPE. | ||
|
||
Calculate the downdraft convective available potential energy (CAPE).The minimum theta-e | ||
value between the surface and top_pressure is used as the parcel starting point. Parcels | ||
descend along a moist adiabat. Area between the parcel path and environmental temperature | ||
is integrated to calculate DCAPE. | ||
|
||
Parameters | ||
---------- | ||
pressure : `pint.Quantity` | ||
The atmospheric pressure level(s) of interest. The first entry should be the starting | ||
point pressure. | ||
temperature : `pint.Quantity` | ||
The atmospheric temperature corresponding to pressure. | ||
dewpoint : `pint.Quantity` | ||
The atmospheric dew point corresponding to pressure. | ||
parcel : tuple | ||
Tuple of pressure and temperature for the starting parcel. | ||
bottom : `pint.Quantity` | ||
Lowest point in the parcel path to consider in integration in height or pressure. | ||
If no heights are given, a standard atmosphere will be assumed. Defaults to None. | ||
top_pressure : `pint.Quantity` | ||
The lowest pressure value to be considered in the calculation. Defaults to 400 hPa. | ||
|
||
Returns | ||
------- | ||
`pint.Quantity` | ||
Downdraft convective available potential energy (CAPE). | ||
|
||
Notes | ||
----- | ||
.. math:: \text{DCAPE} = R_d \int_{P_i}^{P_{sfc}} (T_{env} - T_{parcel}) d\text{ln}(p) | ||
|
||
* :math:`DCAPE` Downdraft convective available potential energy | ||
* :math:`R_d` Gas constant | ||
* :math:`g` Gravitational acceleration | ||
* :math:`T_{parcel}` Parcel temperature | ||
* :math:`T_{env}` Environment temperature | ||
* :math:`p` Atmospheric pressure | ||
|
||
""" | ||
|
||
# If the user specified a parcel starting point, we'll use that instead of the default | ||
if parcel: | ||
if depth: | ||
ValueError('Cannot specify a depth when using a custom parcel.') | ||
|
||
parcel_starting_pressure, parcel_starting_temperature = parcel | ||
|
||
# If no bottom is specified, we'll use the surface (default of get_layer),but | ||
# done explicitly here to depth calculation. | ||
if not bottom: | ||
bottom = np.nanmax(pressure) * pressure.units | ||
|
||
# Calculate the depth of the sounding to use (bottom - the parcel starting point) | ||
depth = bottom - parcel_starting_pressure | ||
|
||
# Trim data to the bottom and depth above it | ||
pressure, temperature = get_layer(pressure, temperature, bottom=bottom, | ||
depth=depth, heights=heights) | ||
|
||
# The user did not give us a parcel, so we'll calculate a sensible default. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E303 too many blank lines (2) |
||
else: | ||
|
||
# Trim data to only top height and below as well as bottom height and above | ||
pressure, temperature, dewpoint = get_layer(pressure, temperature, dewpoint, | ||
depth=depth, | ||
bottom=bottom, | ||
heights=heights) | ||
|
||
# Find minimum theta-e | ||
theta_e = equivalent_potential_temperature(pressure, temperature, dewpoint) | ||
minimum_theta_e_index = np.argmin(theta_e) | ||
|
||
# Trim data to be minimum theta-e down (dewpoint is no longer required) | ||
pressure = pressure[:minimum_theta_e_index] | ||
temperature = temperature[:minimum_theta_e_index] | ||
|
||
# Sort for monotonically increasing pressure that trapz needs to work. | ||
# Also guarantees the logic for calculating a descending parcel is sound. | ||
sort_inds = np.argsort(pressure) | ||
pressure = pressure[sort_inds] | ||
temperature = temperature[sort_inds] | ||
|
||
# Create the parcel profile for decent along a moist adiabat | ||
profile_temperatures = moist_lapse(pressure, temperature[0]) | ||
|
||
# Calculate the difference in profile and environmental temperature to integrate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E303 too many blank lines (2) |
||
y_vals = temperature - profile_temperatures | ||
|
||
# Calculate DCAPE | ||
dcape = Rd * (np.trapz(y_vals, | ||
x=np.log(pressure.m)) * units.kelvin) | ||
return dcape.to('J/kg'), pressure[::-1], profile_temperatures[::-1] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (97 > 95 characters)