Ignore:
Timestamp:
Jun 10, 2018, 12:28:07 PM (8 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
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)
Message:
  • Transfered all code related to parsing gnuboat log files into a new module datahandler
  • datahandler/loghandler.py: Code to write and parse gnuboat log files
  • datahandler/logdataset.py: Object, which holds a complete dataset from gnuboat log files
  • updated gnuboat-log-converter to use the loghandler module
  • updated gnuboat daemon to use the loghandler module
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/python/bin/gnuboat-log-converter

    r9f28f0a rea6eba1  
    2424from enum import Enum
    2525import csv
    26 import re
    2726import tempfile
    2827import subprocess
     28
     29from gnuboat.datahandler import loghandler, logdataset
    2930
    3031class Format(Enum):
    … …  
    3334    GPX = 3
    3435
    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]+)$")
     36class CsvCallback:
     37    def __init__(self, output_file):
     38        self._output_file = output_file
    3939
    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())
    7742
    7843def convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time):
    79     global regex_nmea_sentence
    80     global regex_nmea_sentence_without_checksum
    81 
    8244    try:
    8345        output_file = open(output_file_path, 'w', 0)
    … …  
    9456    output_file_csv.writeheader()
    9557
    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)
    14061    output_file.close()
    141     input_file.close()
    14262
    14363def convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time):
    144     global regex_nmea_sentence
    145     global regex_nmea_sentence_without_checksum
    146 
    14764    try:
    14865        output_file = open(output_file_path, 'w', 0)
    … …  
    15774        print('ERROR: Could not open the input file: {}'.format(error))
    15875        sys.exit(3)
     76       
     77    log_handler = loghandler.LogHandler()
    15978    current_row = 1
    16079    last_row = None
    16180    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))
    17184            if not errors_ignore:
    17285                input_file.close()
    … …  
    17487                sys.exit(4)
    17588        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)
    18092            output_file.write('{}\r\n'.format(str(output_string)))
     93        current_row += 1
    18194    output_file.close()
    18295    input_file.close()
    … …  
    198111    errors_ignore = False
    199112    strip_time = False
    200        
     113
    201114    formats = []
    202115    for file_format in Format:
Note: See TracChangeset for help on using the changeset viewer.