# Buggy code
def storm_relative_helicity(
    u,
    v,
    p,
    hgt,
    top,
    bottom=0 * units("meter"),
    storm_u=0 * units("m/s"),
    storm_v=0 * units("m/s"),
):
    # Partially adapted from similar SharpPy code
    r"""Calculate storm relative helicity.

    Needs u and v wind components, heights and pressures,
    and top and bottom of SRH layer. An optional storm
    motion vector can be specified. SRH is calculated using the
    equation specified on p. 230-231 in the Markowski and Richardson
    meso textbook [Markowski2010].

    .. math:: \int\limits_0^d (\bar v - c) \cdot \bar\omega_{h} \,dz

    This is applied to the data from a hodograph with the following summation:

    .. math:: \sum_{n = 1}^{N-1} [(u_{n+1} - c_{x})(v_{n} - c_{y}) -
                                  (u_{n} - c_{x})(v_{n+1} - c_{y})]

    Parameters
    ----------
    u : array-like
        The u components of winds, same length as hgts
    v : array-like
        The u components of winds, same length as hgts
    p : array-like
        Pressure in hPa, same length as hgts
    hgt : array-like
        The heights associatd with the data, provided in meters above mean
        sea level and converted into meters AGL.
    top : number
        The height of the top of the desired layer for SRH.
    bottom : number
        The height at the bottom of the SRH layer. Default is sfc (None).
    storm_u : number
        u component of storm motion
    storm_v : number
        v component of storm motion

    Returns
    -------
    number
        p_srh : positive storm-relative helicity
    number
        n_srh : negative storm-relative helicity
    number
        t_srh : total storm-relative helicity

    """
    # Converting to m/s to make sure output is in m^2/s^2
    u = u.to("meters/second")
    v = v.to("meters/second")
    storm_u = storm_u.to("meters/second")
    storm_v = storm_v.to("meters/second")

    w_int = get_layer(p, u, v, heights=hgt, bottom=bottom, depth=top - bottom)

    sru = w_int[1] - storm_u
    srv = w_int[2] - storm_v

    int_layers = sru[1:] * srv[:-1] - sru[:-1] * srv[1:]

    p_srh = int_layers[int_layers.magnitude > 0.0].sum()
    n_srh = int_layers[int_layers.magnitude < 0.0].sum()
    t_srh = p_srh + n_srh

    return p_srh, n_srh, t_srh


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-b6078272f411> in <module>()
11 hgt = dataset['height'].values * units(dataset.units['height'])
12
---> 13 sreh = mpcalc.storm_relative_helicity(u, v, p, hgt.to('meter'), 6000 * units('meter'))

C:\Users\bradi\Anaconda3\lib\site-packages\metpy\units.py in wrapper(*args, **kwargs)
206                                 '    x = x * units.meter / units.second')
207                     raise ValueError(msg)
--> 208                 return func(*args, **kwargs)
209
210             return wrapper

C:\Users\bradi\Anaconda3\lib\site-packages\metpy\calc\kinematics.py in storm_relative_helicity(u, v, p, hgt, top, bottom, storm_u, storm_v)
596     storm_v = storm_v.to('meters/second')
597
--> 598     w_int = get_layer(p, u, v, heights=hgt, bottom=bottom, depth=top - bottom)
599
600     sru = w_int[1] - storm_u

C:\Users\bradi\Anaconda3\lib\site-packages\metpy\units.py in wrapper(*args, **kwargs)
206                                 '    x = x * units.meter / units.second')
207                     raise ValueError(msg)
--> 208                 return func(*args, **kwargs)
209
210             return wrapper

C:\Users\bradi\Anaconda3\lib\site-packages\metpy\calc\tools.py in get_layer(pressure, *args, **kwargs)
448     bottom_pressure, bottom_height = _get_bound_pressure_height(pressure, bottom,
449                                                                 heights=heights,
--> 450                                                                 interpolate=interpolate)
451
452     # Calculate the top if whatever units depth is in

C:\Users\bradi\Anaconda3\lib\site-packages\metpy\calc\tools.py in _get_bound_pressure_height(pressure, bound, heights, interpolate)
390     if heights is not None:
391         if (bound_height > np.max(heights)) or (bound_height < np.min(heights)):
--> 392             raise ValueError('Specified bound is outside height range.')
393
394     return bound_pressure, bound_height

ValueError: Specified bound is outside height range.

# Fixed code
def storm_relative_helicity(
    u,
    v,
    heights,
    depth,
    bottom=0 * units.m,
    storm_u=0 * units("m/s"),
    storm_v=0 * units("m/s"),
):
    # Partially adapted from similar SharpPy code
    r"""Calculate storm relative helicity.

    Calculates storm relatively helicity following [Markowski2010] 230-231.

    .. math:: \int\limits_0^d (\bar v - c) \cdot \bar\omega_{h} \,dz

    This is applied to the data from a hodograph with the following summation:

    .. math:: \sum_{n = 1}^{N-1} [(u_{n+1} - c_{x})(v_{n} - c_{y}) -
                                  (u_{n} - c_{x})(v_{n+1} - c_{y})]

    Parameters
    ----------
    u : array-like
        u component winds
    v : array-like
        v component winds
    heights : array-like
        atmospheric heights, will be converted to AGL
    depth : number
        depth of the layer
    bottom : number
        height of layer bottom AGL (default is surface)
    storm_u : number
        u component of storm motion (default is 0 m/s)
    storm_v : number
        v component of storm motion (default is 0 m/s)

    Returns
    -------
    `pint.Quantity, pint.Quantity, pint.Quantity`
        positive, negative, total storm-relative helicity

    """
    _, u, v = get_layer_heights(heights, depth, u, v, with_agl=True, bottom=bottom)

    storm_relative_u = u - storm_u
    storm_relative_v = v - storm_v

    int_layers = (
        storm_relative_u[1:] * storm_relative_v[:-1]
        - storm_relative_u[:-1] * storm_relative_v[1:]
    )

    positive_srh = int_layers[int_layers.magnitude > 0.0].sum()
    negative_srh = int_layers[int_layers.magnitude < 0.0].sum()

    return (
        positive_srh.to("meter ** 2 / second ** 2"),
        negative_srh.to("meter ** 2 / second ** 2"),
        (positive_srh + negative_srh).to("meter ** 2 / second ** 2"),
    )