-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_hr.py
79 lines (63 loc) · 2.35 KB
/
merge_hr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gpxpy
import gpxpy.gpx
import argparse
import logging
import sys
import os
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def get_extension_hr(gpxpy_instance):
"""
return a dict with all extension values from all track points.
"""
hr = {}
for track in gpxpy_instance.tracks:
for segment in track.segments:
for point in segment.points:
extensions = point.extensions
if not extensions:
return None
for child in extensions[0].getchildren():
tag = child.tag.rsplit("}", 1)[-1] # FIXME
if tag == 'hr':
hr[point.time] = child
return hr
def main():
arg_parser = argparse.ArgumentParser(
description='Merge HR data from Apple Watch GPX in Wahoo GPX')
arg_parser.add_argument('wahoo_file', help='File Wahoo', type=str)
arg_parser.add_argument('apple_file', help='File Wahoo', type=str)
args = arg_parser.parse_args()
if not args.wahoo_file:
logger.error('Missing argument: Wahoo File')
sys.exit()
if not args.apple_file:
logger.error('Missing argument: Apple File')
sys.exit()
if os.path.exists(args.wahoo_file):
try:
wahoo_gpx_file = open(args.wahoo_file, 'r')
wahoo_gpx = gpxpy.parse(wahoo_gpx_file)
except IOError: # whatever reader errors you care about
logger.error('Cannot open Wahoo File')
sys.exit()
if os.path.exists(args.apple_file):
try:
apple_gpx_file = open(args.apple_file, 'r')
apple_gpx = gpxpy.parse(apple_gpx_file)
except IOError: # whatever reader errors you care about
logger.error('Cannot open Apple File')
sys.exit()
hr = get_extension_hr(apple_gpx)
for wahoo_track in wahoo_gpx.tracks:
for wahoo_segment in wahoo_track.segments:
for wahoo_point in wahoo_segment.points:
if hr.get(wahoo_point.time,None) is not None:
wahoo_extensions = wahoo_point.extensions
if not wahoo_extensions:
pass
wahoo_extensions[0].append(hr[wahoo_point.time])
print(wahoo_gpx.to_xml())
if __name__ == '__main__':
main()