SECOORA / GUTILS

🌊 🚤 Python utilities for reading, merging, and post processing Teledyne Webb Slocum Glider data
MIT License
7 stars 11 forks source link

get_decimal_degrees() has errors near equator #3

Closed kwilcox closed 7 years ago

kwilcox commented 7 years ago

From Kerfoot:

I found a bug in the GUTILS get_decimal_degrees() when trying to convert small (near the equator) GPS positions from NMEA coordinates to decimal degrees:

https://github.com/SECOORA/GUTILS/blob/master/gutils/gbdr/methods.py#L281

I rewrote the function to do a straight mathematical conversion instead of converting to a string, parsing, etc. Here's the code:

def get_decimal_degrees(lat_lon):
     """Converts glider gps coordinate ddmm.mmm to decimal degrees dd.ddd

     Arguments:
     lat_lon - A floating point latitude or longitude in the format ddmm.mmm
         where dd's are degrees and mm.mmm is decimal minutes.

     Returns decimal degrees float
     """
     # Absolute value of the coordinate
     try:
         pos_lat_lon = abs(lat_lon)
     except (TypeError, ValueError) as e:
         return

     # Calculate NMEA degrees as an integer
     nmea_degrees = int(pos_lat_lon/100)*100

     # Subtract the NMEA degrees from the absolute value of lat_lon and divide by 60
     # to get the minutes in decimal format
     gps_decimal_minutes = (pos_lat_lon - nmea_degrees)/60.0

     # Divide NMEA degrees by 100 and add the decimal minutes
     decimal_degrees = (nmea_degrees/100) + gps_decimal_minutes

     if lat_lon < 0:
         return -decimal_degrees

     return decimal_degrees
kwilcox commented 7 years ago

@kerfoot This function is going to occasionally return floating point rounding errors:

>>> get_decimal_degrees(10601.6986)
106.02830999999999

Is there a sensible precision we should lock this to? I can't find the right documentation that states what precision NMEA reports at. 5 decimal places is going to be inches... can we round to that?

kerfoot commented 7 years ago

I set the precision attribute to 5 decimal places.