-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatlog.py
More file actions
239 lines (209 loc) · 7.33 KB
/
Copy pathcatlog.py
File metadata and controls
239 lines (209 loc) · 7.33 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import csv
import io
import pandas as pd
from ..helpers import stream_starts_with
from ..parser_base import CSVParser, Parsable
from .columns import GPSHarmonizedColumn
from .mixin import GPSHarmonizationMixin
class GPSCatTrackParser(GPSHarmonizationMixin, CSVParser):
"""
Parser for a format, its a GPS CSV like format
with the following fields
"""
DATATYPE = "gps_cattrack"
DIVIDER = "--------\n"
START_WITH = "Name:CatLog"
FIELDS = [
"Date",
"Time",
"Latitude",
"Longitude",
"Altitude",
"Satellites",
"HDOP",
"PDOP",
"Temperature [C]",
"Speed [km/h]",
"TTFF",
"SNR",
"tbd",
]
MAPPINGS = {
GPSHarmonizedColumn.ID: "",
GPSHarmonizedColumn.TIMESTAMP: None,
GPSHarmonizedColumn.LATITUDE: "Latitude",
GPSHarmonizedColumn.LONGITUDE: "Longitude",
GPSHarmonizedColumn.ALTITUDE: "Altitude",
GPSHarmonizedColumn.SPEED_KM_H: "Speed [km/h]",
GPSHarmonizedColumn.TYPE: None,
GPSHarmonizedColumn.DISTANCE: None,
GPSHarmonizedColumn.COURSE: None,
GPSHarmonizedColumn.HDOP: "HDOP",
GPSHarmonizedColumn.PDOP: "PDOP",
GPSHarmonizedColumn.SATELLITES_COUNT: "Satellites",
GPSHarmonizedColumn.TEMPERATURE: "Temperature [C]",
GPSHarmonizedColumn.SOLAR_I_MA: None,
GPSHarmonizedColumn.BAT_SOC_PCT: None,
GPSHarmonizedColumn.RING_NR: None,
GPSHarmonizedColumn.TRIP_NR: None,
}
@classmethod
def can_parse(cls, parsable):
"""Check if the file starts with the expected START_WITH prefix."""
if not cls.START_WITH:
return True
try:
with parsable.get_stream(binary=False) as stream:
return stream_starts_with(stream, cls.START_WITH)
except (UnicodeDecodeError, OSError):
return False
def harmonize_data(self, data):
# Combine Date and Time columns into timestamp
data["timestamp"] = pd.to_datetime(
data["Date"] + " " + data["Time"], errors="raise"
)
return super().harmonize_data(data)
def __init__(self, parsable: Parsable):
self.file = parsable
self.data = []
with self.file.get_stream(binary=False) as stream:
if not stream.seekable():
self._raise_not_supported("Stream not seekable")
if self.START_WITH and not stream_starts_with(stream, self.START_WITH):
self._raise_not_supported("Stream must start with Name:CatLog")
if self.DIVIDER:
full_content = stream.read()
if self.DIVIDER not in full_content:
self._raise_not_supported(
f"Stream doesn't have the divider {self.DIVIDER}"
)
_intro, data = full_content.split(self.DIVIDER, 1)
content = io.StringIO(data)
else:
content = stream
reader = csv.reader(
content,
delimiter=self.SEPARATOR,
skipinitialspace=self.SKIP_INITIAL_SPACE,
)
header = next(reader)
if header != self.FIELDS:
self._raise_not_supported(
f"Stream have fields different than expected, "
f"{header} != {self.FIELDS}"
)
self.data = pd.read_csv(
content,
header=0,
names=self.FIELDS,
sep=self.SEPARATOR,
index_col=False,
)
class GPSCatTrack2(GPSCatTrackParser):
FIELDS = [
"Date",
"Time",
"Latitude",
"Longitude",
"Altitude",
"Satellites",
"HDOP",
"PDOP",
"TTF [s]",
"Info",
]
DIVIDER = "-----\n"
MAPPINGS = {
GPSHarmonizedColumn.ID: "",
GPSHarmonizedColumn.TIMESTAMP: None,
GPSHarmonizedColumn.LATITUDE: "Latitude",
GPSHarmonizedColumn.LONGITUDE: "Longitude",
GPSHarmonizedColumn.ALTITUDE: "Altitude",
GPSHarmonizedColumn.SPEED_KM_H: None,
GPSHarmonizedColumn.TYPE: None,
GPSHarmonizedColumn.DISTANCE: None,
GPSHarmonizedColumn.COURSE: None,
GPSHarmonizedColumn.HDOP: "HDOP",
GPSHarmonizedColumn.PDOP: "PDOP",
GPSHarmonizedColumn.SATELLITES_COUNT: "Satellites",
GPSHarmonizedColumn.TEMPERATURE: None,
GPSHarmonizedColumn.SOLAR_I_MA: None,
GPSHarmonizedColumn.BAT_SOC_PCT: None,
GPSHarmonizedColumn.RING_NR: None,
GPSHarmonizedColumn.TRIP_NR: None,
}
class GPSCatTrack3(GPSCatTrackParser):
"""
This CatTrack logger has a wrong number of columns, 2 are missing
"""
FIELDS = [
"Date",
"Time",
"Latitude",
"Longitude",
"Altitude",
"Satellites",
"HDOP",
"PDOP",
"TTF [s]",
"Info",
]
DIVIDER = "--------\n"
MAPPINGS = {
GPSHarmonizedColumn.ID: "",
GPSHarmonizedColumn.TIMESTAMP: None,
GPSHarmonizedColumn.LATITUDE: "Latitude",
GPSHarmonizedColumn.LONGITUDE: "Longitude",
GPSHarmonizedColumn.ALTITUDE: "Altitude",
GPSHarmonizedColumn.SPEED_KM_H: None,
GPSHarmonizedColumn.TYPE: None,
GPSHarmonizedColumn.DISTANCE: None,
GPSHarmonizedColumn.COURSE: None,
GPSHarmonizedColumn.HDOP: "HDOP",
GPSHarmonizedColumn.PDOP: "PDOP",
GPSHarmonizedColumn.SATELLITES_COUNT: "Satellites",
GPSHarmonizedColumn.TEMPERATURE: None,
GPSHarmonizedColumn.SOLAR_I_MA: None,
GPSHarmonizedColumn.BAT_SOC_PCT: None,
GPSHarmonizedColumn.RING_NR: None,
GPSHarmonizedColumn.TRIP_NR: None,
}
def __init__(self, parsable: Parsable):
self.file = parsable
self.data = []
with self.file.get_stream(binary=False) as stream:
if not stream.seekable():
self._raise_not_supported("Stream not seekable")
if self.START_WITH and not stream_starts_with(stream, self.START_WITH):
self._raise_not_supported("Stream must start with Name:CatLog")
if self.DIVIDER:
full_content = stream.read()
if self.DIVIDER not in full_content:
self._raise_not_supported(
f"Stream doesn't have the divider {self.DIVIDER}"
)
_intro, data = full_content.split(self.DIVIDER, 1)
content = io.StringIO(data)
else:
content = stream
reader = csv.reader(
content,
delimiter=self.SEPARATOR,
skipinitialspace=self.SKIP_INITIAL_SPACE,
)
header = next(reader)
if header != self.FIELDS:
self._raise_not_supported(
f"Stream have fields different than expected, "
f"{header} != {self.FIELDS}"
)
# Ensure that the headers are present, then ignore them
names = self.FIELDS[:-2]
self.data = pd.read_csv(
content, header=0, names=names, sep=self.SEPARATOR, index_col=False
)
PARSERS = [
GPSCatTrackParser,
GPSCatTrack2,
GPSCatTrack3,
]