1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
"""A file-like interface to NMEA parsing.
>>> import ais
>>> import json
>>> with ais.open('test/data/typeexamples.nmea') as src:
... msg = next(src)
... print(json.dumps(next(msg), indent=4, sort_keys=True))
{
"decoded": {
"day": 0,
"fix_type": 1,
"hour": 24,
"id": 4,
"md5": "7ecb187e7edc1789de436b0c2ccf2963",
"minute": 60,
"mmsi": 3669713,
"month": 0,
"position_accuracy": 0,
"raim": false,
"repeat_indicator": 0,
"second": 60,
"slot_number": 2105,
"slot_timeout": 2,
"spare": 0,
"sync_state": 1,
"transmission_ctl": 0,
"x": 181.0,
"y": 91.0,
"year": 0
},
"line_nums": [
1
],
"line_type": "USCG",
"lines": [
"!AIVDM,1,1,,A,403Ovl@000Htt<tSF0l4Q@100`Pq,0*28,d-109,S2105,t050056.00,T56.13718694,r003669946,1325394060,1325394001"
],
"matches": [
{
"body": "403Ovl@000Htt<tSF0l4Q@100`Pq",
"chan": "A",
"checksum": "28",
"counter": null,
"fill_bits": 0,
"hour": 5,
"minute": 0,
"payload": "!AIVDM,1,1,,A,403Ovl@000Htt<tSF0l4Q@100`Pq,0*28",
"receiver_time": 50056.0,
"rssi": null,
"second": 56.0,
"sen_num": 1,
"sen_tot": 1,
"seq_id": null,
"signal_strength": -109,
"slot": 2105,
"station": "r003669946",
"station_type": "r",
"talker": "AI",
"time": 1325394060,
"time_of_arrival": 56.13718694,
"uscg_metadata": ",d-109,S2105,t050056.00,T56.13718694,r003669946,1325394060",
"vdm": "!AIVDM,1,1,,A,403Ovl@000Htt<tSF0l4Q@100`Pq,0*28",
"vdm_type": "VDM"
}
]
}
"""
import codecs
import sys
import six
import ais.nmea_queue
def open(name, mode='r', **kwargs):
"""Open a file containing NMEA and instantiate an instance of `NmeaFile()`.
Lke Python's `open()`, set the `mode` parameter to 'r' for normal reading or
or 'rU' for opening the file in universal newline mode.
Args:
name: A file path, file-like object, or '-' for stdin.
mode: I/O mode for opening the input file. r, rU, or U
Raises:
TypeError: Invalid object for name parameter.
ValueError: Invalid value for mode parameter.
Returns:
An instance of NmeaFile that is ready for reading.
"""
io_modes = ('r', 'rU', 'U')
if mode not in io_modes:
raise ValueError("Mode '{m}' is unsupported. Must be one of: {ms}".format(
m=mode, ms=', '.join(io_modes)))
if name == '-':
fobj = sys.stdin
elif isinstance(name, six.string_types):
fobj = codecs.open(name, **kwargs)
elif hasattr(name, 'close') and \
(hasattr(name, 'next') or hasattr(name, '__next__')):
fobj = name
else:
raise TypeError("'name' must be a file path, file-like object, "
"or '-' for stdin.")
return NmeaFile(fobj)
class NmeaFile(object):
"""Provides a file-like object interface to the `ais.nmea_queue` module."""
def __init__(self, fobj):
"""Construct a parsing stream.
Args:
fobj: File-like object.
"""
self._fobj = fobj
self._queue = ais.nmea_queue.NmeaQueue()
@property
def closed(self):
"""Is the file-like object from which we are reading open for reading?"""
return self._fobj.closed
@property
def name(self):
"""Name of the file-like object from which we are reading."""
return self._fobj.name
def close(self):
"""Close the file-like object from which we are reading and dump whats left
in the queue."""
return self._fobj.close()
def __iter__(self):
return self
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# Destroy the queue, which could be a large in-memory object.
self._queue = None
return self.close()
def __next__(self):
"""Return the next decoded AIS message."""
msg = None
while not msg:
self._queue.put(next(self._fobj))
msg = self._queue.GetOrNone()
return msg
next = __next__
|