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

cape_cin ValueError: zero-size array to reduction operation minimum which has no identity #1190

Closed
akrherz opened this issue Oct 2, 2019 · 6 comments · Fixed by #1202
Closed
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Milestone

Comments

@akrherz
Copy link
Contributor

akrherz commented Oct 2, 2019

Found an observed profile that produces a ValueError. The reproducer

import datetime

import pandas as pd
from siphon.simplewebservice.iastate import IAStateUpperAir
from metpy.calc import surface_based_cape_cin
from metpy.units import units

res = IAStateUpperAir.request_data(datetime.datetime(2018, 4, 7, 17), 'KXMR')
filtered = res[pd.notnull(res['dewpoint'])]
(sbcape, sbcin) = surface_based_cape_cin(
        filtered['pressure'].values * units.hPa,
        filtered['temperature'].values * units.degC,
        filtered['dewpoint'].values * units.degC) 

Results with

Traceback (most recent call last):
  File "trouble_sounding.py", line 18, in <module>
    filtered['dewpoint'].values * units.degC)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/xarray.py", line 571, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/units.py", line 312, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/calc/thermo.py", line 1756, in surface_based_cape_cin
    return cape_cin(p, t, td, profile)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/xarray.py", line 571, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/units.py", line 312, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/calc/thermo.py", line 1430, in cape_cin
    parcel_temperature_profile=parcel_profile)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/xarray.py", line 571, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/units.py", line 312, in wrapper
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/calc/thermo.py", line 451, in lfc
    if np.min(el_pres) > this_lcl[0]:
  File "<__array_function__ internals>", line 6, in amin
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 2746, in amin
    keepdims=keepdims, initial=initial, where=where)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 88, in _wrapreduction
    return reduction(axis=axis, out=out, **passkwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/pint/quantity.py", line 1357, in __numpy_method_wrap
    value = func(*args, **kwargs)
  File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/numpy/core/_methods.py", line 34, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity

@akrherz akrherz added the Type: Bug Something is not working like it should label Oct 2, 2019
@zbruick
Copy link
Contributor

zbruick commented Oct 2, 2019

Looks like something I introduced in #1172...yay. I'll investigate.

@zbruick zbruick added the Area: Calc Pertains to calculations label Oct 2, 2019
@zbruick
Copy link
Contributor

zbruick commented Oct 2, 2019

Interestingly, lfc works for these arrays, which is included in this traceback leaves MetPy...

import metpy.calc as mpcalc
mpcalc.lfc(filtered['pressure'].values * units.hPa,
        filtered['temperature'].values * units.degC,
        filtered['dewpoint'].values * units.degC)

(900.5197306102967 <Unit('hectopascal')>, 17.93280318995917 <Unit('degC')>)

@zbruick
Copy link
Contributor

zbruick commented Oct 3, 2019

Alright, I solved the initial problem here which wasn't related to #1172, but an inaccurate unit comparison earlier in lfc. However, I've now discovered an issue with this profile where CIN is returned as a positive value because the entire area below the LCL is positive (see figure). On a science level, what happens to this positive area - is it actually buoyant (and therefore added to CAPE), is it disregarded and CIN set to 0?
skewt_problem

@zbruick
Copy link
Contributor

zbruick commented Oct 3, 2019

One source from the USAF here, which basically says its how you lift the parcel. Given that this parcel is unstable from the surface, I would think that all positive area should be included, but the canonical definition of the LFC gives me pause. (LFC in this case is also the LCL at 900 hPa).

@jthielen
Copy link
Collaborator

jthielen commented Oct 3, 2019

Speaking of canonical definition of the LFC, what exact definition is MetPy using? (I couldn't find a reference in the docs.) I have a feeling resolving that will be key here.

Based on #826, it was decided that the LFC must be at or above the LCL, but it wasn't fully explained why that is the case. At least to me, the definition given in the USAF text doesn't seem to preclude an LFC below the LCL:

The level of free convection (LFC) is the height at which a parcel of air lifted dry-adiabatically until saturated and saturation-adiabatically thereafter would first become warmer (less dense) than the surrounding air. The parcel will then continue to rise freely above this level until it becomes colder (more dense) that the surrounding air.

However, while the AMS glossary gives similar phrasing, it also includes "in a conditionally unstable atmosphere," which is not the case for the lowest levels in this example sounding. Should the LFC be undefined in this case?

@kgoebber Would you have input to help clear this up?

@dopplershift dopplershift pinned this issue Oct 8, 2019
@dopplershift dopplershift unpinned this issue Oct 8, 2019
@zbruick
Copy link
Contributor

zbruick commented Oct 9, 2019

Based on conversations with @kgoebber and @dopplershift, we are sticking with no LFC below the LCL. CIN will be set to 0 in this case, but the positive area below the LCL will not be added to CAPE. Based on the USAF text, a parcel must be lifted dry adiabatically to saturation before it can be eligible to "rise freely", therefore causing LFC == LCL in this case.

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

Successfully merging a pull request may close this issue.

3 participants