I broke the algorithm for this out from the code section because it warranted its own explanation.

In a perfect world with perfect sensors and perfect weather (Ideal Gas Law anyone?) we wouldn't need a special algorithm to handle the data from the sensor. Ideally we would just add up all the positive changes in altitude (distance climbed vertically) and that would be it. Since we're not that lucky, we need an a way to smooth out the data coming from the sensor to get an accurate estimation of the distance climbed.

This is illustrated in the attached graphs of the test data I produced in R v3.1.1 to model the algorithm. This is very simplistic, but it illustrates what we will encounter with real world data without being too simplistic.

The first graph (black line) is the actual "perfect world" data with the actual total vertical distance climbed.

The second graph (red line) is the simulated sensor data with the sum of the positive values as the vertical distance climbed.

The third graph (green line) is the smoothed simulated sensor data with its sum of the positive values as the total vertical distance climbed.

The fourth graph is a plot of all the lines on the same graph.

The second image is a larger version of the plot of all the lines on the same graph. In this one you can see the noise in the simulated sensor data (red line) and how the algorithm smooths it out (green line, take a look at the corners).

As you can see, just taking the positive changes in altitude from the sensor data as we would do in a perfect world ends up skewing the total distance climbed in the positive direction. This error is iterative with every sensor reading so over time the amount the total distance is skewed by will grow. Smoothing the data by taking an average of the past 10 readings avoids this.

Smoothing the data in this fashion comes with its own limitations, but it's better than not smoothing it. Here we can see it rounds off the corners, subtracting distance at ever acute angle and adding distance at every obtuse angle. Of course better algorithms will model the actual distance climbed better, however I'm not looking to get that complicated.