Skip to content
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

In calculating CAPE value, why is the minus sign in the formula not reflected in the code? #3544

Open
hechentao opened this issue Jun 21, 2024 · 2 comments
Labels
Type: Bug Something is not working like it should

Comments

@hechentao
Copy link

hechentao commented Jun 21, 2024

What went wrong?

CAPE formula:
$\text{CAPE} = -R_d \int_{LFC}^{EL} (T_{{v}{parcel}} - T{{v}_{env}}) d\text{ln}(p)$
And in the code:

  # Difference between the parcel path and measured temperature profiles
  y = (parcel_profile - temperature).to(units.degK)
  
  # Estimate zero crossings
  x, y = _find_append_zero_crossings(np.copy(pressure), y)
  
  # CAPE
  # Only use data between the LFC and EL for calculation
  p_mask = _less_or_close(x.m, lfc_pressure) & _greater_or_close(x.m, el_pressure)
  x_clipped = x[p_mask].magnitude
  y_clipped = y[p_mask].magnitude
  cape = (mpconsts.Rd
          * units.Quantity(np.trapz(y_clipped, np.log(x_clipped)), 'K')).to(units('J/kg'))

I didn't find anything about negative signs. Did I not understand this code thoroughly myself?

Operating System

Windows

Version

1.6.2

Python Version

3.8.19

Code to Reproduce

None

Errors, Traceback, and Logs

No response

@hechentao hechentao added the Type: Bug Something is not working like it should label Jun 21, 2024
@DWesl
Copy link
Contributor

DWesl commented Jun 25, 2024

$-\int_{LFC}^{EL} (T_p - T_{env}) d\log p = \int_{EL}^{LFC} (T_p - T_{env}) d\log p$
Does anything check whether the pressures are increasing and reverse them (and the data) if not?

@DWesl
Copy link
Contributor

DWesl commented Jun 26, 2024

in _find_append_zero_crossings:

MetPy/src/metpy/calc/thermo.py

Lines 2509 to 2512 in d27e97c

# Resort so that data are in order
sort_idx = np.argsort(x)
x = x[sort_idx]
y = y[sort_idx]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something is not working like it should
Projects
None yet
Development

No branches or pull requests

2 participants