| #!/usr/bin/env python3 | |||||
| from argparse import ArgumentParser | |||||
| from time import sleep | |||||
| import pandas as pd | |||||
| import csv | |||||
| from datetime import datetime | |||||
| import math | |||||
| if __name__ == "__main__": | |||||
| parser = ArgumentParser() | |||||
| parser.add_argument("-f", "--file", required=True, help="NMEA file.") | |||||
| args = parser.parse_args() | |||||
| epoch = datetime.utcfromtimestamp(0) | |||||
| outputfile = open(args.file.replace("nmea", "csv"), "w") | |||||
| writer = csv.writer(outputfile, delimiter=",", lineterminator="\n") | |||||
| writer.writerow( | |||||
| ["datetime", "time_epoch", "latitude", "longitude", "speed (knots)", "systime_epoch"] | |||||
| ) | |||||
| csv_string = "" | |||||
| for line in open(args.file, "r").readlines(): | |||||
| if line.startswith("$GPRMC"): | |||||
| row = line.split(",") | |||||
| time = row[1] | |||||
| lat = row[3] | |||||
| lat_direction = row[4] | |||||
| lon = row[5] | |||||
| lon_direction = row[6] | |||||
| speed = row[7] | |||||
| date = row[9] | |||||
| systime_epoch = row[13] if len(row) < 14 else 0 | |||||
| date_and_time = datetime.strptime(date + " " + time, "%d%m%y %H%M%S.%f") | |||||
| time_since_epoch = date_and_time.timestamp() | |||||
| date_and_time = date_and_time.strftime("%y-%m-%d %H:%M:%S.%f") # [:-3] | |||||
| lat = round(math.floor(float(lat) / 100) + (float(lat) % 100) / 60, 6) | |||||
| if lat_direction == "S": | |||||
| lat = lat * -1 | |||||
| lon = round(math.floor(float(lon) / 100) + (float(lon) % 100) / 60, 6) | |||||
| if lon_direction == "W": | |||||
| lon = lon * -1 | |||||
| writer.writerow([date_and_time, time_since_epoch, lat, lon, speed, systime_epoch]) | |||||
| outputfile.close() | |||||
| sleep(1) | |||||
| gps_df = pd.read_csv(args.file.replace("nmea", "csv")) | |||||
| #gps_df["datetime"] = pd.to_datetime( | |||||
| # gps_df["systime_epoch"].apply(lambda x: datetime.fromtimestamp(x)) | |||||
| #) | |||||
| gps_df.to_csv(args.file.replace("nmea", "csv")) |