-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadEM_kmall.m
980 lines (842 loc) · 37.1 KB
/
readEM_kmall.m
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
% play with reading the kmall files
% used Liz's parsing codes as a start but have pulled in files from the
% CoFFee code to read the new format and better grasp structure of files
%% manual entry of parameters
% sonar parameters
%maxWCSampIdx = 1060; % setting a fixed watercolumn buffer in meters
% (this is 600 in Liz's code)
% maxWCSampIdx needs to be set for the particular data set
% outer beams should have maximum number of samples based on plotted
% patterns for number of samples so can use the following after
% metadata is read:
% numSamps=wcdat(idgm).beamData_p.numSampleData;
% maxWCSampIdx=numSamps(1);
RxBeamWidth=1; % setting receive beamwidth arbitrarily (need to find where to read)
cartspeed=0.05; % setting velocity for along track direction (0.34 max at UNH)
% operations choices
dotest=1;
describe_datagrams=1;
% always reads kmall file info and kmwcd data
check_dgmtimes=1; % always computes times, this just controls plots
vizping=1;
do3Dviz=1;
savefiles=1;
% file specific choices
startatping=1;
endatping=500;
plotdepthmax=50; % 7 for UNH, 50 or 75 for Sentry
outdir='../MBES_mat_files/';
%expcode='geoA_Apr15_single_15Hz_slow_hot';
%expcode='secondApr15runs';
expcode='sentry_mbes';
%expcode='';
%% open an EM data file
filelocation = '../../OET/sentry_NA165/';
%filelocation = '../../OET/';
%filelocation = '../../UNH_data/';
%filelocation = '../MBES_raw_data/';
%filelocation = ['../MBES_raw_data/' expcode '/'];
%filelocation = 'e:UNH_tank_experiment\EM2040\UNH_EM2040_40_Apr_13_2023\';
%filelocation = 'e:UNH_tank_experiment\EM2040\UNH_EM2040_40_Apr2023_geoA\';
%filename='0009_20200918_094230.kmall';
%filename='0010_20200918_095915.kmall';
%filename='0004_20230406_123411.kmall'; % apr 6 - flow setup issues
%filename='0004_20230406_123411.kmwcd'; % too long (3407 pings) to read
% in one gulp
%filename='0017_20230405_163521.kmwcd'; % apr 5 - hot flow good
%filename='0005_20230413_141335.kmwcd';
%filename='0006_20230413_143146.kmwcd';
%filename='0007_20230413_143346.kmwcd';
%filename='0008_20230413_143848.kmwcd';
%filename='0009_20230413_143947.kmwcd';
%filename='0010_20230413_144205.kmwcd';
%filename='0011_20230413_144251.kmwcd';
%filename='0000_20230413_160721.kmwcd';
%filename='0001_20230413_160826.kmwcd';
%filename='0002_20230413_161101.kmwcd';
%filename='0003_20230413_161138.kmwcd';
%filename='0004_20230413_161651.kmwcd';
%filename='0017_20230405_163521.kmwcd'; % early run
%filename='0020_20230413_171151.kmwcd'; % apr13-575Hz
%filename='0021_20230413_171231.kmwcd'; % apr13-B-hot flow-300 kHz
%filename='0022_20230413_172311.kmwcd'; % apr13-B-hot flow-200 kHz-gas
%filename='0023_20230413_172348.kmwcd'; % apr13-B-hot flow-200 kHz-gas
%filename='0024_20230413_172544.kmwcd'; % apr13-320Hz
%filename='0024_20230413_172544.kmwcd'; % apr13-B-hot flow-600 kHz
%filename='0025_20230413_172618.kmwcd'; % apr13-B-
%filename='0026_20230413_174909.kmwcd'; % apr13-? flow-? kHz
%filename='0027_20230413_175000.kmwcd'; titlestr='apr13-B-? flow-200kHz';
% try 46 (geoC 400kHz looks like good plume)
%filename='0046_20230413_180331.kmwcd';
% try 48
%filename='0048_20230413_180417.kmwcd';
%filename2='0048_20230413_180417.kmall';
%filename='0053_20230413_181816.kmwcd';
%filename='0075_20230414_193631.kmwcd'; % Liz got plume but I don't :-(
%filename2='0075_20230414_193631.kmall'; % Liz got plume but I don't :-(
%filename='0077_20230413_190346.kmwcd';
%filename='0078_20230413_190400.kmwcd';
%filename='0079_20230413_190422.kmwcd';
%filename='0081_20230413_190450.kmwcd';
%filename='0065_20230413_184653.kmwcd';
%filename='0024_20230414_163451.kmwcd'; % geoA 400hz
%filename='0023_20230414_163427.kmwcd';
%filename='0046_20230414_175425.kmwcd';
%filename='0048_20230414_175530.kmwcd';
%filename='0000_20230415_123519.kmwcd'; % geoA hot slow 200Hz
%filename='0001_20230415_123600.kmwcd'; % geoA hot slow 200Hz
% no 0002 file
%filename='0003_20230415_124320.kmwcd'; % geoA hot slow 200Hz
%filename='0004_20230415_124403.kmwcd'; % geoA hot slow 200Hz
%filename='0005_20230415_124611.kmwcd'; % geoA hot slow 300Hz
%filename='0006_20230415_124653.kmwcd'; % geoA hot slow 300Hz
%filename='0007_20230415_124807.kmwcd'; % geoA hot slow 400Hz
%filename='0008_20230415_124857.kmwcd'; % geoA hot slow 400Hz
%filename='0000_20230415_193500.kmwcd'; % calibration
%filename='0003_20230415_200249.kmwcd';
%filename='0004_20230415_204544.kmwcd'; % test file for student work
%filename2='0004_20230415_204544.kmall'; % absolute calibration speed test - different setup
%filename='0017_20230405_163521.kmwcd'; % early run
%filename2='0017_20230405_163521.kmall';
%filename='0001_20230415_120215.kmwcd'; % early run
%filename2='0001_20230415_120215.kmall';
%filename='0020_20230415_122405.kmwcd';
%filename2='0020_20230415_122405.kmall';
%filename='0004_20230415_124403.kmwcd';
%filename2='0004_20230415_124403.kmall';
%filename='0004_20230415_204544.kmwcd';
%filename2=[];
%filename='20240521_051137_sentry.kmwcd';
%filename2=[];
%filename='0056_20230414_181435.kmwcd';
%filename2='0056_20230414_181435.kmall';
%filename='0048_20230413_180417.kmwcd';
%filename2='0048_20230413_180417.kmall';
%filename='0013_20230414_151949.kmwcd';
%filename2='0013_20230414_151949.kmall';
%filename='0011_20230414_151858.kmwcd';
%filename2='0011_20230414_151858.kmall';
%filename='20240919_004509_sentry.kmwcd';
%filename='20240919_013335_sentry.kmwcd';
%filename='20240919_015708_sentry.kmwcd';
filename='20240919_015519_sentry.kmwcd';
filename2=[];
%filename='20240918_214503_sentry.kmwcd';
%filename2='20240918_214503_sentry.kmall';
fname = fullfile(filelocation,filename);
fprintf('reading file: %s \n',fname)
% open selected file
fid = fopen(fname,'r');
%% test reading the data file
% this part of code copied from CoFFee
% Authors: Alex Schimel (NGU, [email protected]) and Yoann
% Ladroit (NIWA, [email protected])
% 2017-2021; Last revision: 20-08-2021
if dotest
fprintf('Initial header info in file:\n')
% Datagram length in bytes. The length field at the start (4 bytes)
% and end of the datagram (4 bytes) are included in the length count.
out_struct.numBytesDgm = fread(fid,1,'uint32');
fprintf('size of next datagram: %d\n',out_struct.numBytesDgm)
% Multi beam datagram type definition, e.g. #AAA
out_struct.dgmType = fscanf(fid,'%c',4);
fprintf('datagram type: %s\n',out_struct.dgmType)
% Datagram version.
out_struct.dgmVersion = fread(fid,1,'uint8');
fprintf('datagram version: %d\n',out_struct.dgmVersion)
% System ID. Parameter used for separating datagrams from different
% echosounders if more than one system is connected to SIS/K-Controller.
out_struct.systemID = fread(fid,1,'uint8');
fprintf('system ID: %d\n',out_struct.systemID)
% Echo sounder identity, e.g. 124, 304, 712, 2040, 2045 (EM 2040C)
out_struct.echoSounderID = fread(fid,1,'uint16');
fprintf('echo sounder identity: %d\n',out_struct.echoSounderID)
% UTC time in seconds. Epoch 1970-01-01. time_nanosec part to be added for
% more exact time.
out_struct.time_sec = fread(fid,1,'uint32');
fprintf('time seconds: %d\n',out_struct.dgmVersion)
% Nano seconds remainder. time_nanosec part to be added to time_sec for
% more exact time.
out_struct.time_nanosec = fread(fid,1,'uint32');
fprintf('time nanoseconds: %d\n',out_struct.time_nanosec)
fprintf('\n')
% close file as next steps will reopen
fclose(fid);
end % dotest
%% Call CFF function to get datagram structure in kmall file
KMALLfileinfo = CFF_kmall_file_info(fname);
% * |KMALLfileinfo|: structure about datagrams in KMALLfilename, with fields:
% * |file_name|: input file name
% * |fileSize|: file size in bytes
% * |dgm_num|: number of datagram in file
% * |dgm_type_code|: datagram type as string, e.g. '#IIP' (Kongsberg .kmall format)
% * |dgm_type_text|: datagram type description (Kongsberg .kmall format)
% * |dgm_type_version|: version for this type of datagram, as int (Kongsberg .kmall format)
% * |dgm_counter|: counter for this type and version of datagram in the
% file. There should not be multiple versions of a same type in
% a same file, but we never know...
% * |dgm_start_pif|: position of beginning of datagram in file
% * |dgm_size|: datagram size in bytes
% * |dgm_sys_ID|: System ID. Parameter used for separating datagrams from different echosounders.
% * |dgm_EM_ID|: Echo sounder identity, e.g. 124, 304, 712, 2040, 2045 (EM 2040C)
% * |sync_counter|: number of bytes found between this datagram and the
% previous one (any number different than zero indicates a sync error)
% * |date_time|: datagram date in datetime format
% * |parsed|: flag for whether the datagram has been parsed. Initiated
% at 0 at this stage. To be later turned to 1 for parsing.
% structure also has
% * |list_dgm_type|: this has the unique dgm_type_codes
% * |list_dgm_counter|: not sure if counts of instances?
%% explore what is in the kmall file
if describe_datagrams
figure(3)
plot(KMALLfileinfo.dgm_num,'.')
title('counting the datagrams in the file')
xlabel('count')
ylabel('datagram number in file')
fprintf('min dgm_num= %d; max dgm_num=%d\n',...
min(KMALLfileinfo.dgm_num),max(KMALLfileinfo.dgm_num))
if isfield(KMALLfileinfo,'sync_counter')
fprintf('min sync_counter= %d; max sync_counter=%d\n',...
min(KMALLfileinfo.sync_counter),max(KMALLfileinfo.sync_counter))
elseif isfield(KMALLfileinfo,'syncCounter')
fprintf('min sync_counter= %d; max sync_counter=%d\n',...
min(KMALLfileinfo.syncCounter),max(KMALLfileinfo.syncCounter))
else
fprintf('invalid sync counter field name in KMALLfileinfo\n')
end
fprintf('min parsed= %d; max parsed=%d\n',...
min(KMALLfileinfo.parsed),max(KMALLfileinfo.parsed))
figure(4)
b1=barh(KMALLfileinfo.list_dgm_counter,...
'FaceColor',[0.7 0.9 0.7],'EdgeColor',[0.7 0.9 0.7]);
yticklabels(KMALLfileinfo.list_dgm_type)
ylabel('Datagram Types')
xlabel('Counts')
datalabels=string(b1(1).YData);
datalabelpos=max(KMALLfileinfo.list_dgm_counter)+2;
potdatalabels={'IIP','IOP','SVP','SKM','MWC','SVT','CHE'};
fullinfolabellist={'IIP=Installation parameters and sensor setup',...
'IOP= Runtime parameters as chosen by operator',...
'SVP= sensor data from sound velocity profile or CTD',...
'SKM= sensor KM binar sensor format',...
'MWC= Multibeam water column datagram', ...
'SVT= sensor data for sound velocity at transducer',...
'CHE= Compatibility data for heave',...
'SPO= sensor data for position',...
'CPO= Compatibility data for position',...
'MRZ= Multibeam raw range and depth datagram',...
'SCL= sensor data from clock'};
%infolabels
if sscanf(version('-release'),'%d')>2019
text(datalabelpos*ones(size(yticklabels)),b1(1).XEndPoints,datalabels,'VerticalAlignment','middle')
%text(2000*ones(size(yticklabels)),b1(1).XEndPoints,infolabels,'VerticalAlignment','middle')
else
text(datalabelpos*ones(size(yticklabels)),b1(1).XData,datalabels,'VerticalAlignment','middle')
%text(2000*ones(size(yticklabels)),b1(1).XData,infolabels,'VerticalAlignment','middle')
end
end % describe_datagrams
%% now we want to read some datagrams and figure out what to do with the data!
% this should read all the meta data
KMALLdata = CFF_read_kmall(fname);
% 5/31/23 - try making a cell with both .kmwcd and .kmall names
%fnamepair{1,1}=fname;
%fnamepair{2,1}=fname2;
%KMALLdata = CFF_read_kmall(fnamepair);
% the second version will only work if I upgrade the version to higher than
% 2020a
% now have a set of stuctures in KMALLdata that contain the datagrams for
% each type plus an info strucure
%% get the non-water column information
if ~isempty(filename2)
fname2 = fullfile(filelocation,filename2);
KMALLdata2 = CFF_read_kmall(fname2);
end % isempty
%% start parsing out the water column data
wcdat=KMALLdata.EMdgmMWC; % water column data
% next step - convert datagram time to matlab datenumber
fprintf('start date = %s \n',...
datestr(datetime(wcdat(1).header.time_sec,'ConvertFrom','posixtime')))
fprintf('end date = %s \n',...
datestr(datetime(wcdat(end).header.time_sec,'ConvertFrom','posixtime')))
fprintf('central frequency of center sector = %f \n',wcdat(1).sectorData(1).centreFreq_Hz)
% Note: might be able to replace this section with with four lines that
% just index into the single number, e.g. "wcdat.header.time_sec"
% Second Note: pingcount is really an incremeted label that may or may not
% start at 1 ==> subtract to get number of pings between two times
totalNdgm=length(wcdat);
% check user set limits on what to read and reset accordingly
startDgm=startatping;
endDgm=endatping;
if endDgm>totalNdgm
endDgm=totalNdgm;
fprintf('User setting (%d) for end ping exceeds number of pings (%d) \n',...
endatping,totalNdgm)
end
Ndgm=endDgm-startDgm+1;
fprintf('reading %d pings from ping %d to ping %d \n',...
Ndgm,startDgm,endDgm)
%%dgmtimes=NaT(Ndgm,1);
%numOfDgms=zeros(Ndgm,1);
%dgmNum=zeros(Ndgm,1);
%posixtimes=zeros(Ndgm,1);
%pingcount=zeros(Ndgm,1);
%tnanosec=zeros(Ndgm,1);
%ckNrx=zeros(Ndgm,1);
%for i=1:Ndgm
% dgmtimes(i)=datetime(wcdat(i).header.time_sec,'ConvertFrom','posixtime');
% posixtimes(i)=wcdat(i).header.time_sec;
% tnanosec(i)=wcdat(i).header.time_nanosec;
% numOfDgms(i)= wcdat(i).partition.numOfDgms;
% dgmNum(i)= wcdat(i).partition.dgmNum;
% pingcount(i)=wcdat(i).cmnPart.pingCnt;
% ckNrx(i)=wcdat(i).rxInfo.numBeams;
%end
ckNrx=arrayfun(@(x) x.rxInfo.numBeams,wcdat);
if abs(mean(diff(ckNrx)))>0
fprintf('WARNING: inconsistent number of beams across datagrams\n')
end
posixtimes=arrayfun(@(x) x.header.time_sec,wcdat);
tnanosec=arrayfun(@(x) x.header.time_nanosec,wcdat);
numOfDgms= arrayfun(@(x) x.partition.numOfDgms,wcdat);
dgmNum= arrayfun(@(x) x.partition.dgmNum,wcdat);
pingcount=arrayfun(@(x) x.cmnPart.pingCnt,wcdat);
numbeams=arrayfun(@(x) x.rxInfo.numBeams,wcdat);
%
ckNumSectors=arrayfun(@(x) x.txInfo.numTxSectors,wcdat);
%dgmtimes=arrayfun(@(x) datetime(x.header.time_sec,'ConvertFrom','posixtime','format','yyyy-MM-dd HH:mm:ss.SSS'),wcdat);
% time in seconds not sufficient for accurate timing
dgmsec=arrayfun(@(x) datetime(x.header.time_sec,'ConvertFrom','posixtime'),wcdat);
dgmnanosec=arrayfun(@(x) x.header.time_nanosec,wcdat);
dgmtimes=dgmsec+seconds(dgmnanosec/1e9);
if check_dgmtimes
figure(5)
subplot(311)
plot(dgmtimes,numOfDgms,'p',dgmtimes,dgmNum)
xlabel('time of datagram')
ylabel('# datagrams')
title('number of datagrams at this time')
subplot(312)
plot(dgmtimes(1:end-1)+0.5*diff(dgmtimes),diff(pingcount),'.-')
xlabel('time of datagram')
ylabel('counter difference')
title('increment in sonar-side ping counter value')
subplot(313)
plot(dgmtimes(1:end-1)+0.5*diff(dgmtimes),diff(dgmtimes),'.-')
xlabel('time of datagram')
ylabel('time difference')
title('time between pings in file')
fprintf('first ping count = %d\n',pingcount(1))
fprintf('last ping count = %d\n',pingcount(end))
fprintf('total counts = %d\n',pingcount(end)-pingcount(1)+1)
end
figure(11)
hold on
p = arrayfun(@(a) plot(a.beamData_p.beamTxSectorNum),wcdat);
xlabel('beam number')
ylabel('sector number')
%% section loop over pings out
% if numOfDgms always 1, can just proceed
% assuming this for now
% setting these above now
% setting a fixed watercolumn buffer
% maxWCSampIdx = 600;
% setting receive beamwidth arbitrarily (need to find where to read)
% RxBeamWidth=1;
% setting velocity for along track direction
% cartspeed=0.34;
% need to know buffer size in order to do all setup
numSamps1=wcdat(1).beamData_p.numSampleData; % not sure this is correct
maxWCSampIdx1=numSamps1(1);
fprintf('number samples (ping 1, beam 1) = %d\n',maxWCSampIdx1)
maxSamps=zeros(Ndgm,1);
keepSamps=zeros(ckNrx(1),totalNdgm);
for i=1:totalNdgm
numSamps=wcdat(i).beamData_p.numSampleData;
maxSamps(i)=max(numSamps);
keepSamps(:,i)=numSamps;
end
maxWCSampIdx=max(maxSamps(startDgm:endDgm));
fprintf('maximum number samples for all pings = %d\n',max(maxSamps))
fprintf('maximum number samples for pings to read = %d\n',maxWCSampIdx)
figure(12)
plot(1:totalNdgm,maxSamps,'p-')
xlabel('Ping Number = Datagram Number')
ylabel('Maximum Beam Samples per Ping')
% try plotting keepSamps to see why too big numSamps
%dgmtime=NaT(num,1); would create empty datetime array
YY=zeros(ckNrx(1),maxWCSampIdx,Ndgm);
ZZ=zeros(ckNrx(1),maxWCSampIdx,Ndgm);
SV=zeros(ckNrx(1),maxWCSampIdx,Ndgm);
TT=zeros(ckNrx(1),maxWCSampIdx,Ndgm);
XX=zeros(ckNrx(1),maxWCSampIdx,Ndgm);
xBottom=zeros(Ndgm,ckNrx(1));
dTime=zeros(Ndgm,1);
%for idgm=1:Ndgm % just do the first ping for now
for idgm=startDgm:endDgm % just do the first ping for now
fprintf('reading %d-th ping now\n',idgm)
% header
% time coming from datagram header - already pulled
%dgmtime(idgm)=datetime(wcdat(idgm).header.dgtime,'ConvertFrom','posixtime');
%thistime=second(dgmtimes(idgm))+tnanosec(idgm)/1e9;
thistime=posixtimes(idgm)+tnanosec(idgm)/1e9;
% partition - has the datagram number and split info
DatagramNum=wcdat(idgm).partition.dgmNum;
NumDatagrams=wcdat(idgm).partition.numOfDgms;
% cmnPart - not sure if will need this info
% txInfo - not sure if will need this info
NumSectors=wcdat(idgm).txInfo.numTxSectors;
switch NumSectors
case 1
cenSec=1;
case 2
cenSec=1;
case 3
cenSec=2;
end
% sectorData - not sure if will need this info
% EM2040 tranmits in multiple sectors -- need to check how actually
% set up - could be a 3-sector or 4-sector or other
TxBeamWidth=zeros(NumSectors,1);
for isec=1:NumSectors
TxBeamWidth(isec)=wcdat(idgm).sectorData(isec).txBeamWidthAlong_deg;
end
% rxInfo - not sure if will need this info
SoundSpeed=wcdat(idgm).rxInfo.soundVelocity_mPerSec;
SampFreq=wcdat(idgm).rxInfo.sampleFreq_Hz;
TVGFuncApplied=wcdat(idgm).rxInfo.TVGfunctionApplied;
TVGOffset=wcdat(idgm).rxInfo.TVGoffset_dB;
%RxBeamWidth=wcdat(iping).rxInfo.nothere beamAngle=wcdat(i).beamData_p.beamPointAngReVertical_deg;
startRangeSampNum=wcdat(idgm).beamData_p.startRangeSampleNum;
numSamps=wcdat(idgm).beamData_p.numSampleData; % not sure this is correct
% maxWCSampIdx=numSamps(1); % actually need this to be the max, max
% so set it outside this loop
xmitSectNum=wcdat(idgm).beamData_p.beamTxSectorNum;
%beamNum=wcdat(i).beamData_p.? ask Liz what this is
%beamAmp - need to read from binary file still
beamAmpdata=read_bin_kmall(fname,wcdat(idgm));
%DR huh?
Nrx=wcdat(idgm).rxInfo.numBeams;
fprintf('number of beams = %d \n',Nrx)
beamAngle=wcdat(idgm).beamData_p.beamPointAngReVertical_deg';
DR=wcdat(idgm).beamData_p.detectedRangeInSamples; % no idea if this makes sense
beamAmp=zeros(Nrx,1);
for ibeam=1:Nrx
tempBeamAmp=beamAmpdata(ibeam).sampleAmplitude05dB_p;
beamAmp(ibeam,1:numSamps(ibeam))=tempBeamAmp;
end
wcdat(idgm).beamData_p.beamAmp=beamAmp;
%wcdat(idgm).beamData_p.beamPhase=
% this part is very much Liz's code
if DatagramNum == NumDatagrams
pingidx = idgm-startDgm+1; % changing to use this as the count
% although since we know ping numbers, just set rather than counting
% use idgm if want to pull something from wcdat as this is the
% correct value for the full set of datagrams
% use pingidx if want the index corresponding to the current
% counter of pings being read
% these differ if not reading all binary data!
pingTime(pingidx) = thistime; % datetime
%elapsedsec=second(pingTime(idgm)-pingTime(1));
elapsedsec=pingTime(pingidx)-pingTime(1);
%elapsedsec=posixtimes(idgm)-posixtimes(1);
%size(beamAmp)
%size(zeros(Nrx,maxWCSampIdx-length(beamAmp(1,:)))-999)
beamAmp = [beamAmp zeros(Nrx,maxWCSampIdx-length(beamAmp(1,:)))-999];
%disp({'SoundSpeed=' SoundSpeed});
%disp({'SampFreq=' SampFreq});
% range in meters
%range = (1:(length(beamAmp(1,:))))*SoundSpeed/10/2/(SampFreq/100);
range = (1:(length(beamAmp(1,:))))*SoundSpeed/2/SampFreq;
% pad with zeros out to the maximum range
range = [range zeros(1,maxWCSampIdx-length(range))];
% this is depth
%z = cos(beamAngle'/100*pi/180)*range;
z = cos(beamAngle*pi/180)*range; % my angle in straight degrees
% this is across-trackd distance
%y = sin(beamAngle'/100*pi/180)*range;
y = sin(beamAngle*pi/180)*range;
Awc = beamAmp/2;
X = TVGFuncApplied;
C = TVGOffset;
clear TS
%RTval=10*log10((RxBeamWidth/10)*pi/1800*(TxBeamWidth/10)*pi/1800);
RTval=10*log10(RxBeamWidth*pi/180*TxBeamWidth(cenSec)*pi/180);
TS = Awc + RTval.*ones(size(Awc)) - double(X).*log10(ones(length(Awc(:,1)),1)*range) + 40*log10(ones(length(Awc(:,1)),1)*range) - double(C);
%tsBuf1(pingidx,1:length(TS(:,1)),1:length(TS(1,:))) = TS;
%steeringangle = beamAngle/100;
steeringangle = beamAngle;
recieveAngle = 1./cos(steeringangle*pi/180);
%RxRad = (RxBeamWidth/10)*pi/1800;
RxRad = RxBeamWidth*pi/180;
Length = 2*range*sin(RxRad/2);
%TxRad = (TxBeamWidth/10)*pi/1800*recieveAngle;
TxRad = TxBeamWidth(cenSec)*pi/180*recieveAngle;
%Width = 2*ones(length(TxRad),length(range)).*range.*(sin(TxRad./2)).';
Width = 2*ones(length(TxRad),length(range)).*range.*(sin(TxRad./2));
BeamArea = Length.*Width;
Tau = 3500/1e5;
Sv=zeros(size(TS));
for ii = 1:size(TS,2)
Vol_log = 10*log10(BeamArea(:,ii)*Tau*SoundSpeed/2);
Sv(:,ii) = TS(:,ii) - Vol_log;
end
for aa = 1:Nrx
if DR(aa) ~= 0
yBottom(pingidx,aa) = y(aa,DR(aa));
zBottom(pingidx,aa) = z(aa,DR(aa));
else
yBottom(pingidx,aa) = 0;
zBottom(pingidx,aa) = 0;
end
end
[~,nadir_idx] = min(abs(yBottom(pingidx,:)-35));
if zBottom(nadir_idx) ~= 0
nadir_idx = nadir_idx;
elseif zBottom(nadir_idx + 1) ~= 0
nadir_idx = nadir_idx + 1;
else
nadir_idx = nadir_idx - 1;
end
if vizping
figure(1)
subplot(211)
pcolor(y,z,TS);
shading flat; axis equal;
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
caxis([-140 -60]); colorbar
title(['Ping ' num2str(idgm) ': TS'])
ylim([0 plotdepthmax])
hold on
plot(yBottom(pingidx,:),zBottom(pingidx,:),'r.')
hold off
subplot(212)
pcolor(y,z,Sv);
shading flat; axis equal;
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
caxis([-140 -60]); colorbar
title(['Ping ' num2str(idgm) ' :Sv'])
hold on
plot(yBottom(pingidx,:),zBottom(pingidx,:),'r.')
ylim([0 plotdepthmax])
hold off
end % vizping
% cut_Sv = Sv(:,1:nadir_idx);
% cut_y = y(:,1:nadir_idx);
% cut_z = z(:,1:nadir_idx);
% bin_edges = -150:10:150;
% b_x = beam_x(pingidx,:);
% b_y = beam_y(pingidx,:);
% b_z = beam_z(pingidx,:);
% for iping = 1:(length(bin_edges)-1)
% bin_idx = cut_y < bin_edges(iping+1) & cut_y > bin_edges(iping);
% test_mean(i) = mean(cut_Sv(bin_idx));
% test_max(i) = max(cut_Sv(bin_idx));
% test1 = movmean(cut_Sv(bin_idx),10);
% test_mmax(iping) = max(test1);
% y_mid(iping) = mean(cut_y(bin_idx));
% end
% y_mid_bin(pingidx,:) = y_mid;
% z_nadir(pingidx) = zBottom(nadir_idx);
% Sv_mmax(pingidx,:) = test_mmax;
%ping_ind=idgm-startDgm+1;
YY(:,:,pingidx)=y(:,1:maxWCSampIdx);
ZZ(:,:,pingidx)=z(:,1:maxWCSampIdx);
SV(:,:,pingidx)=Sv(:,1:maxWCSampIdx);
TT(:,:,pingidx)=TS(:,1:maxWCSampIdx);
XX(:,:,pingidx)=cartspeed*elapsedsec*ones(256,maxWCSampIdx);
xBottom(pingidx,:)=cartspeed*elapsedsec*ones(1,256);
dTime(pingidx)=elapsedsec;
end % inside datagram split check
%pause
end
%% summary visualizations
if do3Dviz
pingtoplot=fix(Ndgm/2); % 180
thisbeam=128; % 150
%plot along track profile
figure(6)
pcolor(squeeze(XX(thisbeam,:,:)),squeeze(ZZ(thisbeam,:,:)),squeeze(TT(thisbeam,:,:)))
shading flat
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
if sscanf(version('-release'),'%d')<2022
caxis([-140 -40]); colorbar
else
clim([-140 -40]); colorbar
end
ylim([0 plotdepthmax])
title(['end Ping ' num2str(idgm) ' :TS'])
hold on
plot(xBottom(:,thisbeam),zBottom(:,thisbeam),'r.')
plot(xBottom(pingtoplot,thisbeam).*ones(2,1),[0 6],'k')
hold off
xlabel('distance along track (arbitrary)')
ylabel('depth (m)')
figure(7)
pcolor(squeeze(XX(thisbeam,:,:)),squeeze(ZZ(thisbeam,:,:)),squeeze(SV(thisbeam,:,:)))
shading flat
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
if sscanf(version('-release'),'%d')<2022
caxis([-140 -40]); colorbar
else
clim([-140 -40]); colorbar
end
title(['end Ping ' num2str(idgm) ' :SV'])
hold on
plot(xBottom(:,thisbeam),zBottom(:,thisbeam),'r.')
ylim([0 plotdepthmax])
hold off
% plot a summary of pings
figure(8)
pcolor(mean(YY(:,:,fix(Ndgm/2):ceil(3*Ndgm/4)),3),...
mean(ZZ(:,:,fix(Ndgm/2):ceil(3*Ndgm/4)),3),...
mean(TT(:,:,fix(Ndgm/2):ceil(3*Ndgm/4)),3))
shading flat
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
if sscanf(version('-release'),'%d')<2022
caxis([-140 -40]); colorbar
else
clim([-140 -40]); colorbar
end
title(['average pings ' num2str(idgm-Ndgm+1) '-' num2str(idgm) ' :Sv'])
hold on
plot(mean(yBottom,1),mean(zBottom,1),'r.')
ylim([0 plotdepthmax])
hold off
% plot a specific ping
figure(9)
pcolor(YY(:,:,pingtoplot),ZZ(:,:,pingtoplot),TT(:,:,pingtoplot))
shading flat
set(gca,'ydir','reverse');
set(gca,'fontname','Times');
if sscanf(version('-release'),'%d')<2022
caxis([-140 -40]); colorbar
else
clim([-140 -40]); colorbar
end
title(['end Ping ' num2str(idgm) ' :Sv'])
hold on
plot(mean(yBottom,1),mean(zBottom,1),'r.')
plot(YY(thisbeam,:,pingtoplot),ZZ(thisbeam,:,pingtoplot),'k')
ylim([0 plotdepthmax])
hold off
%figure(10)
%p = patch(isosurface(XX,YY,ZZ,SV,-80));
%isonormals(XX,YY,ZZ,SV,p)
%p.FaceColor = 'red';
%p.EdgeColor = 'none';
%daspect([1 1 1])
%view(3);
%axis tight
%camlight
%lighting
end % do3Dviz
%% save files
if savefiles
% outdir='../MBES_mat_data/'; % moving to top
datafile=filename(1:end-6);
source=filename(end-4:end);
outmatfile=fullfile(outdir,...
[datafile '_' source '_pings_' num2str(startDgm) '_' num2str(endDgm) '.mat']);
outvizfile=fullfile(outdir,...
[datafile '_' source '_pings_' num2str(startDgm) '_' num2str(endDgm) '_viz.mat']);
outallstruct=fullfile(outdir,...
[datafile '_pings_' num2str(startDgm) '_' num2str(endDgm) '_bothstructs.mat']);
fprintf('saving to file %s \n',outmatfile)
save(outmatfile,'wcdat','startDgm','endDgm')
save(outvizfile,'XX','YY','ZZ','SV','TT','xBottom','yBottom','zBottom','dTime','startDgm','endDgm');
if exist('KMALLdata2','var')
save(outallstruct,'KMALLdata','KMALLdata2')
else
save(outallstruct,'KMALLdata')
end
end % savefiles
fclose(fid);
return
%%
% every thing beyond here is Liz's - mostly using as guide to which
% datagrams are needed and other steps
%********************************************************************
%********************************************************************
%********************************************************************
%********************************************************************
%********************************************************************
%********************************************************************
%% start reading data file
%tic
pingidx = 0;
rangeSB = [];
BufSize = 1000;
tsBuf1 = zeros(BufSize,256,2500);
stopp = 0;
bufCt1 = 0;
while ~stopp
sz = fread(fid,1,'uint32'); % the size of the next datagram. note: the size does not include this field
fprintf('size of next datagram: %d\n',sz)
startid = fread(fid,1,'uint8'); % the start id of a datagram should be 2
fprintf('start id: %d\n',startid)
if startid == 2
datatype = fread(fid,1,'char'); % this is the type of datagram that we are reading
fprintf('data type id is %d',datatype)
% char(83) = S, seabead image data
if datatype == 83
[DateVal,TimeVal,PingCount,MeanAbsorp,PulseLength,NormIncRange,TVGstartRange,...
TVGstopRange,NormincBSN,OblqincBSO,TxBeamWidth,TVGcrossoverAng,NumBeamsHere,...
beamIndexNum,sortDir,NumSampsPerBeam,BeamCenterSampNum,SimradSnippets,BeamDetAmp] = readEM_S(fid);
pause
% char(82) = R, runtime parameters data
elseif datatype == 82
[ModelNum,DateVal,TimeVal,PingCount,SystemSerNum,Mode,MinDepth,MaxDepth,AbsCoef,TxPulseLength,...
TxBeamWidth, TxPower,RxBeamWidth,RxBandWidth,RxFixedGain, TVGXover,MaxPortCoverage, MaxStbdCoverage,...
MaxPortSwath,BeamSpacing,MaxStbdSwath] = readEM_R(fid);
elseif datatype == 78
[DateVal,TimeVal,PingCount,SSPD,SampFreq,TiltAng,FocRange,SignalLength,SectorXmitDelay,CentFreq,MeanAbsorpt,SigWavID,XmitSecNum,BW,BeamAngle,XmitSecNumByBeam,TwoWayTT,DetInfo,BS] = readEM_N(fid);
%pause
% char(107) = k, watercolumn data
elseif datatype == 107
[DateVal,TimeVal,PingCount,NumDatagrams,DatagramNum,NumXmitSect,...
NumRecBeams,NumBeamsHereTemp, SoundSpeed, SampFreq, TxTimeHeave, TVGFuncApplied, TVGOffset,...
centFreqTemp,tiltAngle, xmitSectNumTemp,beamAngleTemp,startRangeSampNumTemp,numSampsTemp,xmitSectNumRecTemp,...
beamNumTemp,beamAmpTemp,DRtemp] = readEM_k(fid);
% convert datagram time to matlab datenumber
YY = floor(DateVal/10000);
MM = floor( (DateVal-YY*10000)/100);
DD = floor( (DateVal-YY*10000-MM*100));
dn = datenum(YY,MM,DD)+TimeVal/1000/3600/24;
% there may be more than one water column datagram due to the
% size limit on a single datagram, and we want to stitch all of
% the data for a single ping together. The offset calculated
% below allows the data to be associated with its proper beam
% number
NumBeamsHere(DatagramNum) = NumBeamsHereTemp;
if DatagramNum > 1
offsetidx = sum(NumBeamsHere(1:(DatagramNum-1)));
else
offsetidx = 0;
end
indx = 1:length(beamAngleTemp);
beamAngle(offsetidx + indx) = beamAngleTemp;
startRangeSampNum(offsetidx + indx) = startRangeSampNumTemp;
numSamps(offsetidx + indx) = numSampsTemp;
xmitSectNum(offsetidx + indx) = xmitSectNumRecTemp;
beamNum(offsetidx + indx) = beamNumTemp;
beamAmp(offsetidx + indx,1:length(beamAmpTemp(1,:))) = beamAmpTemp;
DR(offsetidx + indx) = DRtemp;
% if we have read all of the data for a single ping (plot this
% data)
if DatagramNum == NumDatagrams
%disp( [num2str(floor(TimeVal/1000/3600)) ':' num2str(floor(rem(TimeVal/1000/3600,1)*60),'%2d') ':' num2str(rem(rem(TimeVal/1000/3600,1)*60,1)*60,'%2.2f')])
pingidx = pingidx + 1
pingTime(pingidx) = TimeVal/1000; % time in seconds since midnight
beamAmp = [beamAmp zeros(NumRecBeams,maxWCSampIdx-length(beamAmp(1,:)))-999];
%disp({'SoundSpeed=' SoundSpeed});
%disp({'SampFreq=' SampFreq});
% range in meters
range = (1:(length(beamAmp(1,:))))*SoundSpeed/10/2/(SampFreq/100);
% pad with zeros out to the maximum range
range = [range zeros(1,maxWCSampIdx-length(range))];
% this is depth
z = cos(beamAngle'/100*pi/180)*range;
% this is across-trackd distance
y = sin(beamAngle'/100*pi/180)*range;
Awc = beamAmp/2;
X = TVGFuncApplied;
C = TVGOffset;
clear TS
TS = Awc + 10*log10((RxBeamWidth/10)*pi/1800*(TxBeamWidth/10)*pi/1800) - X*log10(ones(length(Awc(:,1)),1)*range) + 40*log10(ones(length(Awc(:,1)),1)*range) - C;
tsBuf1(pingidx,1:length(TS(:,1)),1:length(TS(1,:))) = TS;
steeringangle = beamAngle/100;
recieveAngle = 1./cos(steeringangle*pi/180);
RxRad = (RxBeamWidth/10)*pi/1800;
Length = 2*range*sin(RxRad/2);
TxRad = (TxBeamWidth/10)*pi/1800*recieveAngle;
Width = 2*ones(length(TxRad),length(range)).*range.*(sin(TxRad./2)).';
BeamArea = Length.*Width;
Tau = 3500/1e5;
for ii = 1:size(TS,2)
Vol_log = 10*log10(BeamArea(:,ii)*Tau*SoundSpeed/2);
Sv(:,ii) = TS(:,ii) - Vol_log;
end
for aa = 1:NumBeamsHere
if DR(aa) ~= 0
yBottom(pingidx,aa) = y(aa,DR(aa));
zBottom(pingidx,aa) = z(aa,DR(aa));
else
yBottom(pingidx,aa) = 0;
zBottom(pingidx,aa) = 0;
end
end
[~,nadir_idx] = min(abs(yBottom(pingidx,:)-35));
if zBottom(nadir_idx) ~= 0
nadir_idx = nadir_idx;
elseif zBottom(nadir_idx + 1) ~= 0
nadir_idx = nadir_idx + 1;
else
nadir_idx = nadir_idx - 1;
end
% subplot(211)
% pcolor(y,z,TS); shading flat; axis equal; set(gca,'ydir','reverse');
% set(gca,'fontname','Times'); caxis([-100 0]); colorbar
% title(['Ping ' num2str(pingidx) ': TS'])
% ylim([0 300])
% hold on
% plot(yBottom(pingidx,:),zBottom(pingidx,:),'r.')
% hold off
%
% subplot(212)
% pcolor(y,z,Sv); shading flat; axis equal; set(gca,'ydir','reverse');
% set(gca,'fontname','Times'); caxis([-100 0]); colorbar
% title(['Ping ' num2str(pingidx) ' :Sv'])
% hold on
% plot(yBottom(pingidx,:),zBottom(pingidx,:),'r.')
% ylim([0 300])
% hold off
% drawnow
% % plot(y(bin_idx),z(bin_idx),'rs')
% pause(0.05)
cut_Sv = Sv(:,1:nadir_idx);
cut_y = y(:,1:nadir_idx);
cut_z = z(:,1:nadir_idx);
bin_edges = -150:10:150;
% b_x = beam_x(pingidx,:);
% b_y = beam_y(pingidx,:);
% b_z = beam_z(pingidx,:);
for i = 1:(length(bin_edges)-1)
bin_idx = cut_y < bin_edges(i+1) & cut_y > bin_edges(i);
% test_mean(i) = mean(cut_Sv(bin_idx));
% test_max(i) = max(cut_Sv(bin_idx));
test1 = movmean(cut_Sv(bin_idx),10);
test_mmax(i) = max(test1);
y_mid(i) = mean(cut_y(bin_idx));
end
y_mid_bin(pingidx,:) = y_mid;
z_nadir(pingidx) = zBottom(nadir_idx);
Sv_mmax(pingidx,:) = test_mmax;
end
% all other datagrams, skip and move to next
else
% char(73) = I, extra detects datagram
% char(196) = A, attitude datagram
fread(fid,sz-2,'uint8');
end
else
% the start id was not equal to 2, so either this is not a file
% containing simrad datagrams or we have made an error reading
% it...
disp('start id not equal to 2!')
pause(.1)
if feof(fid)
stopp = 1;
end
end
end
fclose(fid)
%HeatMap_0030.nadir_z = z_nadir;
%HeatMap_0030.across_y = y_mid_bin;
%HeatMap_0030.Sv = Sv_mmax;
%save('HeatMap_0030.mat','HeatMap_0030')