/// <summary> /// Writes the binary file header. /// </summary> /// <param name="binaryHeader">The binary file header to write.</param> public void Write(SegyFileHeader binaryHeader) { // write binary file header binaryHeader.DataSampleFormatCode = (short)FormatCode.IbmFloatingPoint4; _writer.BaseStream.Position = TextHeaderSize; _writer.Write(binaryHeader.GetBytes()); }
/// <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)); }