Skip to content

Commit

Permalink
adding writeSupport for yUnit fields (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: magnaw <[email protected]>
  • Loading branch information
pohldk and magnusenevoldsen authored Jan 20, 2022
1 parent 53380b5 commit 6f7c192
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/SpaFileReader/SpaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public static Span<byte> ReadYUnitAsSpanByte(ref Span<byte> bytes)
return yUnitAsBytes;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<byte> WriteYUnitAsSpanByte(Span<byte> bytes, Span<byte> yUnits)
{
return WriteByteAt(ref bytes, YUnitFlag, yUnits);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<float> ReadYUnitAsSpanFloat(ref Span<byte> bytes)
{
Expand All @@ -132,6 +138,12 @@ public static Span<float> ReadYUnitAsSpanFloat(ref Span<byte> bytes)
return yUnitAsFloats;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<byte> WriteYUnitAsSpanFloat(Span<byte> bytes, Span<float> yUnit)
{
return WriteByteAt(ref bytes, YUnitFlag, MemoryMarshal.Cast<float, byte>(yUnit));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<byte> ReadInterferogramAsSpanByte(ref Span<byte> bytes)
{
Expand Down Expand Up @@ -236,4 +248,15 @@ public static int ReadInt32At(this ref Span<byte> data, int offset)
? 0
: MemoryMarshal.Read<int>(data.Slice(offset, 4));
}
private static Span<byte> WriteByteAt(this ref Span<byte> data, short expectedFlag, Span<byte> bytesToWrite)
{
var (start, length) = ReadSpecificFlagPositions(ref data, expectedFlag);
var n = 0;
for (var i = start; start + length > i; i ++)
{
data[i] = bytesToWrite[n];
n++;
}
return data;
}
}
33 changes: 33 additions & 0 deletions tests/SpaFileReader.Tests/WriterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace SpaFileReader.Tests;

public class WriterTest
{
private readonly byte[] _bytes;
private readonly byte[] _noInterferogramBytes;

public WriterTest()
{
_bytes = File.ReadAllBytes(TestFile("test1.spa"));
_noInterferogramBytes = File.ReadAllBytes(TestFile("test2.spa"));
}

[Fact]
public void WriteYUnitAsBytesTest()
{
var convertedSpa = SpaFile.WriteYUnitAsSpanByte(_bytes, SpaFile.ReadYUnitAsSpanByte(_noInterferogramBytes));
var spaToWrite = SpaFile.ReadYUnitAsFloatArray(_noInterferogramBytes);
var writtenSpa = SpaFile.ReadYUnitAsFloatArray(convertedSpa.ToArray());
Assert.Equal(spaToWrite, writtenSpa);
}

[Fact]
public void WriteYUnitAsFloatsTest()
{
var convertedSpa = SpaFile.WriteYUnitAsSpanFloat( _bytes, SpaFile.ReadYUnitAsFloatArray(_noInterferogramBytes));
var spaToWrite = SpaFile.ReadYUnitAsFloatArray(_noInterferogramBytes);
var writtenSpa = SpaFile.ReadYUnitAsFloatArray(convertedSpa.ToArray());
Assert.Equal(spaToWrite, writtenSpa);
}


}

0 comments on commit 6f7c192

Please sign in to comment.