/// <summary> /// Serializes the trace into a byte array. /// </summary> /// <returns>an array of bytes representing both the trace header and data</returns> public byte[] GetBytes() { byte[] headerBytes = Header.GetBytes(); byte[] traceBytes = new byte[headerBytes.Length + Data.Length * sizeof(float)]; float[] traceDataCpy = new float[Data.Length]; byte[] traceDataBytes = new byte[Data.Length * sizeof(float)]; // copy header bytes to output byte array Buffer.BlockCopy(headerBytes, 0, traceBytes, 0, headerBytes.Length); // copy data float array to copy array Buffer.BlockCopy(Data, 0, traceDataCpy, 0, traceDataCpy.Length * sizeof(float)); IbmConverter.float_to_ibm(traceDataCpy, traceDataCpy, traceDataCpy.Length); Buffer.BlockCopy(traceDataCpy, 0, traceDataBytes, 0, traceDataBytes.Length); for (int i = 0; i < traceDataCpy.Length; i++) { //var ibmBytes = IbmConverter.GetBytes(Data[i]); for (int j = 0; j < 4; j++) { traceBytes[240 + i * 4 + j] = traceDataBytes[i * 4 + j]; } } return(traceBytes); }
private static void VerifyToSingleReturns(float expected, byte[] value) { // Act float result = IbmConverter.ToSingle(value); // Assert result.Should().BeInRange(expected - 0.0001f, expected + 0.0001f); }
private static void VerifySingleConversion(Single value) { byte[] result = IbmConverter.GetBytes(value); var reverseValue = IbmConverter.ToSingle(result); var epsilon = 0.0001f; reverseValue.Should().BeInRange(value - epsilon, value + epsilon); }
public void DecimalShouldBeTheSame() { var expected = (decimal)123.45; var bytes = IbmConverter.GetBytes(expected); var result = IbmConverter.ToUnpackedDecimal(bytes, 2); result.Should().Be(expected); }
public void FillTextHeader(TextHeaderEncoding TextHeaderEncoding, BinaryReader InSgyBinReader) { InSgyBinReader.BaseStream.Seek(0, SeekOrigin.Begin); var bytes = InSgyBinReader.ReadBytes(3200); Text = (TextHeaderEncoding == TextHeaderEncoding.ASCII) ? ASCIIEncoding.Default.GetString(bytes) : IbmConverter.ToString(bytes); }
/// <summary> /// Given a BinaryReader reads the text header. /// Assumes the BinaryReader is at the start of the file. /// </summary> public virtual string ReadTextHeader(BinaryReader reader) { var textHeaderLength = Options.TextHeaderColumnCount * Options.TextHeaderRowCount; var bytes = reader.ReadBytes(textHeaderLength); string text = (bytes[0] == 'C') || Options.IsEbcdic == false? ASCIIEncoding.Default.GetString(bytes) : IbmConverter.ToString(bytes); return(Options.TextHeaderInsertNewLines ? InsertNewLines(text) : text); }
public void ShouldConvertFromEbcdicToUnicode2() { // Arrange var bytes = new byte[] { _l, _o, _l, _bang }; // Act string result = IbmConverter.ToString(bytes); // Assert result.Should().Be("lol!"); }
public void ShouldIgnoreTrailingBytesInt32() { // Arrange var bytes = new byte[] { 0, 0, 0, 1, 99, 99 }; // Act var result = IbmConverter.ToInt32(bytes); // Assert result.Should().Be(1); }
public void ShouldConvertNegativeInt32() { // Arrange var bytes = new byte[] { 0x89, 0xAB, 0xCD, 0xEF }; // Act var result = IbmConverter.ToInt32(bytes); // Assert result.Should().Be(-1985229329); }
public void ShouldConvertZeroInt32() { // Arrange var bytes = new byte[4]; // Act Int32 result = IbmConverter.ToInt32(bytes); // Assert result.Should().Be(0); }
public void ShouldConvertFromInt16() { // Arrange Int16 value = -21555; // Act byte[] result = IbmConverter.GetBytes(value); // Assert result.Should().Equal(new byte[] { 0xAB, 0xCD }); }
public void ShouldConvertNegativeInt16() { // Arrange var value = new byte[] { 0xAB, 0xCD }; // Act var result = IbmConverter.ToInt16(value); // Assert result.Should().Be(-21555); }
public void ShouldConvertFromEbcdicToUnicode() { // Arrange var bytes = new byte[] { _H, _e, _l, _l, _o, _comma }; // Act string result = IbmConverter.ToString(bytes); // Assert result.Should().Be("Hello,"); }
public void ShouldConvertZeroInt16() { // Arrange var value = new byte[2]; // Act Int16 result = IbmConverter.ToInt16(value); // Assert result.Should().Be(0); }
public void ShouldConvertFromUnicodeToEbcdic() { // Arrange var value = "Hello,"; // Act byte[] result = IbmConverter.GetBytes(value); // Assert result.Should().Equal(new byte[] { _H, _e, _l, _l, _o, _comma }); }
public void ShouldConvertFromInt32() { // Arrange Int32 value = -1985229329; // Act byte[] result = IbmConverter.GetBytes(value); // Assert result.Should().Equal(new byte[] { 0x89, 0xAB, 0xCD, 0xEF }); }
public void ShouldConvertToStringGivenStartingIndex2() { // Arrange var bytes = new byte[] { _H, _e, _l, _l, _o }; var startingIndex = 1; // Act string result = IbmConverter.ToString(bytes, startingIndex); // Assert result.Should().Be("ello"); }
public void ShouldConvertFromStringGivenStartingIndex() { // Arrange var value = "lol SHe!"; var startingIndex = 5; // Act byte[] result = IbmConverter.GetBytes(value, startingIndex); // Assert result.Should().Equal(new byte[] { _H, _e, _bang }); }
public void ShouldConvertAnotherInt16WithStartIndex() { // Arrange var value = new byte[] { 99, 99, 2, 0, 99 }; var startIndex = 2; // Act Int16 result = IbmConverter.ToInt16(value, startIndex); // Assert result.Should().Be(512); }
public void ShouldConvertInt32WithStartIndex() { // Arrange var value = new byte[] { 99, 99, 99, 0, 0, 0, 1, 99 }; var startIndex = 3; // Act Int32 result = IbmConverter.ToInt32(value, startIndex); // Assert result.Should().Be(1); }
public void ShouldConvertToStringGivenStartingIndex() { // Arrange var bytes = new byte[] { _bang, _bang, _H, _e }; var startingIndex = 2; // Act string result = IbmConverter.ToString(bytes, startingIndex); // Assert result.Should().Be("He"); }
public void ShouldConvertAnotherInt32WithStartIndex() { // Arrange var value = new byte[] { 99, 99, 1, 2, 4, 0, 99 }; var startIndex = 2; // Act var result = IbmConverter.ToInt32(value, startIndex); // Assert result.Should().Be(16909312); }
public void ShouldConvertFromStringGivenStartingIndexAndLength() { // Arrange var value = "Hole in the ground"; var startingIndex = 1; var length = 3; // Act byte[] result = IbmConverter.GetBytes(value, startingIndex, length); // Assert result.Should().Equal(new byte[] { _o, _l, _e }); }
public void ShouldConvertToStringGivenIndexAndLength2() { // Arrange var bytes = new byte[] { _H, _o, _H, _o, _H, _o, _l, _e, _bang }; var startingIndex = 0; var length = 6; // Act string result = IbmConverter.ToString(bytes, startingIndex, length); // Assert result.Should().Be("HoHoHo"); }
/// <summary> /// Gets an array of floats from a big endian encoded byte array /// </summary> /// <param name="trBytes">The byte array to interpret</param> /// <param name="sampleFormat">The sample format</param> /// <param name="sampleCount">the number of samples to read</param> /// <returns>The float array</returns> private static float[] GetData(byte[] trBytes, FormatCode sampleFormat, int sampleCount) { float[] traceData = new float[sampleCount]; switch (sampleFormat) { case FormatCode.IbmFloatingPoint4: Buffer.BlockCopy(trBytes, 0, traceData, 0, trBytes.Length); IbmConverter.ibm_to_float(traceData, traceData, traceData.Length); break; case FormatCode.IeeeFloatingPoint4: ByteConverter.ToSingles(trBytes, ref traceData); break; case FormatCode.TwosComplementInteger1: for (int j = 0; j < sampleCount; j++) { traceData[j] = trBytes[j] < 128 ? trBytes[j] : trBytes[j] - 256; } break; case FormatCode.TwosComplementInteger2: for (int j = 0; j < sampleCount; j++) { traceData[j] = IbmConverter.ToInt16(new[] { trBytes[(j * 2) + 1], trBytes[(j * 2) + 0] }); } break; case FormatCode.TwosComplementInteger4: for (int j = 0; j < sampleCount; j++) { traceData[j] = BitConverter.ToInt32(new[] { trBytes[(j * 4) + 3], trBytes[(j * 4) + 2], trBytes[(j * 4) + 1], trBytes[(j * 4) + 0] }, 0); } break; default: throw new NotSupportedException($"Unsupported sample format: {sampleFormat}"); } return(traceData); }
public void IbmToIeeeMethod(float[] from, float[] to) { IbmConverter.ibm_to_float(from, to, from.Length); }
public byte[] GetBytes() { byte[] bytes = new byte[240]; // TODO : performance optimize this by removing all "Concat" statements. Insert(bytes, IbmConverter.GetBytes(TraceNumInLine), 0); Insert(bytes, IbmConverter.GetBytes(TraceNumInFile), 4); Insert(bytes, IbmConverter.GetBytes(ShotNumOrStackTraceNum), 8); Insert(bytes, IbmConverter.GetBytes(TraceNumInShot), 12); Insert(bytes, IbmConverter.GetBytes(EnergySourcePtNum), 16); Insert(bytes, IbmConverter.GetBytes(CdpNum), 20); Insert(bytes, IbmConverter.GetBytes(TraceNumber), 24); Insert(bytes, IbmConverter.GetBytes((Int16)TraceId), 28); Insert(bytes, IbmConverter.GetBytes((Int16)NumVerticalStackedTraces), 30); Insert(bytes, IbmConverter.GetBytes((Int16)CdpFold), 32); Insert(bytes, IbmConverter.GetBytes((Int16)DataUse), 34); Insert(bytes, IbmConverter.GetBytes(SourceReceiverDistance), 36); Insert(bytes, IbmConverter.GetBytes(RecieverGroupElevation), 40); Insert(bytes, IbmConverter.GetBytes(SurfaceElevationAtSource), 44); Insert(bytes, IbmConverter.GetBytes(SourceDepthBelowSurf), 48); Insert(bytes, IbmConverter.GetBytes(DatumElevAtRecieverGroup), 52); Insert(bytes, IbmConverter.GetBytes(DatumElevationAtSource), 56); Insert(bytes, IbmConverter.GetBytes(WaterDepthAtSource), 60); Insert(bytes, IbmConverter.GetBytes(WaterDepthAtRecieverGroup), 64); Insert(bytes, IbmConverter.GetBytes((Int16)ScalarForElevationAndDepth), 68); Insert(bytes, IbmConverter.GetBytes((Int16)ScalarForCoordinates), 70); Insert(bytes, IbmConverter.GetBytes(XSourceCoordinate), 72); Insert(bytes, IbmConverter.GetBytes(YSourceCoordinate), 76); Insert(bytes, IbmConverter.GetBytes(XRecieverGroupCoordinate), 80); Insert(bytes, IbmConverter.GetBytes(YRecieverGroupCoordinate), 84); Insert(bytes, IbmConverter.GetBytes((Int16)CoordinateUnit), 88); Insert(bytes, IbmConverter.GetBytes((Int16)WeatheringVelocity), 90); Insert(bytes, IbmConverter.GetBytes((Int16)SubweatheringVelocity), 92); Insert(bytes, IbmConverter.GetBytes((Int16)UpholeTimeAtSource), 94); Insert(bytes, IbmConverter.GetBytes((Int16)UpholeTimeAtReceiverGroup), 96); Insert(bytes, IbmConverter.GetBytes((Int16)SourceStaticCorrection), 98); Insert(bytes, IbmConverter.GetBytes((Int16)ReceiverGroupStaticCorrection), 100); Insert(bytes, IbmConverter.GetBytes((Int16)TotalStaticApplied), 102); Insert(bytes, IbmConverter.GetBytes((Int16)HeaderTimeBreakLagMs), 104); Insert(bytes, IbmConverter.GetBytes((Int16)TimeBreakShotLagMs), 106); Insert(bytes, IbmConverter.GetBytes((Int16)ShotRecordingLag), 108); Insert(bytes, IbmConverter.GetBytes((Int16)MuteTimeStart), 110); Insert(bytes, IbmConverter.GetBytes((Int16)MuteTimeEnd), 112); Insert(bytes, IbmConverter.GetBytes((UInt16)SampleCount), _nsStart); Insert(bytes, IbmConverter.GetBytes((Int16)SampleIntervalMs), 116); Insert(bytes, IbmConverter.GetBytes((Int16)GainType), 118); Insert(bytes, IbmConverter.GetBytes((Int16)GainConst), 120); Insert(bytes, IbmConverter.GetBytes((Int16)EarlyGainDb), 122); Insert(bytes, IbmConverter.GetBytes((Int16)Correlated), 124); Insert(bytes, IbmConverter.GetBytes((Int16)SweepFrequencyStart), 126); Insert(bytes, IbmConverter.GetBytes((Int16)SweepFrequencyEnd), 128); Insert(bytes, IbmConverter.GetBytes((Int16)SweepLengthMs), 130); Insert(bytes, IbmConverter.GetBytes((Int16)SweepType), 132); Insert(bytes, IbmConverter.GetBytes((Int16)SweepTaperTraceLengthStartMs), 134); Insert(bytes, IbmConverter.GetBytes((Int16)SweepTaperTraceLengthEndMs), 136); Insert(bytes, IbmConverter.GetBytes((Int16)TaperType), 138); Insert(bytes, IbmConverter.GetBytes((Int16)AliasFilterFrequency), 140); Insert(bytes, IbmConverter.GetBytes((Int16)AliasFilterSlope), 142); Insert(bytes, IbmConverter.GetBytes((Int16)NotchFilterFrequency), 144); Insert(bytes, IbmConverter.GetBytes((Int16)NotchFilterSlope), 146); Insert(bytes, IbmConverter.GetBytes((Int16)LowCutFrequency), 148); Insert(bytes, IbmConverter.GetBytes((Int16)HighCutFrequency), 150); Insert(bytes, IbmConverter.GetBytes((Int16)LowCutSlope), 152); Insert(bytes, IbmConverter.GetBytes((Int16)HighCutSlope), 154); Insert(bytes, IbmConverter.GetBytes((Int16)Yr), 156); Insert(bytes, IbmConverter.GetBytes((Int16)Day), 158); Insert(bytes, IbmConverter.GetBytes((Int16)Hour), 160); Insert(bytes, IbmConverter.GetBytes((Int16)Minute), 162); Insert(bytes, IbmConverter.GetBytes((Int16)Second), 164); Insert(bytes, IbmConverter.GetBytes((Int16)TimeBasis), 166); Insert(bytes, IbmConverter.GetBytes((Int16)TraceWeightFactor), 168); Insert(bytes, IbmConverter.GetBytes((Int16)GeophoneGroupNumOfRollSwitchPositionOne), 170); Insert(bytes, IbmConverter.GetBytes((Int16)GeophoneGroupNumOfFirstTraceOrigRecord), 172); Insert(bytes, IbmConverter.GetBytes((Int16)GeophoneGroupNumOfLastTraceOrigRecord), 174); Insert(bytes, IbmConverter.GetBytes((Int16)GapSize), 176); Insert(bytes, IbmConverter.GetBytes((Int16)TaperOverTravel), 178); Insert(bytes, IbmConverter.GetBytes(Local1), 180); Insert(bytes, IbmConverter.GetBytes(Local2), 184); Insert(bytes, IbmConverter.GetBytes(Local3), 188); Insert(bytes, IbmConverter.GetBytes(Local4), 192); Insert(bytes, IbmConverter.GetBytes(Local5), 196); Insert(bytes, IbmConverter.GetBytes(Local6), 200); Insert(bytes, IbmConverter.GetBytes(NumTr), 204); Insert(bytes, IbmConverter.GetBytes((Int16)Mark), 208); Insert(bytes, IbmConverter.GetBytes((Int16)ShortPad), 210); Insert(bytes, IbmConverter.GetBytes(Local7), 212); Insert(bytes, IbmConverter.GetBytes(Local8), 216); Insert(bytes, IbmConverter.GetBytes(Local9), 220); Insert(bytes, IbmConverter.GetBytes(Local10), 224); Insert(bytes, IbmConverter.GetBytes(Local11), 228); Insert(bytes, IbmConverter.GetBytes(Local12), 232); Insert(bytes, IbmConverter.GetBytes(Local13), 236); return(bytes); }
/// <summary> /// Ctor /// </summary> /// <param name="filepath">The path string of the file</param> public SegyReader(string filepath) { CodeContract.Requires(!string.IsNullOrEmpty(filepath)); CodeContract.Requires <FileNotFoundException>(File.Exists(filepath), $"File {filepath} was not found."); _filepath = filepath; _stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Write); _reader = new BinaryReader(_stream); FileInfo = new FileInfo(filepath); // // Data Endianess // _reader.BaseStream.Seek(3224, SeekOrigin.Begin); var formatBytes = _reader.ReadBytes(2); var lilEndianFormatCode = BitConverter.ToInt16(formatBytes, 0); var bigEndianFormatCode = IbmConverter.ToInt16(formatBytes, 0); bool endianessSuccess = false; for (var i = 0; i <= 8; i++) { if (bigEndianFormatCode == i) { IsLittleEndian = false; endianessSuccess = true; } if (lilEndianFormatCode == i) { IsLittleEndian = true; endianessSuccess = true; } } if (endianessSuccess == false) { throw new SegyEndianessException("Cannot infer endianess from format code. format code value is not within range 0 - 8 in either endianess"); } // // File Binary Header // _reader.BaseStream.Seek(TextHeaderBytesCount, SeekOrigin.Begin); byte[] binaryHeaderBytes = _reader.ReadBytes(BinaryHeaderBytesCount); FileBinaryHeader = SegyFileHeader.From(binaryHeaderBytes, IsLittleEndian); FileBinaryHeaderBytes = binaryHeaderBytes; // // File Textual Headers // _stream.Seek(0, SeekOrigin.Begin); List <string> textFileHeaders = new List <string>(); byte[] bytes = _reader.ReadBytes(TextHeaderBytesCount); string textHeader = bytes[0] == 'C' ? Encoding.Default.GetString(bytes) : _textHeaderEncoding.GetString(bytes); textFileHeaders.Add(textHeader); // as per rev 1, all data values are assumed big endian var binaryFileHeader = FileBinaryHeader; _stream.Seek(3600, SeekOrigin.Begin); byte[] extendedFileHeaders = binaryFileHeader.ExtendedTextHeadersCount < 0 ? new byte[0] : _reader.ReadBytes(binaryFileHeader.ExtendedTextHeadersCount * 3200); for (int i = 0; i < binaryFileHeader.ExtendedTextHeadersCount; i++) { var extTextHeaderBytes = new byte[3200]; Buffer.BlockCopy(extendedFileHeaders, i * 3200, bytes, 0, 3200); var extendedHeader = bytes[0] == 'C' ? Encoding.Default.GetString(extTextHeaderBytes) : _textHeaderEncoding.GetString(extTextHeaderBytes); textFileHeaders.Add(extendedHeader); } FileTextualHeaders = textFileHeaders.ToArray(); // //Trace Count // var sampleformat = (FormatCode)FileBinaryHeader.DataSampleFormatCode; var dataStartIndex = TextHeaderBytesCount + BinaryHeaderBytesCount + TextHeaderBytesCount * (FileTextualHeaders.Length - 1); var dataEndIndex = _stream.Length; var sampleSz = SizeFrom(sampleformat); var fileSz = _stream.Length; var nsamples = FileBinaryHeader.SamplesPerTraceOfFile; var extTxtHdrCt = FileTextualHeaders.Length - 1; TraceCount = (fileSz - (3600 + (extTxtHdrCt * 3200))) / (240 + (nsamples * sampleSz)); }
private static int ToInt32(byte[] bytes, int index, bool isLittleEndian) { return(isLittleEndian ? BitConverter.ToInt32(bytes, index) : IbmConverter.ToInt32(bytes, index)); }
public void SampleValueFromWikipediaToBytes() { IbmConverter.GetBytes(_wikipediaSingle).Should().Equal(GetWikipediaSampleBytes()); }