Skip to content

Instantly share code, notes, and snippets.

@thesamovar
Created November 20, 2021 19:18
Show Gist options
  • Save thesamovar/2d07c9f2853310357dcc876eb0db95a4 to your computer and use it in GitHub Desktop.
Save thesamovar/2d07c9f2853310357dcc876eb0db95a4 to your computer and use it in GitHub Desktop.
Extract fitness data from tcx files
from pathlib import Path
import xml.etree.ElementTree as ET
import dateutil
import matplotlib.pyplot as plt
import numpy as np
dates = []
distances_km = []
durations_min = []
speeds_km_h = []
paces_min_km = []
total_climbs_m = []
for p in list(Path('.').rglob('*Run*.tcx')):
doc = ET.parse(p)
laps = doc.getroot().findall('.//{*}Lap')
for lap in laps:
start_time = dateutil.parser.parse(lap.attrib['StartTime'])
distance = float(lap.find('{*}DistanceMeters').text)
duration = float(lap.find('{*}TotalTimeSeconds').text)
distance_km = distance/1000
duration_min = duration/60
if distance_km<1:
continue
km_h = (distance_km)/(duration/(60*60))
min_km = duration_min/distance_km
if 1<min_km<10:
# compute total climb
track = lap.find('{*}Track')
altitudes = np.array([float(A.text) for A in track.findall('{*}Trackpoint/{*}AltitudeMeters')])
delta_altitudes = np.diff(altitudes)
total_climb = sum(delta_altitudes[delta_altitudes>0])
# append
dates.append(start_time)
distances_km.append(distance_km)
durations_min.append(duration_min)
speeds_km_h.append(km_h)
paces_min_km.append(min_km)
total_climbs_m.append(total_climb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment