# Buggy code
def lfc(
    pressure,
    temperature,
    dewpt,
    parcel_temperature_profile=None,
    dewpt_start=None,
    which="top",
):
    r"""Calculate the level of free convection (LFC).

    This works by finding the first intersection of the ideal parcel path and
    the measured parcel temperature.

    Parameters
    ----------
    pressure : `pint.Quantity`
        The atmospheric pressure
    temperature : `pint.Quantity`
        The temperature at the levels given by `pressure`
    dewpt : `pint.Quantity`
        The dewpoint at the levels given by `pressure`
    parcel_temperature_profile: `pint.Quantity`, optional
        The parcel temperature profile from which to calculate the LFC. Defaults to the
        surface parcel profile.
    dewpt_start: `pint.Quantity`, optional
        The dewpoint of the parcel for which to calculate the LFC. Defaults to the surface
        dewpoint.
    which: str, optional
        Pick which LFC to return. Options are 'top', 'bottom', and 'all'.
        Default is the 'top' (lowest pressure) LFC.

    Returns
    -------
    `pint.Quantity`
        The LFC pressure, or array of same if which='all'
    `pint.Quantity`
        The LFC temperature, or array of same if which='all'

    See Also
    --------
    parcel_profile

    """
    # Default to surface parcel if no profile or starting pressure level is given
    if parcel_temperature_profile is None:
        new_stuff = parcel_profile_with_lcl(pressure, temperature, dewpt)
        pressure, temperature, _, parcel_temperature_profile = new_stuff
        parcel_temperature_profile = parcel_temperature_profile.to(temperature.units)

    if dewpt_start is None:
        dewpt_start = dewpt[0]

    # The parcel profile and data may have the same first data point.
    # If that is the case, ignore that point to get the real first
    # intersection for the LFC calculation. Use logarithmic interpolation.
    if np.isclose(parcel_temperature_profile[0].m, temperature[0].m):
        x, y = find_intersections(
            pressure[1:],
            parcel_temperature_profile[1:],
            temperature[1:],
            direction="increasing",
            log_x=True,
        )
    else:
        x, y = find_intersections(
            pressure,
            parcel_temperature_profile,
            temperature,
            direction="increasing",
            log_x=True,
        )

    # Compute LCL for this parcel for future comparisons
    this_lcl = lcl(pressure[0], parcel_temperature_profile[0], dewpt_start)

    # The LFC could:
    # 1) Not exist
    # 2) Exist but be equal to the LCL
    # 3) Exist and be above the LCL

    # LFC does not exist or is LCL
    if len(x) == 0:
        # Is there any positive area above the LCL?
        mask = pressure < this_lcl[0]
        if np.all(_less_or_close(parcel_temperature_profile[mask], temperature[mask])):
            # LFC doesn't exist
            x, y = np.nan * pressure.units, np.nan * temperature.units
        else:  # LFC = LCL
            x, y = this_lcl
        return x, y

    # LFC exists. Make sure it is no lower than the LCL
    else:
        idx = x < this_lcl[0]
        # LFC height < LCL height, so set LFC = LCL
        if not any(idx):
            el_pres, _ = find_intersections(
                pressure[1:],
                parcel_temperature_profile[1:],
                temperature[1:],
                direction="decreasing",
                log_x=True,
            )
            if np.min(el_pres) > this_lcl[0]:
                x, y = np.nan * pressure.units, np.nan * temperature.units
            else:
                x, y = this_lcl
            return x, y
        # Otherwise, find all LFCs that exist above the LCL
        # What is returned depends on which flag as described in the docstring
        else:
            return _multiple_el_lfc_options(x, y, idx, which)


# Traceback (ValueError)
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

# Fixed code
def lfc(
    pressure,
    temperature,
    dewpt,
    parcel_temperature_profile=None,
    dewpt_start=None,
    which="top",
):
    r"""Calculate the level of free convection (LFC).

    This works by finding the first intersection of the ideal parcel path and
    the measured parcel temperature. If this intersection occurs below the LCL,
    the LFC is determined to be the same as the LCL, based upon the conditions
    set forth in [USAF1990]_, pg 4-14, where a parcel must be lifted dry adiabatically
    to saturation before it can freely rise.

    Parameters
    ----------
    pressure : `pint.Quantity`
        The atmospheric pressure
    temperature : `pint.Quantity`
        The temperature at the levels given by `pressure`
    dewpt : `pint.Quantity`
        The dewpoint at the levels given by `pressure`
    parcel_temperature_profile: `pint.Quantity`, optional
        The parcel temperature profile from which to calculate the LFC. Defaults to the
        surface parcel profile.
    dewpt_start: `pint.Quantity`, optional
        The dewpoint of the parcel for which to calculate the LFC. Defaults to the surface
        dewpoint.
    which: str, optional
        Pick which LFC to return. Options are 'top', 'bottom', and 'all'.
        Default is the 'top' (lowest pressure) LFC.

    Returns
    -------
    `pint.Quantity`
        The LFC pressure, or array of same if which='all'
    `pint.Quantity`
        The LFC temperature, or array of same if which='all'

    See Also
    --------
    parcel_profile

    """
    # Default to surface parcel if no profile or starting pressure level is given
    if parcel_temperature_profile is None:
        new_stuff = parcel_profile_with_lcl(pressure, temperature, dewpt)
        pressure, temperature, _, parcel_temperature_profile = new_stuff
        parcel_temperature_profile = parcel_temperature_profile.to(temperature.units)

    if dewpt_start is None:
        dewpt_start = dewpt[0]

    # The parcel profile and data may have the same first data point.
    # If that is the case, ignore that point to get the real first
    # intersection for the LFC calculation. Use logarithmic interpolation.
    if np.isclose(
        parcel_temperature_profile[0].to(temperature.units).m, temperature[0].m
    ):
        x, y = find_intersections(
            pressure[1:],
            parcel_temperature_profile[1:],
            temperature[1:],
            direction="increasing",
            log_x=True,
        )
    else:
        x, y = find_intersections(
            pressure,
            parcel_temperature_profile,
            temperature,
            direction="increasing",
            log_x=True,
        )

    # Compute LCL for this parcel for future comparisons
    this_lcl = lcl(pressure[0], parcel_temperature_profile[0], dewpt_start)

    # The LFC could:
    # 1) Not exist
    # 2) Exist but be equal to the LCL
    # 3) Exist and be above the LCL

    # LFC does not exist or is LCL
    if len(x) == 0:
        # Is there any positive area above the LCL?
        mask = pressure < this_lcl[0]
        if np.all(_less_or_close(parcel_temperature_profile[mask], temperature[mask])):
            # LFC doesn't exist
            x, y = np.nan * pressure.units, np.nan * temperature.units
        else:  # LFC = LCL
            x, y = this_lcl
        return x, y

    # LFC exists. Make sure it is no lower than the LCL
    else:
        idx = x < this_lcl[0]
        # LFC height < LCL height, so set LFC = LCL
        if not any(idx):
            el_pres, _ = find_intersections(
                pressure[1:],
                parcel_temperature_profile[1:],
                temperature[1:],
                direction="decreasing",
                log_x=True,
            )
            if np.min(el_pres) > this_lcl[0]:
                x, y = np.nan * pressure.units, np.nan * temperature.units
            else:
                x, y = this_lcl
            return x, y
        # Otherwise, find all LFCs that exist above the LCL
        # What is returned depends on which flag as described in the docstring
        else:
            return _multiple_el_lfc_options(x, y, idx, which)