From 73e7d239dc885990be1e8c7d27d239e32bfad45e Mon Sep 17 00:00:00 2001 From: madrigal Date: Wed, 17 Sep 2025 23:16:17 -0400 Subject: [PATCH] Fixed longitude parsing, added fixer functions --- helper_functions.py | 70 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/helper_functions.py b/helper_functions.py index 4228392..d437591 100644 --- a/helper_functions.py +++ b/helper_functions.py @@ -83,9 +83,19 @@ def parse_lat_lon(value: str, direction: str) -> float: """ if not value or not direction: return None - degrees = int(value[:2]) - minutes = float(value[2:]) + + # Latitude has 2° digits, longitude has 3° digits + if direction in ["N", "S"]: + deg_len = 2 + elif direction in ["E", "W"]: + deg_len = 3 + else: + return None # Invalid direction + + degrees = int(value[:deg_len]) + minutes = float(value[deg_len:]) decimal = degrees + minutes / 60 + if direction in ["S", "W"]: decimal = -decimal return decimal @@ -119,3 +129,59 @@ def save_data_to_json(data, filename): json.dump(existing_data, file, indent=4) except Exception as e: print(f"Error saving data to JSON: {e}") + + +def fix_long(bad_long): + """ + Fix longitude values incorrectly parsed from NMEA (leading zero dropped degrees). + + Assumes all values should be around -79.x based on recording location. + Removes the spurious leading digit from minutes. + """ + broken_degrees = 7 + # Recover "minutes" from the broken decimal + extended_minutes = (abs(bad_long) - broken_degrees) * 60 + + # Remove the extra leading digit (e.g. 920 -> 20) + minutes = extended_minutes % 100 + + corrected_degrees = 79 + decimal = corrected_degrees + minutes / 60 + + if bad_long < 0: + decimal = -decimal + + return decimal + + +def add_distance_after(filename: str, base_location: dict): + try: + with open(filename, "r") as file: + data = json.load(file) + except FileNotFoundError: + print('No file by that name') + return None + + distance = 'Unknown' + + for dictionary in data: + if 'latitude' in dictionary: + # dictionary['longitude'] = fix_long(dictionary['longitude']) + distance = calculate_distance(base_location, dictionary) + dictionary["distance"] = distance + elif 'iperf_full' in dictionary: + dictionary['start_distance'] = distance + + # Save updated data back to the file + with open(filename, "w") as file: + json.dump(data, file, indent=4) + + +if __name__ == "__main__": + filename = '/home/madrigal/repos/range-testing/data/boat_relay_sept_17/test_1758123903_copy.json' + base_location = { + 'latitude': 43.656328, + 'longitude': -79.307884, + 'altitude': 80, + } + add_distance_after(filename=filename, base_location=base_location)