-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyall.py
2000 lines (1728 loc) · 68.5 KB
/
pyall.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
#name: pyALL
#created: May 2018
#by: [email protected]
#description:python module to read a Kongsberg ALL sonar file
#notes: See main at end of script for example how to use this
#based on ALL Revision R October 2013
# See readme.md for more details
import ctypes
import math
import pprint
import struct
import os.path
import time
from datetime import datetime
from datetime import timedelta
def main():
#open the ALL file for reading by creating a new ALLReader class and passin in the filename to open.
# filename ="C:/Python27/ArcGIS10.3/pyall-master/0314_20170421_222154_SA1702-FE_302.all"
# filename ="C:/development/Python/m3Sample.all"
# filename = "C:/development/python/0004_20110307_041009.all"
filename = "C:/projects/CARIS/GA-0362_ShoalBay_East_2/raw/0211_20170906_000453_AIMS_Capricornus.all"
# filename = "C:/development/python/sample.all"
# filename = "d:/projects/RVInvestigator/0073_20161001_103120_Investigator_em710.all"
# filename = "C:/projects/RVInvestigator/0016_20160821_150810_Investigator_em710.all"
# filename = "c:/projects/carisworldtour/preprocess/0010_20110308_194752.all"
r = ALLReader(filename)
pingCount = 0
start_time = time.time() # time the process
# navigation = r.loadNavigation()
# print("Load Navigation Duration: %.2fs" % (time.time() - start_time)) # time the process
# print (navigation)
while r.moreData():
# read a datagram. If we support it, return the datagram type and aclass for that datagram
# The user then needs to call the read() method for the class to undertake a fileread and binary decode. This keeps the read super quick.
typeOfDatagram, datagram = r.readDatagram()
print(typeOfDatagram, end='')
rawbytes = r.readDatagramBytes(datagram.offset, datagram.numberOfBytes)
# hereis how we compute the checksum
# print(sum(rawbytes[5:-3]))
if typeOfDatagram == '3':
datagram.read()
print (datagram.data)
continue
if typeOfDatagram == 'A':
datagram.read()
# for a in datagram.Attitude:
# print ("%.5f, %.3f, %.3f, %.3f, %.3f" % (r.to_timestamp(r.to_DateTime(a[0], a[1])), a[3], a[4], a[5], a[6]))
continue
if typeOfDatagram == 'C':
datagram.read()
continue
if typeOfDatagram == 'D':
datagram.read()
nadirBeam = int(datagram.NBeams / 2)
# print (("Nadir Depth: %.3f AcrossTrack %.3f TransducerDepth %.3f Checksum %s" % (datagram.Depth[nadirBeam], datagram.AcrossTrackDistance[nadirBeam], datagram.TransducerDepth, datagram.checksum)))
continue
if typeOfDatagram == 'f':
datagram.read()
if typeOfDatagram == 'H':
datagram.read()
if typeOfDatagram == 'i':
datagram.read()
continue
if typeOfDatagram == 'I':
datagram.read()
# print (datagram.installationParameters)
# print ("Lat: %.5f Lon: %.5f" % (datagram.Latitude, datagram.Longitude))
continue
if typeOfDatagram == 'n':
datagram.read()
continue
if typeOfDatagram == 'N':
datagram.read()
# print ("Raw Travel Times Recorded for %d beams" % datagram.NumReceiveBeams)
continue
if typeOfDatagram == 'O':
datagram.read()
continue
if typeOfDatagram == 'R':
datagram.read()
continue
if typeOfDatagram == 'U':
datagram.read()
continue
if typeOfDatagram == 'X':
datagram.read()
nadirBeam = int(datagram.NBeams / 2)
# print (("Nadir Depth: %.3f AcrossTrack %.3f TransducerDepth %.3f" % (datagram.Depth[nadirBeam], datagram.AcrossTrackDistance[nadirBeam], datagram.TransducerDepth)))
pingCount += 1
continue
if typeOfDatagram == 'Y':
datagram.read()
continue
print("Read Duration: %.3f seconds, pingCount %d" % (time.time() - start_time, pingCount)) # print the processing time. It is handy to keep an eye on processing performance.
r.rewind()
print("Complete reading ALL file :-)")
r.close()
class ALLReader:
'''class to read a Kongsberg EM multibeam .all file'''
ALLPacketHeader_fmt = '=LBBHLL'
ALLPacketHeader_len = struct.calcsize(ALLPacketHeader_fmt)
ALLPacketHeader_unpack = struct.Struct(ALLPacketHeader_fmt).unpack_from
def __init__(self, ALLfileName):
if not os.path.isfile(ALLfileName):
print ("file not found:", ALLfileName)
self.fileName = ALLfileName
self.fileptr = open(ALLfileName, 'rb')
self.fileSize = os.path.getsize(ALLfileName)
self.recordDate = ""
self.recordTime = ""
self.recordCounter=0
def __str__(self):
return pprint.pformat(vars(self))
def currentRecordDateTime(self):
'''return a python date object from the current datagram objects raw date and time fields '''
date_object = datetime.strptime(str(self.recordDate), '%Y%m%d') + timedelta(0,self.recordTime)
return date_object
def to_DateTime(self, recordDate, recordTime):
'''return a python date object from a split date and time record'''
date_object = datetime.strptime(str(recordDate), '%Y%m%d') + timedelta(0,recordTime)
return date_object
# def to_timestamp(self, dateObject):
# '''return a unix timestamp from a python date object'''
# return (dateObject - datetime(1970, 1, 1)).total_seconds()
def close(self):
'''close the current file'''
self.fileptr.close()
def rewind(self):
'''go back to start of file'''
self.fileptr.seek(0, 0)
def currentPtr(self):
'''report where we are in the file reading process'''
return self.fileptr.tell()
def moreData(self):
'''report how many more bytes there are to read from the file'''
return self.fileSize - self.fileptr.tell()
def readDatagramHeader(self):
'''read the common header for any datagram'''
try:
curr = self.fileptr.tell()
data = self.fileptr.read(self.ALLPacketHeader_len)
s = self.ALLPacketHeader_unpack(data)
numberOfBytes= s[0]
STX = s[1]
typeOfDatagram = chr(s[2])
EMModel = s[3]
RecordDate = s[4]
RecordTime = float(s[5]/1000.0)
self.recordDate = RecordDate
self.recordTime = RecordTime
# now reset file pointer
self.fileptr.seek(curr, 0)
# we need to add 4 bytes as the message does not contain the 4 bytes used to hold the size of the message
# trap corrupt datagrams at the end of a file. We see this in EM2040 systems.
if (curr + numberOfBytes + 4 ) > self.fileSize:
numberOfBytes = self.fileSize - curr - 4
typeOfDatagram = 'XXX'
return numberOfBytes + 4, STX, typeOfDatagram, EMModel, RecordDate, RecordTime
return numberOfBytes + 4, STX, typeOfDatagram, EMModel, RecordDate, RecordTime
except struct.error:
return 0,0,0,0,0,0
def readDatagramBytes(self, offset, byteCount):
'''read the entire raw bytes for the datagram without changing the file pointer. this is used for file conditioning'''
curr = self.fileptr.tell()
self.fileptr.seek(offset, 0)# move the file pointer to the start of the record so we can read from disc
data = self.fileptr.read(byteCount)
self.fileptr.seek(curr, 0)
return data
def getRecordCount(self):
'''read through the entire file as fast as possible to get a count of all records. useful for progress bars so user can see what is happening'''
count = 0
start = 0
end = 0
self.rewind()
numberOfBytes, STX, typeOfDatagram, EMModel, RecordDate, RecordTime = self.readDatagramHeader()
start = to_timestamp(to_DateTime(RecordDate, RecordTime))
self.rewind()
while self.moreData():
numberOfBytes, STX, typeOfDatagram, EMModel, RecordDate, RecordTime = self.readDatagramHeader()
self.fileptr.seek(numberOfBytes, 1)
count += 1
self.rewind()
end = to_timestamp(to_DateTime(RecordDate, RecordTime))
return count, start, end
def readDatagram(self):
'''read the datagram header. This permits us to skip datagrams we do not support'''
numberOfBytes, STX, typeOfDatagram, EMModel, RecordDate, RecordTime = self.readDatagramHeader()
self.recordCounter += 1
if typeOfDatagram == '3': # 3_EXTRA PARAMETERS DECIMAL 51
dg = E_EXTRA(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'A': # A ATTITUDE
dg = A_ATTITUDE(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'C': # C Clock
dg = C_CLOCK(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'D': # D DEPTH
dg = D_DEPTH(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'f': # f Raw Range
dg = f_RAWRANGE(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'h': # h Height, not to be confused with H_Heading!
dg = h_HEIGHT(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'I': # I Installation (Start)
dg = I_INSTALLATION(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'i': # i Installation (Stop)
dg = I_INSTALLATION(self.fileptr, numberOfBytes)
dg.typeOfDatagram = 'i' #override with the install stop code
return dg.typeOfDatagram, dg
if typeOfDatagram == 'n': # n ATTITUDE
dg = n_ATTITUDE(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'N': # N Angle and Travel Time
dg = N_TRAVELTIME(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'O': # O_QUALITYFACTOR
dg = O_QUALITYFACTOR(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'R': # R_RUNTIME
dg = R_RUNTIME(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'P': # P Position
dg = P_POSITION(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'U': # U Sound Velocity
dg = U_SVP(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'X': # X Depth
dg = X_DEPTH(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
if typeOfDatagram == 'Y': # Y_SeabedImage
dg = Y_SEABEDIMAGE(self.fileptr, numberOfBytes)
return dg.typeOfDatagram, dg
else:
dg = UNKNOWN_RECORD(self.fileptr, numberOfBytes, typeOfDatagram)
return dg.typeOfDatagram, dg
# self.fileptr.seek(numberOfBytes, 1)
###############################################################################
def loadInstallationRecords(self):
'''loads all the installation into lists'''
installStart = None
installStop = None
initialMode = None
datagram = None
self.rewind()
while self.moreData():
typeOfDatagram, datagram = self.readDatagram()
if (typeOfDatagram == 'I'):
installStart = self.readDatagramBytes(datagram.offset, datagram.numberOfBytes)
datagram.read()
break
if (typeOfDatagram == 'i'):
installStop = self.readDatagramBytes(datagram.offset, datagram.numberOfBytes)
if (initialMode == None and typeOfDatagram == 'R'):
datagram.read()
initialMode = datagram.DepthMode
self.rewind()
return installStart, installStop, initialMode
###############################################################################
def loadCenterFrequency(self):
'''determine the central frequency of the first record in the file'''
self.rewind()
while self.moreData():
typeOfDatagram, datagram = self.readDatagram()
if (typeOfDatagram == 'N'):
datagram.read()
centerFrequency = datagram.CentreFrequency[0]
break
self.rewind()
return centerFrequency
###############################################################################
def loadNavigation(self, firstRecordOnly=False):
'''loads all the navigation into lists'''
navigation = []
selectedPositioningSystem = None
self.rewind()
while self.moreData():
typeOfDatagram, datagram = self.readDatagram()
if (typeOfDatagram == 'P'):
datagram.read()
recDate = self.currentRecordDateTime()
if (selectedPositioningSystem == None):
selectedPositioningSystem = datagram.Descriptor
if (selectedPositioningSystem == datagram.Descriptor):
# for python 2.7
navigation.append([to_timestamp(recDate), datagram.Latitude, datagram.Longitude])
# for python 3.4
# navigation.append([recDate.timestamp(), datagram.Latitude, datagram.Longitude])
if firstRecordOnly: #we only want the first record, so reset the file pointer and quit
self.rewind()
return navigation
self.rewind()
return navigation
def getDatagramName(typeOfDatagram):
'''Convert the datagram type from the code to a user readable string. Handy for displaying to the user'''
#Multibeam Data
if (typeOfDatagram == 'D'):
return "D_Depth"
if (typeOfDatagram == 'X'):
return "XYZ_Depth"
if (typeOfDatagram == 'K'):
return "K_CentralBeam"
if (typeOfDatagram == 'F'):
return "F_RawRange"
if (typeOfDatagram == 'f'):
return "f_RawRange"
if (typeOfDatagram == 'N'):
return "N_RawRange"
if (typeOfDatagram == 'S'):
return "S_SeabedImage"
if (typeOfDatagram == 'Y'):
return "Y_SeabedImage"
if (typeOfDatagram == 'k'):
return "k_WaterColumn"
if (typeOfDatagram == 'O'):
return "O_QualityFactor"
# ExternalSensors
if (typeOfDatagram == 'A'):
return "A_Attitude"
if (typeOfDatagram == 'n'):
return "network_Attitude"
if (typeOfDatagram == 'C'):
return "C_Clock"
if (typeOfDatagram == 'h'):
return "h_Height"
if (typeOfDatagram == 'H'):
return "H_Heading"
if (typeOfDatagram == 'P'):
return "P_Position"
if (typeOfDatagram == 'E'):
return "E_SingleBeam"
if (typeOfDatagram == 'T'):
return "T_Tide"
# SoundSpeed
if (typeOfDatagram == 'G'):
return "G_SpeedSoundAtHead"
if (typeOfDatagram == 'U'):
return "U_SpeedSoundProfile"
if (typeOfDatagram == 'W'):
return "W_SpeedSOundProfileUsed"
# Multibeam parameters
if (typeOfDatagram == 'I'):
return "I_Installation_Start"
if (typeOfDatagram == 'i'):
return "i_Installation_Stop"
if (typeOfDatagram == R):
return "R_Runtime"
if (typeOfDatagram == J):
return "J_TransducerTilt"
if (typeOfDatagram == '3'):
return "3_ExtraParameters"
# PU information and status
if (typeOfDatagram == '0'):
return "0_PU_ID"
if (typeOfDatagram == '1'):
return "1_PU_Status"
if (typeOfDatagram == 'B'):
return "B_BIST_Result"
###############################################################################
class cBeam:
def __init__(self, beamDetail, angle):
self.sortingDirection = beamDetail[0]
self.detectionInfo = beamDetail[1]
self.numberOfSamplesPerBeam = beamDetail[2]
self.centreSampleNumber = beamDetail[3]
self.sector = 0
self.takeOffAngle = angle # used for ARC computation
self.sampleSum = 0 # used for backscatter ARC computation process
self.samples = []
###############################################################################
class A_ATTITUDE_ENCODER:
def __init__(self):
self.data = 0
def encode(self, recordsToAdd, counter):
'''Encode a list of attitude records where the format is timestamp, roll, pitch, heave heading'''
if (len(recordsToAdd) == 0):
return
fullDatagram = bytearray()
header_fmt = '=LBBHLLHHH'
header_len = struct.calcsize(header_fmt)
rec_fmt = "HHhhhHB"
rec_len = struct.calcsize(rec_fmt)
footer_fmt = '=BH'
footer_len = struct.calcsize(footer_fmt)
STX = 2
typeOfDatagram = 65
model = 2045
systemDescriptor = 0
systemDescriptor = set_bit(systemDescriptor, 0) #set heading is ENABLED (go figure!)
serialNumber = 999
numEntries = len(recordsToAdd)
fullDatagramByteCount = header_len + (rec_len*len(recordsToAdd)) + footer_len
firstRecordTimestamp = float(recordsToAdd[0][0]) #we need to know the first record timestamp as all observations are milliseconds from that time
firstRecordDate = from_timestamp(firstRecordTimestamp)
recordDate = int(dateToKongsbergDate(firstRecordDate))
recordTime = int(dateToSecondsSinceMidnight(firstRecordDate)*1000)
# we need to deduct 4 bytes as the field does not account for the 4-byte message length data which precedes the message
try:
header = struct.pack(header_fmt, fullDatagramByteCount-4, STX, typeOfDatagram, model, recordDate, recordTime, counter, serialNumber, numEntries)
except:
print ("error encoding attitude")
# header = struct.pack(header_fmt, fullDatagramByteCount-4, STX, typeOfDatagram, model, recordDate, recordTime, counter, serialNumber, numEntries)
fullDatagram = fullDatagram + header
# now pack avery record from the list
for record in recordsToAdd:
timeMillisecs = round((float(record[0]) - firstRecordTimestamp) * 1000) # compute the millisecond offset of the record from the first record in the datagram
sensorStatus = 0
roll = float(record[1])
pitch= float(record[2])
heave= float(record[3])
heading = float(record[4])
try:
bodyRecord = struct.pack(rec_fmt, timeMillisecs, sensorStatus, int(roll*100), int(pitch*100), int(heave*100), int(heading*100), systemDescriptor)
except:
print ("error encoding attitude")
bodyRecord = struct.pack(rec_fmt, timeMillisecs, sensorStatus, int(roll*100), int(pitch*100), int(heave*100), int(heading*100), systemDescriptor)
fullDatagram = fullDatagram + bodyRecord
# now do the footer
# systemDescriptor = set_bit(systemDescriptor, 1) #set roll is DISABLED
# systemDescriptor = set_bit(systemDescriptor, 2) #set pitch is DISABLED
# systemDescriptor = set_bit(systemDescriptor, 3) #set heave is DISABLED
# systemDescriptor = set_bit(systemDescriptor, 4) #set SENSOR as system 2
# systemDescriptor = 30
ETX = 3
checksum = sum(fullDatagram[5:]) % 65536
footer = struct.pack('=BH', ETX, checksum)
fullDatagram = fullDatagram + footer
# TEST THE CRC CODE pkpk
# c = CRC16()
# chk = c.calculate(fullDatagram)
return fullDatagram
###############################################################################
class A_ATTITUDE:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = 'A'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.data = ""
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHHH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
s = rec_unpack(self.fileptr.read(rec_len))
# self.numberOfBytes= s[0]
self.STX = s[1]
self.typeOfDatagram = chr(s[2])
self.EMModel = s[3]
self.RecordDate = s[4]
self.Time = float(s[5]/1000.0)
self.Counter = s[6]
self.SerialNumber = s[7]
self.NumberEntries= s[8]
rec_fmt = '=HHhhhH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack
# we need to store all the attitude data in a list
self.Attitude = [0 for i in range(self.NumberEntries)]
i = 0
while i < self.NumberEntries:
data = self.fileptr.read(rec_len)
s = rec_unpack(data)
# time,status,roll,pitch,heave,heading
self.Attitude[i] = [self.RecordDate, self.Time + float (s[0]/1000.0), s[1], s[2]/100.0, s[3]/100.0, s[4]/100.0, s[5]/100.0]
i = i + 1
rec_fmt = '=BBH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
data = self.fileptr.read(rec_len)
s = rec_unpack(data)
self.systemDescriptor = s[0]
self.ETX = s[1]
self.checksum = s[2]
###############################################################################
class C_CLOCK:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = 'C'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.data = ""
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHHLLBBH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack
# bytesRead = rec_len
s = rec_unpack(self.fileptr.read(rec_len))
# self.numberOfBytes= s[0]
self.STX = s[1]
self.typeOfDatagram = chr(s[2])
self.EMModel = s[3]
self.RecordDate = s[4]
self.time = float(s[5] / 1000.0)
self.ClockCounter = s[6]
self.SerialNumber = s[7]
self.ExternalDate = s[8]
self.ExternalTime = s[9] / 1000.0
self.PPS = s[10]
self.ETX = s[11]
self.checksum = s[12]
def __str__(self):
if self.PPS == 0:
ppsInUse = "PPS NOT in use"
else:
ppsInUse = "PPS in use"
s = '%d,%d,%.3f,%.3f,%.3f,%s' %(self.RecordDate, self.ExternalDate, self.time, self.ExternalTime, self.time - self.ExternalTime, ppsInUse)
return s
###############################################################################
class D_DEPTH:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = 'D'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.data = ""
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHHHHHBBBBH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
s = rec_unpack(self.fileptr.read(rec_len))
# self.numberOfBytes= s[0]
self.STX = s[1]
self.typeOfDatagram = chr(s[2])
self.EMModel = s[3]
self.RecordDate = s[4]
self.Time = float(s[5]/1000.0)
self.Counter = s[6]
self.SerialNumber = s[7]
self.Heading = float (s[8] / float (100))
self.SoundSpeedAtTransducer = float (s[9] / float (10))
self.TransducerDepth = float (s[10] / float (100))
self.MaxBeams = s[11]
self.NBeams = s[12]
self.ZResolution = float (s[13] / float (100))
self.XYResolution = float (s[14] / float (100))
self.SampleFrequency = s[15]
self.Depth = [0 for i in range(self.NBeams)]
self.AcrossTrackDistance = [0 for i in range(self.NBeams)]
self.AlongTrackDistance = [0 for i in range(self.NBeams)]
self.BeamDepressionAngle = [0 for i in range(self.NBeams)]
self.BeamAzimuthAngle = [0 for i in range(self.NBeams)]
self.Range = [0 for i in range(self.NBeams)]
self.QualityFactor = [0 for i in range(self.NBeams)]
self.LengthOfDetectionWindow = [0 for i in range(self.NBeams)]
self.Reflectivity = [0 for i in range(self.NBeams)]
self.BeamNumber = [0 for i in range(self.NBeams)]
# now read the variable part of the Record
if self.EMModel < 700 :
rec_fmt = '=H3h2H2BbB'
else:
rec_fmt = '=4h2H2BbB'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack
i = 0
while i < self.NBeams:
data = self.fileptr.read(rec_len)
s = rec_unpack(data)
self.Depth[i] = float (s[0] / float (100))
self.AcrossTrackDistance[i] = float (s[1] / float (100))
self.AlongTrackDistance[i] = float (s[2] / float (100))
self.BeamDepressionAngle[i] = float (s[3] / float (100))
self.BeamAzimuthAngle[i] = float (s[4] / float (100))
self.Range[i] = float (s[5] / float (100))
self.QualityFactor[i] = s[6]
self.LengthOfDetectionWindow[i] = s[7]
self.Reflectivity[i] = float (s[8] / float (100))
self.BeamNumber[i] = s[9]
# now do some sanity checks. We have examples where the Depth and Across track values are NaN
if (math.isnan(self.Depth[i])):
self.Depth[i] = 0
if (math.isnan(self.AcrossTrackDistance[i])):
self.AcrossTrackDistance[i] = 0
if (math.isnan(self.AlongTrackDistance[i])):
self.AlongTrackDistance[i] = 0
i = i + 1
rec_fmt = '=bBH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
data = self.fileptr.read(rec_len)
s = rec_unpack(data)
self.RangeMultiplier = s[0]
self.ETX = s[1]
self.checksum = s[2]
###############################################################################
def encode(self):
'''Encode a Depth D datagram record'''
header_fmt = '=LBBHLLHHHHHBBBBH'
header_len = struct.calcsize(header_fmt)
fullDatagram = bytearray()
# now read the variable part of the Record
if self.EMModel < 700 :
rec_fmt = '=H3h2H2BbB'
else:
rec_fmt = '=4h2H2BbB'
rec_len = struct.calcsize(rec_fmt)
footer_fmt = '=BBH'
footer_len = struct.calcsize(footer_fmt)
fullDatagramByteCount = header_len + (rec_len*self.NBeams) + footer_len
# pack the header
recordTime = int(dateToSecondsSinceMidnight(from_timestamp(self.Time))*1000)
header = struct.pack(header_fmt,
fullDatagramByteCount-4,
self.STX,
ord(self.typeOfDatagram),
self.EMModel,
self.RecordDate,
recordTime,
int(self.Counter),
int(self.SerialNumber),
int(self.Heading * 100),
int(self.SoundSpeedAtTransducer * 10),
int(self.TransducerDepth * 100),
int(self.MaxBeams),
int(self.NBeams),
int(self.ZResolution * 100),
int(self.XYResolution * 100),
int(self.SampleFrequency))
fullDatagram = fullDatagram + header
header_fmt = '=LBBHLLHHHHHBBBBH'
# pack the beam summary info
for i in range (self.NBeams):
bodyRecord = struct.pack(rec_fmt,
int(self.Depth[i] * 100),
int(self.AcrossTrackDistance[i] * 100),
int(self.AlongTrackDistance[i] * 100),
int(self.BeamDepressionAngle[i] * 100),
int(self.BeamAzimuthAngle[i] * 100),
int(self.Range[i] * 100),
self.QualityFactor[i],
self.LengthOfDetectionWindow[i],
int(self.Reflectivity[i] * 100),
self.BeamNumber[i])
fullDatagram = fullDatagram + bodyRecord
tmp = struct.pack('=b', self.RangeMultiplier)
fullDatagram = fullDatagram + tmp
# now pack the footer
# systemDescriptor = 1
ETX = 3
checksum = sum(fullDatagram[5:]) % 65536
footer = struct.pack('=BH', ETX, checksum)
fullDatagram = fullDatagram + footer
return fullDatagram
###############################################################################
class E_EXTRA:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = '3'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.ExtraData = ""
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHHH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
s = rec_unpack(self.fileptr.read(rec_len))
# self.numberOfBytes= s[0]
self.STX = s[1]
self.typeOfDatagram = chr(s[2])
self.EMModel = s[3]
self.RecordDate = s[4]
self.Time = float(s[5]/1000.0)
self.Counter = s[6]
self.SerialNumber = s[7]
self.ContentIdentifier= s[8]
# now read the variable position part of the Record
if self.numberOfBytes % 2 != 0:
bytesToRead = self.numberOfBytes - rec_len - 5 # 'sBBH'
else:
bytesToRead = self.numberOfBytes - rec_len - 4 # 'sBH'
# now read the block of data whatever it may contain
self.data = self.fileptr.read(bytesToRead)
# # now spare byte only if necessary
# if self.numberOfBytes % 2 != 0:
# self.fileptr.read(1)
# read an empty byte
self.fileptr.read(1)
# now read the footer
self.ETX, self.checksum = readFooter(self.numberOfBytes, self.fileptr)
###############################################################################
class f_RAWRANGE:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = 'f'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.data = ""
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHH HHLl4H'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack
bytesRead = rec_len
s = rec_unpack(self.fileptr.read(rec_len))
# self.numberOfBytes= s[0]
self.STX = s[1]
self.typeOfDatagram = chr(s[2])
self.EMModel = s[3]
self.RecordDate = s[4]
self.Time = float(s[5]/1000.0)
self.PingCounter = s[6]
self.SerialNumber = s[7]
self.NumTransmitSector = s[8]
self.NumReceiveBeams = s[9]
self.SampleFrequency = float (s[10] / 100)
self.ROVDepth = s[11]
self.SoundSpeedAtTransducer = s[12] / 10
self.MaxBeams = s[13]
self.Spare1 = s[14]
self.Spare2 = s[15]
self.TiltAngle = [0 for i in range(self.NumTransmitSector)]
self.FocusRange = [0 for i in range(self.NumTransmitSector)]
self.SignalLength = [0 for i in range(self.NumTransmitSector)]
self.SectorTransmitDelay = [0 for i in range(self.NumTransmitSector)]
self.CentreFrequency = [0 for i in range(self.NumTransmitSector)]
self.MeanAbsorption = [0 for i in range(self.NumTransmitSector)]
self.SignalWaveformID = [0 for i in range(self.NumTransmitSector)]
self.TransmitSectorNumberTX = [0 for i in range(self.NumTransmitSector)]
self.SignalBandwidth = [0 for i in range(self.NumTransmitSector)]
self.BeamPointingAngle = [0 for i in range(self.NumReceiveBeams)]
self.TransmitSectorNumber = [0 for i in range(self.NumReceiveBeams)]
self.DetectionInfo = [0 for i in range(self.NumReceiveBeams)]
self.DetectionWindow = [0 for i in range(self.NumReceiveBeams)]
self.QualityFactor = [0 for i in range(self.NumReceiveBeams)]
self.DCorr = [0 for i in range(self.NumReceiveBeams)]
self.TwoWayTravelTime = [0 for i in range(self.NumReceiveBeams)]
self.Reflectivity = [0 for i in range(self.NumReceiveBeams)]
self.RealtimeCleaningInformation = [0 for i in range(self.NumReceiveBeams)]
self.Spare = [0 for i in range(self.NumReceiveBeams)]
self.BeamNumber = [0 for i in range(self.NumReceiveBeams)]
# # now read the variable part of the Transmit Record
rec_fmt = '=hHLLLHBB'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack
for i in range(self.NumTransmitSector):
data = self.fileptr.read(rec_len)
bytesRead += rec_len
s = rec_unpack(data)
self.TiltAngle[i] = float (s[0]) / 100.0
self.FocusRange[i] = s[1] / 10
self.SignalLength[i] = s[2]
self.SectorTransmitDelay[i] = s[3]
self.CentreFrequency[i] = s[4]
self.SignalBandwidth[i] = s[5]
self.SignalWaveformID[i] = s[6]
self.TransmitSectorNumberTX[i] = s[7]
# now read the variable part of the recieve record
rx_rec_fmt = '=hHBbBBhH'
rx_rec_len = struct.calcsize(rx_rec_fmt)
rx_rec_unpack = struct.Struct(rx_rec_fmt).unpack
for i in range(self.NumReceiveBeams):
data = self.fileptr.read(rx_rec_len)
rx_s = rx_rec_unpack(data)
bytesRead += rx_rec_len
self.BeamPointingAngle[i] = float (rx_s[0]) / 100.0
self.TwoWayTravelTime[i] = float (rx_s[1]) / (4 * self.SampleFrequency)
self.TransmitSectorNumber[i] = rx_s[2]
self.Reflectivity[i] = rx_s[3] / 2.0
self.QualityFactor[i] = rx_s[4]
self.DetectionWindow[i] = rx_s[5]
self.BeamNumber[i] = rx_s[6]
rec_fmt = '=BBH'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
data = self.fileptr.read(rec_len)
s = rec_unpack(data)
self.ETX = s[1]
self.checksum = s[2]
###############################################################################
def encode(self):
'''Encode a Depth f datagram record'''
systemDescriptor = 1
header_fmt = '=LBBHLLHH HHLl4H'
header_len = struct.calcsize(header_fmt)
fullDatagram = bytearray()
# # now read the variable part of the Transmit Record
rec_fmt = '=hHLLLHBB'
rec_len = struct.calcsize(rec_fmt)
# now read the variable part of the recieve record
rx_rec_fmt = '=hHBbBBhHB'
rx_rec_len = struct.calcsize(rx_rec_fmt)
footer_fmt = '=BH'
footer_len = struct.calcsize(footer_fmt)
fullDatagramByteCount = header_len + (rec_len*self.NumTransmitSector) + (rx_rec_len*self.NumReceiveBeams) + footer_len
# pack the header
recordTime = int(dateToSecondsSinceMidnight(from_timestamp(self.Time))*1000)
header = struct.pack(header_fmt,
fullDatagramByteCount-4,
self.STX,
ord(self.typeOfDatagram),
self.EMModel,
self.RecordDate,
recordTime,
self.PingCounter,
self.SerialNumber,
self.NumTransmitSector,
self.NumReceiveBeams,
int(self.SampleFrequency * 100),
self.ROVDepth,
int(self.SoundSpeedAtTransducer * 10),
self.MaxBeams,
self.Spare1,
self.Spare2)
fullDatagram = fullDatagram + header
for i in range (self.NumTransmitSector):
sectorRecord = struct.pack(rec_fmt,
int(self.TiltAngle[i] * 100),
int(self.FocusRange[i] * 10),
self.SignalLength[i],
self.SectorTransmitDelay[i],
self.CentreFrequency[i],
self.SignalBandwidth[i],
self.SignalWaveformID[i],
self.TransmitSectorNumberTX[i])
fullDatagram = fullDatagram + sectorRecord
# pack the beam summary info
for i in range (self.NumReceiveBeams):
bodyRecord = struct.pack(rx_rec_fmt,
int(self.BeamPointingAngle[i] * 100.0),
int(self.TwoWayTravelTime[i] * (4 * self.SampleFrequency)),
self.TransmitSectorNumber[i],
int(self.Reflectivity[i] * 2.0),
self.QualityFactor[i],
self.DetectionWindow[i],
self.BeamNumber[i],
self.Spare1,
systemDescriptor)
fullDatagram = fullDatagram + bodyRecord
# now pack the footer
ETX = 3
checksum = sum(fullDatagram[5:]) % 65536
footer = struct.pack('=BH', ETX, checksum)
fullDatagram = fullDatagram + footer
return fullDatagram
###############################################################################
class h_HEIGHT:
def __init__(self, fileptr, numberOfBytes):
self.typeOfDatagram = 'h'
self.offset = fileptr.tell()
self.numberOfBytes = numberOfBytes
self.fileptr = fileptr
self.fileptr.seek(numberOfBytes, 1)
self.data = ""
self.Height = 0
self.HeightType = 0
def read(self):
self.fileptr.seek(self.offset, 0)
rec_fmt = '=LBBHLLHHlB'
rec_len = struct.calcsize(rec_fmt)
rec_unpack = struct.Struct(rec_fmt).unpack_from
s = rec_unpack(self.fileptr.read(rec_len))
self.STX = s[1]
self.typeOfDatagram = chr(s[2])