-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
348 lines (315 loc) · 10.3 KB
/
constants.py
File metadata and controls
348 lines (315 loc) · 10.3 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from typing import Optional, Union, List, Dict, Any
# -- Constants --
"""EFIX Types List (in order of EFIX type number)"""
"""EFIX = Electronic Fixing Device, this describes which GNSS system the device uses"""
EFIX_TYPES: Dict[int,str] = {
0: "Undefined / Unknown",
1: "GPS",
2: "GLONASS",
3: "Combined GPS/GLONASS",
4: "Loran-C",
5: "Chayka",
6: "Integrated Navigation System",
7: "Surveyed",
8: "Galileo",
**{i: "Unknown" for i in range(9, 15)},
15: "Internal GNSS"
}
NAVAID_TYPES: Dict[int,str] = {
0: "Undefined / Unknown",
1: "Reference point",
2: "RACON (Radar marking navigation hazard)",
3: "Fixed structure off shore",
4: "Spare",
5: "Light without sectors",
6: "Light with sectors",
7: "Leading Light Front",
8: "Leading Light Rear",
9: "Beacon, Cardinal N",
10: "Beacon, Cardinal E",
11: "Beacon, Cardinal S",
12: "Beacon, Cardinal W",
13: "Beacon, Port hand",
14: "Beacon, Starboard hand",
15: "Beacon, Preferred Channel port hand",
16: "Beacon, Preferred Channel starboard hand",
17: "Beacon, Isolated danger",
18: "Beacon, Safe water",
19: "Beacon, Special mark",
20: "Cardinal Mark N",
21: "Cardinal Mark E",
22: "Cardinal Mark S",
23: "Cardinal Mark W",
24: "Port hand Mark",
25: "Starboard hand Mark",
26: "Preferred Channel Port hand",
27: "Preferred Channel Starboard hand",
28: "Isolated danger",
29: "Safe Water",
30: "Special Mark",
31: "Light Vessel / LANBY / Rigs"
}
"""Message Types List (in order of message type number)"""
MESSAGE_TYPES: List[str] = [
"Position Report Class A",
"Position Report Class A (Assigned schedule)",
"Position Report Class A (Response to interrogation)",
"Base Station Report",
"Static and Voyage Related Data",
"Binary Addressed Message",
"Binary Acknowledge",
"Binary Broadcast Message",
"Standard SAR Aircraft Position Report",
"UTC and Date Inquiry",
"UTC and Date Response",
"Addressed Safety Related Message",
"Safety Related Acknowledge",
"Safety Related Broadcast Message",
"Interrogation",
"Assignment Mode Command",
"DGNSS Binary Broadcast Message",
"Standard Class B CS Position Report",
"Extended Class B CS Position Report",
"Data Link Management",
"Aid-to-Navigation Report",
"Channel Management",
"Group Assignment Command",
"Static Data Report",
"Single Slot Binary Message",
"Multiple Slot Binary Message",
"Long Range AIS Broadcast Message"
]
NAVIGATION_STATUS: List[str] = [
"Under way (Power)",
"At anchor",
"Not under command",
"Restricted maneuverability",
"Draft Constrained",
"Moored",
"Aground",
"Engaged in fishing",
"Under way (Sailing)",
"---", # Reserved for future use
"---", # Reserved for future use
"Towing Astern", # Regional use
"Towing Alongside", # Regional use
"---", # Reserved for future use
"AIS-SART is active",
"Undefined (default)"
]
SHIP_TYPE: Dict[int,str] = {
0: "Not available (default)",
**{i: "Reserved for future use" for i in range(1, 20)},
20: "Wing in ground (WIG)",
21: "Wing in ground (WIG), Hazardous category A",
22: "Wing in ground (WIG), Hazardous category B",
23: "Wing in ground (WIG), Hazardous category C",
24: "Wing in ground (WIG), Hazardous category D",
**{i: "Wing in ground (WIG), Reserved for future use" for i in range(25, 30)},
30: "Fishing",
31: "Towing",
32: "Towing: length exceeds 200m or breadth exceeds 25m",
33: "Dredging or underwater ops",
34: "Diving ops",
35: "Military ops",
36: "Sailing",
37: "Pleasure Craft",
38: "Reserved",
39: "Reserved",
40: "High speed craft (HSC)",
41: "High speed craft (HSC), Hazardous category A",
42: "High speed craft (HSC), Hazardous category B",
43: "High speed craft (HSC), Hazardous category C",
44: "High speed craft (HSC), Hazardous category D",
**{i: "High speed craft (HSC), Reserved for future use" for i in range(45, 49)},
49: "High speed craft (HSC)",
50: "Pilot Vessel",
51: "Search and Rescue vessel",
52: "Tug",
53: "Port Tender",
54: "Anti-pollution equipment",
55: "Law Enforcement",
56: "Spare - Local Vessel",
57: "Spare - Local Vessel",
58: "Medical Transport",
59: "Noncombatant ship according to RR Resolution No. 18",
60: "Passenger",
61: "Passenger, Hazardous category A",
62: "Passenger, Hazardous category B",
63: "Passenger, Hazardous category C",
64: "Passenger, Hazardous category D",
**{i: "Passenger, Reserved for future use" for i in range(65, 69)},
69: "Passenger",
70: "Cargo",
71: "Cargo, Hazardous category A",
72: "Cargo, Hazardous category B",
73: "Cargo, Hazardous category C",
74: "Cargo, Hazardous category D",
**{i: "Cargo, Reserved for future use" for i in range(75, 79)},
79: "Cargo",
80: "Tanker",
81: "Tanker, Hazardous category A",
82: "Tanker, Hazardous category B",
83: "Tanker, Hazardous category C",
84: "Tanker, Hazardous category D",
**{i: "Tanker, Reserved for future use" for i in range(85, 89)},
89: "Tanker",
90: "Other Type",
91: "Other Type, Hazardous category A",
92: "Other Type, Hazardous category B",
93: "Other Type, Hazardous category C",
94: "Other Type, Hazardous category D",
**{i: "Other Type, Reserved for future use" for i in range(95, 99)},
99: "Other Type, no additional information"
}
AIS_TYPES: Dict[int,str] = {
0: "ITU 1371",
1: "Future - 1",
2: "Future - 2",
3: "Future - 3",
}
MONTHS: Dict[int,str] = {
0: "N/A",
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December",
}
PAYLOAD_BINARY_LOOKUP: Dict[str, str] = {
chr(i): bin(i - 48 if i - 48 < 40 else i - 56)[2:].zfill(6)
for i in range(48, 120) # '0' to 'w' in ASCII
}
BINARY_ASCII_LOOKUP: Dict[str, str] = {
bin(i)[2:].zfill(6): chr(i + 64 if i < 31 else i )
for i in range(64) # 0 to 63
}
# -- Utility Functions --
def safe_int(value: Optional[str], base: int = 2, signed: bool = False) -> int:
if(value is not None):
try:
if(signed):
if(value[0] == "1"):
return -(2**(len(value) - 1)) + int(value[1:], base)
else:
return int(value, base)
else:
return int(value, base)
except Exception as e:
print("Error: ", e)
return -1
else:
return -1
def get_segment(binaryString: str, start: int, end: int) -> Optional[str]:
return binaryString[start:end] if len(binaryString) >= end else None
def get_val(val: Any) -> Union[str, Any]:
"""Filter function for returning "N/A" if the value is -1"""
if val == -1:
return "N/A"
else:
return val
def error_tuple(error):
return ({"Error": error}, {"Error": error})
# -- Calculation Functions --
def calculate_longitude(rawLongitude: Optional[str]) -> Union[int, float]:
"""Longitude calculation -- input is in 1/600000 minutes."""
if rawLongitude is None:
return -1
else:
return rawLongitude / 600000
def calculate_latitude(rawLatitude: Optional[int]) -> Union[int, float]:
"""Latitude calculation -- input is in 1/600000 minutes."""
if rawLatitude is None:
return -1
else:
return rawLatitude / 600000
def calculate_speed_over_ground(raw_sog: Optional[int]) -> Union[int, float]:
"""Calculate speed over ground in knots."""
if raw_sog is None:
return -1
elif raw_sog in (1023, 1022):
return raw_sog
else:
return raw_sog / 10
def calculate_course_over_ground(raw_cog: Optional[int]) -> Union[int, float]:
"""Calculate course over ground in degrees."""
if raw_cog is None:
return -1
elif raw_cog == 3600:
return 3600
else:
return raw_cog / 10
def calculate_heading(raw_heading: Optional[int]) -> int:
"""Calculate heading in degrees."""
if raw_heading is None:
return -1
else:
return raw_heading
def calculate_timestamp(raw_timestamp: Optional[int]) -> int:
"""Calculate timestamp in seconds."""
if raw_timestamp is None:
return -1
else:
return raw_timestamp
# -- String Conversion Functions --
def longitude_to_string(longitude: Union[int, float]) -> str:
if longitude == -1:
return "Missing from AIS message"
elif longitude == 181:
return "Position not available"
else:
return f"{longitude}°"
def latitude_to_string(latitude: Union[int, float]) -> str:
if latitude == -1:
return "Missing from AIS message"
elif latitude == 91:
return "Position not available"
else:
return f"{latitude}°"
def bitstring_to_ascii(bitstring: str) -> str:
if bitstring is None:
return "Missing from AIS message"
return "".join(map(lambda x: BINARY_ASCII_LOOKUP.get(x,'') if isinstance(x,str) else '', [bitstring[i:i+6] for i in range(0, len(bitstring), 6)]))
def speed_over_ground_to_string(sog: Union[int, float]) -> str:
if sog == -1:
return "Missing from AIS message"
elif sog == 1023:
return "SOG not available."
elif sog == 1022:
return "SOG exceeds 102.2 knots."
else:
return f"{sog} knots"
def course_over_ground_to_string(cog: Union[int, float]) -> str:
if cog == -1:
return "Missing from AIS message"
elif cog == 3600:
return "COG not available."
else:
return f"{cog}°"
def heading_to_string(heading: int) -> str:
if heading == -1:
return "Missing from AIS message"
elif heading == 511:
return "Not available"
else:
return f"{heading}°"
def timestamp_to_string(timestamp: int) -> str:
if timestamp == -1:
return "Missing from AIS message"
elif timestamp == 60:
return "Timestamp unvailable"
elif timestamp == 61:
return "POS in manual input mode"
elif timestamp == 62:
return "POS in dead reckoning mode"
elif timestamp == 63:
return "System inoperative"
else:
return f"{str(timestamp)}s"