Changeset 7be94aa for src/common/bin/gnuboat-log-converter
- Timestamp:
- Apr 11, 2020, 4:46:15 PM (7 years ago)
- Branches:
- debian/bullseye, master
- Children:
- 3db3d13
- Parents:
- da2be70
- File:
-
- 1 edited
-
src/common/bin/gnuboat-log-converter (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/common/bin/gnuboat-log-converter
rda2be70 r7be94aa 29 29 from gnuboat_common.datahandler import loghandler, logdataset 30 30 31 31 32 class Format(Enum): 32 33 CSV = 1 … … 34 35 GPX = 3 35 36 37 36 38 class CsvCallback: 37 39 def __init__(self, output_file): … … 41 43 self._output_file.writerow(dataset.to_dict()) 42 44 43 def convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 45 46 def convertToCsv( 47 input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time 48 ): 44 49 try: 45 50 output_file = open(output_file_path, 'w') … … 49 54 50 55 output_file_csv = None 51 fields = ['datetime', 'gps_status', 'latitude', 'latitude_direction', 'longitude', 52 'longitude_direction', 'speed_over_ground', 'course_over_ground' , 53 'water_depth', 'water_temperature'] 54 output_file_csv = csv.DictWriter(output_file, delimiter=' ', quotechar='"', 55 quoting=csv.QUOTE_NONNUMERIC, fieldnames=fields) 56 fields = [ 57 'datetime', 'gps_status', 'latitude', 'latitude_direction', 58 'longitude', 'longitude_direction', 'speed_over_ground', 59 'course_over_ground', 'water_depth', 'water_temperature' 60 ] 61 output_file_csv = csv.DictWriter( 62 output_file, 63 delimiter=' ', 64 quotechar='"', 65 quoting=csv.QUOTE_NONNUMERIC, 66 fieldnames=fields 67 ) 56 68 output_file_csv.writeheader() 57 69 58 70 csv_callback = CsvCallback(output_file_csv) 59 71 log_handler = loghandler.LogHandler() 60 result = log_handler.gnuboat_read_file(input_file_path, csv_callback.callback, checksums_fix, errors_ignore) 72 result = log_handler.gnuboat_read_file( 73 input_file_path, csv_callback.callback, checksums_fix, errors_ignore 74 ) 61 75 if result.messages(): 62 76 print('\n'.join(result.errors())) 63 77 output_file.close() 64 78 65 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 79 80 def convertToNmea0183( 81 input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time 82 ): 66 83 try: 67 84 output_file = open(output_file_path, 'w') … … 76 93 print('ERROR: Could not open the input file: {}'.format(error)) 77 94 sys.exit(3) 78 95 79 96 log_handler = loghandler.LogHandler() 80 97 current_row = 1 … … 83 100 nmea_sentence = log_handler.parse_nmea_sentence(line, checksums_fix) 84 101 if nmea_sentence is None: 85 print('INFO: Failed to parse nmea sentence ({}) at line {}'.format(line.strip(), current_row)) 102 print( 103 'INFO: Failed to parse nmea sentence ({}) at line {}'.format( 104 line.strip(), current_row 105 ) 106 ) 86 107 if not errors_ignore: 87 108 input_file.close() … … 91 112 output_string = str(nmea_sentence['nmea_sentence']) 92 113 if not strip_time and nmea_sentence['log_timestamp']: 93 output_string = '{}{}'.format(nmea_sentence['log_timestamp'], output_string) 114 output_string = '{}{}'.format( 115 nmea_sentence['log_timestamp'], output_string 116 ) 94 117 output_file.write('{}\r\n'.format(str(output_string))) 95 118 current_row += 1 96 119 output_file.close() 97 120 input_file.close() 98 99 def convertToGpx(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 121 122 123 def convertToGpx( 124 input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time 125 ): 100 126 # make sure, nmea input file, does not have timestamps in front of the nmea 101 127 # sentences 102 [nmea_without_timestamps_handle, nmea_without_timestamp_file_path] = tempfile.mkstemp(text=True) 103 convertToNmea0183(input_file_path, nmea_without_timestamp_file_path, errors_ignore, checksums_fix, True) 104 105 subprocess.call(['gpsbabel', '-i', 'nmea', '-f', nmea_without_timestamp_file_path, '-o', 'gpx', '-F', output_file_path]) 128 [nmea_without_timestamps_handle, 129 nmea_without_timestamp_file_path] = tempfile.mkstemp(text=True) 130 convertToNmea0183( 131 input_file_path, nmea_without_timestamp_file_path, errors_ignore, 132 checksums_fix, True 133 ) 134 135 subprocess.call([ 136 'gpsbabel', '-i', 'nmea', '-f', nmea_without_timestamp_file_path, '-o', 137 'gpx', '-F', output_file_path 138 ]) 106 139 os.remove(nmea_without_timestamp_file_path) 140 107 141 108 142 def main(argv): … … 116 150 formats = [] 117 151 for file_format in Format: 118 formats.append(file_format.name) 119 120 parser = argparse.ArgumentParser(description='Convert gnuboat logfiles into other formats') 121 parser.add_argument('-f', '--format', dest='output_format', type=str, 122 help='Format of the output file.', choices=formats, default='CSV') 123 parser.add_argument('-i', '--ignore-errors', dest='errors_ignore', action='store_false', 124 help='Ignore any errors reading the log file') 125 parser.add_argument('-c', '--checksums-fix', dest='checksums_fix', action='store_true', 126 help='Ignore all checksums (and replace them with a new correct one, if needed)') 127 parser.add_argument('input_file', metavar='input_file_path', type=str, 128 help='File to be converted (input)') 129 parser.add_argument('output_file', metavar='output_file_path', type=str, 130 help='Write the converted content to file (output)') 131 parser.add_argument('--strip-time', dest='strip_time', action='store_true', 132 help='Removes timestamp in front of the nmea sentence, if it is there') 152 formats.append(file_format.name) 153 154 parser = argparse.ArgumentParser( 155 description='Convert gnuboat logfiles into other formats' 156 ) 157 parser.add_argument( 158 '-f', 159 '--format', 160 dest='output_format', 161 type=str, 162 help='Format of the output file.', 163 choices=formats, 164 default='CSV' 165 ) 166 parser.add_argument( 167 '-i', 168 '--ignore-errors', 169 dest='errors_ignore', 170 action='store_false', 171 help='Ignore any errors reading the log file' 172 ) 173 parser.add_argument( 174 '-c', 175 '--checksums-fix', 176 dest='checksums_fix', 177 action='store_true', 178 help= 179 'Ignore all checksums (and replace them with a new correct one, if needed)' 180 ) 181 parser.add_argument( 182 'input_file', 183 metavar='input_file_path', 184 type=str, 185 help='File to be converted (input)' 186 ) 187 parser.add_argument( 188 'output_file', 189 metavar='output_file_path', 190 type=str, 191 help='Write the converted content to file (output)' 192 ) 193 parser.add_argument( 194 '--strip-time', 195 dest='strip_time', 196 action='store_true', 197 help='Removes timestamp in front of the nmea sentence, if it is there' 198 ) 133 199 arguments = parser.parse_args(argv) 134 200 135 201 if arguments.output_format is not None: 136 202 format_found = False 137 203 for file_format in Format: 138 204 if file_format.name == arguments.output_format: 139 output_format = file_format.value140 format_found = True205 output_format = file_format.value 206 format_found = True 141 207 if not format_found: 142 print('ERROR: Invalid input format specified: {}'.format(arguments.output_format)) 208 print( 209 'ERROR: Invalid input format specified: {}'.format( 210 arguments.output_format 211 ) 212 ) 143 213 sys.exit(1) 144 214 145 215 if arguments.errors_ignore is not None: 146 216 errors_ignore = arguments.errors_ignore 147 217 148 218 if arguments.checksums_fix is not None: 149 219 checksums_fix = arguments.checksums_fix 150 220 151 221 if arguments.strip_time is not None: 152 222 strip_time = arguments.strip_time 153 223 154 224 if arguments.input_file is None: 155 225 print('ERROR: No input file specified') 156 226 sys.exit(2) 157 #elif not os.path.exists(arguments.input_file): 158 print('ERROR: Specified input file does not exists: {}'.format(arguments.input_file)) 227 #elif not os.path.exists(arguments.input_file): 228 print( 229 'ERROR: Specified input file does not exists: {}'.format( 230 arguments.input_file 231 ) 232 ) 159 233 sys.exit(3) 160 234 else: 161 235 input_file_path = arguments.input_file 162 236 163 237 if arguments.output_file is None: 164 238 print('ERROR: No output file specified') 165 239 sys.exit(2) 166 240 elif not os.path.exists(os.path.dirname(arguments.output_file)): 167 print('ERROR: Specified directory of output file does not exists: {}'.format(arguments.output_file)) 241 print( 242 'ERROR: Specified directory of output file does not exists: {}'. 243 format(arguments.output_file) 244 ) 168 245 sys.exit(3) 169 246 else: 170 247 output_file_path = arguments.output_file 171 248 172 if output_format == Format.CSV.value: 173 convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time) 249 if output_format == Format.CSV.value: 250 convertToCsv( 251 input_file_path, output_file_path, errors_ignore, checksums_fix, 252 strip_time 253 ) 174 254 elif output_format == Format.NMEA0183.value: 175 convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time) 255 convertToNmea0183( 256 input_file_path, output_file_path, errors_ignore, checksums_fix, 257 strip_time 258 ) 176 259 elif output_format == Format.GPX.value: 177 convertToGpx(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time) 260 convertToGpx( 261 input_file_path, output_file_path, errors_ignore, checksums_fix, 262 strip_time 263 ) 178 264 else: 179 265 print('ERROR: Undefined output format!\n') … … 182 268 sys.exit(0) 183 269 270 184 271 if __name__ == "__main__": 185 272 main(sys.argv[1:])
Note:
See TracChangeset
for help on using the changeset viewer.
