Changeset ea6eba1 for src/python/bin/gnuboat-log-converter
- Timestamp:
- Jun 10, 2018, 12:28:07 PM (8 years ago)
- Branches:
- debian/bullseye, master
- Children:
- ca02e8f
- Parents:
- 9f28f0a
- git-author:
- Joachim Langenbach <joachim.langenbach@…> (06/10/18 11:49:37)
- git-committer:
- Joachim Langenbach <joachim.langenbach@…> (06/10/18 12:28:07)
- File:
-
- 1 edited
-
src/python/bin/gnuboat-log-converter (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/python/bin/gnuboat-log-converter
r9f28f0a rea6eba1 24 24 from enum import Enum 25 25 import csv 26 import re27 26 import tempfile 28 27 import subprocess 28 29 from gnuboat.datahandler import loghandler, logdataset 29 30 30 31 class Format(Enum): … … 33 34 GPX = 3 34 35 35 regex_nmea_sentence = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+\*[\da-zA-Z]+)$") 36 regex_nmea_sentence_without_checksum = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+)\*[\da-zA-Z]+$") 37 regex_nmea_position_latitude = re.compile(r"^([0-9]{2})([0-9]{2}\.[0-9]+)$") 38 regex_nmea_position_longitude = re.compile(r"^([0-9]{3})([0-9]{2}\.[0-9]+)$") 36 class CsvCallback: 37 def __init__(self, output_file): 38 self._output_file = output_file 39 39 40 def toFloat(string, current_row): 41 try: 42 return float(string) 43 except ValueError as error: 44 try: 45 return int(string) 46 except ValueError as error: 47 print('INFO: Failed to convert value from line {} into numeric: {}\n Message: {}'.format(current_row, string, error)) 48 return 0 49 50 def nmeaLatitudeToDecimalDegrees(nmea_value, current_row): 51 global regex_nmea_position_latitude 52 53 if toFloat(nmea_value, current_row) == 0: 54 return 0 55 56 matches = regex_nmea_position_latitude.match(nmea_value) 57 if matches is None: 58 print('INFO: Failed to convert latitude position value from line {} into decimal degrees: {}'.format(current_row, nmea_value)) 59 return 60 decimalDegrees = toFloat(matches.group(1), current_row) 61 decimalMinutes = toFloat(matches.group(2), current_row) 62 return decimalDegrees + (decimalMinutes / 60.0) 63 64 def nmeaLongitudeToDecimalDegrees(nmea_value, current_row): 65 global regex_nmea_position_longitude 66 67 if toFloat(nmea_value, current_row) == 0: 68 return 0 69 70 matches = regex_nmea_position_longitude.match(nmea_value) 71 if matches is None: 72 print('INFO: Failed to convert longitude position value from line {} into decimal degrees: {}'.format(current_row, nmea_value)) 73 return 74 decimalDegrees = toFloat(matches.group(1), current_row) 75 decimalMinutes = toFloat(matches.group(2), current_row) 76 return decimalDegrees + (decimalMinutes / 60.0) 40 def callback(self, dataset): 41 self._output_file.writerow(dataset.to_dict()) 77 42 78 43 def convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 79 global regex_nmea_sentence80 global regex_nmea_sentence_without_checksum81 82 44 try: 83 45 output_file = open(output_file_path, 'w', 0) … … 94 56 output_file_csv.writeheader() 95 57 96 # reading input file 97 try: 98 input_file = open(input_file_path, 'r') 99 except IOError as error: 100 print('ERROR: Could not open the input file: {}'.format(error)) 101 sys.exit(3) 102 current_row = 1 103 last_row = None 104 for line in input_file: 105 line = line.strip() 106 if checksums_fix: 107 matches = regex_nmea_sentence_without_checksum.match(line) 108 pynmea_parse_check = False 109 else: 110 matches = regex_nmea_sentence.match(line) 111 pynmea_parse_check = True 112 if matches is None: 113 print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row)) 114 if not errors_ignore: 115 input_file.close() 116 output_file.close() 117 sys.exit(4) 118 else: 119 nmea_sentence = pynmea2.parse(matches.group(2), check=pynmea_parse_check) 120 if nmea_sentence.sentence_type == 'RMC': 121 if last_row is not None: 122 output_file_csv.writerow(last_row) 123 last_row = {'datetime': '{} {}'.format(nmea_sentence.datestamp, nmea_sentence.timestamp), 124 'gps_status': nmea_sentence.status, 125 'latitude': nmeaLatitudeToDecimalDegrees(nmea_sentence.lat, current_row), 126 'latitude_direction': nmea_sentence.lat_dir, 127 'longitude': nmeaLongitudeToDecimalDegrees(nmea_sentence.lon, current_row), 128 'longitude_direction': nmea_sentence.lon_dir, 129 'speed_over_ground': toFloat(nmea_sentence.spd_over_grnd, current_row), 130 'course_over_ground': toFloat(nmea_sentence.true_course, current_row), 131 'water_depth': None, 132 'water_temperature': None} 133 elif nmea_sentence.sentence_type == 'DPT' and last_row is not None: 134 last_row['water_depth'] = toFloat(nmea_sentence.depth, current_row) 135 elif nmea_sentence.sentence_type == 'MTW' and last_row is not None: 136 last_row['water_temperature'] = toFloat(nmea_sentence.temperature, current_row) 137 current_row += 1 138 if last_row is not None: 139 output_file_csv.writerow(last_row) 58 csv_callback = CsvCallback(output_file_csv) 59 log_handler = loghandler.LogHandler() 60 log_handler.gnuboat_read(input_file_path, csv_callback.callback, checksums_fix, errors_ignore) 140 61 output_file.close() 141 input_file.close()142 62 143 63 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 144 global regex_nmea_sentence145 global regex_nmea_sentence_without_checksum146 147 64 try: 148 65 output_file = open(output_file_path, 'w', 0) … … 157 74 print('ERROR: Could not open the input file: {}'.format(error)) 158 75 sys.exit(3) 76 77 log_handler = loghandler.LogHandler() 159 78 current_row = 1 160 79 last_row = None 161 80 for line in input_file: 162 line = line.strip() 163 if checksums_fix: 164 matches = regex_nmea_sentence_without_checksum.match(line) 165 pynmea_parse_check = False 166 else: 167 matches = regex_nmea_sentence.match(line) 168 pynmea_parse_check = True 169 if matches is None: 170 print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row)) 81 nmea_sentence = log_handler.parse_nmea_sentence(line, checksums_fix) 82 if nmea_sentence is None: 83 print('INFO: Failed to parse nmea sentence ({}) at line {}'.format(line.strip(), current_row)) 171 84 if not errors_ignore: 172 85 input_file.close() … … 174 87 sys.exit(4) 175 88 else: 176 nmea_sentence = pynmea2.parse(matches.group(2), check=pynmea_parse_check) 177 output_string = str(nmea_sentence) 178 if not strip_time and matches.group(1): 179 output_string = '{}{}'.format(matches.group(1), output_string) 89 output_string = str(nmea_sentence['nmea_sentence']) 90 if not strip_time and nmea_sentence['log_timestamp']: 91 output_string = '{}{}'.format(nmea_sentence['log_timestamp'], output_string) 180 92 output_file.write('{}\r\n'.format(str(output_string))) 93 current_row += 1 181 94 output_file.close() 182 95 input_file.close() … … 198 111 errors_ignore = False 199 112 strip_time = False 200 113 201 114 formats = [] 202 115 for file_format in Format:
Note:
See TracChangeset
for help on using the changeset viewer.
