- File:
-
- 1 edited
-
src/python/bin/gnuboat-log-converter (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/python/bin/gnuboat-log-converter
r6fbf790 rdf8c58a 24 24 from enum import Enum 25 25 import csv 26 import re27 import tempfile28 import subprocess29 26 30 27 class Format(Enum): 31 28 CSV = 1 32 NMEA0183 = 233 GPX = 334 29 35 regex_nmea_sentence = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+\*[\da-zA-Z]+)$") 36 regex_nmea_position = re.compile(r"^([0-9]+)([0-9]{2}\.[0-9]+)$") 30 input_file_path = None 31 output_file_path = None 32 output_format_option = None 33 output_format = Format.CSV 34 errors_ignore = False 37 35 38 36 def toFloat(string, current_row): … … 46 44 return 0 47 45 48 def nmeaToDecimalDegrees(nmea_value, current_row):49 global regex_nmea_position50 51 if toFloat(nmea_value, current_row) == 0:52 return 053 54 matches = regex_nmea_position.match(nmea_value)55 if matches is None:56 print 'INFO: Failed to convert position value from line %s into decimal degrees: %s' % (current_row, nmea_value)57 return58 decimalDegrees = toFloat(matches.group(1), current_row)59 decimalMinutes = toFloat(matches.group(2), current_row)60 return decimalDegrees + (decimalMinutes / 60.0)61 62 def convertToCsv(input_file_path, output_file_path, errors_ignore, strip_time):63 global regex_nmea_sentence64 65 try:66 output_file = open(output_file_path, 'w', 0)67 except IOError as error:68 print "ERROR: Could not open the output file: %s" % error69 sys.exit(3)70 71 output_file_csv = None72 fields = ['datetime', 'gps_status', 'latitude', 'latitude_direction', 'longitude',73 'longitude_direction', 'speed_over_ground', 'course_over_ground' ,74 'water_depth', 'water_temperature']75 output_file_csv = csv.DictWriter(output_file, delimiter=' ', quotechar='"',76 quoting=csv.QUOTE_NONNUMERIC, fieldnames=fields)77 output_file_csv.writeheader()78 79 # reading input file80 try:81 input_file = open(input_file_path, 'r')82 except IOError as error:83 print "ERROR: Could not open the input file: %s" % error84 sys.exit(3)85 current_row = 186 last_row = None87 for line in input_file:88 line = line.strip()89 matches = regex_nmea_sentence.match(line)90 if matches is None:91 print 'INFO: Failed to parse nmea sentence (%s) at line %s\n' % (line, current_row)92 if not errors_ignore:93 input_file.close()94 output_file.close()95 sys.exit(4)96 else:97 nmea_sentence = pynmea2.parse(matches.group(2))98 if nmea_sentence.sentence_type == 'RMC':99 if last_row is not None:100 output_file_csv.writerow(last_row)101 last_row = {'datetime': '%s %s' % (nmea_sentence.datestamp, nmea_sentence.timestamp),102 'gps_status': nmea_sentence.status,103 'latitude': nmeaToDecimalDegrees(nmea_sentence.lat, current_row),104 'latitude_direction': nmea_sentence.lat_dir,105 'longitude': nmeaToDecimalDegrees(nmea_sentence.lon, current_row),106 'longitude_direction': nmea_sentence.lon_dir,107 'speed_over_ground': toFloat(nmea_sentence.spd_over_grnd, current_row),108 'course_over_ground': toFloat(nmea_sentence.true_course, current_row),109 'water_depth': None,110 'water_temperature': None}111 elif nmea_sentence.sentence_type == 'DBT' and last_row is not None:112 last_row['water_depth'] = toFloat(nmea_sentence.depth_meters, current_row)113 elif nmea_sentence.sentence_type == 'MTW' and last_row is not None:114 last_row['water_temperature'] = toFloat(nmea_sentence.temperature, current_row)115 current_row += 1116 if last_row is not None:117 output_file_csv.writerow(last_row)118 output_file.close()119 input_file.close()120 121 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, strip_time):122 global regex_nmea_sentence123 124 try:125 output_file = open(output_file_path, 'w', 0)126 except IOError as error:127 print "ERROR: Could not open the output file: %s" % error128 sys.exit(3)129 130 # reading input file131 try:132 input_file = open(input_file_path, 'r')133 except IOError as error:134 print "ERROR: Could not open the input file: %s" % error135 sys.exit(3)136 current_row = 1137 last_row = None138 for line in input_file:139 line = line.strip()140 matches = regex_nmea_sentence.match(line)141 if matches is None:142 print 'INFO: Failed to parse nmea sentence (%s) at line %s\n' % (line, current_row)143 if not errors_ignore:144 input_file.close()145 output_file.close()146 sys.exit(4)147 else:148 nmea_sentence = pynmea2.parse(matches.group(2))149 output_string = str(nmea_sentence)150 if not strip_time and matches.group(1):151 output_string = '%s%s' % (matches.group(1), output_string)152 output_file.write("%s\r\n" % str(output_string))153 output_file.close()154 input_file.close()155 156 def convertToGpx(input_file_path, output_file_path, errors_ignore, strip_time):157 global regex_nmea_sentence158 159 # make sure, nmea input file, does not have timestamps in front of the nmea160 # sentences161 [nmea_without_timestamps_handle, nmea_without_timestamp_file_path] = tempfile.mkstemp(text=True)162 convertToNmea0183(input_file_path, nmea_without_timestamp_file_path, errors_ignore, True)163 164 subprocess.call(['gpsbabel', '-i', 'nmea', '-f', nmea_without_timestamp_file_path, '-o', 'gpx', '-F', output_file_path])165 os.remove(nmea_without_timestamp_file_path)166 167 46 def main(argv): 168 input_file_path = None169 output_file_path = None170 output_format_option = None171 output_format = Format.CSV172 errors_ignore = False173 strip_time = False174 175 47 formats = [] 176 48 for file_format in Format: … … 186 58 parser.add_argument('output_file', metavar='output_file_path', type=str, 187 59 help='Write the converted content to file (output)') 188 parser.add_argument('--strip-time', dest='strip_time', action='store_true',189 help='Removes timestamp in front of the nmea sentence, if it is there')190 60 arguments = parser.parse_args(argv) 191 61 … … 197 67 format_found = True 198 68 if not format_found: 199 print ' ERROR:Invalid input format specified: %s' % arguments.output_format69 print 'Invalid input format specified: %s' % arguments.output_format 200 70 sys.exit(1) 201 71 202 72 if arguments.errors_ignore is not None: 203 73 errors_ignore = arguments.errors_ignore 204 205 if arguments.strip_time is not None:206 strip_time = arguments.strip_time207 74 208 75 if arguments.input_file is None: 209 print ' ERROR:No input file specified'76 print 'No input file specified' 210 77 sys.exit(2) 211 78 elif not os.path.exists(arguments.input_file): 212 print ' ERROR:Specified input file does not exists: %s' % arguments.input_file79 print 'Specified input file does not exists: %s' % arguments.input_file 213 80 sys.exit(3) 214 81 else: … … 216 83 217 84 if arguments.output_file is None: 218 print ' ERROR:No output file specified'85 print 'No output file specified' 219 86 sys.exit(2) 220 87 elif not os.path.exists(os.path.dirname(arguments.output_file)): 221 print ' ERROR:Specified directory of output file does not exists: %s' % arguments.output_file88 print 'Specified directory of output file does not exists: %s' % arguments.output_file 222 89 sys.exit(3) 223 90 else: 224 91 output_file_path = arguments.output_file 225 92 226 if output_format == Format.CSV.value: 227 convertToCsv(input_file_path, output_file_path, errors_ignore, strip_time) 228 elif output_format == Format.NMEA0183.value: 229 convertToNmea0183(input_file_path, output_file_path, errors_ignore, strip_time) 230 elif output_format == Format.GPX.value: 231 convertToGpx(input_file_path, output_file_path, errors_ignore, strip_time) 232 else: 233 print "ERROR: Undefined output format!\n"; 234 sys.exit(1) 93 try: 94 output_file = open(output_file_path, 'w', 0) 95 except IOError as error: 96 print "Could not open the output file: %s" % error 97 sys.exit(3) 98 99 output_file_csv = None 100 if output_format == Format.CSV.value: 101 fields = ['datetime', 'gps_status', 'latitude', 'latitude_direction', 'longitude', 102 'longitude_direction', 'speed_over_ground', 'course_over_ground' , 103 'water_depth', 'water_temperature'] 104 output_file_csv = csv.DictWriter(output_file, delimiter=' ', quotechar='"', 105 quoting=csv.QUOTE_NONNUMERIC, fieldnames=fields) 106 output_file_csv.writeheader() 235 107 108 # reading input file 109 nmea_errors='raise' 110 if errors_ignore: 111 nmea_errors='ignore' 112 nmea_file = pynmea2.NMEAFile(input_file_path) 113 current_row = 1 114 last_row = None 115 for nmea_sentence in nmea_file: 116 if output_format == Format.CSV.value: 117 if nmea_sentence.sentence_type == 'RMC': 118 if last_row is not None: 119 output_file_csv.writerow(last_row) 120 last_row = {'datetime': '%s %s' % (nmea_sentence.datestamp, nmea_sentence.timestamp), 121 'gps_status': nmea_sentence.status, 122 'latitude': toFloat(nmea_sentence.lat, current_row), 123 'latitude_direction': nmea_sentence.lat_dir, 124 'longitude': toFloat(nmea_sentence.lon, current_row), 125 'longitude_direction': nmea_sentence.lon_dir, 126 'speed_over_ground': toFloat(nmea_sentence.spd_over_grnd, current_row), 127 'course_over_ground': toFloat(nmea_sentence.true_course, current_row), 128 'water_depth': None, 129 'water_temperature': None} 130 elif nmea_sentence.sentence_type == 'DBT' and last_row is not None: 131 last_row['water_depth'] = toFloat(nmea_sentence.depth_meters, current_row) 132 elif nmea_sentence.sentence_type == 'MTW' and last_row is not None: 133 last_row['water_temperature'] = toFloat(nmea_sentence.temperature, current_row) 134 current_row += 1 135 if last_row is not None: 136 output_file_csv.writerow(last_row) 137 138 output_file.close() 236 139 sys.exit(0) 237 140
Note:
See TracChangeset
for help on using the changeset viewer.
