From d028cfc0bb80c003a6c6de62111b1c93361aeb37 Mon Sep 17 00:00:00 2001 From: Lukas Prause Date: Tue, 31 Jan 2023 16:11:30 +0100 Subject: [PATCH] Renames column date_and_time to datetime. --- format_gps_to_csv.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) mode change 100644 => 100755 format_gps_to_csv.py diff --git a/format_gps_to_csv.py b/format_gps_to_csv.py old mode 100644 new mode 100755 index e69de29..b361c9c --- a/format_gps_to_csv.py +++ b/format_gps_to_csv.py @@ -0,0 +1,57 @@ +#!/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"))