-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit_to_csv.py
76 lines (66 loc) · 2.06 KB
/
fit_to_csv.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
import csv
import os
# to install fitparse, run
# sudo pip3 install -e git+https://github.com/dtcooper/python-fitparse#egg=python-fitparse
import fitparse
import pytz
allowed_fields = [
"timestamp",
"position_lat",
"position_long",
"distance",
"enhanced_altitude",
"altitude",
"enhanced_speed",
"speed",
"heart_rate",
"cadence",
"fractional_cadence",
]
required_fields = ["timestamp", "position_lat", "position_long", "altitude"]
UTC = pytz.UTC
CST = pytz.timezone("US/Central")
def main():
files = os.listdir()
fit_files = [file for file in files if file[-4:].lower() == ".fit"]
for file in fit_files:
new_filename = file[:-4] + ".csv"
if os.path.exists(new_filename):
# print('%s already exists. skipping.' % new_filename)
continue
fitfile = fitparse.FitFile(
file, data_processor=fitparse.StandardUnitsDataProcessor()
)
print("converting %s" % file)
write_fitfile_to_csv(fitfile, new_filename)
print("finished conversions")
def write_fitfile_to_csv(fitfile, output_file="test_output.csv"):
messages = fitfile.messages
data = []
for m in messages:
skip = False
if not hasattr(m, "fields"):
continue
fields = m.fields
# check for important data types
mdata = {}
for field in fields:
if field.name in allowed_fields:
if field.name == "timestamp":
mdata[field.name] = UTC.localize(field.value).astimezone(CST)
else:
mdata[field.name] = field.value
for rf in required_fields:
if rf not in mdata:
skip = True
if not skip:
data.append(mdata)
# write to csv
with open(output_file, "w") as f:
writer = csv.writer(f)
writer.writerow(allowed_fields)
for entry in data:
writer.writerow([str(entry.get(k, "")) for k in allowed_fields])
print("wrote %s" % output_file)
if __name__ == "__main__":
main()