Index: src/python/bin/gnuboat-log-converter
===================================================================
--- src/python/bin/gnuboat-log-converter	(revision 26f77a6c5b46b763029dc12b0161763060095ffe)
+++ src/python/bin/gnuboat-log-converter	(revision ea6eba191d891b6871947995e25086af8cb69118)
@@ -24,7 +24,8 @@
 from enum import Enum
 import csv
-import re
 import tempfile
 import subprocess
+
+from gnuboat.datahandler import loghandler, logdataset
 
 class Format(Enum):
@@ -33,51 +34,12 @@
     GPX = 3
 
-regex_nmea_sentence = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+\*[\da-zA-Z]+)$")
-regex_nmea_sentence_without_checksum = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+)\*[\da-zA-Z]+$")
-regex_nmea_position_latitude = re.compile(r"^([0-9]{2})([0-9]{2}\.[0-9]+)$")
-regex_nmea_position_longitude = re.compile(r"^([0-9]{3})([0-9]{2}\.[0-9]+)$")
+class CsvCallback:
+    def __init__(self, output_file):
+        self._output_file = output_file
 
-def toFloat(string, current_row):
-    try:
-        return float(string)
-    except ValueError as error:
-        try:
-           return int(string)
-        except ValueError as error:
-           print('INFO: Failed to convert value from line {} into numeric: {}\n      Message: {}'.format(current_row, string, error))
-           return 0
-
-def nmeaLatitudeToDecimalDegrees(nmea_value, current_row):
-    global regex_nmea_position_latitude
-
-    if toFloat(nmea_value, current_row) == 0:
-        return 0
-
-    matches = regex_nmea_position_latitude.match(nmea_value)
-    if matches is None:
-        print('INFO: Failed to convert latitude position value from line {} into decimal degrees: {}'.format(current_row, nmea_value))
-        return
-    decimalDegrees = toFloat(matches.group(1), current_row)
-    decimalMinutes = toFloat(matches.group(2), current_row)
-    return decimalDegrees + (decimalMinutes / 60.0)
-
-def nmeaLongitudeToDecimalDegrees(nmea_value, current_row):
-    global regex_nmea_position_longitude
-
-    if toFloat(nmea_value, current_row) == 0:
-        return 0
-
-    matches = regex_nmea_position_longitude.match(nmea_value)
-    if matches is None:
-        print('INFO: Failed to convert longitude position value from line {} into decimal degrees: {}'.format(current_row, nmea_value))
-        return
-    decimalDegrees = toFloat(matches.group(1), current_row)
-    decimalMinutes = toFloat(matches.group(2), current_row)
-    return decimalDegrees + (decimalMinutes / 60.0)
+    def callback(self, dataset):
+        self._output_file.writerow(dataset.to_dict())
 
 def convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time):
-    global regex_nmea_sentence
-    global regex_nmea_sentence_without_checksum
-
     try:
         output_file = open(output_file_path, 'w', 0)
@@ -94,55 +56,10 @@
     output_file_csv.writeheader()
 
-    # reading input file
-    try:
-        input_file = open(input_file_path, 'r')
-    except IOError as error:
-        print('ERROR: Could not open the input file: {}'.format(error))
-        sys.exit(3)
-    current_row = 1
-    last_row = None
-    for line in input_file:
-        line = line.strip()
-        if checksums_fix:
-            matches = regex_nmea_sentence_without_checksum.match(line)
-            pynmea_parse_check = False
-        else:
-            matches = regex_nmea_sentence.match(line)
-            pynmea_parse_check = True
-        if matches is None:
-            print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row))
-            if not errors_ignore:
-                input_file.close()
-                output_file.close()
-                sys.exit(4)
-        else:
-            nmea_sentence = pynmea2.parse(matches.group(2), check=pynmea_parse_check)
-            if nmea_sentence.sentence_type == 'RMC':
-                if last_row is not None:
-                    output_file_csv.writerow(last_row)
-                last_row = {'datetime': '{} {}'.format(nmea_sentence.datestamp, nmea_sentence.timestamp),
-                            'gps_status': nmea_sentence.status,
-                            'latitude': nmeaLatitudeToDecimalDegrees(nmea_sentence.lat, current_row),
-                            'latitude_direction': nmea_sentence.lat_dir,
-                            'longitude': nmeaLongitudeToDecimalDegrees(nmea_sentence.lon, current_row),
-                            'longitude_direction': nmea_sentence.lon_dir,
-                            'speed_over_ground': toFloat(nmea_sentence.spd_over_grnd, current_row),
-                            'course_over_ground': toFloat(nmea_sentence.true_course, current_row),
-                            'water_depth': None,
-                            'water_temperature': None}
-            elif nmea_sentence.sentence_type == 'DPT' and last_row is not None:
-                last_row['water_depth'] = toFloat(nmea_sentence.depth, current_row)
-            elif nmea_sentence.sentence_type == 'MTW' and last_row is not None:
-                last_row['water_temperature'] = toFloat(nmea_sentence.temperature, current_row)
-            current_row += 1
-    if last_row is not None:
-        output_file_csv.writerow(last_row)
+    csv_callback = CsvCallback(output_file_csv)
+    log_handler = loghandler.LogHandler()
+    log_handler.gnuboat_read(input_file_path, csv_callback.callback, checksums_fix, errors_ignore)
     output_file.close()
-    input_file.close()
 
 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time):
-    global regex_nmea_sentence
-    global regex_nmea_sentence_without_checksum
-
     try:
         output_file = open(output_file_path, 'w', 0)
@@ -157,16 +74,12 @@
         print('ERROR: Could not open the input file: {}'.format(error))
         sys.exit(3)
+        
+    log_handler = loghandler.LogHandler()
     current_row = 1
     last_row = None
     for line in input_file:
-        line = line.strip()
-        if checksums_fix:
-            matches = regex_nmea_sentence_without_checksum.match(line)
-            pynmea_parse_check = False
-        else:
-            matches = regex_nmea_sentence.match(line)
-            pynmea_parse_check = True
-        if matches is None:
-            print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row))
+        nmea_sentence = log_handler.parse_nmea_sentence(line, checksums_fix)
+        if nmea_sentence is None:
+            print('INFO: Failed to parse nmea sentence ({}) at line {}'.format(line.strip(), current_row))
             if not errors_ignore:
                 input_file.close()
@@ -174,9 +87,9 @@
                 sys.exit(4)
         else:
-            nmea_sentence = pynmea2.parse(matches.group(2), check=pynmea_parse_check)
-            output_string = str(nmea_sentence)
-            if not strip_time and matches.group(1):
-                output_string = '{}{}'.format(matches.group(1), output_string)
+            output_string = str(nmea_sentence['nmea_sentence'])
+            if not strip_time and nmea_sentence['log_timestamp']:
+                output_string = '{}{}'.format(nmea_sentence['log_timestamp'], output_string)
             output_file.write('{}\r\n'.format(str(output_string)))
+        current_row += 1
     output_file.close()
     input_file.close()
@@ -198,5 +111,5 @@
     errors_ignore = False
     strip_time = False
-	
+
     formats = []
     for file_format in Format:
