def elevation_gain(elevations, isMiles=True, upthreshold=8, downthreshold=8,
    debug=False):
    """
    calculate elevation gain over a series of elevation points

    NOTE: thresholds of 8 meters approximately matches strava

    :param elevations: list of elevation points in meters
    :param isMiles:  if True return feet, if False return m
    :param upthreshold: threshold of increase when to decide climbing, meters
    :param downthreshold: threshold of decrease when to decide descending, meters
    :param debug: if True, return tuple with gain, debuginfo
    :rtype: gain[, debuginfo] - gain in meters or feet depending on isMiles
    """
    if debug:
        debuginfo = ['ele,state,highup,lowdown,totclimb\n']
    state = 'unknown'
    thisel = elevations[0]
    highup = thisel
    lowdown = thisel
    totclimb = 0.0
    if debug:
        debuginfo.append('{},{},{},{},{}\n'.format(thisel, state, highup,
            lowdown, totclimb))
    for thisel in elevations[1:]:
        if state == 'unknown':
            if thisel >= highup + upthreshold:
                state = 'climbing'
                highup = thisel
            elif thisel <= lowdown - downthreshold:
                state = 'descending'
                lowdown = thisel
        elif state == 'climbing':
            if thisel > highup:
                highup = thisel
            elif thisel <= highup - downthreshold:
                state = 'descending'
                totclimb += highup - lowdown
                lowdown = thisel
        elif state == 'descending':
            if thisel < lowdown:
                lowdown = thisel
            elif thisel >= lowdown + upthreshold:
                state = 'climbing'
                highup = thisel
        if debug:
            debuginfo.append('{},{},{},{},{}\n'.format(thisel, state,
                highup, lowdown, totclimb))
    if state == 'climbing':
        totclimb += highup - lowdown
        if debug:
            debuginfo.pop()
            debuginfo.append('{},{},{},{},{}\n'.format(thisel, state,
                highup, lowdown, totclimb))
    if isMiles:
        totclimb = totclimb / 1609.344 * 5280
    if debug:
        return totclimb, debuginfo
    return totclimb
