-
Notifications
You must be signed in to change notification settings - Fork 5
/
SL3Reader.cs
766 lines (642 loc) · 36.7 KB
/
SL3Reader.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Win32.SafeHandles;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.IO.MemoryMappedFiles;
using System.Collections.ObjectModel;
using System.Text;
using System.Runtime.InteropServices;
namespace SL3Reader;
public class SL3Reader : IDisposable
{
#region Public properties
public ReadOnlyCollection<nuint> Frames { get; }
public ReadOnlyCollection<GeoPoint> AugmentedCoordinates { get; }
public ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> FrameByType { get; }
#endregion End Public properties
#region Private variables
private readonly MemoryMappedFile memoryMappedFile;
private readonly MemoryMappedViewAccessor viewAccessor;
private readonly SafeMemoryMappedViewHandle viewHandle;
private readonly ReadOnlyCollection<int> Coordinate3DHelper;
#endregion End Private variables
[SkipLocalsInit]
public unsafe SL3Reader([DisallowNull] string path)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path, nameof(path));
long len = new FileInfo(path!).Length;
if (len < (SLFileHeader.Size + Frame.BasicSize))
throw new EndOfStreamException("The file is too short to be valid.");
// Init Frames
const long averageFrameSize = 2118L; // Empirically set value to avoid frequent resize of the underlying array.
int estimatedCount = (int)(len / averageFrameSize);
ReadOnlyCollectionBuilder<nuint> frames = new(estimatedCount);
// Init index lists
int p8 = estimatedCount / 8, p4 = estimatedCount / 4, p10 = estimatedCount / 10;
ReadOnlyCollectionBuilder<nuint> Primary = new(p8),
Secondary = new(p8),
DownScan = new(p10),
LeftSidescan = [],
RightSidescan = [],
SideScan = new(p10),
Unknown6 = [],
Unknown7 = new(p4),
Unknown8 = new(p4),
ThreeDimensional = new(p10),
DebugDigital = [],
DebugNoise = [];
ReadOnlyCollectionBuilder<int> coordinate3DHelper = new(p10);
ReadOnlyCollectionBuilder<int> coordinateSidescanHelper = new(p10);
memoryMappedFile = MemoryMappedFile.CreateFromFile(
File.OpenHandle(path!, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.RandomAccess, 0L),
null, len, MemoryMappedFileAccess.Read, HandleInheritability.None, false);
viewAccessor = memoryMappedFile.CreateViewAccessor(0, len, MemoryMappedFileAccess.Read);
viewHandle = viewAccessor.SafeMemoryMappedViewHandle;
byte* ptr = null;
viewHandle.AcquirePointer(ref ptr);
byte* maxPtr = ptr + len;
((SLFileHeader*)ptr)->ThrowIfInvalidFormatDetected();
ptr += SLFileHeader.Size; // Advance for the first frame
Frame* currentFrame = (Frame*)ptr;
Frame.InitTimestampBase(currentFrame->HardwareTime); // Init time
bool unknownFrameDetected = false;
// Load frames
for (int i = 0;
ptr < maxPtr && (ptr + currentFrame->LengthOfFrame) < maxPtr; // Use whole frames only!
ptr += currentFrame->LengthOfFrame,
currentFrame = (Frame*)ptr,
i++)
{
nuint current = (nuint)currentFrame;
frames.Add(current);
switch (currentFrame->SurveyType)
{
case SurveyType.Primary:
Primary.Add(current);
break;
case SurveyType.Secondary:
Secondary.Add(current);
break;
case SurveyType.DownScan:
DownScan.Add(current);
break;
case SurveyType.LeftSideScan:
LeftSidescan.Add(current);
break;
case SurveyType.RightSideScan:
RightSidescan.Add(current);
break;
case SurveyType.SideScan:
SideScan.Add(current);
break;
case SurveyType.Unknown6:
Unknown6.Add(current);
break;
case SurveyType.Unknown7:
Unknown7.Add(current);
break;
case SurveyType.Unknown8:
Unknown8.Add(current);
break;
case SurveyType.ThreeDimensional:
ThreeDimensional.Add(current);
coordinate3DHelper.Add(i);
break;
case SurveyType.DebugDigital:
DebugDigital.Add(current);
break;
case SurveyType.DebugNoise:
DebugNoise.Add(current);
break;
default:
unknownFrameDetected = true;
break;
}
}
if (unknownFrameDetected)
Console.WriteLine("Warning! Unknown frame type detected!\nContact the developer for further analysis.");
// Init AugmentedCoordinates
AugmentedCoordinates = frames.Count is 0 ?
new ReadOnlyCollectionBuilder<GeoPoint>().ToReadOnlyCollection() :
AugmentTrajectory(frames);
Frames = frames.ToReadOnlyCollection();
// Populate major index
FrameByType = new SortedDictionary<SurveyType, ReadOnlyCollection<nuint>>()
{
{ SurveyType.Primary, Primary.ToReadOnlyCollection() },
{ SurveyType.Secondary, Secondary.ToReadOnlyCollection() },
{ SurveyType.DownScan, DownScan.ToReadOnlyCollection() },
{ SurveyType.LeftSideScan, LeftSidescan.ToReadOnlyCollection() },
{ SurveyType. RightSideScan, RightSidescan.ToReadOnlyCollection() },
{ SurveyType.SideScan, SideScan.ToReadOnlyCollection() },
{ SurveyType.Unknown6, Unknown6.ToReadOnlyCollection() },
{ SurveyType.Unknown7, Unknown7.ToReadOnlyCollection() },
{ SurveyType.Unknown8, Unknown8.ToReadOnlyCollection() },
{ SurveyType.ThreeDimensional, ThreeDimensional.ToReadOnlyCollection() },
{ SurveyType.DebugDigital, DebugDigital.ToReadOnlyCollection() },
{ SurveyType.DebugNoise, DebugNoise.ToReadOnlyCollection() }
}.AsReadOnly();
Coordinate3DHelper = coordinate3DHelper.ToReadOnlyCollection();
//ExamineUnknown8Datasets();
return;
// Local functions:
static ReadOnlyCollection<GeoPoint> AugmentTrajectory(ReadOnlyCollectionBuilder<nuint> frames)
{
const double lim = 1.2d;
const double C = 1.4326d; // Empirical, around √2.
ReadOnlyCollectionBuilder<GeoPoint> coordinates = new(frames.Count);
(double x0, double y0, double z0, double v0, double t0, double d0) = ((Frame*)frames[0])->QueryMetric();
coordinates.Add(new(x0, y0, d0, z0, 0)); // The first one.
double distance = 0, xprev = x0, yprev = y0;
for (int i = 1, frameCount = frames.Count; i != frameCount;)
{
Frame* frame = (Frame*)frames[i++];
(double x1, double y1, double z1, double v1, double t1, double d1) =
frame->QueryMetric();
(double sin0, double cos0) = double.SinCos(d0);
(double sin1, double cos1) = double.SinCos(d1);
double vx0 = cos0 * v0,
vy0 = sin0 * v0,
vx1 = cos1 * v1,
vy1 = sin1 * v1,
dt = t1 - t0;
x0 += C * .5d * (vx0 + vx1) * dt;
y0 += C * .5d * (vy0 + vy1) * dt;
d0 = d1; t0 = t1; v0 = v1;
if (frame->SurveyType is SurveyType.Primary or SurveyType.Secondary or
SurveyType.Unknown7 or SurveyType.Unknown8)
{
double dy = y0 - y1;
if (double.Abs(dy) > lim)
y0 = y1 + double.CopySign(lim, dy);
double dx = x0 - x1;
if (double.Abs(dx) > lim)
x0 = x1 + double.CopySign(lim, dx);
}
else
{
double dy = y0 - y1;
if (double.Abs(dy) > 50) // Detect serious errors.
y0 = frame->Y;
double dx = x0 - x1;
if (double.Abs(dx) > 50) // Detect serious errors.
x0 = frame->X;
}
coordinates.Add(new(x0, y0, d0, z1, distance += double.Hypot(x0 - xprev, y0 - yprev)));
xprev = x0; yprev = y0;
}
return coordinates.ToReadOnlyCollection();
}
}
public unsafe void ExportRoutePoints(string path)
{
ReadOnlyCollection<nuint> frames = Frames;
int count = frames.Count;
using FileStream textWriter = new(path,
new FileStreamOptions()
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Share = FileShare.Read,
Options = FileOptions.SequentialScan,
PreallocationSize = 151 * count, // Empirical guess.
BufferSize = 65536
});
textWriter.Write("CampaignID[#],DateTime[UTC],SurveyType,WaterDepth[Feet],Longitude[°WGS84],Latitude[°WGS84],GNSSAltitude[Feet_WGS84],GNSSHeading[rad_azimuth],GNSSSpeed[m/s],MagneticHeading[rad_azimuth],MinRange[Feet],MaxRange[Feet],WaterTemperature[°C],WaterSpeed[Feet],HardwareTime[ms],Frequency,Milliseconds[ms],AugmentedX[m],AugmentedY[m]\r\n"u8);
// TODO: Filter is not working!
//ReadOnlyCollection<nuint> frames = filter is SurveyType.All ? Frames : IndexByType[filter];
ReadOnlyCollection<GeoPoint> augmentedCoordinates = AugmentedCoordinates;
scoped Span<byte> buffer = stackalloc byte[256];
for (int i = 0; i != count;)
{
textWriter.Write(((Frame*)frames[i])->Format(buffer));
textWriter.Write(augmentedCoordinates[i++].Format(buffer));
}
}
public unsafe void ExportImagery(string path, SurveyType surveyType = SurveyType.SideScan, bool exportGeoreference = false)
{
ReadOnlyCollection<nuint> imageFrames = FrameByType[surveyType];
if (imageFrames.Count < 1) return; // Return when no imagery exists.
ArgumentNullException.ThrowIfNull(path);
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
List<int> breakpoints = GetBreakPoints(imageFrames, out int maxHeight);
int numberOfColumns = (int)((Frame*)imageFrames[0])->LengthOfEchoData;
byte[] buffer = BitmapHelper.CreateBuffer(maxHeight, numberOfColumns);
for (int i = 0, maxIndex = breakpoints.Count - 1; i < maxIndex; i++)
{
int first = breakpoints[i], final = breakpoints[1 + i];
BitmapHelper.UpdateBuffer(
buffer, final - first, numberOfColumns,
out int fullStride,
out Span<byte> fileBuffer,
out Span<byte> pixelData);
byte* offset = (byte*)((Frame*)imageFrames[0])->HeaderSize;
fixed (byte* pixelPtr = pixelData)
for (int j = first, k = 0; j < final; j++)
Buffer.MemoryCopy(imageFrames[j] + offset, pixelPtr + (fullStride * k++), numberOfColumns, numberOfColumns);
string prefix = GetPrefix(surveyType);
string filePath = Path.Combine(path, prefix + final + ".bmp");
using SafeFileHandle handle = File.OpenHandle(filePath!,
FileMode.Create, FileAccess.Write, FileShare.None, FileOptions.SequentialScan, fileBuffer.Length);
RandomAccess.Write(handle, fileBuffer, 0);
handle.Dispose();
// World file
// TODO: Remove intermediate solution:
GeoPoint firstStrip = AugmentedCoordinates[Frames.IndexOf(imageFrames[first])];
GeoPoint lastStrip = AugmentedCoordinates[Frames.IndexOf(imageFrames[final - 1])];
Frame* lastFrame = (Frame*)imageFrames[final - 1];
if (!exportGeoreference)
{
double XSize = -(lastStrip.Distance - firstStrip.Distance) / (final - first - 1);
double YSize = -10 * lastFrame->MaxRange * .3048 / numberOfColumns;
string WorldString = string.Join("\r\n",
["0",
YSize.ToString(invariantCulture),
XSize.ToString(invariantCulture),
"0",
lastStrip.Distance.ToString(invariantCulture),
lastFrame->SurveyType is SurveyType.SideScan ? (-1400d * YSize).ToString(): "0"], 0, 6);
File.WriteAllText(Path.Combine(path, prefix + final + ".bpw"), WorldString);
}
// End world file
// AUX file
if (exportGeoreference)
{
double delta = .3084 * lastFrame->MaxRange;
List<GeoPoint> sourceGCPs = new((final - first) / 10);
List<GeoPoint> targetGCPs = new(sourceGCPs.Capacity);
for (int s = first; s <= final - 1; s += 50)
{
sourceGCPs.Add(new(1400.5, -(s - first) + .5, default, default, default));
sourceGCPs.Add(new(2799.5, -(s - first) + .5, default, default, default));
sourceGCPs.Add(new(.5, -(s - first) + .5, default, default, default));
GeoPoint Strip = AugmentedCoordinates[Frames.IndexOf(imageFrames[s])];
Frame* frame = (Frame*)imageFrames[s];
(double sin, double cos) =
double.SinCos(frame->GNSSHeading - .5 * double.Pi);
targetGCPs.AddRange([
Strip,
new(double.FusedMultiplyAdd(-delta, sin, Strip.X),
double.FusedMultiplyAdd(-delta, cos, Strip.Y),
0, 0, 0),
new(double.FusedMultiplyAdd(delta, sin, Strip.X),
double.FusedMultiplyAdd(delta, cos, Strip.Y),
0, 0, 0)]);
}
GeoReferenceHelper.WriteGeoreferencedPAM(Path.ChangeExtension(filePath, ".bmp.aux.xml"), sourceGCPs, targetGCPs);
}
else
{
WriteAUXFile(filePath);
}
}
[SkipLocalsInit, MethodImpl(MethodImplOptions.AggressiveInlining)]
static string GetPrefix(SurveyType surveyType) =>
surveyType switch
{
SurveyType.SideScan => "SS_",
SurveyType.DownScan => "DS_",
SurveyType.Primary => "PS_",
SurveyType.Secondary => "SES_",
SurveyType.Unknown8 => "U8_",
SurveyType.Unknown7 => "U7_",
_ => "UNK_"
};
static unsafe List<int> GetBreakPoints(ReadOnlyCollection<nuint> framesToCheck, out int contiguousLength)
{
int frameCount = framesToCheck.Count;
List<int> breakpoints = new(frameCount / 300) { 0 }; // ~300 empirical guess.
// 'i' have to be incremented to prevent double test.
float previousRange = ((Frame*)framesToCheck[0])->MaxRange;
int i = 1;
for (; i != frameCount; i++)
{
float currentRange = ((Frame*)framesToCheck[i])->MaxRange;
if (currentRange != previousRange)
{
previousRange = currentRange;
breakpoints.Add(i);
}
}
if (breakpoints[^1] != --i)
breakpoints.Add(i); // There was no change in the range, so we have to add the last one.
contiguousLength = 0;
for (int j = 0,
maxIndex = breakpoints.Count - 1,
delta;
j < maxIndex;
j++)
{
if (contiguousLength < (delta = breakpoints[1 + j] - breakpoints[j]))
contiguousLength = delta;
}
return breakpoints;
}
}
public unsafe void ExamineUnknown8Datasets()
{
ReadOnlyCollection<nuint> unknown8Frames = FrameByType[SurveyType.Unknown8];
int unknown8FrameCount = unknown8Frames.Count;
if (unknown8FrameCount < 1) return; // Return when no U8 exists
for (int i = 0; i < unknown8FrameCount; i++)
{
Frame* currentFrame = (Frame*)unknown8Frames[i];
byte* ptr = ((byte*)currentFrame) + currentFrame->HeaderSize;
for (int j = 0; j < 512; j += 2, ptr += 2)
{
//Debug.Print((*(short*)ptr).ToString());
if ((*(short*)ptr) is not 0)
Debug.Print("M");
}
}
}
public unsafe void Export3D(string path, bool includeUnreliable = false, bool magneticHeading = false, bool flip = false)
{
ArgumentNullException.ThrowIfNull(path);
const string doubleFormat = "0.####";
const int bufferSize = 16384;
ReadOnlyCollection<nuint> frames3D = FrameByType[SurveyType.ThreeDimensional];
int frames3DLength = frames3D.Count;
if (frames3DLength < 1) return;
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
ReadOnlyCollection<GeoPoint> augmentedCoordinates = AugmentedCoordinates;
ReadOnlyCollection<int> coordinate3DHelper = Coordinate3DHelper;
string[] stringArray = new string[8];
StringBuilder stringBuilder = new(bufferSize);
using StreamWriter streamWriter = new(path!, Encoding.UTF8,
new FileStreamOptions()
{
Access = FileAccess.Write,
BufferSize = bufferSize,
Mode = FileMode.OpenOrCreate,
Options = FileOptions.SequentialScan
});
streamWriter.BaseStream.Write("CampaignID,DateTime,X[Lowrance_m],Y[Lowrance_m],Z[m_WGS84],Depth[m],Angle[°],Distance[m],Reliable\r\n"u8);
double leftConversionUnit = flip ? 0.3048d : -0.3048d;
double rightConversionUnit = -leftConversionUnit;
for (int i = 0; i < frames3DLength; i++)
{
Frame* frame = (Frame*)frames3D[i];
ThreeDimensionalFrameHeader* header = (ThreeDimensionalFrameHeader*)((byte*)frame + frame->HeaderSize);
GeoPoint augmentedCoordinate = augmentedCoordinates[coordinate3DHelper[i]];
byte* measurements = (byte*)header + ThreeDimensionalFrameHeader.Size;
double centralX = augmentedCoordinate.X,
centralY = augmentedCoordinate.Y,
centralZ = .3048 * augmentedCoordinate.Altitude;
(double sin, double cos) =
double.SinCos((magneticHeading ? frame->MagneticHeading : frame->GNSSHeading) - .5 * double.Pi);
stringArray[0] = frame->CampaignID.ToString();
stringArray[1] = frame->Timestamp.ToString("yyyy'-'MM'-'dd HH':'mm':'ss.fff'Z'", invariantCulture);
// *************************** THIS IS TERRIBLE ***************************
// *************************** Refactor ASAP ***************************
// Left side
for (byte* limit = measurements + header->NumberOfLeftBytes;
measurements < limit;
measurements += InterferometricMeasurement.Size)
{
InterferometricMeasurement* measurement = (InterferometricMeasurement*)measurements;
double delta = leftConversionUnit * measurement->Delta, // Negative side
depth = 0.3048d * measurement->Depth;
stringArray[2] = double.FusedMultiplyAdd(delta, sin, centralX).ToString(doubleFormat, invariantCulture); // Azimuthal direction
stringArray[3] = double.FusedMultiplyAdd(delta, cos, centralY).ToString(doubleFormat, invariantCulture);
stringArray[4] = (centralZ - depth).ToString(doubleFormat, invariantCulture);
stringArray[5] = depth.ToString(doubleFormat, invariantCulture);
stringArray[6] = measurement->AngleInDegrees.ToString(doubleFormat, invariantCulture);
stringArray[7] = measurement->MetricDistance.ToString(doubleFormat, invariantCulture);
stringBuilder.AppendJoin(',', stringArray).AppendLine(",Y");
}
// Right side
for (byte* limit = measurements + header->NumberOfRightBytes;
measurements < limit;
measurements += InterferometricMeasurement.Size)
{
InterferometricMeasurement* measurement = (InterferometricMeasurement*)measurements;
double delta = rightConversionUnit * measurement->Delta, // Positive side
depth = .3048d * measurement->Depth;
stringArray[2] = double.FusedMultiplyAdd(delta, sin, centralX).ToString(doubleFormat, invariantCulture); // Azimuthal direction
stringArray[3] = double.FusedMultiplyAdd(delta, cos, centralY).ToString(doubleFormat, invariantCulture);
stringArray[4] = (centralZ - depth).ToString(doubleFormat, invariantCulture);
stringArray[5] = depth.ToString(doubleFormat, invariantCulture);
stringArray[6] = measurement->AngleInDegrees.ToString(doubleFormat, invariantCulture);
stringArray[7] = measurement->MetricDistance.ToString(doubleFormat, invariantCulture);
stringBuilder.AppendJoin(',', stringArray).AppendLine(",Y");
}
if (includeUnreliable)
{
for (byte* limit = measurements + header->NumberOfUnreliableLeftBytes;
measurements < limit;
measurements += InterferometricMeasurement.Size)
{
InterferometricMeasurement* measurement = (InterferometricMeasurement*)measurements;
if (measurement->IsValid)
{
double delta = leftConversionUnit * measurement->Delta, // Negative side
depth = 0.3048d * measurement->Depth;
stringArray[2] = double.FusedMultiplyAdd(delta, sin, centralX).ToString(doubleFormat, invariantCulture); // Azimuthal direction
stringArray[3] = double.FusedMultiplyAdd(delta, cos, centralY).ToString(doubleFormat, invariantCulture);
stringArray[4] = (centralZ - depth).ToString(doubleFormat, invariantCulture);
stringArray[5] = depth.ToString(doubleFormat, invariantCulture);
stringArray[6] = measurement->AngleInDegrees.ToString(doubleFormat, invariantCulture);
stringArray[7] = measurement->MetricDistance.ToString(doubleFormat, invariantCulture);
stringBuilder.AppendJoin(',', stringArray).AppendLine(",N");
}
}
for (byte* limit = measurements + header->NumberOfUnreliableRightBytes;
measurements < limit;
measurements += InterferometricMeasurement.Size)
{
InterferometricMeasurement* measurement = (InterferometricMeasurement*)measurements;
if (measurement->IsValid)
{
double delta = rightConversionUnit * measurement->Delta, // Positive side
depth = 0.3048d * measurement->Depth;
stringArray[2] = double.FusedMultiplyAdd(delta, sin, centralX).ToString(doubleFormat, invariantCulture); // Azimuthal direction
stringArray[3] = double.FusedMultiplyAdd(delta, cos, centralY).ToString(doubleFormat, invariantCulture);
stringArray[4] = (centralZ - depth).ToString(doubleFormat, invariantCulture);
stringArray[5] = depth.ToString(doubleFormat, invariantCulture);
stringArray[6] = measurement->AngleInDegrees.ToString(doubleFormat, invariantCulture);
stringArray[7] = measurement->MetricDistance.ToString(doubleFormat, invariantCulture);
stringBuilder.AppendJoin(',', stringArray).AppendLine(",N");
}
}
}
streamWriter.Write(stringBuilder);
stringBuilder.Clear();
}
}
#region AUX support
[SkipLocalsInit]
private static void WriteAUXFile(string originalPath)
{
ReadOnlySpan<byte> fileBuffer = "<PAMDataset>\n <Metadata>\n <MDI key=\"DataType\">Processed</MDI>\n <MDI key=\"SensorName\">Lowrance StructureScan3D</MDI>\n </Metadata>\n <PAMRasterBand band=\"1\">\n <Histograms>\n <HistItem>\n <HistMin>-0.5</HistMin>\n <HistMax>168.5</HistMax>\n <BucketCount>169</BucketCount>\n <IncludeOutOfRange>1</IncludeOutOfRange>\n <Approximate>1</Approximate>\n <HistCounts>194107|13838|0|0|0|0|0|0|0|0|13204|0|0|0|0|0|0|0|0|0|12328|0|0|0|11492|0|0|10383|0|0|0|9912|0|0|4621|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|4391|0|0|0|8479|0|0|7695|0|0|0|7038|0|0|6490|5879|5554|5039|4656|4477|4055|3763|3511|3250|3025|2814|2592|2443|2320|2267|1980|1861|3424|3258|2877|2640|2440|2289|2162|2879|1758|1709|1500|1480|1446|1320|2954|2051|1884|1743|1506|1425|1265|1123|1017|909|838|775|729|641|1114|938|852|731|632|547|495|441|427|364|363|317|306|292|255|361|509|315|314|314|360|252|241|204|293|178|206|185|262|294|175|271|217|139|212|182|126|155|160|80|124|1993|85|145|208|157|184|190|129|170|104|84|117|157|381|334|658|1195</HistCounts>\n </HistItem>\n </Histograms>\n <Metadata>\n <MDI key=\"STATISTICS_COVARIANCES\">1748.548089737012</MDI>\n <MDI key=\"STATISTICS_MAXIMUM\">255</MDI>\n <MDI key=\"STATISTICS_MEAN\">128</MDI>\n <MDI key=\"STATISTICS_MEDIAN\">128</MDI>\n <MDI key=\"STATISTICS_MINIMUM\">0</MDI>\n <MDI key=\"STATISTICS_SKIPFACTORX\">1</MDI>\n <MDI key=\"STATISTICS_SKIPFACTORY\">1</MDI>\n <MDI key=\"STATISTICS_STDDEV\">16</MDI>\n </Metadata>\n </PAMRasterBand>\n</PAMDataset>"u8;
using SafeFileHandle handle = File.OpenHandle(Path.ChangeExtension(originalPath, ".bmp.aux.xml"),
FileMode.Create, FileAccess.Write, FileShare.None, FileOptions.SequentialScan, fileBuffer.Length);
RandomAccess.Write(handle, fileBuffer, 0L);
}
#endregion
#region PRJ support
[SkipLocalsInit]
private static void WritePRJFile(string originalPath)
{
using SafeFileHandle handle = File.OpenHandle(Path.ChangeExtension(originalPath, ".prj"),
FileMode.Create, FileAccess.Write, FileShare.None, FileOptions.SequentialScan, 805);
RandomAccess.Write(handle,
"PROJCS[\"Lowrance_Mercator\",GEOGCS[\"Lowrance_Sphere\",DATUM[\"D_Lowrance_Sphere\",SPHEROID[\"Lowrance_Sphere\",6356752.31424518,0.0]],PRIMEM[\"Greenwich\",0.0,AUTHORITY[\"EPSG\",8901]],UNIT[\"Degree\",0.0174532925199433,AUTHORITY[\"EPSG\",9102]]],PROJECTION[\"Mercator\",AUTHORITY[\"Esri\",43004]],PARAMETER[\"False_Easting\",0.0,AUTHORITY[\"Esri\",100001]],PARAMETER[\"False_Northing\",0.0,AUTHORITY[\"Esri\",100002]],PARAMETER[\"Central_Meridian\",0.0,AUTHORITY[\"Esri\",100010]],PARAMETER[\"Standard_Parallel_1\",0.0,AUTHORITY[\"Esri\",100025]],UNIT[\"Meter\",1.0,AUTHORITY[\"EPSG\",9001]]],VERTCS[\"WGS_1984_Geoid\",VDATUM[\"WGS_1984_Geoid\",AUTHORITY[\"Esri\",105100]],PARAMETER[\"Vertical_Shift\",0.0,AUTHORITY[\"Esri\",100006]],PARAMETER[\"Direction\",1.0,AUTHORITY[\"Esri\",100007]],UNIT[\"Meter\",1.0,AUTHORITY[\"EPSG\",9001]],AUTHORITY[\"Esri\",105700]]"u8,
0);
handle.Close();
}
#endregion
#region Dispose Pattern
private bool disposedValue;
public virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
viewHandle.ReleasePointer();
viewHandle.Dispose();
viewAccessor.Dispose();
memoryMappedFile.Dispose();
}
disposedValue = true;
}
}
internal unsafe void ExportSS_Inf_Pair(string outputFolder)
{
using var ssFile = File.OpenWrite(Path.Combine(outputFolder, "SS.txt"));
using var ifFile = File.OpenWrite(Path.Combine(outputFolder, "IF.txt"));
var SideScanFrames = FrameByType[SurveyType.SideScan];
var InterferometricFrames = FrameByType[SurveyType.ThreeDimensional];
var ssStringBuilder = new StringBuilder(2800 * 4 + 2 + 2800 * 19);
var ifStringBuilder = new StringBuilder(2800 * 2 * 19);
int n = 0;
foreach (nuint ss in SideScanFrames)
{
ref readonly Frame ssFrame = ref Unsafe.AsRef<Frame>((void*)ss);
ref Frame ifFrame = ref Unsafe.AsRef<Frame>((void*)InterferometricFrames[n]);
var ssStride = (ssFrame.MaxRange / 2800d) * (.3048 * 100);
var ssHalfStep = .5 * ssStride;
ReadOnlySpan<byte> ssBytes = new(Unsafe.Add<byte>((void*)ss, ssFrame.HeaderSize), 2800);
int i = -1399; bool left = true;
foreach (byte item in ssBytes)
{
ssStringBuilder.Append(double.FusedMultiplyAdd(i, ssStride, ssHalfStep)).Append(' ').Append(item).Append(' ');
if (left && i == 0)
{ left = false; }
else i++;
}
ssStringBuilder.AppendLine();
ssFile.Write(Encoding.UTF8.GetBytes(ssStringBuilder.ToString()));
ssStringBuilder.Clear();
ref ThreeDimensionalFrameHeader frame3DHeader = ref Unsafe.AsRef<ThreeDimensionalFrameHeader>
(Unsafe.Add<byte>(Unsafe.AsPointer<Frame>(ref ifFrame), ifFrame.HeaderSize));
Span<float> leftMeasurements = new(Unsafe.Add<byte>(Unsafe.AsPointer(ref frame3DHeader), frame3DHeader.HeaderSize), frame3DHeader.NumberOfLeftBytes / 4);
Span<float> rightMeasurements = new(Unsafe.Add<byte>(Unsafe.AsPointer(ref frame3DHeader), frame3DHeader.HeaderSize + frame3DHeader.NumberOfLeftBytes), frame3DHeader.NumberOfRightBytes / 4);
Span<(float x, float y)> leftPairs = MemoryMarshal.Cast<float, (float x, float y)>(leftMeasurements).ToArray().AsSpan();
Span<(float x, float y)> rightPairs = MemoryMarshal.Cast<float, (float x, float y)>(rightMeasurements);
MemoryExtensions.Reverse(leftPairs);
for (int k = 0; k < 1400 - leftPairs.Length; k++)
{
ifStringBuilder.Append("-9999 -9999 ");
}
for (int k = 0; k < leftPairs.Length; k++)
{
ifStringBuilder.Append(leftPairs[k].x * (-.3048 * 100)).Append(' ').
Append(leftPairs[k].y * (.3048 * 100)).Append(' ');
}
for (int k = 0; k < rightPairs.Length; k++)
{
ifStringBuilder.Append(rightPairs[k].x * (.3048 * 100)).Append(' ').
Append(rightPairs[k].y * (.3048 * 100)).Append(' ');
}
for (int k = 0; k < 1400 - rightPairs.Length; k++)
{
ifStringBuilder.Append("-9999 -9999 ");
}
ifStringBuilder.AppendLine();
ifFile.Write(Encoding.UTF8.GetBytes(ifStringBuilder.ToString()));
ifStringBuilder.Clear();
n++;
}
}
internal unsafe void ExportSS_Inf_PairCNN(string outputFolder)
{
using var ssFile = File.OpenWrite(Path.Combine(outputFolder, "SS06.dat"));
var SideScanFrames = FrameByType[SurveyType.SideScan];
var ssStringBuilder = new StringBuilder(2800 * 4);
var InterferometricFrames = FrameByType[SurveyType.ThreeDimensional];
using var ifFile = File.OpenWrite(Path.Combine(outputFolder, "IF06.txt"));
var ifStringBuilder = new StringBuilder(2800 * 2 * 19);
int n = 0;
foreach (nuint ss in SideScanFrames)
{
ref readonly Frame ssFrame = ref Unsafe.AsRef<Frame>((void*)ss);
var ssStride = (ssFrame.MaxRange / 1400d);
var ssHalfStep = .5 * ssStride;
ref Frame ifFrame = ref Unsafe.AsRef<Frame>((void*)InterferometricFrames[n]);
ref ThreeDimensionalFrameHeader frame3DHeader = ref Unsafe.AsRef<ThreeDimensionalFrameHeader>
(Unsafe.Add<byte>(Unsafe.AsPointer<Frame>(ref ifFrame), ifFrame.HeaderSize));
Span<float> leftMeasurements = (new Span<float>(Unsafe.Add<byte>(Unsafe.AsPointer(ref frame3DHeader), frame3DHeader.HeaderSize), frame3DHeader.NumberOfLeftBytes / 4))[..^5];
Span<float> rightMeasurements = (new Span<float>(Unsafe.Add<byte>(Unsafe.AsPointer(ref frame3DHeader), frame3DHeader.HeaderSize + frame3DHeader.NumberOfLeftBytes), frame3DHeader.NumberOfRightBytes / 4))[..^5];
Span<(float x, float y)> leftPairs = MemoryMarshal.Cast<float, (float x, float y)>(leftMeasurements);
Span<(float x, float y)> rightPairs = MemoryMarshal.Cast<float, (float x, float y)>(rightMeasurements);
double leftMaxDist = double.Hypot(leftPairs[^1].x, leftPairs[^1].y);
double rightMaxDist = double.Hypot(rightPairs[^1].x, rightPairs[^1].y);
for (int m = 0; m < leftPairs.Length; m++)
{
ifStringBuilder.Append(leftPairs[m].x).Append(',').
Append(leftPairs[m].y).Append(',');
}
ifStringBuilder.AppendLine();
ifFile.Write(Encoding.UTF8.GetBytes(ifStringBuilder.ToString()));
ifStringBuilder.Clear();
for (int m = 0; m < rightPairs.Length; m++)
{
ifStringBuilder.Append(rightPairs[m].x).Append(',').
Append(rightPairs[m].y).Append(',');
}
ifStringBuilder.AppendLine();
ifFile.Write(Encoding.UTF8.GetBytes(ifStringBuilder.ToString()));
ifStringBuilder.Clear();
ReadOnlySpan<byte> ssBytes = new(Unsafe.Add<byte>((void*)ss, ssFrame.HeaderSize), 2800);
ssStringBuilder.Clear();
ssStringBuilder.Append("255,").Append(ssFrame.WaterDepth).Append(',');
int k = 0;
double range = double.FusedMultiplyAdd(ssStride, k, ssHalfStep);
for (int j = 1399; j >= 0 && range<=leftMaxDist; j--)
{
ssStringBuilder.Append(ssBytes[j]).Append(',').Append(range.ToString("0.######")).Append(',');
k++;
range = double.FusedMultiplyAdd(ssStride, k, ssHalfStep);
}
ssStringBuilder.AppendLine();
ssFile.Write(Encoding.UTF8.GetBytes(ssStringBuilder.ToString()));
ssStringBuilder.Clear();
ssStringBuilder.Append("255,").Append(ssFrame.WaterDepth).Append(',');
k = 0;
range = double.FusedMultiplyAdd(ssStride, k, ssHalfStep);
for (int j = 1400; j < 2800 && range<=rightMaxDist; j++)
{
ssStringBuilder.Append(ssBytes[j]).Append(',').Append(range.ToString("0.######")).Append(',');
k++;
range = double.FusedMultiplyAdd(ssStride, k, ssHalfStep);
}
ssStringBuilder.AppendLine();
ssFile.Write(Encoding.UTF8.GetBytes(ssStringBuilder.ToString()));
ssStringBuilder.Clear();
n++;
}
}
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
#endregion Dispose Pattern
};