import pynmea2

"""
Explanation: PyNEMA2
PyNMEA2 is a python library for the NMEA 0183 protocol.
Parsing NMEA messages
End of explanation
"""


msg = pynmea2.parse("$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D", check=True)
msg

"""
Explanation: GGA -  GPS Fix Data
End of explanation
"""


msg.lat, msg.latitude, msg.latitude_minutes, msg.latitude_seconds

msg.lon, msg.longitude, msg.longitude_minutes, msg.longitude_seconds

"""
Explanation: The lat and lon attributes are in DDDMM.SSSSS format while latitude and longitude are their float values.
End of explanation
"""


pynmea2.parse("$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48")

"""
Explanation: VTG - Track Made Good and Ground Speed
End of explanation
"""


pynmea2.parse("$GPZDA,201530.00,04,07,2002,00,00*60")    # Time is in UTC, or GPS time if offset is not yet known

"""
Explanation: ZDA - Time and Date
End of explanation
"""


msg = pynmea2.GGA('GP', 'GGA', ('184353.07', '1929.045', 'S', '02410.506', 'E', '1', '04', '2.6', 
                                '100.00', 'M', '-33.9', 'M', '', '0000'))
msg

str(msg)

"""
Explanation: Generating NMEA messages
End of explanation
"""


def sd_to_dm(latitude, longitude):
    if latitude < 0:
        lat_dir = 'S'
    else:
        lat_dir = 'N'
    lat = ('%010.5f' % (abs(int(latitude)) * 100 + (abs(latitude) % 1.0) * 60)).rstrip('0')
        
    if longitude < 0:
        lon_dir = 'W'
    else:
        lon_dir = 'E'
    lon = ('%011.5f' % (abs(int(longitude)) * 100 + (abs(longitude) % 1.0) * 60)).rstrip('0')
    
    return lat, lat_dir, lon, lon_dir

# 1929.045,S,02410.506,E
sd_to_dm(-19.484083333333334, 24.1751)

"""
Explanation: Notes
* Check talker.py for reference.
* The pynmea2.GGA class and others require lat and lon values in DDDMM.SSSSS format as well as lat_dir and lon_dir. Use the following function to convert floats into that format (the inverse to nmea_utils.py:dm_to_sd()).
End of explanation
"""


