Fixed longitude parsing, added fixer functions

This commit is contained in:
madrigal 2025-09-17 23:16:17 -04:00
parent 31520e6176
commit 73e7d239dc

View File

@ -83,9 +83,19 @@ def parse_lat_lon(value: str, direction: str) -> float:
""" """
if not value or not direction: if not value or not direction:
return None 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 decimal = degrees + minutes / 60
if direction in ["S", "W"]: if direction in ["S", "W"]:
decimal = -decimal decimal = -decimal
return decimal return decimal
@ -119,3 +129,59 @@ def save_data_to_json(data, filename):
json.dump(existing_data, file, indent=4) json.dump(existing_data, file, indent=4)
except Exception as e: except Exception as e:
print(f"Error saving data to JSON: {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)