Skip to content

Commit

Permalink
Towards #8
Browse files Browse the repository at this point in the history
* Split laps by distance only as lap time is completely unreliable as it exludes inactivity time.
* Erroneous (e.g., empty ones in the beginning) laps (e.g. accidental press) will have start time of the next proper lap.

Also Activity Id in UTC
  • Loading branch information
mlt committed Jun 25, 2012
1 parent 0f6d3d2 commit 404bdbd
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/csv2tcx.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,28 +26,25 @@
track_start = tz.localize(datetime.strptime(track["Start"], "%Y-%m-%d %H:%M:%S")) track_start = tz.localize(datetime.strptime(track["Start"], "%Y-%m-%d %H:%M:%S"))
except: # support for legacy format with standalone x1 except: # support for legacy format with standalone x1
track_start = tz.localize(datetime.strptime("{:s}:{:s}".format(track["Start"], track["x1"]), "%Y-%m-%d %H:%M:%S")) track_start = tz.localize(datetime.strptime("{:s}:{:s}".format(track["Start"], track["x1"]), "%Y-%m-%d %H:%M:%S"))
laps_left = int(track["Laps"])


# e = datetime.strptime(track["End"], "%Y-%m-%d %H:%M").replace(tzinfo=tz) # e = datetime.strptime(track["End"], "%Y-%m-%d %H:%M").replace(tzinfo=tz)
print("""<?xml version="1.0" encoding="UTF-8" standalone="no" ?> print("""<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd"> <TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
<Activities> <Activities>
<Activity Sport="Running"> <Activity Sport="Running">
<Id>{:s}</Id>""".format(track_start.isoformat())) <Id>{:s}</Id>""".format(track_start.astimezone(utc).strftime("%Y-%m-%dT%H:%M:%SZ")))
lap_start = track_start lap_start = track_start
time_prev = 0. time_prev = 0.
kcal_prev = 0 kcal_prev = 0
dist_prev = 0. dist_prev = 0.
beats_prev = 0 beats_prev = 0
sec_prev = 0


with open(base+".laps") as l, open(base+".points") as p: with open(base+".laps") as l, open(base+".points") as p:
laps = DictReader(l) laps = DictReader(l)
points = DictReader(p) points = DictReader(p)
thePoint = next(points) thePoint = next(points)
for theLap in laps: for theLap in laps:
laps_left -= 1
time = float(theLap["Time"]) time = float(theLap["Time"])
kcal = int(float(theLap["kcal"])) kcal = int(float(theLap["kcal"]))
lap_dist = float(theLap["Distance"])*1.e3 lap_dist = float(theLap["Distance"])*1.e3
Expand All @@ -63,10 +60,6 @@
theLap["MaxSpeed"], \ theLap["MaxSpeed"], \
kcal - kcal_prev, \ kcal - kcal_prev, \
int((beats - beats_prev)/(time - time_prev)))) int((beats - beats_prev)/(time - time_prev))))
sec = int(theLap["sec"])
lap_start += timedelta(seconds=sec - sec_prev)
# lap_start += timedelta(seconds=time - time_prev)
sec_prev = sec
time_prev = time time_prev = time
kcal_prev = kcal kcal_prev = kcal
dist_prev = lap_dist dist_prev = lap_dist
Expand All @@ -81,13 +74,8 @@
while True: while True:
time = tz.localize(datetime.strptime(thePoint["Time"], "%Y-%m-%d %H:%M:%S")) time = tz.localize(datetime.strptime(thePoint["Time"], "%Y-%m-%d %H:%M:%S"))
dist = float(thePoint["Distance"])*1.e3 dist = float(thePoint["Distance"])*1.e3
if dist > lap_dist: # if for some reason we didn't jump to next lap before if dist > lap_dist: # the only reliable next lap criteria
# print("Point {:s} Over the lap {:s} by distance".format(thePoint["No"], theLap["Lap"])) lap_start = time # the only source of absolute time
lap_start = time
break
if time > lap_start and not int(theLap["autolap"]) and laps_left:
# print("Point {:s} {:s} Over the lap {:s} {:s} by time".format(thePoint["No"], time.isoformat(), theLap["Lap"], lap_start.isoformat()))
lap_start = time
break break
print(""" <Trackpoint> print(""" <Trackpoint>
<Time>{:s}</Time> <Time>{:s}</Time>
Expand Down

0 comments on commit 404bdbd

Please sign in to comment.