libais icon indicating copy to clipboard operation
libais copied to clipboard

ais.decode() doesn't take a raw ais NMEA sentence but it's unclear how to break up

Open caileighf opened this issue 5 years ago • 1 comments

Hi there -- this could my fault, but I'm not totally clear on how to consistently break apart the raw AIS NMEA sentence for the ais.decode method.

When I do this: decoded = ais.decode('!AIVDM,1,1,,A,18UG;P0012G?Uq4EdHa=c;7@051@,0*53',0) I get this error: DecodeError: ais.decode: unknown message - !

But when I remove everything except the body and add the fillbits: decoded = ais.decode('18UG;P0012G?Uq4EdHa=c;7@051@',0) I get this {'id': 1, 'repeat_indicator': 0, 'mmsi': 576048000, 'nav_status': 0, 'rot_over_range': False, 'rot': 0.0, 'sog': 6.599999904632568, 'position_accuracy': 0, 'x': -122.42298333333333, 'y': 37.912166666666664, 'cog': 350.0, 'true_heading': 355, 'timestamp': 40, 'special_manoeuvre': 0, 'spare': 0, 'raim': False, 'sync_state': 0, 'slot_timeout': 1, 'utc_hour': 8, 'utc_min': 20, 'utc_spare': 0}

I am hoping there is a way to pass the AIS NMEA sentence to ais.decode since it seems like there is already logic to get the fill bit and body from ais.stream.decode.

If someone could point me to the source code for ais.decode that would be much appreciated! I did search for it and I wasn't able to find it (I haven't written python bindings for C++ so this could be more obvious to others). Thanks, Caileigh

caileighf avatar Jul 31 '20 23:07 caileighf

I looked through the test module and stole some code that broke up the raw messages. Here's what I came up with:

def decode_ais(raw_nmea):
    if not isinstance(raw_nmea, list):
        raw_nmea = [raw_nmea]
    body = ''.join([line.split(',')[5] for line in raw_nmea])
    pad = int(raw_nmea[-1].split('*')[0][-1])
    msg = ais.decode(body, pad)
    return(msg)

It works for the cases I've tried so far but, I would prefer not to use this code for production if there is a native alternative. Thanks again

caileighf avatar Jul 31 '20 23:07 caileighf