Repository navigation
Expand file tree
/
Copy pathais_msg_4_handcoded.py
More file actions
executable file
·543 lines (488 loc) · 19 KB
/
Copy pathais_msg_4_handcoded.py
File metadata and controls
executable file
·543 lines (488 loc) · 19 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/env python
"""Functions to serialize/deserialize binary messages.
Adds commstate to the msg 4 definition.
"""
import io
import sys
from decimal import Decimal
from optparse import OptionParser
from BitVector import BitVector
from aisutils import binary, sqlhelp, uscg
from . import commstate
# TODO(schwehr): from ais_msg_4 import *.
fieldList = (
"MessageID",
"RepeatIndicator",
"UserID",
"Time_year",
"Time_month",
"Time_day",
"Time_hour",
"Time_min",
"Time_sec",
"PositionAccuracy",
"Position_longitude",
"Position_latitude",
"fixtype",
"Spare",
"RAIM",
*commstate.sotdma_fields,
)
def decode_aivdm(msg):
bv = binary.ais6tobitvec(msg.split(",")[5])
print(decode(bv))
def decode(bv, validate=False):
"""Unpack a bsreport message.
Fields in params:
- MessageID(uint): AIS message number. Must be 4 (field automatically set to "4")
- RepeatIndicator(uint): Indicated how many times a message has been repeated
- UserID(uint): Unique ship identification number (MMSI)
- Time_year(uint): Current time stamp year 1-9999
- Time_month(uint): Current time stamp month 1..12
- Time_day(uint): Current time stamp day of the month 1..31
- Time_hour(uint): Current time stamp UTC hours 0..23
- Time_min(uint): Current time stamp minutes
- Time_sec(uint): Current time stamp seconds
- PositionAccuracy(uint): Accuracy of positioning fixes
- Position_longitude(decimal): Location of base station East West location
- Position_latitude(decimal): Location of base station North South location
- fixtype(uint): Method used for positioning
- Spare(uint): Not used. Should be set to zero. (field automatically set to "0")
- RAIM(bool): Receiver autonomous integrity monitoring flag
- state_syncstate(uint): Communications State - SOTDMA Synchronization state
- state_slottimeout(uint): Communications State - SOTDMA Frames remaining until a new slot is selected
- state_slotoffset(uint): Communications State - SOTDMA In what slot will the next transmission occur. BROKEN
@type bv: BitVector
Args:
bv: Bits defining a message
validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
Returns:
dict
params
"""
r = {}
r["MessageID"] = 4
r["RepeatIndicator"] = int(bv[6:8])
r["UserID"] = int(bv[8:38])
r["Time_year"] = int(bv[38:52])
r["Time_month"] = int(bv[52:56])
r["Time_day"] = int(bv[56:61])
r["Time_hour"] = int(bv[61:66])
r["Time_min"] = int(bv[66:72])
r["Time_sec"] = int(bv[72:78])
r["PositionAccuracy"] = int(bv[78:79])
r["Position_longitude"] = Decimal(binary.signedIntFromBV(bv[79:107])) / Decimal(
"600000"
)
r["Position_latitude"] = Decimal(binary.signedIntFromBV(bv[107:134])) / Decimal(
"600000"
)
r["fixtype"] = int(bv[134:138])
r["Spare"] = 0
r["RAIM"] = bool(int(bv[148:149]))
r.update(commstate.sotdma_parse_bits(bv[-19:]))
return r
def sqlCreateStr(
outfile=sys.stdout,
fields=None,
extraFields=None,
addCoastGuardFields=True,
dbType="postgres",
):
outfile.write(
str(sqlCreate(fields, extraFields, addCoastGuardFields, dbType=dbType))
)
def sqlCreate(
fields=None, extraFields=None, addCoastGuardFields=True, dbType="postgres"
):
if not fields:
fields = fieldList
c = sqlhelp.create("bsreport", dbType=dbType)
c.addPrimaryKey()
if "MessageID" in fields:
c.addInt("MessageID")
if "RepeatIndicator" in fields:
c.addInt("RepeatIndicator")
if "UserID" in fields:
c.addInt("UserID")
if "Time_year" in fields:
c.addInt("Time_year")
if "Time_month" in fields:
c.addInt("Time_month")
if "Time_day" in fields:
c.addInt("Time_day")
if "Time_hour" in fields:
c.addInt("Time_hour")
if "Time_min" in fields:
c.addInt("Time_min")
if "Time_sec" in fields:
c.addInt("Time_sec")
if "PositionAccuracy" in fields:
c.addInt("PositionAccuracy")
if dbType != "postgres" and "Position_longitude" in fields:
c.addDecimal("Position_longitude", 8, 5)
if dbType != "postgres" and "Position_latitude" in fields:
c.addDecimal("Position_latitude", 8, 5)
if "fixtype" in fields:
c.addInt("fixtype")
if "Spare" in fields:
c.addInt("Spare")
if "RAIM" in fields:
c.addBool("RAIM")
commstate.sotdma_sql_fields(c)
if addCoastGuardFields:
# c.addInt('cg_s_rssi') # Relative signal strength indicator
# c.addInt('cg_d_strength') # dBm receive strength
# c.addVarChar('cg_x',10) # Idonno
c.addInt("cg_t_arrival") # Receive timestamp from the AIS equipment 'T'
c.addInt("cg_s_slotnum") # Slot received in
c.addVarChar(
"cg_r", 15
) # Receiver station ID - should usually be an MMSI, but sometimes is a string
c.addInt("cg_sec") # UTC seconds since the epoch
c.addTimestamp(
"cg_timestamp"
) # UTC decoded cg_sec - not actually in the data stream
if dbType == "postgres":
# --- EPSG 4326 : WGS 84
# INSERT INTO "spatial_ref_sys" ("srid","auth_name","auth_srid","srtext","proj4text") VALUES (4326,'EPSG',4326,'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]','+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ');
c.addPostGIS("Position", "POINT", 2, SRID=4326)
return c
def printFields(
params, out=sys.stdout, format="std", fieldList=None, dbType="postgres"
):
"""Print a bsreport message to stdout.
Fields in params:
- MessageID(uint): AIS message number. Must be 4 (field automatically set to "4")
- RepeatIndicator(uint): Indicated how many times a message has been repeated
- UserID(uint): Unique ship identification number (MMSI)
- Time_year(uint): Current time stamp year 1-9999
- Time_month(uint): Current time stamp month 1..12
- Time_day(uint): Current time stamp day of the month 1..31
- Time_hour(uint): Current time stamp UTC hours 0..23
- Time_min(uint): Current time stamp minutes
- Time_sec(uint): Current time stamp seconds
- PositionAccuracy(uint): Accuracy of positioning fixes
- Position_longitude(decimal): Location of base station East West location
- Position_latitude(decimal): Location of base station North South location
- fixtype(uint): Method used for positioning
- Spare(uint): Not used. Should be set to zero. (field automatically set to "0")
- RAIM(bool): Receiver autonomous integrity monitoring flag
- state_syncstate(uint): Communications State - SOTDMA Synchronization state
- state_slottimeout(uint): Communications State - SOTDMA Frames remaining until a new slot is selected
- state_slotoffset(uint): Communications State - SOTDMA In what slot will the next transmission occur. BROKEN
Args:
params: Dictionary of field names/values.
out: File like object to write to
Returns:
stdout
text to out
"""
if format == "std":
out.write("bsreport:\n")
if "MessageID" in params:
out.write(" MessageID: " + str(params["MessageID"]) + "\n")
if "RepeatIndicator" in params:
out.write(
" RepeatIndicator: " + str(params["RepeatIndicator"]) + "\n"
)
if "UserID" in params:
out.write(" UserID: " + str(params["UserID"]) + "\n")
if "Time_year" in params:
out.write(" Time_year: " + str(params["Time_year"]) + "\n")
if "Time_month" in params:
out.write(" Time_month: " + str(params["Time_month"]) + "\n")
if "Time_day" in params:
out.write(" Time_day: " + str(params["Time_day"]) + "\n")
if "Time_hour" in params:
out.write(" Time_hour: " + str(params["Time_hour"]) + "\n")
if "Time_min" in params:
out.write(" Time_min: " + str(params["Time_min"]) + "\n")
if "Time_sec" in params:
out.write(" Time_sec: " + str(params["Time_sec"]) + "\n")
if "PositionAccuracy" in params:
out.write(
" PositionAccuracy: " + str(params["PositionAccuracy"]) + "\n"
)
if "Position_longitude" in params:
out.write(
" Position_longitude: " + str(params["Position_longitude"]) + "\n"
)
if "Position_latitude" in params:
out.write(
" Position_latitude: " + str(params["Position_latitude"]) + "\n"
)
if "fixtype" in params:
out.write(" fixtype: " + str(params["fixtype"]) + "\n")
if "Spare" in params:
out.write(" Spare: " + str(params["Spare"]) + "\n")
if "RAIM" in params:
out.write(" RAIM: " + str(params["RAIM"]) + "\n")
for field in commstate.sotdma_fields:
fieldname = "\t" + (field + ":").ljust(30)
if field in params:
out.write(fieldname + str(params[field]) + "\n")
else:
out.write(fieldname + "n/a\n")
elif format == "csv":
if options.fieldList is None:
options.fieldList = fieldList
needComma = False
for field in fieldList:
if needComma:
out.write(",")
needComma = True
if field in params:
out.write(str(params[field]))
# else: leave it empty
out.write("\n")
elif format == "html":
printHtml(params, out)
elif format == "sql":
sqlInsertStr(params, out, dbType=dbType)
elif format == "kml":
printKml(params, out)
elif format == "kml-full":
out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
out.write('<kml xmlns="http://earth.google.com/kml/2.1">\n')
out.write("<Document>\n")
out.write(" <name>bsreport</name>\n")
printKml(params, out)
out.write("</Document>\n")
out.write("</kml>\n")
else:
print("ERROR: unknown format:", format)
raise AssertionError()
def main():
parser = OptionParser(usage="%prog [options]")
parser.add_option(
"--unit-test",
dest="unittest",
default=False,
action="store_true",
help="run the unit tests",
)
parser.add_option(
"-v",
"--verbose",
dest="verbose",
default=False,
action="store_true",
help="Make the test output verbose",
)
# FIX: remove nmea from binary messages. No way to build the whole packet?
# FIX: or build the surrounding msg 8 for a broadcast?
typeChoices = (
"binary",
"nmeapayload",
"nmea",
) # FIX: what about a USCG type message?
parser.add_option(
"-t",
"--type",
choices=typeChoices,
type="choice",
dest="ioType",
default="nmeapayload",
help="What kind of string to write for encoding ("
+ ", ".join(typeChoices)
+ ") [default: %default]",
)
outputChoices = ("std", "html", "csv", "sql", "kml", "kml-full")
parser.add_option(
"-T",
"--output-type",
choices=outputChoices,
type="choice",
dest="outputType",
default="std",
help="What kind of string to output ("
+ ", ".join(outputChoices)
+ ") [default: %default]",
)
parser.add_option(
"-o",
"--output",
dest="outputFileName",
default=None,
help="Name of the python file to write [default: stdout]",
)
parser.add_option(
"-f",
"--fields",
dest="fieldList",
default=None,
action="append",
choices=fieldList,
help="Which fields to include in the output. Currently only for csv output [default: all]",
)
parser.add_option(
"-p",
"--print-csv-field-list",
dest="printCsvfieldList",
default=False,
action="store_true",
help="Print the field name for csv",
)
parser.add_option(
"-c",
"--sql-create",
dest="sqlCreate",
default=False,
action="store_true",
help="Print out an sql create command for the table.",
)
parser.add_option(
"--latex-table",
dest="latexDefinitionTable",
default=False,
action="store_true",
help="Print a LaTeX table of the type",
)
parser.add_option(
"--text-table",
dest="textDefinitionTable",
default=False,
action="store_true",
help="Print delimited table of the type (for Word table importing)",
)
parser.add_option(
"--delimt-text-table",
dest="delimTextDefinitionTable",
default="\t",
help="Delimiter for text table [default: '%default'](for Word table importing)",
)
dbChoices = ("sqlite", "postgres")
parser.add_option(
"-D",
"--db-type",
dest="dbType",
default="postgres",
choices=dbChoices,
type="choice",
help="What kind of database (" + ", ".join(dbChoices) + ") [default: %default]",
)
addMsgOptions(parser)
options, args = parser.parse_args()
outfile = sys.stdout
if options.outputFileName is not None:
outfile = file(options.outputFileName, "w")
if options.doEncode:
# First make sure all non required options are specified
if options.RepeatIndicatorField is None:
parser.error("missing value for RepeatIndicatorField")
if options.UserIDField is None:
parser.error("missing value for UserIDField")
if options.Time_yearField is None:
parser.error("missing value for Time_yearField")
if options.Time_monthField is None:
parser.error("missing value for Time_monthField")
if options.Time_dayField is None:
parser.error("missing value for Time_dayField")
if options.Time_hourField is None:
parser.error("missing value for Time_hourField")
if options.Time_minField is None:
parser.error("missing value for Time_minField")
if options.Time_secField is None:
parser.error("missing value for Time_secField")
if options.PositionAccuracyField is None:
parser.error("missing value for PositionAccuracyField")
if options.Position_longitudeField is None:
parser.error("missing value for Position_longitudeField")
if options.Position_latitudeField is None:
parser.error("missing value for Position_latitudeField")
if options.fixtypeField is None:
parser.error("missing value for fixtypeField")
if options.RAIMField is None:
parser.error("missing value for RAIMField")
if options.state_syncstateField is None:
parser.error("missing value for state_syncstateField")
if options.state_slottimeoutField is None:
parser.error("missing value for state_slottimeoutField")
if options.state_slotoffsetField is None:
parser.error("missing value for state_slotoffsetField")
msgDict = {
"MessageID": "4",
"RepeatIndicator": options.RepeatIndicatorField,
"UserID": options.UserIDField,
"Time_year": options.Time_yearField,
"Time_month": options.Time_monthField,
"Time_day": options.Time_dayField,
"Time_hour": options.Time_hourField,
"Time_min": options.Time_minField,
"Time_sec": options.Time_secField,
"PositionAccuracy": options.PositionAccuracyField,
"Position_longitude": options.Position_longitudeField,
"Position_latitude": options.Position_latitudeField,
"fixtype": options.fixtypeField,
"Spare": "0",
"RAIM": options.RAIMField,
"state_syncstate": options.state_syncstateField,
"state_slottimeout": options.state_slottimeoutField,
"state_slotoffset": options.state_slotoffsetField,
}
bits = encode(msgDict)
if options.ioType == "binary":
print(str(bits))
elif options.ioType == "nmeapayload":
bitLen = len(bits)
if bitLen % 6 != 0:
bits = bits + BitVector(
size=(6 - (bitLen % 6))
) # Pad out to multiple of 6
print(binary.bitvectoais6(bits)[0])
# FIX: Do not emit this option for the binary message payloads. Does not make sense.
elif options.ioType == "nmea":
nmea = uscg.create_nmea(bits)
print(nmea)
else:
sys.exit("ERROR: unknown ioType. Help!")
if options.sqlCreate:
sqlCreateStr(outfile, options.fieldList, dbType=options.dbType)
if options.latexDefinitionTable:
latexDefinitionTable(outfile)
# For conversion to word tables
if options.textDefinitionTable:
textDefinitionTable(outfile, options.delimTextDefinitionTable)
if options.printCsvfieldList:
# Make a csv separated list of fields that will be displayed for csv
if options.fieldList is None:
options.fieldList = fieldList
buf = io.StringIO()
for field in options.fieldList:
buf.write(field + ",")
result = buf.getvalue()
if result[-1] == ",":
print(result[:-1])
else:
print(result)
if options.doDecode:
if len(args) == 0:
args = sys.stdin
for msg in args:
bv = None
if msg[0] in ("$", "!") and msg[3:6] in ("VDM", "VDO"):
# Found nmea
# FIX: do checksum
bv = binary.ais6tobitvec(msg.split(",")[5])
else: # either binary or nmeapayload... expect mostly nmeapayloads
# assumes that an all 0 and 1 string can not be a nmeapayload
binaryMsg = True
for c in msg:
if c not in ("0", "1"):
binaryMsg = False
break
if binaryMsg:
bv = BitVector.from_bitstring(msg)
else: # nmeapayload
bv = binary.ais6tobitvec(msg)
printFields(
decode(bv),
out=outfile,
format=options.outputType,
fieldList=options.fieldList,
dbType=options.dbType,
)
############################################################
if __name__ == "__main__":
main()