-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSCrx-v01.py
2035 lines (1719 loc) · 70.3 KB
/
DSCrx-v01.py
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ATISrx-v05b.py (11-01-2024)
# pa2ohh
import sys
import time
import pyaudio
import numpy as np
import json
############################################################################################################################################
# Configuration
DEBUG = True # If True, print debug information. Can also be activated with the "Test" button
AUTOscroll = True # Auto scroll text boxes to last messages
SAMPLErate = 44100 # Sample rate of soundcard
CENTERfrequency = 1700 # 1700 for VHF, center frequency for MF - HF (for example 800 or 1000)
SHIFTfrequency = 800 # 800 for VHF
BITrate = 1200.0 # Bitrate 1200 for VHF
chunkbuffer = 0
signals = None
############################################################################################################################################
# Initialisation of global variables required in various routines (DO NOT MODIFY THEM!)
CC = [] # Country Code list
AUDIOsignal1 = [] # Audio trace channel 1
RUNstatus = 0 # 0 stopped, 1 start, 2 running, 3 stop now, 4 stop and restart
RXbuffer = 0.0 # Data contained in input buffer in %
NextBitTime = float(0) # The next bit sample time moment in samples
YBYt1 = float(0) # The four zero crossings, time in fractional audio samples
YBYt2 = float(1)
YBYt3 = float(2)
YBYt4 = float(3)
YBYv1 = float(1) # The value of the previous sample
YBYv2 = float(1) # The value of the next sample
YBYprsample = "P" # the previous audio sample is positive part of period
YBYcusample = "N" # current sample is negative
strYBY = "" # the YBY string from the YBY decoding process
MSGdata = [] # The message data
EXPMSGdata = [] # Extension message data
MSG = 0 # Start of message position
MSGstatus = 0 # 0=Search Phasing;
# 1=Decode Data;
# 2=Decode data to Message;
# 3=Error in Message decoding, continue with next Phasing search
HLINE = "------------------------------------------------"
############################################################################################################################################
# =============== The Mainloop =====================
def MAINloop(): # The Mainloop
global AUDIOsignal1
global SAMPLErate
global RUNstatus
global MSGdata
while(True):
MakeYBY() # Decode audio data in AUDIOsignal1[] to YBY...
FINDphasing() # Search for the phasing signal and the start of a message
MAKEdata() # Make the data and call the message decoders
SELECTdecoder() # Select and call the decoder depending on the format specifier
if RUNstatus == 2:
json_message = decode_dsc_message(MSGdata)
print(json_message)
# Initialize PyAudio
PA = pyaudio.PyAudio()
FORMAT = pyaudio.paInt16
TRACESopened = 1
# ======================= Read audio from stdin ==================================
def AUDIOin(): # Read the audio from stdin and store the data into the arrays
global DEBUG
global AUDIOsignal1
global RUNstatus
global SAMPLErate
global RXbuffer
global signals, readsamples
RUNstatus = 1
# RUNstatus == 1: Initializing the input stream from stdin
if RUNstatus == 1:
print(RUNstatus)
TRACESopened = 1
AUDIOsignal1 = []
try:
chunkbuffer = int(SAMPLErate * 4) # Fixed at xx seconds
readsamples = SAMPLErate # Samples to read
RUNstatus = 2
except Exception as e:
RUNstatus = 0
if RUNstatus == 2:
print(RUNstatus)
try:
# Read samples from stdin
#signals = fileinput.input()
signals = sys.stdin.buffer.read(readsamples * 2) # 2 bytes per sample
print(signals)
if signals:
AUDIOsignal1.extend(np.frombuffer(signals, np.int16))
RUNstatus = 4
except IOError:
pass # Ignore the IOError exception that arises when no data is available
except Exception as e:
RUNstatus = 4
# RUNstatus == 3: Stop; RUNstatus == 4: Stop and restart
if RUNstatus == 3 or RUNstatus == 4:
print(RUNstatus)
PA.terminate()
if RUNstatus == 3:
RUNstatus = 0 # Status is stopped
if RUNstatus == 4:
RUNstatus = 1 # Status is (re)start
AUDIOsignal1 = [] # Clear audio buffer
# ============= Convert AUDIOsignal1[] audio data to strYBY =======================
def MakeYBY(): # Read the audio and make strYBY
global RUNstatus
global strYBY
global AUDIOsignal1
global SAMPLErate
global SAMPLEoffset
global NextBitTime # The next bit sample time moment in samples
global YBYt1 # The four zero crossings, time in fractional audio samples
global YBYt2
global YBYt3
global YBYt4
global YBYv1 # The value of the sample before the zero crossing
global YBYv2 # The value of the sample after the zero crossing
global YBYprsample # The previous audio sample is positive "P" or negative "N"
global YBYcusample # The current audio sample is positive "P" or negative "N"
global CENTERfrequency
global SHIFTfrequency
global BITrate
lowfrequency = CENTERfrequency - SHIFTfrequency / 2.0 # low shift
highfrequency = CENTERfrequency + SHIFTfrequency / 2.0 # high shift
lowsamples = float(SAMPLErate) / lowfrequency # number of audio samples low frequency period
highsamples = float(SAMPLErate) / highfrequency # number of audio samples high frequency period
Decision = float(highsamples + (lowsamples - highsamples) / 4.25) # below this value a high tone 6.0 and 3.0 were okay, 6.5 and 2.5 not
BitTimeStep = float(SAMPLErate) / BITrate # The step in samples
AddYBY = 0 # Counts the number of YBY's that have been added
i = 0
while AddYBY < 50: # Add maximal xx YBY's
while len(AUDIOsignal1) <= i: # If buffer too small, call the audio read routine
AUDIOin()
v = AUDIOsignal1[i]
# v = AUDIOsignal1[i] + OFFSET
if (v >= 0):
YBYcusample = "P"
else:
YBYcusample = "N"
if (YBYcusample != YBYprsample): # A zero crossing
YBYprsample = YBYcusample
YBYv2 = float(abs(v))
y = YBYv2 / (YBYv1 + YBYv2) # Fraction of fractional sample
YBYt1 = YBYt2
YBYt2 = YBYt3
YBYt3 = YBYt4
YBYt4 = float(i) - y # Fractional sample
if (YBYt3-YBYt1) >= Decision and (YBYt4-YBYt2) < Decision: # Synchronisation
if (NextBitTime-YBYt2) < BitTimeStep / 2.0: # Should be 2.0, min 1.4 max 2.5 so correct
NextBitTime = NextBitTime + BitTimeStep * 0.025
else:
NextBitTime = NextBitTime - BitTimeStep * 0.025
while NextBitTime < YBYt3:
AddYBY = AddYBY + 1
NextBitTime = NextBitTime + BitTimeStep
if (YBYt3-YBYt1) < Decision or (YBYt4-YBYt2) < Decision:
strYBY = strYBY + "B" # Add "B" for 0 for high tone
else:
strYBY = strYBY + "Y" # Add "Y" for 1 for low tone
YBYv1 = abs(float(v)) # store in previous memory for fractional calculation of zero crossing
if i > (NextBitTime + 2*BitTimeStep): # If no signal, add "Y"
AddYBY = AddYBY + 1
NextBitTime = float(i) + BitTimeStep
strYBY = strYBY + "Y"
YBYt4 = float(i)
YBYt3 = YBYt4 - BitTimeStep
YBYt2 = YBYt3 - BitTimeStep
YBYt1 = YBYt2 - BitTimeStep
i = i + 1
AUDIOsignal1 = AUDIOsignal1[i:] # Delete the used samples
NextBitTime = NextBitTime - float(i) # The next bit sample time moment in samples set to the begin of the next decoding
YBYt4 = YBYt4 - float(i)
YBYt3 = YBYt3 - float(i)
YBYt2 = YBYt2 - float(i)
YBYt1 = YBYt1 - float(i)
# ================== Start Decoding routines =====================================================
# ============= Find the phasing signal and the start of the message MSG =======================
def FINDphasing():
global DEBUG
global strYBY
global MSG # Start of message in strYBY
global MSGstatus
if MSGstatus != 0 and MSGstatus != 3: # Exit if MSGstatus not 0 or 3 (not necessary)
return()
# ... Find Phasing ...
MinBits = 50 # The minimum of bits in the YBY string (> 20)
Starti = 100 # Start to search from this pointer, so that the data before this pointer can also be read
if MSGstatus == 3: # Start of new search, skip the old part upto the format specifier
strYBY = strYBY[(MSG+120-Starti):] # Ready for next search of phasing signal
MSGstatus = 0 # And set the status to search
while len(strYBY) < (Starti+MinBits+21): # If strYBY is too short, call MakeYBY
MakeYBY()
se1 = TENunit(108) + TENunit(125) # Define search string 1 for phasing
se2 = TENunit(107) + TENunit(125) # Define search string 2 for phasing
i = Starti
L = len(strYBY)
while i < (L - MinBits):
if strYBY[i:(i+20)] == se1:
MSG = i - 70
MSGstatus = 1
break
if strYBY[i:(i+20)] == se2:
MSG = i - 90
MSGstatus = 1
break
i = i + 1
if MSGstatus == 0:
strYBY= strYBY[(L - MinBits - Starti):]
return()
if DEBUG == True:
PrintDSCresult("\n=== DEBUG DATA message ===")
PrintDSCresult("Message found at " + str(MSG))
DATAerror = 0
strDATA = ""
i = 1
while DATAerror < 5:
strDATA = strDATA +"(" + str(GETvalsymbol(i)) +")"
if i > 16: # End of phasing and start of data
if GETvalsymbol(i) < 0:
DATAerror = DATAerror + 1
i = i + 1
PrintDSCresult(strDATA)
PrintDSCresult(HLINE)
# ============= MAKEdata, set the data into MSGdata[]=======================
def MAKEdata():
global DEBUG
global strYBY
global MSGdata
global EXPMSGdata
global MSG # Start of message in strYBY
global MSGstatus
if MSGstatus != 1: # Exit if MSGstatus not 1
return()
# ... Check if the double transmission of the format specifier is identical ...
FS1 = -1 # The 1st format specifier
FS2 = -1 # The 2nd format specifier
FS1 = GETvalsymbol(13)
if FS1 < 100: # If incorrect error check bits (below -1) or not valid (below 100)
FS1 = GETvalsymbol(18)
FS2 = GETvalsymbol(15)
if FS2 < 100: # If incorrect error check bits (below -1) or not valid (below 100)
FS2 = GETvalsymbol(20)
if FS1 != FS2:
MSGstatus = 3 # Initialize next search as both Format specifiers have to be identical
if DEBUG == True:
print("Format specifiers not identical")
return()
# ... Make the message data and store in MSGdata ...
Vprevious = -1
L3Berror = False # True if the initial and retransmission do have a wrong 3 bits error check value
MSGdata = [] # Clear the data
MSGdata.append(FS1) # Append the format specifier
i = 17 # The message starts at position 17
while(1): # Loop until a break occurs
V = GETvalsymbol(i)
if V < 0:
V = GETvalsymbol(i+5) # If 3 bits error check value incorrect, take the RTX signal 5 symbols later
if V >= 0:
MSGdata.append(V) # If the value has a correct CRC value, add it to the data
else:
L3Berror = True # Both the initial and retransmission do have the wrong 3 bits error check value
break
if Vprevious == 117: # EOS sign for Acknowledgement required, end of message
break
if Vprevious == 122: # EOS sign for Acknowledgement given, end of message
break
if Vprevious == 127: # EOS sign for Non acknowledgements, end of message
break
Vprevious = V # Store previous value
i = i + 2
STARTEXPMSG = i + 6 # The possible start of the extension message
if L3Berror == True:
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime())
print(txt + "Error Character Check 3 last bits (2x)")
MSGstatus = 3 # Initialize next search as there was an error that could not be corrected
return()
# ... Check errors with error check character ...
ECC = MSGrdta(0)
i = 1
while i < (len(MSGdata) - 1):
ECC = ECC ^ MSGrdta(i)
i = i + 1
if MSGrdta(len(MSGdata)-1) != ECC: # The last value in the array MSGdata is the Error check symbol
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime())
print(txt + "Data does not match with Error Check Character")
MSGstatus = 3 # Initialize next search as there was an error in the error check
return()
MSGstatus = 2 # Status for decoding the data in MSGdata to a message
# ... Search for extension message ...
V = GETvalsymbol(STARTEXPMSG)
NOEXPmessage = False # True if no expansion message
if V < 100 or V > 106: # Then no expansion message
NOEXPmessage = True
EXPMSGdata = [] # Clear the EXPMSGdata[] array
return()
# ... Start to fill the EXPMSGdata ....
Vprevious = -1
EXPMSGdata = [] # Clear the EXPMSGdata[] array
L3Berror = False # True if the initial and retransmission do have a wrong 3 bits error check value
i = STARTEXPMSG # The possible extension message starts at this position
while(1): # Loop until a break occurs
V = GETvalsymbol(i)
if V < 0:
V = GETvalsymbol(i+5) # If 3 bits error check value incorrect, take the RTX signal 5 symbols later
if V >= 0:
EXPMSGdata.append(V) # If the value has a correct CRC value, add it to the data
else:
L3Berror = True # Both the initial and retransmission do have the wrong 3 bits error check value
break
if Vprevious == 117: # EOS sign for Acknowledgement required, end of message
break
if Vprevious == 122: # EOS sign for Acknowledgement given, end of message
break
if Vprevious == 127: # EOS sign for Non acknowledgements, end of message
break
Vprevious = V # Store previous value
i = i + 2
if L3Berror == True:
print("Error expansion msg, Error Character Check 3 last bits (2x)")
EXPMSGdata = [] # Clear the EXPMSGdata
return()
# ... Check errors with error check character ...
ECC = EXPMSGrdta(0)
i = 1
while i < (len(EXPMSGdata) - 1):
ECC = ECC ^ EXPMSGrdta(i)
i = i + 1
if EXPMSGrdta(len(EXPMSGdata)-1) != ECC: # The last value in the array EXPMSGdata is the Error check symbol
print("Data expansion message does not match with Error Check Character")
EXPMSGdata = []
return()
# ============================ Select the decoder depending on the Format specifier ==============================
def SELECTdecoder():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
global EXPMSGdata
if MSGstatus != 2: # Exit if MSGstatus not 2
return()
if MSGrdta(0) == 121: # Format specifier 121 (ATIS)
DEC121()
if MSGrdta(0) == 102: # Format specifier 102
DEC102()
if MSGrdta(0) == 112: # Format specifier 112
DEC112()
if MSGrdta(0) == 114: # Format specifier 114
DEC114()
if MSGrdta(0) == 116: # Format specifier 116
DEC116()
if MSGrdta(0) == 120: # Format specifier 120
DEC120()
if MSGrdta(0) == 123: # Format specifier 123
DEC123()
if MSGstatus != 3: # The MSGstatus is not reset to 3, so no valid or supported format specifier
print("Error or no supported format specifier: " + str(MSGrdta(0)))
if len(EXPMSGdata) != 0: # Decode the extension message
DSCExpansion821()
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 102 (Selective Geographic Area) ==============================
def DEC102():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 102: Selective geographic area"
PrintDSCresult(txt)
DSC_ZONE(1)
PrintDSCresult(DSC_CAT(MSGrdta(6)))
PrintDSCresult("Self ID:")
DSC_MMSI(7)
PrintDSCresult(DSC_TC1(MSGrdta(12)))
PrintDSCresult("Distress ID:")
DSC_MMSI(13)
PrintDSCresult(DSC_NOD(MSGrdta(18)))
DSC_POS(19)
DSC_UTC(24)
PrintDSCresult(DSC_TC1(MSGrdta(26)))
PrintDSCresult(DSC_EOS(MSGrdta(len(MSGdata)-2)))
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 112 (Disstress) ==============================
def DEC112():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 112: Distress"
PrintDSCresult(txt)
PrintDSCresult("Distress ID:")
DSC_MMSI(1)
PrintDSCresult(DSC_NOD(MSGrdta(6)))
DSC_POS(7)
DSC_UTC(12)
PrintDSCresult(DSC_EOS(MSGrdta(len(MSGdata)-2)))
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 114 (Selective Group Call) ==============================
def DEC114():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 114: Selective group call"
PrintDSCresult(txt)
PrintDSCresult("Address:")
DSC_MMSI(1)
PrintDSCresult(DSC_CAT(MSGrdta(6)))
PrintDSCresult("Self ID:")
DSC_MMSI(7)
PrintDSCresult(DSC_TC1(MSGrdta(12)))
PrintDSCresult(DSC_TC2(MSGrdta(13)))
PrintDSCresult("Called station receive frequency:")
DSC_FREQ(14)
PrintDSCresult("Called station transmit frequency:")
DSC_FREQ(17)
PrintDSCresult(DSC_EOS(MSGrdta(len(MSGdata)-2)))
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 116 (All Ships Call) ==============================
def DEC116():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 116: All ships call"
PrintDSCresult(txt)
PrintDSCresult(DSC_CAT(MSGrdta(1)))
if MSGrdta(1) == 112:
PrintDSCresult("Self ID:")
DSC_MMSI(2)
PrintDSCresult(DSC_TC1(MSGrdta(7)))
PrintDSCresult("Distress ID:")
DSC_MMSI(8)
PrintDSCresult(DSC_NOD(MSGrdta(13)))
DSC_POS(14)
DSC_UTC(19)
PrintDSCresult(DSC_TC1(MSGrdta(21)))
if MSGrdta(1) == 108 or MSGrdta(1) == 110:
PrintDSCresult("Self ID:")
DSC_MMSI(2)
PrintDSCresult(DSC_TC1(MSGrdta(7)))
PrintDSCresult(DSC_TC2(MSGrdta(8)))
PrintDSCresult("Called station receive frequency:")
DSC_FREQ(9)
PrintDSCresult("Called station transmit frequency:")
DSC_FREQ(12)
PrintDSCresult(DSC_EOS(MSGrdta(len(MSGdata)-2)))
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 120 (Selective Individual Call) ==============================
def DEC120():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 120: Selective individual call"
PrintDSCresult(txt)
PrintDSCresult("Address:")
DSC_MMSI(1)
PrintDSCresult(DSC_CAT(MSGrdta(6)))
if MSGrdta(6) == 112: # Category 112
PrintDSCresult("Self ID:")
DSC_MMSI(7)
PrintDSCresult(DSC_TC1(MSGrdta(12)))
PrintDSCresult("Distress ID:")
DSC_MMSI(13)
PrintDSCresult(DSC_NOD(MSGrdta(18)))
DSC_POS(19)
DSC_UTC(24)
PrintDSCresult(DSC_TC1(MSGrdta(25)))
PrintDSCresult(DSC_EOS(MSGrdta(26)))
else:
PrintDSCresult("Self ID:")
DSC_MMSI(7)
PrintDSCresult(DSC_TC1(MSGrdta(12)))
PrintDSCresult(DSC_TC2(MSGrdta(13)))
if MSGrdta(12) == 121: # Position update
DSC_POS(14)
if MSGdata == 122: # EOS for Acknowledgement
DSC_UTC(20)
else:
PrintDSCresult("Called station receive frequency:")
DSC_FREQ(14)
PrintDSCresult("Called station transmit frequency:")
DSC_FREQ(17)
PrintDSCresult(DSC_EOS(MSGrdta(len(MSGdata)-2)))
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 123 (Selective Individual Automatic Call) ==============================
def DEC123():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
PrintDSCresult(HLINE)
txt = time.strftime("<%Y%b%d-%H:%M:%S> ", time.gmtime()) # The time
txt = txt + "Format specifier 120: Selective individual automatic call"
PrintDSCresult(txt)
PrintDSCresult("Address:")
DSC_MMSI(1)
PrintDSCresult(DSC_CAT(MSGrdta(6)))
PrintDSCresult("Self ID:")
DSC_MMSI(7)
PrintDSCresult(DSC_TC1(MSGrdta(12)))
PrintDSCresult(DSC_TC2(MSGrdta(13)))
PrintDSCresult("Frequency:")
DSC_FREQ(14)
DSC_NUMBER(17)
MSGstatus = 3 # Continue with the next search, messages have been decoded
# ============================ Decode format specifier 121 (ATIS) ==============================
def DEC121():
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
global ATISlog
global ATISlogdir
global ATISdatadir
# ... Callsign information ...
strX = ""
i = 1
while i < 6:
s = str(MSGrdta(i))
s = Lzeroes(s, 2) # if 1 digit, make 2
strX = strX + s
i = i + 1
CallSign = strX[0:4] + chr(64 + MSGrdta(3)) + strX[6:]
if CallSign[0:1] != "9":
txt = "ERROR!! MMSI does not start with 9 for Inland Waterways"
print(txt)
return()
if MSGrdta(3) > 26:
txt = "ERROR: Callsign does not start with a letter (1 - 26)"
print(txt)
return()
ATIStxt = time.strftime("<%Y%b%d-%H:%M:%S>", time.gmtime()) # The time
strCC = "unknown" # Country name
strPR = "?" # Prefix character initialise with ?
cc = int(CallSign[1:4])
# T1 = time.time()
if cc == 201:strCC = "Albania";strPR = "Z"
if cc == 202:strCC = "Andorra";strPR = "C"
if cc == 203:strCC = "Austria";strPR = "O"
if cc == 204:strCC = "Azores";strPR = "?"
if cc == 205:strCC = "Belgium";strPR = "O"
if cc == 206:strCC = "Belarus";strPR = "E"
if cc == 207:strCC = "Bulgaria";strPR = "L"
if cc == 208:strCC = "Vatican City";strPR = "H"
if cc == 209:strCC = "Cyprus";strPR = "C"
if cc == 210:strCC = "Cyprus";strPR = "C"
if cc == 211:strCC = "Germany";strPR = "D"
if cc == 212:strCC = "Cyprus";strPR = "C"
if cc == 213:strCC = "Georgia";strPR = "4"
if cc == 214:strCC = "Moldova";strPR = "E"
if cc == 215:strCC = "Malta";strPR = "9"
if cc == 216:strCC = "Armenia";strPR = "E"
if cc == 218:strCC = "Germany";strPR = "D" # 218 from former German Democratic Republic
if cc == 219:strCC = "Denmark";strPR = "O"
if cc == 220:strCC = "Denmark";strPR = "O"
if cc == 224:strCC = "Spain";strPR = "E"
if cc == 225:strCC = "Spain";strPR = "E"
if cc == 226:strCC = "France";strPR = "F"
if cc == 227:strCC = "France";strPR = "F"
if cc == 228:strCC = "France";strPR = "F"
if cc == 229:strCC = "Malta";strPR = "9"
if cc == 230:strCC = "Finland";strPR = "O"
if cc == 231:strCC = "Faroe Islands";strPR = "?"
if cc == 232:strCC = "United Kingdom";strPR = "G"
if cc == 233:strCC = "United Kingdom";strPR = "G"
if cc == 234:strCC = "United Kingdom";strPR = "G"
if cc == 235:strCC = "United Kingdom";strPR = "G"
if cc == 236:strCC = "Gibraltar";strPR = "?"
if cc == 237:strCC = "Greece";strPR = "S"
if cc == 238:strCC = "Croatia";strPR = "9"
if cc == 239:strCC = "Greece";strPR = "S"
if cc == 240:strCC = "Greece";strPR = "S"
if cc == 241:strCC = "Greece";strPR = "S"
if cc == 242:strCC = "Morocco";strPR = "C"
if cc == 243:strCC = "Hungary";strPR = "H"
if cc == 244:strCC = "Netherlands";strPR = "P"
if cc == 245:strCC = "Netherlands";strPR = "P"
if cc == 246:strCC = "Netherlands";strPR = "P"
if cc == 247:strCC = "Italy";strPR = "I"
if cc == 248:strCC = "Malta";strPR = "9"
if cc == 249:strCC = "Malta";strPR = "9"
if cc == 250:strCC = "Ireland";strPR = "E"
if cc == 251:strCC = "Iceland";strPR = "T"
if cc == 252:strCC = "Liechtenstein";strPR = "H"
if cc == 253:strCC = "Luxembourg";strPR = "L"
if cc == 254:strCC = "Monaco";strPR = "3"
if cc == 255:strCC = "Madeira";strPR = "?"
if cc == 256:strCC = "Malta";strPR = "9"
if cc == 257:strCC = "Norway";strPR = "L"
if cc == 258:strCC = "Norway";strPR = "L"
if cc == 259:strCC = "Norway";strPR = "L"
if cc == 261:strCC = "Poland";strPR = "S"
if cc == 262:strCC = "Montenegro";strPR = "4"
if cc == 263:strCC = "Portugal";strPR = "C"
if cc == 264:strCC = "Romania";strPR = "Y"
if cc == 265:strCC = "Sweden";strPR = "S"
if cc == 266:strCC = "Sweden";strPR = "S"
if cc == 267:strCC = "Slovakia";strPR = "O"
if cc == 268:strCC = "San Marino";strPR = "T"
if cc == 269:strCC = "Switzerland";strPR = "H"
if cc == 270:strCC = "Czech Republic";strPR = "O"
if cc == 271:strCC = "Turkey";strPR = "T"
if cc == 272:strCC = "Ukraine";strPR = "U"
if cc == 273:strCC = "Russia";strPR = "U"
if cc == 274:strCC = "Macedonia";strPR = "Z"
if cc == 275:strCC = "Latvia";strPR = "Y"
if cc == 276:strCC = "Estonia";strPR = "E"
if cc == 277:strCC = "Lithuania";strPR = "L"
if cc == 278:strCC = "Slovenia";strPR = "S"
if cc == 279:strCC = "Serbia";strPR = "Y"
# T2 = time.time();print(T2-T1)
ATIStxt = ATIStxt + " " + strPR + CallSign[4:] + " Country: " + CallSign[1:4] + " (" + strCC + ")"
print(ATIStxt)
MSGstatus = 3
if ATISlog == False:
return()
if strPR == "?": # Unknown country
filename = "-" + CallSign[1:4] + "-" + CallSign[4:] + ".txt"
else: # Known country
filename = strPR + CallSign[4:] + ".txt"
try:
filename = ATISdatadir + filename
Wfile = open(filename,'a') # output file setting
Wfile.write(ATIStxt + "\n")
Wfile.close() # Close the file
except:
print("File append error: " + filename)
try:
filename = time.strftime("%Y%m%d", time.gmtime()) # The time
filename = ATISlogdir + filename + "-ATISlog.txt"
Wfile = open(filename,'a') # output file setting
Wfile.write(ATIStxt + "\n")
Wfile.close() # Close the file
except:
print("File append error: " + filename)
# ============================ Decode Expansion message ==============================
def DSCExpansion821(): # Expansion message decoder ITU-R M.821
global MSG # Start of message in strYBY
global HLINE # Dashed line
global DEBUG
global MSGdata
global MSG # Start of message in strYBY
global MSGstatus
global EXPMSGdata
if len(EXPMSGdata) == 0: # Return if no message
return()
PrintDSCresult(HLINE)
PrintDSCresult("Expansion message ITU-R M.821")
PrintDSCresult(HLINE)
P = 0 # The pointer in EXPMSGdata[]
while(1):
if P > len(EXPMSGdata): # Stop if the end of EXPMSGdata[] has been reached
break
if EXPMSGrdta(P) == 117: # Stop if one of the 3 EOS characters
break
if EXPMSGrdta(P) == 122:
break
if EXPMSGrdta(P) == 127:
break
if EXPMSGrdta(P) < 100 or EXPMSGrdta(P) > 106: # Stop if not a known expansion data specifier
PrintDSCresult(str(EXPMSGrdta(P)) + " Unknown expansion data specifier:")
break
# ... 100 Enhanced position resolution ...
if EXPMSGrdta(P) == 100:
P = P + 1
PrintDSCresult("100 Enhanced position resolution:")
if EXPMSGrdta(P) == 126 or EXPMSGrdta(P) == 110:
if EXPMSGrdta(P) == 110:
PrintDSCresult("110 Enhanced position data request")
if EXPMSGrdta(P) == 126:
PrintDSCresult("126 No enhanced position information")
P = P + 1 # Point to the next possible expansion data specifier
else:
strX = ""
N = 0
while N < 4:
if EXPMSGrdta(P+N) < 10:
strX = strX + "0"
s = str(EXPMSGrdta(P+N))
strX = strX + s.strip()
N = N + 1
P = P + N # Point to the next possible expansion data specifier
PrintDSCresult("<" + strX + ">")
PrintDSCresult("Latitude : " + "0." + strX[0:4])
PrintDSCresult("Longitude: " + "0." + strX[4:9])
# ... 101 Source and datum of position ...
if EXPMSGrdta(P) == 101:
P = P + 1
PrintDSCresult("101 Source and datum of position:")
if EXPMSGrdta(P) == 126 or EXPMSGrdta(P) == 110:
if EXPMSGrdta(P) == 110:
PrintDSCresult("110 Source and datum of position data request")
if EXPMSGrdta(P) == 126:
PrintDSCresult("126 No source and datum of position information")
P = P + 1 # Point to the next possible expansion data specifier
else:
strX = ""
N = 0
while N < 3:
if EXPMSGrdta(P+N) < 10:
strX = strX + "0"
s = str(EXPMSGrdta(P+N))
strX = strX + s.strip()
N = N + 1
P = P + N # Point to the next possible expansion data specifier
PrintDSCresult("<" + strX + ">")
TXT = " ERROR!! INVALID SOURCE CHARACTER"
intX = int(strX[0:2])
if intX == 0:
TXT = " Current position data invalid"
if intX == 1:
TXT = " Position data from differential GPS"
if intX == 2:
TXT = " Position data from uncorrected GPS"
if intX == 3:
TXT = " Position data from differential LORAN-C"
if intX == 4:
TXT = " Position data from uncorrected LORAN-C"
if intX == 5:
TXT = " Position data from GLONASS"
if intX == 6:
TXT = " Position data from radar fix"
if intX == 7:
TXT = " Position data from Decca"
if intX == 8:
TXT = " Position data from other source"
PrintDSCresult("Source : " + strX[0:2] + TXT)
PrintDSCresult("Fix : " + strX[2] + "." + strX[3])
TXT = " ERROR!! INVALID DATE CHARACTER"
intX = int(strX[4:6])
if intX == 0:
TXT = " WGS-84"
if intX == 1:
TXT = " WGS-72"
if intX == 2:
TXT = " Other"
PrintDSCresult("Date : " + strX[4:6] + TXT)
# ... 102 Vessel speed ...
if EXPMSGrdta(P) == 102:
P = P + 1
PrintDSCresult("102 Vessel speed:")
if EXPMSGrdta(P) == 126 or EXPMSGrdta(P) == 110:
if EXPMSGrdta(P) == 110:
PrintDSCresult("110 Vessel speed data request")
if EXPMSGrdta(P) == 126:
PrintDSCresult("126 No vessel speed information")
P = P + 1 # Point to the next possible expansion data specifier
else:
strX = ""
N = 0
while N < 2:
if EXPMSGrdta(P+N) < 10:
strX = strX + "0"
s = str(EXPMSGrdta(P+N))
strX = strX + s.strip()
N = N + 1
P = P + N # Point to the next possible expansion data specifier
PrintDSCresult("<" + strX + ">")
PrintDSCresult("Speed: " + strX[0:3] + "." + strX[3] + " knots")
# ... 103 Current course of the vessel ...
if EXPMSGrdta(P) == 103:
P = P + 1
PrintDSCresult("103 Current course of the vessel:")
if EXPMSGrdta(P) == 126 or EXPMSGrdta(P) == 110:
if EXPMSGrdta(P) == 110:
PrintDSCresult("110 Vessel course data request")
if EXPMSGrdta(P) == 126:
PrintDSCresult("126 No vessel course information")
P = P + 1 # Point to the next possible expansion data specifier
else:
strX=""
N = 0
while N < 2:
if EXPMSGrdta(P+N) < 10:
strX = strX + "0"
s = str(EXPMSGrdta(P+N))
strX = strX + s.strip()
N = N + 1
P = P + N # Point to the next possible expansion data specifier
PrintDSCresult("<" + strX + ">")
PrintDSCresult("Course: " + strX[0:3] + "." + strX[3] + " Degrees")
# ... 104 Additional station information ...
strExpChar = "0123456789?ABCDEFGHIJKLMNOPQRSTUVWXYZ.,-/ "
if EXPMSGrdta(P) == 104:
P = P + 1
PrintDSCresult("104 Additional station information:")
if EXPMSGrdta(P) == 126 or EXPMSGrdta(P) == 110:
if EXPMSGrdta(P) == 110:
PrintDSCresult("110 Additional station information data request")
if EXPMSGrdta(P) == 126:
PrintDSCresult("126 No additional station information")
P = P + 1 # Point to the next possible expansion data specifier
else:
strX = ""
N = 0
while N < 99: # Limited to 99 characters but max 10 is allowed
if EXPMSGrdta(P+N) <= 41:
strX = strX + strExpChar[EXPMSGrdta(P+N)]
if EXPMSGrdta(P+N) > 41 and EXPMSGrdta(P+N) <= 99:
strX = strX + "?"
if EXPMSGrdta(P+N) > 99: # It has to stop once...
break
N = N + 1
P = P + N # Point to the next possible expansion data specifier
PrintDSCresult("<" + strX + ">")