-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from GlobalFishingWatch/type-9
add support for type 9
- Loading branch information
Showing
8 changed files
with
159 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from ais_tools.transcode import NmeaBits | ||
from ais_tools.transcode import NmeaStruct as Struct | ||
from ais_tools.transcode import UintField as Uint | ||
from ais_tools.transcode import Uint10Field as Uint10 | ||
from ais_tools.transcode import LatLonField as LatLon | ||
from ais_tools.transcode import BoolField as Bool | ||
from ais_tools.ais_commstate import ais_commstate_decode | ||
from ais_tools.ais_commstate import ais_commstate_encode | ||
from ais_tools.ais_commstate import ais_commstate_CS | ||
|
||
|
||
def ais9_decode(body, pad): | ||
bits = NmeaBits.from_nmea(body, pad) | ||
message = bits.unpack(ais9_fields) | ||
|
||
ais_commstate_decode(bits, message) | ||
|
||
return message | ||
|
||
|
||
def ais9_encode(message): | ||
bits = NmeaBits(ais9_fields.nbits + ais_commstate_CS.nbits) | ||
bits.pack(ais9_fields, message) | ||
|
||
ais_commstate_encode(bits, message) | ||
|
||
return bits.to_nmea() | ||
|
||
|
||
ais9_fields = Struct( | ||
Uint(name='id', nbits=6, default=18), | ||
Uint(name='repeat_indicator', nbits=2, default=0), | ||
Uint(name='mmsi', nbits=30), | ||
Uint(name='alt', nbits=12, default=4095), | ||
Uint(name='sog', nbits=10, default=1023), | ||
Uint(name='position_accuracy', nbits=1, default=0), | ||
LatLon(name='x', nbits=28, default=181), | ||
LatLon(name='y', nbits=27, default=91), | ||
Uint10(name='cog', nbits=12, default=360), | ||
Uint(name='timestamp', nbits=6, default=60), | ||
Uint(name='alt_sensor', nbits=1, default=0), | ||
Uint(name='spare', nbits=7, default=0), | ||
Uint(name='dte', nbits=1, default=0), | ||
Uint(name='spare2', nbits=3, default=0), | ||
Bool(name='assigned_mode', nbits=1, default=0), | ||
Bool(name='raim', nbits=1, default=0), | ||
Uint(name='commstate_flag', nbits=1, default=0) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from ais_tools.transcode import DecodeError | ||
from ais_tools.transcode import UintField as Uint | ||
from ais_tools.transcode import BitField as Bits | ||
from ais_tools.transcode import NmeaStruct as Struct | ||
from ais_tools.transcode import BoolField as Bool | ||
|
||
|
||
def ais_commstate_decode(bits, message): | ||
cs, fields = ais_commstate_fields(message) | ||
message.update(bits.unpack(fields)) | ||
|
||
if cs == 'SOTDMA': | ||
fields = sotdma_timeout_fields(message) | ||
message.update(bits.unpack(fields)) | ||
|
||
|
||
def ais_commstate_encode(bits, message): | ||
cs, commstate_fields = ais_commstate_fields(message) | ||
timeout_fields = sotdma_timeout_fields(message) | ||
|
||
bits.pack(commstate_fields, message) | ||
if cs == 'SOTDMA': | ||
bits.pack(timeout_fields, message) | ||
|
||
|
||
def ais_commstate_fields(message): | ||
if message.get('unit_flag', 0): | ||
return 'CS', ais_commstate_CS | ||
elif message.get('commstate_flag', 0): | ||
return 'ITDMA', ais_commstate_ITDMA | ||
else: | ||
return 'SOTDMA', ais_commstate_SOTDMA | ||
|
||
|
||
def sotdma_timeout_fields(message): | ||
slot_timeout = message.get('slot_timeout', 0) | ||
if slot_timeout == 0: | ||
return ais_commstate_SOTDMA_timeout_0 | ||
elif slot_timeout == 1: | ||
return ais_commstate_SOTDMA_timeout_1 | ||
elif slot_timeout in (2, 4, 6): | ||
return ais_commstate_SOTDMA_timeout_2_4_6 | ||
elif slot_timeout in (3, 5, 7): | ||
return ais_commstate_SOTDMA_timeout_3_5_7 | ||
else: | ||
raise DecodeError(f'AIS18: unknown slot_timeout value {slot_timeout}') | ||
|
||
|
||
ais_commstate_CS = Struct( | ||
Bits(name='commstate', nbits=19, default='1100000000000000110') | ||
) | ||
|
||
ais_commstate_ITDMA = Struct( | ||
Uint(name='sync_state', nbits=2, default=0), | ||
Uint(name='slot_increment', nbits=13, default=0), | ||
Uint(name='slots_to_allocate', nbits=3, default=0), | ||
Bool(name='keep_flag', nbits=1, default=0) | ||
) | ||
|
||
ais_commstate_SOTDMA = Struct( | ||
Uint(name='sync_state', nbits=2, default=0), | ||
Uint(name='slot_timeout', nbits=3, default=0), | ||
) | ||
|
||
ais_commstate_SOTDMA_timeout_0 = Struct( | ||
Uint(name='slot_offset', nbits=14, default=0), | ||
) | ||
|
||
ais_commstate_SOTDMA_timeout_1 = Struct( | ||
Uint(name='utc_hour', nbits=5, default=0), | ||
Uint(name='utc_min', nbits=7, default=0), | ||
Uint(name='utc_spare', nbits=2, default=0), | ||
) | ||
|
||
ais_commstate_SOTDMA_timeout_2_4_6 = Struct( | ||
Uint(name='slot_number', nbits=14, default=0), | ||
) | ||
|
||
ais_commstate_SOTDMA_timeout_3_5_7 = Struct( | ||
Uint(name='received_stations', nbits=14, default=0), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
!AIVDM,1,1,,B,8Nj<9D0000ttt0<D04@<tt<8`H0H@@l44L`<40<@tT`<4T0`=h0,2*47 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters