Created
March 16, 2025 10:30
-
-
Save monperrus/cab33728b0637333f330fdc1912e4a2d to your computer and use it in GitHub Desktop.
a python program to compute pace min/kilo from a tcx file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
from datetime import datetime | |
import math | |
import sys | |
def parse_tcx_file(tcx_file): | |
"""Parse TCX file and return trackpoints with time and distance""" | |
# Create namespace dictionary for TCX format | |
ns = {'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'} | |
# Parse the XML file | |
tree = ET.parse(tcx_file) | |
root = tree.getroot() | |
trackpoints = [] | |
# Find all Trackpoint elements | |
for trackpoint in root.findall('.//ns:Trackpoint', ns): | |
time = trackpoint.find('ns:Time', ns) | |
distance = trackpoint.find('ns:DistanceMeters', ns) | |
if time is not None: | |
trackpoints.append({ | |
'time': datetime.strptime(time.text, '%Y-%m-%dT%H:%M:%S.%fZ'), | |
'distance': float(distance.text) | |
}) | |
return trackpoints | |
def calculate_pace(trackpoints, segment_distance=1000): | |
"""Calculate pace for each kilometer segment""" | |
paces = [] | |
prev = trackpoints[0] | |
for i in range(len(trackpoints)-1): | |
if i == 0: | |
continue | |
newp = trackpoints[i] | |
# Calculate distance and time difference between consecutive points | |
distance_diff = newp['distance'] - prev['distance'] | |
time_diff = (newp['time'] - prev['time']).total_seconds() | |
# print(distance_diff,time_diff) | |
if time_diff>10 and time_diff<200 and distance_diff > 10: | |
print(distance_diff,time_diff) | |
# Calculate pace in minutes per kilometer | |
pace = (time_diff / 60) / (distance_diff / 1000) | |
paces.append(pace) | |
prev = newp | |
return paces | |
def format_pace(pace): | |
"""Convert pace to minutes:seconds format""" | |
minutes = int(pace) | |
seconds = int((pace - minutes) * 60) | |
return f"{minutes}:{seconds:02d}" | |
def main(): | |
# Replace with your TCX file path | |
tcx_file = sys.argv[1] | |
# Parse TCX file | |
trackpoints = parse_tcx_file(tcx_file) | |
if not trackpoints: | |
print("No trackpoints found in the TCX file") | |
return | |
# Calculate paces | |
paces = calculate_pace(trackpoints) | |
# Calculate average pace | |
avg_pace = sum(paces) / len(paces) | |
# Print results | |
print(f"Average pace: {format_pace(avg_pace)} min/km") | |
# Print pace for each kilometer | |
print("\nPace by kilometer:") | |
for i, pace in enumerate(paces, 1): | |
print(f"Datapoint {i}: {format_pace(pace)} min/km") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment