# Buggy code
def lcl(pressure, temperature, dewpt, max_iters=50, eps=1e-5):
    r"""Calculate the lifted condensation level (LCL) using from the starting point.

    The starting state for the parcel is defined by `temperature`, `dewpt`,
    and `pressure`.

    Parameters
    ----------
    pressure : `pint.Quantity`
        The starting atmospheric pressure
    temperature : `pint.Quantity`
        The starting temperature
    dewpt : `pint.Quantity`
        The starting dewpoint

    Returns
    -------
    `pint.Quantity`
        The LCL pressure
    `pint.Quantity`
        The LCL temperature

    Other Parameters
    ----------------
    max_iters : int, optional
        The maximum number of iterations to use in calculation, defaults to 50.
    eps : float, optional
        The desired relative error in the calculated value, defaults to 1e-5.

    See Also
    --------
    parcel_profile

    Notes
    -----
    This function is implemented using an iterative approach to solve for the
    LCL. The basic algorithm is:

    1. Find the dewpoint from the LCL pressure and starting mixing ratio
    2. Find the LCL pressure from the starting temperature and dewpoint
    3. Iterate until convergence

    The function is guaranteed to finish by virtue of the `max_iters` counter.

    """

    def _lcl_iter(p, p0, w, t):
        td = dewpoint(vapor_pressure(units.Quantity(p, pressure.units), w))
        return (p0 * (td / t) ** (1.0 / mpconsts.kappa)).m

    w = mixing_ratio(saturation_vapor_pressure(dewpt), pressure)
    fp = so.fixed_point(
        _lcl_iter,
        pressure.m,
        args=(pressure.m, w, temperature),
        xtol=eps,
        maxiter=max_iters,
    )
    lcl_p = fp * pressure.units
    return lcl_p, dewpoint(vapor_pressure(lcl_p, w)).to(temperature.units)


# Traceback (RuntimeError)
/opt/miniconda3/envs/prod/lib/python3.6/site-packages/metpy/interpolate/one_dimension.py:144: UserWarning: Interpolation point out of data bounds encountered
warnings.warn('Interpolation point out of data bounds encountered')
Traceback (most recent call last):
File "trouble_sounding.py", line 17, 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 425, in lfc
this_lcl = lcl(pressure[0], parcel_temperature_profile[0], dewpt_start)
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 360, in lcl
xtol=eps, maxiter=max_iters)
File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 903, in fixed_point
return _fixed_point_helper(func, x0, args, xtol, maxiter, use_accel)
File "/opt/miniconda3/envs/prod/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 857, in _fixed_point_helper
raise RuntimeError(msg)
RuntimeError: Failed to converge after 50 iterations, value is nan

# Fixed code
def lcl(pressure, temperature, dewpt, max_iters=50, eps=1e-5):
    r"""Calculate the lifted condensation level (LCL) using from the starting point.

    The starting state for the parcel is defined by `temperature`, `dewpt`,
    and `pressure`.

    Parameters
    ----------
    pressure : `pint.Quantity`
        The starting atmospheric pressure
    temperature : `pint.Quantity`
        The starting temperature
    dewpt : `pint.Quantity`
        The starting dewpoint

    Returns
    -------
    `pint.Quantity`
        The LCL pressure
    `pint.Quantity`
        The LCL temperature

    Other Parameters
    ----------------
    max_iters : int, optional
        The maximum number of iterations to use in calculation, defaults to 50.
    eps : float, optional
        The desired relative error in the calculated value, defaults to 1e-5.

    See Also
    --------
    parcel_profile

    Notes
    -----
    This function is implemented using an iterative approach to solve for the
    LCL. The basic algorithm is:

    1. Find the dewpoint from the LCL pressure and starting mixing ratio
    2. Find the LCL pressure from the starting temperature and dewpoint
    3. Iterate until convergence

    The function is guaranteed to finish by virtue of the `max_iters` counter.

    """

    def _lcl_iter(p, p0, w, t):
        td = dewpoint(vapor_pressure(units.Quantity(p, pressure.units), w))
        return (p0 * (td / t) ** (1.0 / mpconsts.kappa)).m

    w = mixing_ratio(saturation_vapor_pressure(dewpt), pressure)
    fp = so.fixed_point(
        _lcl_iter,
        pressure.m,
        args=(pressure.m, w, temperature),
        xtol=eps,
        maxiter=max_iters,
    )
    lcl_p = fp * pressure.units

    # Conditional needed due to precision error with np.log in dewpoint.
    # Causes issues with parcel_profile_with_lcl if removed. Issue #1187
    if np.isclose(lcl_p, pressure):
        lcl_p = pressure

    return lcl_p, dewpoint(vapor_pressure(lcl_p, w)).to(temperature.units)