import re
import datetime


def parse(string):
    if '\r\n' not in string:
        raise ValueError('\\r\\n not found')
    else:
        string = string.replace('\r\n', '')

    type_list = string.split('#')[1:]
    if type_list[0] == 'SD':
        print('Type = ' + type_list[0])
        parse_sd_package(type_list)
    elif type_list[0] == 'M':
        print('Type = ' + type_list[0])
        parse_m_package(type_list)
    else:
        print('Incorrect package type ' + type_list[0])


def parse_sd_package(init_list):

    data = init_list[1].split(';')

    try:
        date = data[0]
        datetime.datetime.strptime(date, '%d%m%Y')  # check date format
        print('Date = ' + date)
    except ValueError:
        raise ValueError('Wrong date format')

    try:
        time = data[1]
        datetime.datetime.strptime(time, '%H%M%S')  # check time format
        print('Time = ' + time)
    except ValueError:
        raise ValueError('Wrong time format')

    try:
        lat1 = float(data[2])

        lat2 = data[3]
        if lat2 == 'N':
            lat = lat1
        elif lat2 == 'S':
            lat = -lat1
        print('Lat = ' + str(lat))
    except ValueError:
        raise ValueError('Incorrect latitude value')

    try:
        lon1 = float(data[4])

        lon2 = data[5]
        if lon2 == 'E':
            lon = lon1
        elif lon2 == 'W':
            lon = -lon1
        print('Lon = ' + str(lon))
    except ValueError:
        raise ValueError('Incorrect longitude value')

    try:
        speed = int(data[6])
        print('Speed = ' + str(speed))
    except ValueError:
        raise ValueError('Incorrect speed value')

    try:
        course = int(data[7])
        print('Course = ' + str(course))
    except ValueError:
        raise ValueError('Incorrect course value')

    try:
        height = int(data[8])
        print('Height = ' + str(height))
    except ValueError:
        raise ValueError('Incorrect height value')

    try:
        sats = int(data[9])
        print('Sats = ' + str(sats))
    except ValueError:
        raise ValueError('Incorrect sats value')


def parse_m_package(init_list):

    message = init_list[1]
    print('Message = ' + message)


parse('#SD#04012011;130515;5544.6025;N;03739.6834;W;35;215;110;7\r\n')
print('*********')
parse('#M#груз доставлен\r\n')
