-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmea2csv.py
158 lines (129 loc) · 4.28 KB
/
nmea2csv.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
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
#!/usr/bin/python
# FIlename: nmea2csv.py
'''
Created on 29 Aug 2014
@author: [email protected]
'''
import os
import sys
import json
import time
import glob
import math
def writeLog(msg, isDate=True):
sys.stdout.write("%s: %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), msg))
sys.stdout.flush()
def writeErr(msg, isDate=True):
sys.stderr.write("%s: %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), msg))
sys.stderr.flush()
def isNoneOrEmptry(val):
if (val==None) or (val==""):
return True
return False
def toDoubleLatLong(latlon, side):
val = None
if (isNoneOrEmptry(latlon) or isNoneOrEmptry(side)):
return None
try:
tmp = float(latlon)
tmp /= 100
val = math.floor(tmp)
tmp = (tmp - val) * 100
val += tmp/60
tmp -= math.floor(tmp)
tmp *= 60
if ((side.upper() == "S") or (side.upper()=="W")):
val *= -1
except ValueError:
writeErr("Can't calculate from {0} side {1}".format(latlon, side))
val = None
return val
def toFloat(value):
val = None
if isNoneOrEmptry(value):
return None
try:
val = float(value)
except ValueError:
writeErr("Can't convert to float: {0}".format(value))
val = None
return val
def toInt(value):
val = None
if isNoneOrEmptry(value):
return None
try:
val = int(value)
except ValueError:
writeErr("Can't convert to int: {0}".format(value))
val = None
return val
if __name__ == "__main__":
__inputDir = None
__inputFile = None
__outputFile = None
__separate = ','
if len(sys.argv) == 2:
__inputDir = sys.argv[1]
else:
writeLog("Need input file...")
sys.exit(0)
if __inputDir == '.':
__gpsFiles = glob.glob('*.nmea')
else:
__gpsFiles = glob.glob(__inputDir + '/*.nmea')
cnt = 20
GGA = None
with open('geoLapse-full.csv', 'w') as s:
with open('geoLapse-light.csv', 'w') as l:
s.write("timestamp%s date%s time%s Lat%s Lon%s Alt%s speed%s direction\r\n" % (__separate, __separate, __separate, __separate, __separate, __separate, __separate))
l.write("timestamp%s date%s time%s Lat%s Lon%s Alt%s speed%s direction\r\n" % (__separate, __separate, __separate, __separate, __separate, __separate, __separate))
for __inputFile in __gpsFiles:
__outputFile = __inputFile[:-4] + 'csv'
record = int(__inputFile[9:19]) - 3600
linesNMEA = None
writeLog(__inputFile + ' >> ' + __outputFile)
with open(__inputFile, 'r') as f:
rawNMEA = f.read()
linesNMEA = rawNMEA.split("\r")
if linesNMEA == None:
writeLog("Error reading NMEA file")
sys.exit(0)
with open(__outputFile, 'w') as f:
f.write("timestamp%s date%s time%s Lat%s Lon%s Alt%s speed%s direction\r\n" % (__separate, __separate, __separate, __separate, __separate, __separate, __separate))
for line in linesNMEA:
if (line.startswith('$GPGGA')):
GGA = line.split(',')
if (line.startswith('$GPRMC')):
if GGA == None:
continue
if GGA[6] != '1':
continue
RMC = line.split(',')
timeGGA = int(math.floor(toFloat(GGA[1])))
timeRMC = int(math.floor(toFloat(RMC[1])))
if timeGGA != timeRMC:
writeErr("ERROR in data capture")
writeErr(line)
writeErr(oldLine)
knots = toFloat(RMC[7])
date = toInt(RMC[9])
Alt = toFloat(GGA[9])
direction = toFloat(RMC[8])
DateTime = toInt(RMC[9])
Lat = toDoubleLatLong(GGA[2], GGA[3])
Lon = toDoubleLatLong(GGA[4], GGA[5])
kmh = mph = mps = None
if knots != None:
kmh = knots * 1.85200000
mph = knots * 1.15077945
mps = knots * 0.51444444
f.write("%s%s %s%s %s%s %s%s %s%s %s%s %s%s %s\r\n" % (record, __separate, DateTime, __separate, timeGGA, __separate, Lat, __separate, Lon, __separate, Alt, __separate, kmh, __separate, direction))
s.write("%s%s %s%s %s%s %s%s %s%s %s%s %s%s %s\r\n" % (record, __separate, DateTime, __separate, timeGGA, __separate, Lat, __separate, Lon, __separate, Alt, __separate, kmh, __separate, direction))
cnt = cnt - 1
if cnt == 0:
l.write("%s%s %s%s %s%s %s%s %s%s %s%s %s%s %s\r\n" % (record, __separate, DateTime, __separate, timeGGA, __separate, Lat, __separate, Lon, __separate, Alt, __separate, kmh, __separate, direction))
cnt = 20
record = record + 1
GGA = None
writeLog("Finished converting GPS data...")