This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
csv_export.py
73 lines (55 loc) · 2.36 KB
/
csv_export.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
#!/usr/bin/env python
from functools import partial
from flask import flash
from filters import radian_to_degree, format_distance, format_speed, format_altitude, format_temparature, format_hr, \
format_energyconsumption, format_date_time
from model import Sample
csv_export_unit = False
def add_value(row_array, value, formatter=None):
if not value:
row_array.append("")
else:
if formatter:
row_array.append(str(formatter(value)))
else:
row_array.append(str(value))
def csv_export(move):
samples = move.samples.order_by(Sample.time.asc())
if samples.count() == 0:
flash("No samples found for CSV export", 'error')
return None
# Header
header = ["Timestamp",
"Duration",
"Latitude [deg]",
"Longitude [deg]",
"Altitude [m]",
"Distance [km]",
"Speed [km/h]",
"Temperature [°C]",
"Heart rate [bpm]",
"Energy consumption [kcal/min]",
"HDOP",
"Vertical speed [km/h]",
"Number of satellites"
]
rows = [";".join(header)]
for sample in samples:
row = []
add_value(row, move.date_time + sample.time, formatter=format_date_time)
add_value(row, sample.time)
add_value(row, sample.latitude, formatter=radian_to_degree)
add_value(row, sample.longitude, formatter=radian_to_degree)
add_value(row, sample.altitude, formatter=partial(format_altitude, unit=csv_export_unit))
add_value(row, sample.distance, formatter=partial(format_distance, unit=csv_export_unit))
add_value(row, sample.speed, formatter=partial(format_speed, unit=csv_export_unit))
add_value(row, sample.temperature, formatter=partial(format_temparature, unit=csv_export_unit))
add_value(row, sample.hr, formatter=partial(format_hr, unit=csv_export_unit))
add_value(row, sample.energy_consumption, formatter=partial(format_energyconsumption, unit=csv_export_unit))
add_value(row, sample.gps_hdop)
add_value(row, sample.vertical_speed, formatter=partial(format_speed, unit=csv_export_unit))
add_value(row, sample.number_of_satellites)
rows.append(";".join(row))
# Finally merge all rows
csv_data = "\r\n".join(rows)
return csv_data