-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathtrack.py
More file actions
214 lines (193 loc) · 6.24 KB
/
Copy pathpathtrack.py
File metadata and controls
214 lines (193 loc) · 6.24 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
import csv
import io
import pandas as pd
from ..helpers import stream_starts_with
from ..parser_base import CSVParser, Parsable, Parser
from .columns import GPSHarmonizedColumn
from .mixin import GPSHarmonizationMixin
class PathtrackParser(GPSHarmonizationMixin, Parser):
DATATYPE = "gps_pathtrack"
DIVIDER = "*" * 85 + "\n"
HEAD = DIVIDER + "PathTrack Archival Tracking System Results File"
FIELDS = (
"day",
"month",
"year",
"hour",
"minute",
"second",
"second_of_day",
"satellites",
"lat",
"lon",
"altitude",
"clock_offset",
"accuracy", # HDOP?
"battery",
"unknown1",
"unknown2",
)
SEPARATOR = ","
MAPPINGS = {
GPSHarmonizedColumn.ID: None,
GPSHarmonizedColumn.TIMESTAMP: None,
GPSHarmonizedColumn.LATITUDE: "lat",
GPSHarmonizedColumn.LONGITUDE: "lon",
GPSHarmonizedColumn.ALTITUDE: "altitude",
GPSHarmonizedColumn.SPEED_KM_H: None,
GPSHarmonizedColumn.TYPE: None,
GPSHarmonizedColumn.DISTANCE: None,
GPSHarmonizedColumn.COURSE: None,
GPSHarmonizedColumn.HDOP: "accuracy",
GPSHarmonizedColumn.PDOP: None,
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,
}
@classmethod
def can_parse(cls, parsable):
"""Check if the file starts with the expected HEAD bytes."""
try:
with parsable.get_stream(binary=False) as stream:
if not stream.seekable():
return False
return stream_starts_with(stream, cls.HEAD)
except (UnicodeDecodeError, OSError):
return False
def harmonize_data(self, data):
# Combine date and time fields into timestamp
data["timestamp"] = pd.to_datetime(
"20"
+ data["year"].astype(str)
+ "/"
+ data["month"].astype(str)
+ "/"
+ data["day"].astype(str)
+ " "
+ data["hour"].astype(str)
+ ":"
+ data["minute"].astype(str)
+ ":"
+ data["second"].astype(str),
format="%Y/%m/%d %H:%M:%S",
errors="coerce",
)
return super().harmonize_data(data)
def __init__(self, parsable: Parsable):
super().__init__(parsable)
with self.file.get_stream(binary=False) as stream:
if not stream.seekable():
self._raise_not_supported("Stream not seekable")
if not stream_starts_with(stream, self.HEAD):
self._raise_not_supported("Stream head different than expected")
_soi, _metadata, data = stream.read().split(self.DIVIDER, 2)
content = io.StringIO(data)
reader = csv.reader(content, delimiter=self.SEPARATOR)
header = next(reader)
if len(header) != len(self.FIELDS):
self._raise_not_supported(
f"Stream have a number of fields different than expected, "
f"{len(header)} != {len(self.FIELDS)}"
)
self.data = pd.read_csv(
content,
header=0,
names=self.FIELDS,
sep=self.SEPARATOR,
index_col=False,
)
class PathtrackParserNoUnknown(PathtrackParser):
FIELDS = (
"day",
"month",
"year",
"hour",
"minute",
"second",
"second_of_day",
"satellites",
"lat",
"lon",
"altitude",
"clock_offset",
"accuracy", # HDOP?
"battery",
)
class CSVPathtrack(GPSHarmonizationMixin, CSVParser):
DATATYPE = "gps_pathtrack"
FIELDS = [
"day",
"month",
"year",
"hour",
"minute",
"second",
"second_of_the_day",
"satellites",
"latitude",
"longitude",
"altitude",
"clock_offset",
"accuracy_indicator",
"battery",
"processing_parameterA",
"processing_parameterB",
]
SEPARATOR = ";"
MAPPINGS = {
GPSHarmonizedColumn.ID: None,
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: "accuracy_indicator",
GPSHarmonizedColumn.PDOP: None,
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 harmonize_data(self, data):
# Combine date and time fields into a timestamp
data["timestamp"] = pd.to_datetime(
"20" # There are just the last 2 digits of the year
+ data["year"].astype(str)
+ "/"
+ data["month"].astype(str)
+ "/"
+ data["day"].astype(str)
+ " "
+ data["hour"].astype(str)
+ ":"
+ data["minute"].astype(str)
+ ":"
+ data["second"].astype(str),
format="%Y/%m/%d %H:%M:%S",
errors="coerce",
)
for column in [
"latitude",
"longitude",
"altitude",
]:
try:
data[column] = data[column].str.replace(",", ".", regex=False)
data[column] = pd.to_numeric(data[column], errors="coerce")
except AttributeError:
# this fails only when the column is already numeric
pass
return super().harmonize_data(data)
PARSERS = [
PathtrackParser,
PathtrackParserNoUnknown,
CSVPathtrack,
]