def main(): fname = "summary.dat" tracks = {} f = open(fname, "r") if f == None: return found = None num = 0 numFields = 13 for line in f.readlines(): if line.strip() == "": continue if line.lstrip()[0] == "#": if found != None: found.comment += "\n" + line.rstrip() continue tokens = line.split(None, numFields) if len(tokens) == numFields: comment = "" else: comment = tokens[numFields].rstrip() savedTrack = Track(startTime=datetime.strptime(tokens[0] + tokens[1], "%Y-%m-%d%H:%M:%S"), dist=float(tokens[2]), duration=float(tokens[4]), maxPace=float(tokens[5]), maxHR=float(tokens[7]), avHR=float(tokens[8]), trackpoints=None, comment=comment) found = None for track in tracks.values(): if savedTrack.startTime == track.startTime: track.comment = savedTrack.comment found = track num += 1 break if found == None: tracks[savedTrack.getStartTimeAsStr()] = savedTrack num += 1 print >> sys.stderr, "Loaded additional data for", num, "tracks from \"" + fname + "\"" f.close() for track in sorted(tracks.values(), key=lambda track: track.startTime): xmlFname = track.getStartTimeAsStr() + ".tcx" idTime = track.startTime.strftime("%Y-%m-%dT%H:%M:%SZ") f = open(xmlFname, "w+") print>>f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n",\ "<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\">" print >> f, " <Activities>" print >> f, " <Activity Sport=\"Running\">" print >> f, " <Id>" + idTime + "</Id>" print >> f, " <Lap StartTime=\"" + idTime + "\">" print >> f, " <TotalTimeSeconds>" + str( track.duration * 60.0) + "</TotalTimeSeconds>" print >> f, " <DistanceMeters>" + str( track.dist * METERS_PER_MILE) + "</DistanceMeters>" # FIXME: if you include maxPace, you need to convert from minutes per mile to km/hr # print>>f, " <MaximumSpeed>" + str(track.maxPace) + "</MaximumSpeed> # print>>f, " <Calories>0</Calories>" print >> f, " <AverageHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">" print >> f, " <Value>" + str(track.avHR) + "</Value>" print >> f, " </AverageHeartRateBpm>" print >> f, " <MaximumHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">" print >> f, " <Value>" + str(track.maxHR) + "</Value>" print >> f, " </MaximumHeartRateBpm>" print >> f, " <Intensity>Active</Intensity>" print >> f, " <TriggerMethod>Manual</TriggerMethod>" print >> f, " </Lap>" print >> f, " <Creator xsi:type=\"Device_t\">" print >> f, " <Name>green</Name>" print >> f, " <UnitId>3439781637</UnitId>" print >> f, " <ProductID>717</ProductID>" print >> f, " <Version>" print >> f, " <VersionMajor>2</VersionMajor>" print >> f, " <VersionMinor>50</VersionMinor>" print >> f, " <BuildMajor>0</BuildMajor>" print >> f, " <BuildMinor>0</BuildMinor>" print >> f, " </Version>" print >> f, " </Creator>" print >> f, " </Activity>" print >> f, " </Activities>" print >> f, "" print >> f, " <Author xsi:type=\"Application_t\">" print >> f, " <Name>Garmin ANT Agent(tm)</Name>" print >> f, " <Build>" print >> f, " <Version>" print >> f, " <VersionMajor>2</VersionMajor>" print >> f, " <VersionMinor>2</VersionMinor>" print >> f, " <BuildMajor>7</BuildMajor>" print >> f, " <BuildMinor>0</BuildMinor>" print >> f, " </Version>" print >> f, " <Type>Release</Type>" print >> f, " <Time>Jul 30 2009, 17:42:56</Time>" print >> f, " <Builder>sqa</Builder>" print >> f, " </Build>" print >> f, " <LangID>EN</LangID>" print >> f, " <PartNumber>006-A0214-00</PartNumber>" print >> f, " </Author>" print >> f, "</TrainingCenterDatabase>" f.close()
def main(): cmdOptParser = optparse.OptionParser() (options, fnames) = cmdOptParser.parse_args() numFields = 13 fname = "summary.dat" elevs = {} knownDists = {} comments = {} courses = {} f = open(fname, "r") if f == None: return num = 0 METERS_PER_MILE = 1609.344 FEET_PER_METER = 3.28084 for line in f.readlines(): if line.strip() == "": continue if line.lstrip()[0] == "#": if num > 0: comments[startTime] += "\n" + line.rstrip() continue tokens = line.split(None, numFields) if len(tokens) == numFields: comment = "" else: comment = tokens[numFields].rstrip() startTime = tokens[0] dist = float(tokens[3]) * METERS_PER_MILE if dist > 0: knownDists[startTime] = dist elev = float(tokens[9]) / FEET_PER_METER if elev > 0: elevs[startTime] = elev if tokens[12] != "": courses[startTime] = tokens[12] comments[startTime] = comment num += 1 print >> sys.stderr, "Read", num, "tracks from summary.dat" f.close() # for track in summaryTracks.values(): track.write(sys.stdout, False) for fname in fnames: (track, tree) = Track.from_xml_file(fname, "US/Pacific") if tree is None: continue tree.root = "t:Activities/t:Activity" startTime = track.start_time.strftime("%Y-%m-%d") if not (startTime in knownDists or startTime in elevs or startTime in comments): print startTime, "not found" else: print startTime, if startTime in elevs: print "elev", elevs[startTime] * FEET_PER_METER, if elevs[startTime] > 0: tree.add_elem("", "KnownElevationMeters", elevs[startTime]) if startTime in knownDists: print "dist", knownDists[startTime] / METERS_PER_MILE, if knownDists[startTime] > 0: tree.add_elem("", "KnownDistanceMeters", knownDists[startTime]) if startTime in comments: print "comment", comments[startTime], if comments[startTime] != "": tree.add_elem("", "Comment", comments[startTime]) if startTime in courses: print "course", courses[startTime], tree.add_elem("", "Course", courses[startTime]) print "" tree.write(track.start_time.strftime("%Y-%m-%d-%H%M%S.tcx"))
def main(): fname = "summary.dat" tracks = {} f = open(fname, "r") if f == None: return found = None num = 0 numFields = 13 for line in f.readlines(): if line.strip() == "": continue if line.lstrip()[0] == "#": if found != None: found.comment += "\n" + line.rstrip() continue tokens = line.split(None, numFields) if len(tokens) == numFields: comment = "" else: comment = tokens[numFields].rstrip() savedTrack = Track(startTime = datetime.strptime(tokens[0] + tokens[1], "%Y-%m-%d%H:%M:%S"), dist = float(tokens[2]), duration = float(tokens[4]), maxPace = float(tokens[5]), maxHR = float(tokens[7]), avHR = float(tokens[8]), trackpoints = None, comment = comment) found = None for track in tracks.values(): if savedTrack.startTime == track.startTime: track.comment = savedTrack.comment found = track num += 1 break if found == None: tracks[savedTrack.getStartTimeAsStr()] = savedTrack num += 1 print>>sys.stderr, "Loaded additional data for", num, "tracks from \"" + fname + "\"" f.close() for track in sorted(tracks.values(), key=lambda track: track.startTime): xmlFname = track.getStartTimeAsStr() + ".tcx" idTime = track.startTime.strftime("%Y-%m-%dT%H:%M:%SZ") f = open(xmlFname, "w+") print>>f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n",\ "<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\">" print>>f, " <Activities>" print>>f, " <Activity Sport=\"Running\">" print>>f, " <Id>" + idTime + "</Id>" print>>f, " <Lap StartTime=\"" + idTime + "\">" print>>f, " <TotalTimeSeconds>" + str(track.duration * 60.0) + "</TotalTimeSeconds>" print>>f, " <DistanceMeters>" + str(track.dist * METERS_PER_MILE) + "</DistanceMeters>" # FIXME: if you include maxPace, you need to convert from minutes per mile to km/hr # print>>f, " <MaximumSpeed>" + str(track.maxPace) + "</MaximumSpeed> # print>>f, " <Calories>0</Calories>" print>>f, " <AverageHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">" print>>f, " <Value>" + str(track.avHR) + "</Value>" print>>f, " </AverageHeartRateBpm>" print>>f, " <MaximumHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">" print>>f, " <Value>" + str(track.maxHR) + "</Value>" print>>f, " </MaximumHeartRateBpm>" print>>f, " <Intensity>Active</Intensity>" print>>f, " <TriggerMethod>Manual</TriggerMethod>" print>>f, " </Lap>" print>>f, " <Creator xsi:type=\"Device_t\">" print>>f, " <Name>green</Name>" print>>f, " <UnitId>3439781637</UnitId>" print>>f, " <ProductID>717</ProductID>" print>>f, " <Version>" print>>f, " <VersionMajor>2</VersionMajor>" print>>f, " <VersionMinor>50</VersionMinor>" print>>f, " <BuildMajor>0</BuildMajor>" print>>f, " <BuildMinor>0</BuildMinor>" print>>f, " </Version>" print>>f, " </Creator>" print>>f, " </Activity>" print>>f, " </Activities>" print>>f, "" print>>f, " <Author xsi:type=\"Application_t\">" print>>f, " <Name>Garmin ANT Agent(tm)</Name>" print>>f, " <Build>" print>>f, " <Version>" print>>f, " <VersionMajor>2</VersionMajor>" print>>f, " <VersionMinor>2</VersionMinor>" print>>f, " <BuildMajor>7</BuildMajor>" print>>f, " <BuildMinor>0</BuildMinor>" print>>f, " </Version>" print>>f, " <Type>Release</Type>" print>>f, " <Time>Jul 30 2009, 17:42:56</Time>" print>>f, " <Builder>sqa</Builder>" print>>f, " </Build>" print>>f, " <LangID>EN</LangID>" print>>f, " <PartNumber>006-A0214-00</PartNumber>" print>>f, " </Author>" print>>f, "</TrainingCenterDatabase>" f.close()