# Buggy 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"),
    )


# Traceback (IndexError)
Traceback (most recent call last):
File "cal_sounding_data.py", line 360, in <module>
newset_dic[k] = specStationSounding(k,dataset_dic)
File "cal_sounding_data.py", line 284, in specStationSounding
result_dic = soundingCalculation(num,p,t,td,ws,wd)
File "cal_sounding_data.py", line 170, in soundingCalculation
lfc_p, lfc_T = mpcalc.lfc(P,T,Td)
File "/anaconda2/envs/py3/lib/python3.6/site-packages/metpy/xarray.py", line 138, in wrapper
return func(*args, **kwargs)
File "/anaconda2/envs/py3/lib/python3.6/site-packages/metpy/units.py", line 290, in wrapper
return func(*args, **kwargs)
File "/anaconda2/envs/py3/lib/python3.6/site-packages/metpy/calc/thermo.py", line 378, in lfc
return x[0], y[0]
File "/anaconda2/envs/py3/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 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:]
    )

    # Need to manually check for masked value because sum() on masked array with non-default
    # mask will return a masked value rather than 0. See numpy/numpy#11736
    positive_srh = int_layers[int_layers.magnitude > 0.0].sum()
    if np.ma.is_masked(positive_srh):
        positive_srh = 0.0 * units("meter**2 / second**2")
    negative_srh = int_layers[int_layers.magnitude < 0.0].sum()
    if np.ma.is_masked(negative_srh):
        negative_srh = 0.0 * units("meter**2 / second**2")

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