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

LFC calc: if EL is above top of sounding, use top of sounding as EL #3132

Closed
wants to merge 1 commit into from
Closed

LFC calc: if EL is above top of sounding, use top of sounding as EL #3132

wants to merge 1 commit into from

Conversation

wx4stg
Copy link
Contributor

@wx4stg wx4stg commented Jul 28, 2023

If attempting to calculate the LFC of a partial sounding, particularly one where the data is cut off below the EL, calculating the LFC or CAPE causes a value error. This commit uses the top of the sounding as the EL if the EL is absent (for the LFC calculation only).

This may or may not be a fix for the issue described in #2315, I'm not sure.

@wx4stg wx4stg requested a review from a team as a code owner July 28, 2023 05:48
@wx4stg wx4stg requested review from dcamron and removed request for a team July 28, 2023 05:48
@wx4stg
Copy link
Contributor Author

wx4stg commented Jul 28, 2023

hmm. This was somewhat of a quick fix for an exception getting raised when I threw a particular dataset at cape_cin a few weeks ago, and it made sense to me at the time, but now that I'm actually going about writing a test for this, it's returning an LFC that's above the profile I'm providing....

I think crashing is clearly the wrong behavior, but so is providing an LFC based on interpolated data. I'm going to think about this some more

@wx4stg wx4stg marked this pull request as draft July 28, 2023 08:25
@dopplershift
Copy link
Member

The scenario you're describing fits that of the May 3rd sounding we have in the test data (may4_sounding.txt) and that works fine with surface_based_cape_cin. I wonder if there's something else going on here.

@dopplershift dopplershift added the Area: Calc Pertains to calculations label Jul 28, 2023
@wx4stg
Copy link
Contributor Author

wx4stg commented Jul 28, 2023

the "sounding" that caused the crash in my case was very much an edge case...

from metpy import calc as mpcalc
import numpy as np
from metpy.units import units

level = np.array([733.0, 732.0, 731.0, 730.0]) * units.hPa
temp = np.array([16.740580530590336, 16.645407342070797, 16.550234153551262, 16.39797906332763]) * units.degC
dew = np.array([-1.5180807300997776, -1.3453806517560651, -1.1726805734123527, -1.0063032908045617]) * units.degC

# this will cause a traceback
mpcalc.surface_based_cape_cin(levels, temp, dew)

The traceback is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stgardner4/Downloads/metpy/src/metpy/xarray.py", line 1328, in wrapper
    result = func(*bound_args.args, **bound_args.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/units.py", line 325, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/calc/thermo.py", line 2865, in surface_based_cape_cin
    return cape_cin(p, t, td, profile)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/xarray.py", line 1328, in wrapper
    result = func(*bound_args.args, **bound_args.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/units.py", line 325, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/calc/thermo.py", line 2363, in cape_cin
    lfc_pressure, _ = lfc(pressure, temperature, dewpoint,
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/xarray.py", line 1328, in wrapper
    result = func(*bound_args.args, **bound_args.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/units.py", line 325, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/stgardner4/Downloads/metpy/src/metpy/calc/thermo.py", line 722, in lfc
    if np.min(el_pressure) > this_lcl[0]:
       ^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in amin
  File "/opt/mamba/envs/HDWX/lib/python3.11/site-packages/pint/facets/numpy/quantity.py", line 66, in __array_function__
    return numpy_wrap("function", func, args, kwargs, types)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mamba/envs/HDWX/lib/python3.11/site-packages/pint/facets/numpy/numpy_func.py", line 1034, in numpy_wrap
    return handled[name](*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mamba/envs/HDWX/lib/python3.11/site-packages/pint/facets/numpy/numpy_func.py", line 828, in implementation
    ret = func(*bound_args.args, **bound_args.kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in amin
  File "/opt/mamba/envs/HDWX/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 2946, in amin
    return _wrapreduction(a, np.minimum, 'min', axis, None, out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mamba/envs/HDWX/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation minimum which has no identity

There is a point to this, I promise... I'm searching for the bottom and top of the effective inflow layer of an ACARS profile:
https://github.com/wx4stg/acars-sounding-server/blob/5f63295525aceb5a3ef1d89287a0d5449dc1c40c/soundingPlot.py#L88

This led me to investigate why el_pressure was empty... which led to my fix of just checking "if it's empty use the top pressure available". This (correctly) returns 0 J/kg CAPE and CIN, but internally, uses an LFC of 556 hPa, and if the user asks for the LFC, that's what it returns. I don't see how that's possible, given that is above the provided environmental profile.

>>> mpcalc.lfc(level, temp, dew)
<stdin>:1: UserWarning: Interpolation point out of data bounds encountered
(<Quantity(556.424774, 'hectopascal')>, <Quantity(-5.21169013, 'degree_Celsius')>)

I haven't had time to look into why this happens and probably won't until next week... This might be better suited for an issue instead of a draft PR.

@wx4stg
Copy link
Contributor Author

wx4stg commented Sep 11, 2023

I cannot reproduce this now, so I'll close this and if it comes back up I'll do a better job of recording what happened.

@wx4stg wx4stg closed this Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Calc Pertains to calculations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants