Skip to content

Commit fbe977e

Browse files
committed
Reorganize GDI
Apply subjective code style cleanup to reduce repetition. Test 950 level check sooner. Collapse array dimension handling.
1 parent a5210e5 commit fbe977e

1 file changed

Lines changed: 33 additions & 67 deletions

File tree

src/metpy/calc/thermo.py

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4565,7 +4565,7 @@ def galvez_davison_index(pressure, temperature, mixing_ratio, surface_pressure,
45654565
45664566
Examples
45674567
--------
4568-
>>> from metpy.calc import dewpoint_from_relative_humidity, k_index
4568+
>>> from metpy.calc import mixing_ratio_from_relative_humidity
45694569
>>> from metpy.units import units
45704570
>>> # pressure
45714571
>>> p = [1008., 1000., 950., 900., 850., 800., 750., 700., 650., 600.,
@@ -4586,9 +4586,6 @@ def galvez_davison_index(pressure, temperature, mixing_ratio, surface_pressure,
45864586
>>> galvez_davison_index(p, T, mixrat, p[0])
45874587
<Quantity(-8.78797532, 'dimensionless')>
45884588
"""
4589-
# Calculate potential temperature
4590-
potential_temp = potential_temperature(pressure, temperature)
4591-
45924589
if np.any(np.max(pressure, axis=vertical_dim) < 950 * units.hectopascal):
45934590
indices_without_950 = np.where(
45944591
np.max(pressure, axis=vertical_dim) < 950 * units.hectopascal
@@ -4603,96 +4600,65 @@ def galvez_davison_index(pressure, temperature, mixing_ratio, surface_pressure,
46034600
f'\nMax provided pressures:'
46044601
f'\n{np.max(pressure, axis=0)[indices_without_950]}'
46054602
)
4606-
# Convert temperature before interpolation
4607-
temperature_k = temperature.to('kelvin')
4608-
potential_temp_k = potential_temp.to('kelvin')
46094603

4610-
# Interpolate to find temperature and mixing ratio at 950, 850, 700, and 500 hPa
4604+
potential_temp = potential_temperature(pressure, temperature)
4605+
4606+
# Interpolate to appropriate level with appropriate units
46114607
(
46124608
(t950, t850, t700, t500),
46134609
(r950, r850, r700, r500),
46144610
(th950, th850, th700, th500)
46154611
) = interpolate_1d(
46164612
units.Quantity([950, 850, 700, 500], 'hPa'),
4617-
pressure, temperature_k, mixing_ratio, potential_temp_k,
4613+
pressure, temperature.to('K'), mixing_ratio, potential_temp.to('K'),
46184614
axis=vertical_dim,
46194615
)
46204616

4621-
th_a = th950
4622-
r_a = r950
4623-
4624-
th_b = 0.5 * (th850 + th700)
4625-
r_b = 0.5 * (r850 + r700)
4626-
4627-
th_c = th500
4628-
r_c = r500
4629-
4630-
alpha = units.Quantity(-10, 'K') # Empirical adjustment
4631-
4632-
# Latent heat of vaporization of water - different from MetPy constant
4633-
# Using different value, from MetPy, since GDI unlikely to be used to
4634-
# derive other metrics and a more appropriate value for tropical
4635-
# atmosphere.
4617+
# L_v definition preserved from referenced paper
4618+
# Value differs heavily from metpy.constants.Lv in tropical context
4619+
# and using MetPy value affects resulting GDI
46364620
l_0 = units.Quantity(2.69e6, 'J/kg')
46374621

4638-
# Temperature math from here on requires kelvin units
4639-
eptp_a = th_a * np.exp((l_0 * r_a) / (mpconsts.Cp_d * t850))
4640-
eptp_b = th_b * np.exp((l_0 * r_b) / (mpconsts.Cp_d * t850)) + alpha
4641-
eptp_c = th_c * np.exp((l_0 * r_c) / (mpconsts.Cp_d * t850)) + alpha
4642-
4643-
if t950.size == 1:
4644-
is_array = False
4645-
else:
4646-
is_array = True
4622+
# Calculate adjusted equivalent potential temperatures
4623+
alpha = units.Quantity(-10, 'K')
4624+
eptp_a = th950 * np.exp(l_0 * r950 / (mpconsts.Cp_d * t850))
4625+
eptp_b = ((th850 + th700) / 2 *
4626+
np.exp(l_0 * (r850 + r700) / 2 / (mpconsts.Cp_d * t850)) + alpha)
4627+
eptp_c = th500 * np.exp(l_0 * r500 / (mpconsts.Cp_d * t850)) + alpha
46474628

4648-
# Calculate C.B.I.
4649-
beta = units.Quantity(303, 'K') # Empirical adjustment
4650-
l_e = eptp_a - beta # Low-troposphere EPT
4651-
m_e = eptp_c - beta # Mid-troposphere EPT
4629+
# Calculate Column Buoyanci Index (CBI)
4630+
# Apply threshold to low and mid levels
4631+
beta = units.Quantity(303, 'K')
4632+
l_e = eptp_a - beta
4633+
m_e = eptp_c - beta
46524634

46534635
# Gamma unit - likely a typo from the paper, should be units of K^(-2) to
46544636
# result in dimensionless CBI
46554637
gamma = units.Quantity(6.5e-2, '1/K^2')
4656-
zero_kelvin = units.Quantity(0, 'K')
4657-
4658-
if is_array:
4659-
# Replace conditional in paper for array compatibility.
4660-
# Will set CBI for any l_e < 0 to 0
4661-
l_e[l_e <= zero_kelvin] = zero_kelvin
4662-
else:
4663-
l_e = max(l_e, zero_kelvin)
4664-
column_buoyancy_index = gamma * (l_e * m_e)
46654638

4666-
# Calculate Mid-tropospheric Warming Index
4667-
tau = units.Quantity(263.15, 'K') # Threshold
4668-
mu = units.Quantity(-7, '1/K') # Empirical adjustment
4639+
column_buoyancy_index = np.atleast_1d(gamma * l_e * m_e)
4640+
column_buoyancy_index[l_e <= 0] = 0
46694641

4642+
# Calculate Mid-tropospheric Warming Index (MWI)
4643+
# Apply threshold to 500-hPa temperature
4644+
tau = units.Quantity(263.15, 'K')
46704645
t_diff = t500 - tau
4671-
if is_array:
4672-
t_diff[t_diff <= zero_kelvin] = zero_kelvin
4673-
else:
4674-
t_diff = max(t_diff, zero_kelvin)
4675-
mid_tropospheric_warming_index = mu * t_diff
46764646

4677-
# Calculate Inversion Index
4678-
sigma = units.Quantity(1.5, '1/K') # Empirical scaling constant
4647+
mu = units.Quantity(-7, '1/K') # Empirical adjustment
4648+
mid_tropospheric_warming_index = np.atleast_1d(mu * t_diff)
4649+
mid_tropospheric_warming_index[t_diff <= 0] = 0
4650+
4651+
# Calculate Inversion Index (II)
46794652
s = t950 - t700
46804653
d = eptp_b - eptp_a
4681-
46824654
inv_sum = s + d
4683-
if is_array:
4684-
inv_sum[inv_sum >= zero_kelvin] = zero_kelvin
4685-
else:
4686-
inv_sum = min(inv_sum, zero_kelvin)
46874655

4688-
inversion_index = sigma * inv_sum
4656+
sigma = units.Quantity(1.5, '1/K') # Empirical scaling constant
4657+
inversion_index = np.atleast_1d(sigma * inv_sum)
4658+
inversion_index[inv_sum >= 0] = 0
46894659

46904660
# Calculate Terrain Correction
4691-
p_3 = 18
4692-
p_2 = 9000 * units.hectopascal
4693-
p_1 = 500 * units.hectopascal
4694-
p_sfc = surface_pressure
4695-
terrain_correction = p_3 - (p_2 / (p_sfc - p_1))
4661+
terrain_correction = 18 - 9000 / (surface_pressure.m_as('hPa') - 500)
46964662

46974663
# Calculate G.D.I.
46984664
return (column_buoyancy_index

0 commit comments

Comments
 (0)