| [df8c58a] | 1 | #!/usr/bin/python
|
|---|
| 2 | # -*- coding: utf-8 -*-
|
|---|
| 3 | '''
|
|---|
| [9af0638] | 4 | Copyright (C) 2016 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| [df8c58a] | 5 |
|
|---|
| 6 | This program is free software: you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | '''
|
|---|
| 19 |
|
|---|
| 20 | import os
|
|---|
| 21 | import argparse
|
|---|
| 22 | import sys
|
|---|
| 23 | import pynmea2
|
|---|
| 24 | from enum import Enum
|
|---|
| 25 | import csv
|
|---|
| [6fbf790] | 26 | import tempfile
|
|---|
| 27 | import subprocess
|
|---|
| [df8c58a] | 28 |
|
|---|
| [9af0638] | 29 | from gnuboat_common.datahandler import loghandler, logdataset
|
|---|
| [ea6eba1] | 30 |
|
|---|
| [7be94aa] | 31 |
|
|---|
| [df8c58a] | 32 | class Format(Enum):
|
|---|
| 33 | CSV = 1
|
|---|
| [6fbf790] | 34 | NMEA0183 = 2
|
|---|
| 35 | GPX = 3
|
|---|
| [df8c58a] | 36 |
|
|---|
| [7be94aa] | 37 |
|
|---|
| [ea6eba1] | 38 | class CsvCallback:
|
|---|
| 39 | def __init__(self, output_file):
|
|---|
| 40 | self._output_file = output_file
|
|---|
| [df8c58a] | 41 |
|
|---|
| [ea6eba1] | 42 | def callback(self, dataset):
|
|---|
| 43 | self._output_file.writerow(dataset.to_dict())
|
|---|
| [26f77a6] | 44 |
|
|---|
| [7be94aa] | 45 |
|
|---|
| 46 | def convertToCsv(
|
|---|
| 47 | input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time
|
|---|
| 48 | ):
|
|---|
| [6fbf790] | 49 | try:
|
|---|
| [aa68798] | 50 | output_file = open(output_file_path, 'w')
|
|---|
| [6fbf790] | 51 | except IOError as error:
|
|---|
| [b45e75d] | 52 | print('ERROR: Could not open the output file: {}'.format(error))
|
|---|
| [6fbf790] | 53 | sys.exit(3)
|
|---|
| 54 |
|
|---|
| 55 | output_file_csv = None
|
|---|
| [7be94aa] | 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 | )
|
|---|
| [6fbf790] | 68 | output_file_csv.writeheader()
|
|---|
| 69 |
|
|---|
| [ea6eba1] | 70 | csv_callback = CsvCallback(output_file_csv)
|
|---|
| 71 | log_handler = loghandler.LogHandler()
|
|---|
| [7be94aa] | 72 | result = log_handler.gnuboat_read_file(
|
|---|
| 73 | input_file_path, csv_callback.callback, checksums_fix, errors_ignore
|
|---|
| 74 | )
|
|---|
| [9af0638] | 75 | if result.messages():
|
|---|
| 76 | print('\n'.join(result.errors()))
|
|---|
| [6fbf790] | 77 | output_file.close()
|
|---|
| 78 |
|
|---|
| [7be94aa] | 79 |
|
|---|
| 80 | def convertToNmea0183(
|
|---|
| 81 | input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time
|
|---|
| 82 | ):
|
|---|
| [6fbf790] | 83 | try:
|
|---|
| [aa68798] | 84 | output_file = open(output_file_path, 'w')
|
|---|
| [6fbf790] | 85 | except IOError as error:
|
|---|
| [b45e75d] | 86 | print('ERROR: Could not open the output file: {}'.format(error))
|
|---|
| [6fbf790] | 87 | sys.exit(3)
|
|---|
| 88 |
|
|---|
| 89 | # reading input file
|
|---|
| 90 | try:
|
|---|
| 91 | input_file = open(input_file_path, 'r')
|
|---|
| 92 | except IOError as error:
|
|---|
| [b45e75d] | 93 | print('ERROR: Could not open the input file: {}'.format(error))
|
|---|
| [6fbf790] | 94 | sys.exit(3)
|
|---|
| [7be94aa] | 95 |
|
|---|
| [ea6eba1] | 96 | log_handler = loghandler.LogHandler()
|
|---|
| [6fbf790] | 97 | current_row = 1
|
|---|
| 98 | last_row = None
|
|---|
| 99 | for line in input_file:
|
|---|
| [ea6eba1] | 100 | nmea_sentence = log_handler.parse_nmea_sentence(line, checksums_fix)
|
|---|
| 101 | if nmea_sentence is None:
|
|---|
| [7be94aa] | 102 | print(
|
|---|
| 103 | 'INFO: Failed to parse nmea sentence ({}) at line {}'.format(
|
|---|
| 104 | line.strip(), current_row
|
|---|
| 105 | )
|
|---|
| 106 | )
|
|---|
| [6fbf790] | 107 | if not errors_ignore:
|
|---|
| 108 | input_file.close()
|
|---|
| 109 | output_file.close()
|
|---|
| 110 | sys.exit(4)
|
|---|
| 111 | else:
|
|---|
| [ea6eba1] | 112 | output_string = str(nmea_sentence['nmea_sentence'])
|
|---|
| 113 | if not strip_time and nmea_sentence['log_timestamp']:
|
|---|
| [7be94aa] | 114 | output_string = '{}{}'.format(
|
|---|
| 115 | nmea_sentence['log_timestamp'], output_string
|
|---|
| 116 | )
|
|---|
| [b45e75d] | 117 | output_file.write('{}\r\n'.format(str(output_string)))
|
|---|
| [ea6eba1] | 118 | current_row += 1
|
|---|
| [6fbf790] | 119 | output_file.close()
|
|---|
| 120 | input_file.close()
|
|---|
| [7be94aa] | 121 |
|
|---|
| 122 |
|
|---|
| 123 | def convertToGpx(
|
|---|
| 124 | input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time
|
|---|
| 125 | ):
|
|---|
| [6fbf790] | 126 | # make sure, nmea input file, does not have timestamps in front of the nmea
|
|---|
| 127 | # sentences
|
|---|
| [7be94aa] | 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 | ])
|
|---|
| [6fbf790] | 139 | os.remove(nmea_without_timestamp_file_path)
|
|---|
| 140 |
|
|---|
| [7be94aa] | 141 |
|
|---|
| [df8c58a] | 142 | def main(argv):
|
|---|
| [6fbf790] | 143 | input_file_path = None
|
|---|
| 144 | output_file_path = None
|
|---|
| 145 | output_format_option = None
|
|---|
| 146 | output_format = Format.CSV
|
|---|
| 147 | errors_ignore = False
|
|---|
| 148 | strip_time = False
|
|---|
| [ea6eba1] | 149 |
|
|---|
| [df8c58a] | 150 | formats = []
|
|---|
| 151 | for file_format in Format:
|
|---|
| [7be94aa] | 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 | )
|
|---|
| [df8c58a] | 199 | arguments = parser.parse_args(argv)
|
|---|
| [7be94aa] | 200 |
|
|---|
| [df8c58a] | 201 | if arguments.output_format is not None:
|
|---|
| 202 | format_found = False
|
|---|
| 203 | for file_format in Format:
|
|---|
| 204 | if file_format.name == arguments.output_format:
|
|---|
| [7be94aa] | 205 | output_format = file_format.value
|
|---|
| 206 | format_found = True
|
|---|
| [df8c58a] | 207 | if not format_found:
|
|---|
| [7be94aa] | 208 | print(
|
|---|
| 209 | 'ERROR: Invalid input format specified: {}'.format(
|
|---|
| 210 | arguments.output_format
|
|---|
| 211 | )
|
|---|
| 212 | )
|
|---|
| [df8c58a] | 213 | sys.exit(1)
|
|---|
| 214 |
|
|---|
| 215 | if arguments.errors_ignore is not None:
|
|---|
| 216 | errors_ignore = arguments.errors_ignore
|
|---|
| [7be94aa] | 217 |
|
|---|
| [26f77a6] | 218 | if arguments.checksums_fix is not None:
|
|---|
| 219 | checksums_fix = arguments.checksums_fix
|
|---|
| [7be94aa] | 220 |
|
|---|
| [6fbf790] | 221 | if arguments.strip_time is not None:
|
|---|
| 222 | strip_time = arguments.strip_time
|
|---|
| [7be94aa] | 223 |
|
|---|
| [df8c58a] | 224 | if arguments.input_file is None:
|
|---|
| [b45e75d] | 225 | print('ERROR: No input file specified')
|
|---|
| [df8c58a] | 226 | sys.exit(2)
|
|---|
| [7be94aa] | 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 | )
|
|---|
| [df8c58a] | 233 | sys.exit(3)
|
|---|
| 234 | else:
|
|---|
| 235 | input_file_path = arguments.input_file
|
|---|
| [7be94aa] | 236 |
|
|---|
| [df8c58a] | 237 | if arguments.output_file is None:
|
|---|
| [b45e75d] | 238 | print('ERROR: No output file specified')
|
|---|
| [df8c58a] | 239 | sys.exit(2)
|
|---|
| 240 | elif not os.path.exists(os.path.dirname(arguments.output_file)):
|
|---|
| [7be94aa] | 241 | print(
|
|---|
| 242 | 'ERROR: Specified directory of output file does not exists: {}'.
|
|---|
| 243 | format(arguments.output_file)
|
|---|
| 244 | )
|
|---|
| [df8c58a] | 245 | sys.exit(3)
|
|---|
| 246 | else:
|
|---|
| 247 | output_file_path = arguments.output_file
|
|---|
| 248 |
|
|---|
| [7be94aa] | 249 | if output_format == Format.CSV.value:
|
|---|
| 250 | convertToCsv(
|
|---|
| 251 | input_file_path, output_file_path, errors_ignore, checksums_fix,
|
|---|
| 252 | strip_time
|
|---|
| 253 | )
|
|---|
| [6fbf790] | 254 | elif output_format == Format.NMEA0183.value:
|
|---|
| [7be94aa] | 255 | convertToNmea0183(
|
|---|
| 256 | input_file_path, output_file_path, errors_ignore, checksums_fix,
|
|---|
| 257 | strip_time
|
|---|
| 258 | )
|
|---|
| [6fbf790] | 259 | elif output_format == Format.GPX.value:
|
|---|
| [7be94aa] | 260 | convertToGpx(
|
|---|
| 261 | input_file_path, output_file_path, errors_ignore, checksums_fix,
|
|---|
| 262 | strip_time
|
|---|
| 263 | )
|
|---|
| [6fbf790] | 264 | else:
|
|---|
| [b45e75d] | 265 | print('ERROR: Undefined output format!\n')
|
|---|
| [6fbf790] | 266 | sys.exit(1)
|
|---|
| [df8c58a] | 267 |
|
|---|
| 268 | sys.exit(0)
|
|---|
| 269 |
|
|---|
| [7be94aa] | 270 |
|
|---|
| [df8c58a] | 271 | if __name__ == "__main__":
|
|---|
| 272 | main(sys.argv[1:])
|
|---|