| 1 | #!/usr/bin/python
|
|---|
| 2 | # -*- coding: utf-8 -*-
|
|---|
| 3 | '''
|
|---|
| 4 | Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 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
|
|---|
| 26 |
|
|---|
| 27 | class Format(Enum):
|
|---|
| 28 | CSV = 1
|
|---|
| 29 |
|
|---|
| 30 | input_file_path = None
|
|---|
| 31 | output_file_path = None
|
|---|
| 32 | output_format_option = None
|
|---|
| 33 | output_format = Format.CSV
|
|---|
| 34 | errors_ignore = False
|
|---|
| 35 |
|
|---|
| 36 | def toFloat(string, current_row):
|
|---|
| 37 | try:
|
|---|
| 38 | return float(string)
|
|---|
| 39 | except ValueError as error:
|
|---|
| 40 | try:
|
|---|
| 41 | return int(string)
|
|---|
| 42 | except ValueError as error:
|
|---|
| 43 | print 'INFO: Failed to convert value from line %s into numeric: %s\n Message: %s' % (current_row, string, error)
|
|---|
| 44 | return 0
|
|---|
| 45 |
|
|---|
| 46 | def main(argv):
|
|---|
| 47 | formats = []
|
|---|
| 48 | for file_format in Format:
|
|---|
| 49 | formats.append(file_format.name)
|
|---|
| 50 |
|
|---|
| 51 | parser = argparse.ArgumentParser(description="Convert gnuboat logfiles into other formats")
|
|---|
| 52 | parser.add_argument('-f', '--format', dest='output_format', metavar='output_format', type=str,
|
|---|
| 53 | help='Format of the output file.', choices=formats, default='CSV')
|
|---|
| 54 | parser.add_argument('-i', '--ignore-errors', dest='errors_ignore', metavar='errors_ignore', type=bool,
|
|---|
| 55 | help='Ignore any errors reading the log file', default=False)
|
|---|
| 56 | parser.add_argument('input_file', metavar='input_file_path', type=str,
|
|---|
| 57 | help='File to be converted (input)')
|
|---|
| 58 | parser.add_argument('output_file', metavar='output_file_path', type=str,
|
|---|
| 59 | help='Write the converted content to file (output)')
|
|---|
| 60 | arguments = parser.parse_args(argv)
|
|---|
| 61 |
|
|---|
| 62 | if arguments.output_format is not None:
|
|---|
| 63 | format_found = False
|
|---|
| 64 | for file_format in Format:
|
|---|
| 65 | if file_format.name == arguments.output_format:
|
|---|
| 66 | output_format = file_format.value
|
|---|
| 67 | format_found = True
|
|---|
| 68 | if not format_found:
|
|---|
| 69 | print 'Invalid input format specified: %s' % arguments.output_format
|
|---|
| 70 | sys.exit(1)
|
|---|
| 71 |
|
|---|
| 72 | if arguments.errors_ignore is not None:
|
|---|
| 73 | errors_ignore = arguments.errors_ignore
|
|---|
| 74 |
|
|---|
| 75 | if arguments.input_file is None:
|
|---|
| 76 | print 'No input file specified'
|
|---|
| 77 | sys.exit(2)
|
|---|
| 78 | elif not os.path.exists(arguments.input_file):
|
|---|
| 79 | print 'Specified input file does not exists: %s' % arguments.input_file
|
|---|
| 80 | sys.exit(3)
|
|---|
| 81 | else:
|
|---|
| 82 | input_file_path = arguments.input_file
|
|---|
| 83 |
|
|---|
| 84 | if arguments.output_file is None:
|
|---|
| 85 | print 'No output file specified'
|
|---|
| 86 | sys.exit(2)
|
|---|
| 87 | elif not os.path.exists(os.path.dirname(arguments.output_file)):
|
|---|
| 88 | print 'Specified directory of output file does not exists: %s' % arguments.output_file
|
|---|
| 89 | sys.exit(3)
|
|---|
| 90 | else:
|
|---|
| 91 | output_file_path = arguments.output_file
|
|---|
| 92 |
|
|---|
| 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()
|
|---|
| 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()
|
|---|
| 139 | sys.exit(0)
|
|---|
| 140 |
|
|---|
| 141 | if __name__ == "__main__":
|
|---|
| 142 | main(sys.argv[1:])
|
|---|