Skip to content

Commit 6893387

Browse files
committed
Add type 52 updates and placeholders for types 53 and 54
1 parent 9bee1a7 commit 6893387

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

microSWIFTtelemetry/sbd/definitions.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,43 @@
77
"get_variable_definitions",
88
]
99

10+
import struct
1011
from typing import List, Tuple
1112

1213

13-
def get_sensor_type_definition(sensor_type: int) -> str:
14+
def get_sensor_type_definition(sensor_type: str, file_size: int) -> str:
1415
"""
1516
Dictionary of microSWIFT sensor type definitions;
1617
see https://github.com/alexdeklerk/microSWIFT.
1718
1819
Arguments:
19-
- sensor_type (int), sensor type defintion to return
20+
- sensor_type (str), sensor type definition to return
21+
- file_size (int), size of the SBD file in bytes
2022
2123
Raises:
2224
- ValueError, raise error if the sensor type is not one of the
2325
types defined in microSWIFT.py and configured to be
2426
parsed on the sever.
2527
2628
Returns:
27-
- (str), sensor type defintion in Python's struct module format
29+
- (str), sensor type definition in Python's struct module format
2830
* See: https://docs.python.org/3/library/struct.html
2931
"""
3032

3133
# Define the sensor type using Python's struct module format
3234
PAYLOAD_DEFINITIONS = {
33-
50: '<sbbhfff42f42f42f42f42f42f42ffffffffiiiiii',
34-
51: '<sbbhfff42fffffffffffiiiiii',
35-
52: '<sbBheee42eee42b42b42b42b42Bffeeef', # original v1 has `b` in third pos
35+
'50': '<sbbhfff42f42f42f42f42f42f42ffffffffiiiiii',
36+
'51': '<sbbhfff42fffffffffffiiiiii',
37+
'52': '<sbBheee42eee42b42b42b42b42Bffeeef', # original v1 has `b` in third pos
38+
'52-2': '<sbbheee42eee42b42b42b42b42BIIeeeII', # Phil Mar 2025 edits
39+
'53': '<sbbH' + 3 * 'iiiiIIHH',
40+
'54': '<sbbH' + 6 * 'iiiiII13H',
3641
}
3742

43+
# Accommodate modified sensor type 52 definition past Nov 2024
44+
if sensor_type == '52' and file_size == struct.calcsize(PAYLOAD_DEFINITIONS['52-2']):
45+
sensor_type = '52-2'
46+
3847
if sensor_type not in PAYLOAD_DEFINITIONS.keys():
3948
raise ValueError((f'sensor_type not defined - can only be value in:'
4049
f'{list(PAYLOAD_DEFINITIONS.keys())}'))

microSWIFTtelemetry/sbd/read_sbd.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import struct
99
import warnings
1010
from datetime import datetime, timezone
11-
from typing import Tuple
11+
from typing import Tuple, Union
1212

1313
import numpy as np
1414

@@ -52,8 +52,8 @@ def unpack_sbd(file_name: str, file_content: bytes) -> Tuple:
5252
sensor_type = get_sensor_type(file_content)
5353

5454
if sensor_type:
55-
payload_struct = get_sensor_type_definition(sensor_type)
5655
file_size = len(file_content)
56+
payload_struct = get_sensor_type_definition(sensor_type, file_size)
5757
expected_file_size = struct.calcsize(payload_struct)
5858
if file_size == expected_file_size:
5959
data = struct.unpack(payload_struct, file_content)
@@ -70,10 +70,14 @@ def unpack_sbd(file_name: str, file_content: bytes) -> Tuple:
7070
error_message['error'] = _rstrip_null(file_content) # decode('ascii')
7171

7272
if data:
73-
if sensor_type == 51:
73+
if sensor_type == '51':
7474
swift = unpack_sensor_type_51(data)
75-
elif sensor_type == 52:
75+
elif sensor_type == '52':
7676
swift = unpack_sensor_type_52(data)
77+
elif sensor_type == '53':
78+
swift = {} # TODO: ignore for now, but implement later
79+
elif sensor_type == '54':
80+
swift = {} # TODO: ignore for now, but implement later
7781
else:
7882
raise NotImplementedError(f'The specified sensor type '
7983
f'({sensor_type}) is not supported.')
@@ -83,7 +87,7 @@ def unpack_sbd(file_name: str, file_content: bytes) -> Tuple:
8387
return swift, error_message
8488

8589

86-
def get_sensor_type(file_content: bytes) -> int:
90+
def get_sensor_type(file_content: bytes) -> Union[str, None]:
8791
"""
8892
Determine sensor type from an SBD message.
8993
@@ -96,13 +100,13 @@ def get_sensor_type(file_content: bytes) -> int:
96100
file_content (bytes): binary SBD message
97101
98102
Returns:
99-
(int): int corresponding to sensor type
103+
(str): str corresponding to sensor type
100104
"""
101105
payload_type = \
102106
file_content[PAYLOAD_START:PAYLOAD_START+1].decode(errors='replace')
103107

104108
if payload_type == PAYLOAD_TYPE:
105-
sensor_type = ord(file_content[PAYLOAD_START+1:PAYLOAD_START+2])
109+
sensor_type = str(ord(file_content[PAYLOAD_START+1:PAYLOAD_START+2]))
106110
else:
107111
sensor_type = None
108112

microSWIFTtelemetry/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Format expected by setup.py and doc/source/conf.py: string of form "X.Y.Z"
55
_version_major = 0
66
_version_minor = 3
7-
_version_micro = 4 # use '' for first of series, number for 1 and above
7+
_version_micro = 5 # use '' for first of series, number for 1 and above
88
# _version_extra = 'dev'
99
_version_extra = '' # TODO: Uncomment this for full releases
1010

0 commit comments

Comments
 (0)