Skip to content

Commit

Permalink
Merge pull request #37 from GlobalFishingWatch/update-tagblock
Browse files Browse the repository at this point in the history
Add update_tagblock()
  • Loading branch information
pwoods25443 authored Jan 12, 2023
2 parents 3252442 + 4b006a0 commit 0bd3fd5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ais_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tools for managing AIS messages
"""

__version__ = 'v0.1.5.dev1'
__version__ = 'v0.1.5.dev2'
__author__ = 'Paul Woods'
__email__ = '[email protected]'
__source__ = 'https://github.com/GlobalFishingWatch/ais-tools'
Expand Down
8 changes: 8 additions & 0 deletions ais_tools/tagblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,11 @@ def decode_tagblock(tagblock_str, validate_checksum=False):
raise DecodeError('Unable to decode tagblock string')

return fields


def update_tagblock(nmea, **kwargs):
tagblock_str, nmea = split_tagblock(nmea)
tagblock = decode_tagblock(tagblock_str)
tagblock.update(kwargs)
tagblock_str = encode_tagblock(**tagblock)
return join_tagblock(tagblock_str, nmea)
13 changes: 13 additions & 0 deletions tests/test_tagblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ def test_decode_tagblock_invalid_checksum(tagblock_str):
def test_decode_tagblock_invalid(tagblock_str):
with pytest.raises(DecodeError, match='Unable to decode tagblock string'):
tagblock.decode_tagblock(tagblock_str, validate_checksum=False)


@pytest.mark.parametrize("tagblock_str,new_fields,expected", [
('!AIVDM', {'q':123}, '\\q:123*7B\\!AIVDM'),
('\\!AIVDM', {'q':123}, '\\q:123*7B\\!AIVDM'),
('\\s:00*00\\!AIVDM', {'tagblock_station':99}, '\\s:99*49\\!AIVDM'),
('\\s:00*00\\!AIVDM\\s:00*00\\!AIVDM', {'tagblock_station':99}, '\\s:99*49\\!AIVDM\\s:00*00\\!AIVDM'),
('\\c:123456789*68\\!AIVDM', {}, '\\c:123456789*68\\!AIVDM'),
('\\c:123456789*68\\!AIVDM', {'tagblock_station':99}, '\\c:123456789,s:99*0D\\!AIVDM'),
('\\c:123456789*68\\!AIVDM', {'q':123}, '\\c:123456789,q:123*3F\\!AIVDM'),
])
def test_update_tagblock(tagblock_str, new_fields, expected):
assert expected == tagblock.update_tagblock(tagblock_str, **new_fields)

0 comments on commit 0bd3fd5

Please sign in to comment.