Repository: Unidata/MetPy

Issue:
ValueError: zero-size array to reduction operation minimum which has no identity
### What went wrong?

# Example

Given this example in calculating the `most_unstable_cape_cin`...

```python
P = [
    101300.0,
    100000.0,
    97500.0,
    95000.0,
    92500.0,
    90000.0,
    87500.0,
    85000.0,
    82500.0,
    80000.0,
    77500.0,
    75000.0,
    72500.0,
    70000.0,
    65000.0,
    60000.0,
    55000.0,
    50000.0,
    45000.0,
    40000.0,
    35000.0,
    30000.0,
    25000.0,
    20000.0,
]
T = [
    297.54987283445246,
    296.88002814889177,
    295.7800726652342,
    294.78940763027333,
    292.9596739891891,
    293.0196074610551,
    291.93022847243265,
    290.53941937530794,
    288.93002938078973,
    287.759563930231,
    287.01920927776916,
    286.03030699198086,
    284.6906176208595,
    283.3897087315337,
    281.5599750904495,
    276.50969871115626,
    272.8995884058186,
    267.7999550496946,
    261.2795458605129,
    255.03060004390053,
    248.24049023132204,
    239.7704804573242,
    229.55006075500586,
    217.17027331634054,
]
Td = [
    295.903247655174,
    295.69122116536846,
    295.0838908693815,
    293.5371919545537,
    292.24457112007633,
    288.04982069988165,
    285.37780607791353,
    284.17380677750697,
    282.68806839907216,
    276.70189149107654,
    269.4555485732638,
    263.42704064619716,
    258.0106646774842,
    249.26453828772148,
    239.24704466847052,
    244.3324971464268,
    238.1753929086274,
    234.09412244735233,
    231.72751436757792,
    232.84305312811804,
    241.34976858460612,
    236.3692283618347,
    225.98551449499607,
    213.61268518282787,
]
P *= units.Pa
T *= units.K
Td *= units.K
assert np.all(T >= Td)

mpcalc.most_unstable_cape_cin(P, T, Td)
```


An error is triggered when evaluating the `LFC` https://github.com/Unidata/MetPy/blob/main/src/metpy/calc/thermo.py#L976

```python
if np.min(el_pressure) > this_lcl[0]:
```

Seemingly because the profile contains no `EL` . This can be solved with a size check on `el_pressure` as such.

```python
if el_pressure.size and np.min(el_pressure) > this_lcl[0]: # the LFC is this_lcl
```

or

```python
if not el_pressure.size or np.min(el_pressure) > this_lcl[0]: # the LFC is nan
```


### Operating System

Windows

### Version

MetPy==1.6.3

### Python Version

3.12

### Code to Reproduce

```python
P = [
    101300.0,
    100000.0,
    97500.0,
    95000.0,
    92500.0,
    90000.0,
    87500.0,
    85000.0,
    82500.0,
    80000.0,
    77500.0,
    75000.0,
    72500.0,
    70000.0,
    65000.0,
    60000.0,
    55000.0,
    50000.0,
    45000.0,
    40000.0,
    35000.0,
    30000.0,
    25000.0,
    20000.0,
]
T = [
    297.54987283445246,
    296.88002814889177,
    295.7800726652342,
    294.78940763027333,
    292.9596739891891,
    293.0196074610551,
    291.93022847243265,
    290.53941937530794,
    288.93002938078973,
    287.759563930231,
    287.01920927776916,
    286.03030699198086,
    284.6906176208595,
    283.3897087315337,
    281.5599750904495,
    276.50969871115626,
    272.8995884058186,
    267.7999550496946,
    261.2795458605129,
    255.03060004390053,
    248.24049023132204,
    239.7704804573242,
    229.55006075500586,
    217.17027331634054,
]
Td = [
    295.903247655174,
    295.69122116536846,
    295.0838908693815,
    293.5371919545537,
    292.24457112007633,
    288.04982069988165,
    285.37780607791353,
    284.17380677750697,
    282.68806839907216,
    276.70189149107654,
    269.4555485732638,
    263.42704064619716,
    258.0106646774842,
    249.26453828772148,
    239.24704466847052,
    244.3324971464268,
    238.1753929086274,
    234.09412244735233,
    231.72751436757792,
    232.84305312811804,
    241.34976858460612,
    236.3692283618347,
    225.98551449499607,
    213.61268518282787,
]
P *= units.Pa
T *= units.K
Td *= units.K
assert np.all(T >= Td)

mpcalc.most_unstable_cape_cin(P, T, Td)
```

