# Buggy code
def lfc(pressure, temperature, dewpt, parcel_temperature_profile=None):
    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 dew point 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.

    Returns
    -------
    `pint.Quantity`
        The LFC pressure and temperature

    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
        temperature = temperature.to("degC")
        parcel_temperature_profile = parcel_temperature_profile.to("degC")

    # The parcel profile and data have the same first data point, so we ignore
    # that point to get the real first intersection for the LFC calculation.
    x, y = find_intersections(
        pressure[1:],
        parcel_temperature_profile[1:],
        temperature[1:],
        direction="increasing",
    )

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

    # 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
            return np.nan * pressure.units, np.nan * temperature.units
        else:  # LFC = LCL
            x, y = this_lcl
            return x, y

    # LFC exists and is not LCL. Make sure it is above the LCL.
    else:
        idx = x < lcl(pressure[0], temperature[0], dewpt[0])[0]
        x = x[idx]
        y = y[idx]
        return x[0], y[0]


# Traceback (IndexError)
0.9.1+6.gb7f97991
1024.95703125 hectopascal 5.743482661750363 degC 2.08279974748133 degC
[278.89348266 278.24322133 277.35755297 276.22928809 274.81361678 273.52555096 272.08325952 270.57324142 269.10199475 267.56801846 265.96421247 263.5399307  260.21335861 256.7576317  253.17725629 249.48027885 245.67713199 241.78102546 237.80684667 233.77057953 229.68754004 225.57206704 221.43686749 217.29167908 213.14477576 209.0024604  204.86851485 200.74607368 196.63681225 192.54139059 188.45988434 184.39212913 180.33722545 176.29398272 172.27346048 168.31396424 164.44241275 160.65710232 156.95530012 153.33548532 149.79516513 146.33278761 142.94622388 139.63343896 136.39268027 133.22221317 130.12009053 127.08448424 124.11358965] kelvin
49 49 49 49
Traceback (most recent call last):
File "lfc_bug.py", line 47, in <module>
mllfc_p, mllfc_T = mpcalc.lfc(P, T, Td, parcel_temperature_profile=ml_profile)
File "/home/decker/src/git_repos/metpy/metpy/xarray.py", line 381, in wrapper
return func(*args, **kwargs)
File "/home/decker/src/git_repos/metpy/metpy/units.py", line 305, in wrapper
return func(*args, **kwargs)
File "/home/decker/src/git_repos/metpy/metpy/calc/thermo.py", line 383, in lfc
return x[0], y[0]
File "/home/decker/local/anaconda3/envs/devel/lib/python3.6/site-packages/pint/quantity.py", line 1281, in __getitem__
value = self._magnitude[key]
IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def lfc(pressure, temperature, dewpt, parcel_temperature_profile=None):
    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 dew point 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.

    Returns
    -------
    `pint.Quantity`
        The LFC pressure and temperature

    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
        temperature = temperature.to("degC")
        parcel_temperature_profile = parcel_temperature_profile.to("degC")

    # The parcel profile and data have the same first data point, so we ignore
    # that point to get the real first intersection for the LFC calculation.
    x, y = find_intersections(
        pressure[1:],
        parcel_temperature_profile[1:],
        temperature[1:],
        direction="increasing",
    )

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

    # 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
            return 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):
            x, y = this_lcl
            return x, y
        # Otherwise, make select first candidate LFC above the LCL
        else:
            x = x[idx]
            y = y[idx]
            return x[0], y[0]