### Errors, Traceback, and Logs

```pytb
728 el_pressure, _ = find_intersections(pressure[1:], parcel_temperature_profile[1:],
    729                                     temperature[1:], direction='decreasing',
    730                                     log_x=True)
    731 # if not el_pressure.size or np.min(el_pressure) > this_lcl[0]:
    732 # if el_pressure.size and np.min(el_pressure) > this_lcl[0]:
--> 733 if np.min(el_pressure) > this_lcl[0]:
    734     x = units.Quantity(np.nan, pressure.units)
    735     y = units.Quantity(np.nan, temperature.units)
```


Discussion:
Thanks for the detailed reproducer and the fix, which looks correct to me. Any interest in submitting a PR with the (second) fix? You can use this same profile to add a test for `lfc`.



Patch:
diff --git a/src/metpy/calc/thermo.py b/src/metpy/calc/thermo.py
index eb5958978bc..10c221deef5 100644
--- a/src/metpy/calc/thermo.py
+++ b/src/metpy/calc/thermo.py
@@ -973,10 +973,12 @@ def lfc(pressure, temperature, dewpoint, parcel_temperature_profile=None, dewpoi
             el_pressure, _ = find_intersections(pressure[1:], parcel_temperature_profile[1:],
                                                 temperature[1:], direction='decreasing',
                                                 log_x=True)
-            if np.min(el_pressure) > this_lcl[0]:
+            if el_pressure.size and np.min(el_pressure) > this_lcl[0]:
+                # EL exists and it is below the LCL
                 x = units.Quantity(np.nan, pressure.units)
                 y = units.Quantity(np.nan, temperature.units)
             else:
+                # EL exists and it is above the LCL or the EL does not exist
                 x, y = this_lcl
             return x, y
         # Otherwise, find all LFCs that exist above the LCL


Test patch:
diff --git a/tests/calc/test_thermo.py b/tests/calc/test_thermo.py
index a6ef1bfbff3..01e547c2135 100644
--- a/tests/calc/test_thermo.py
+++ b/tests/calc/test_thermo.py
@@ -2075,6 +2075,30 @@ def test_lfc_not_below_lcl():
     assert_almost_equal(lfc_temp, 6.48644650 * units.celsius, 3)
 
 
+def test_lfc_with_no_el():
+    """Test sounding with no EL."""
+    pressure = [97500.0, 96507.19031, 95000.0, 92500.0, 90000.0, 87500.0, 85000.0, 82500.0,
+                80000.0, 77500.0, 75000.0, 72500.0, 70000.0, 65000.0, 60000.0, 55000.0,
+                50000.0, 45000.0, 40000.0, 35000.0, 30000.0, 25000.0, 20000.0] * units.Pa
+    temperature = [298.82991, 298.3494, 297.62432, 295.62864, 295.11843, 293.73578, 292.24718,
+                   290.51384, 288.83507, 287.67248, 286.45351, 284.97209, 283.52667, 281.61741,
+                   276.60909, 272.95879, 267.84228, 261.31541, 255.07487, 248.35583, 239.85012,
+                   229.58049, 217.17851] * units.K
+    dewpoint = [295.08389, 294.46966, 293.53719, 292.24457, 288.04982, 285.37781, 284.17381,
+                282.68807, 276.70189, 269.45555, 263.42704, 258.01066, 249.26454, 239.24704,
+                244.3325, 238.17539, 234.09412, 231.72751, 232.84305, 241.34977, 236.36923,
+                225.98551, 213.61269] * units.K
+    parcel_profile = [298.82991, 298.04683, 297.43067, 296.2386, 295.35901, 294.24402,
+                      293.0487, 291.79052, 290.56817, 289.36855, 288.08644, 286.70644,
+                      285.27995, 282.33855, 278.74485, 274.96722, 270.61709, 265.60125,
+                      259.78397, 252.77763, 244.10103, 233.21095, 219.55314] * units.K
+
+    lfc_p, lfc_t = lfc(pressure, temperature, dewpoint, parcel_profile, which='top')
+
+    assert_almost_equal(lfc_p, 92303.546 * units.Pa, 3)
+    assert_almost_equal(lfc_t, 294.190 * units.K, 3)
+
+
 @pytest.fixture
 def multiple_intersections():
     """Create profile with multiple LFCs and ELs for testing."""