def readSEGY(filename, headonly=False, byteorder=None, textual_header_encoding=None, unpack_trace_headers=False, **kwargs): # @UnusedVariable """ Reads a SEG Y file and returns an ObsPy Stream object. .. warning:: This function should NOT be called directly, it registers via the ObsPy :func:`~obspy.core.stream.read` function, call this instead. :type filename: str :param filename: SEG Y rev1 file to be read. :type headonly: bool, optional :param headonly: If set to True, read only the header and omit the waveform data. :type byteorder: str or ``None`` :param byteorder: Determines the endianness of the file. Either ``'>'`` for big endian or ``'<'`` for little endian. If it is ``None``, it will try to autodetect the endianness. The endianness is always valid for the whole file. Defaults to ``None``. :type textual_header_encoding: str or ``None`` :param textual_header_encoding: The encoding of the textual header. Can be ``'EBCDIC'``, ``'ASCII'`` or ``None``. If it is ``None``, autodetection will be attempted. Defaults to ``None``. :type unpack_trace_headers: bool, optional :param unpack_trace_headers: Determines whether or not all trace header values will be unpacked during reading. If ``False`` it will greatly enhance performance and especially memory usage with large files. The header values can still be accessed and will be calculated on the fly but tab completion will no longer work. Look in the headers.py for a list of all possible trace header values. Defaults to ``False``. :returns: A ObsPy :class:`~obspy.core.stream.Stream` object. .. rubric:: Example >>> from obspy import read >>> st = read("/path/to/00001034.sgy_first_trace") >>> st # doctest: +ELLIPSIS <obspy.core.stream.Stream object at 0x...> >>> print(st) # doctest: +ELLIPSIS 1 Trace(s) in Stream: Seq. No. in line: 1 | 2009-06-22T14:47:37.000000Z - ... 2001 samples """ # Read file to the internal segy representation. segy_object = readSEGYrev1(filename, endian=byteorder, textual_header_encoding=textual_header_encoding, unpack_headers=unpack_trace_headers) # Create the stream object. stream = Stream() # SEGY has several file headers that apply to all traces. They will be # stored in Stream.stats. stream.stats = AttribDict() # Get the textual file header. textual_file_header = segy_object.textual_file_header # The binary file header will be a new AttribDict binary_file_header = AttribDict() for key, value in segy_object.binary_file_header.__dict__.items(): setattr(binary_file_header, key, value) # Get the data encoding and the endianness from the first trace. data_encoding = segy_object.traces[0].data_encoding endian = segy_object.traces[0].endian textual_file_header_encoding = segy_object.textual_header_encoding.upper() # Add the file wide headers. stream.stats.textual_file_header = textual_file_header stream.stats.binary_file_header = binary_file_header # Also set the data encoding, endianness and the encoding of the # textual_file_header. stream.stats.data_encoding = data_encoding stream.stats.endian = endian stream.stats.textual_file_header_encoding = \ textual_file_header_encoding # Loop over all traces. for tr in segy_object.traces: # Create new Trace object for every segy trace and append to the Stream # object. trace = Trace() stream.append(trace) # skip data if headonly is set if headonly: trace.stats.npts = tr.npts else: trace.data = tr.data trace.stats.segy = AttribDict() # If all values will be unpacked create a normal dictionary. if unpack_trace_headers: # Add the trace header as a new attrib dictionary. header = AttribDict() for key, value in tr.header.__dict__.items(): setattr(header, key, value) # Otherwise use the LazyTraceHeaderAttribDict. else: # Add the trace header as a new lazy attrib dictionary. header = LazyTraceHeaderAttribDict(tr.header.unpacked_header, tr.header.endian) trace.stats.segy.trace_header = header # The sampling rate should be set for every trace. It is a sample # interval in microseconds. The only sanity check is that is should be # larger than 0. tr_header = trace.stats.segy.trace_header if tr_header.sample_interval_in_ms_for_this_trace > 0: trace.stats.delta = \ float(tr.header.sample_interval_in_ms_for_this_trace) / \ 1E6 # If the year is not zero, calculate the start time. The end time is # then calculated from the start time and the sampling rate. if tr_header.year_data_recorded > 0: year = tr_header.year_data_recorded # The SEG Y rev 0 standard specifies the year to be a 4 digit # number. Before that it was unclear if it should be a 2 or 4 # digit number. Old or wrong software might still write 2 digit # years. Every number <30 will be mapped to 2000-2029 and every # number between 30 and 99 will be mapped to 1930-1999. if year < 100: if year < 30: year += 2000 else: year += 1900 julday = tr_header.day_of_year hour = tr_header.hour_of_day minute = tr_header.minute_of_hour second = tr_header.second_of_minute trace.stats.starttime = UTCDateTime( year=year, julday=julday, hour=hour, minute=minute, second=second) return stream
def readSEGY(filename, headonly=False, byteorder=None, textual_header_encoding=None, unpack_trace_headers=False, **kwargs): # @UnusedVariable """ Reads a SEG Y file and returns an ObsPy Stream object. .. warning:: This function should NOT be called directly, it registers via the ObsPy :func:`~obspy.core.stream.read` function, call this instead. :type filename: str :param filename: SEG Y rev1 file to be read. :type headonly: boolean, optional :param headonly: If set to True, read only the header and omit the waveform data. :type byteorder: ``'<'``, ``'>'``, or ``None`` :param byteorder: Determines the endianness of the file. Either ``'>'`` for big endian or ``'<'`` for little endian. If it is ``None``, it will try to autodetect the endianness. The endianness is always valid for the whole file. Defaults to ``None``. :type textual_header_encoding: ``'EBCDIC'``, ``'ASCII'`` or ``None`` :param textual_header_encoding: The encoding of the textual header. If it is ``None``, autodetection will be attempted. Defaults to ``None``. :type unpack_trace_headers: bool, optional :param unpack_trace_headers: Determines whether or not all trace header values will be unpacked during reading. If ``False`` it will greatly enhance performance and especially memory usage with large files. The header values can still be accessed and will be calculated on the fly but tab completion will no longer work. Look in the headers.py for a list of all possible trace header values. Defaults to ``False``. :returns: A ObsPy :class:`~obspy.core.stream.Stream` object. .. rubric:: Example >>> from obspy import read >>> st = read("/path/to/00001034.sgy_first_trace") >>> st # doctest: +ELLIPSIS <obspy.core.stream.Stream object at 0x...> >>> print(st) # doctest: +ELLIPSIS 1 Trace(s) in Stream: Seq. No. in line: 1 | 2009-06-22T14:47:37.000000Z - ... 2001 samples """ # Read file to the internal segy representation. segy_object = readSEGYrev1(filename, endian=byteorder, textual_header_encoding=textual_header_encoding, unpack_headers=unpack_trace_headers) # Create the stream object. stream = Stream() # SEGY has several file headers that apply to all traces. They will be # stored in Stream.stats. stream.stats = AttribDict() # Get the textual file header. textual_file_header = segy_object.textual_file_header # The binary file header will be a new AttribDict binary_file_header = AttribDict() for key, value in segy_object.binary_file_header.__dict__.iteritems(): setattr(binary_file_header, key, value) # Get the data encoding and the endianness from the first trace. data_encoding = segy_object.traces[0].data_encoding endian = segy_object.traces[0].endian textual_file_header_encoding = segy_object.textual_header_encoding.upper() # Add the file wide headers. stream.stats.textual_file_header = textual_file_header stream.stats.binary_file_header = binary_file_header # Also set the data encoding, endianness and the encoding of the # textual_file_header. stream.stats.data_encoding = data_encoding stream.stats.endian = endian stream.stats.textual_file_header_encoding = \ textual_file_header_encoding # Loop over all traces. for tr in segy_object.traces: # Create new Trace object for every segy trace and append to the Stream # object. trace = Trace() stream.append(trace) # skip data if headonly is set if headonly: trace.stats.npts = tr.npts else: trace.data = tr.data trace.stats.segy = AttribDict() # If all values will be unpacked create a normal dictionary. if unpack_trace_headers: # Add the trace header as a new attrib dictionary. header = AttribDict() for key, value in tr.header.__dict__.iteritems(): setattr(header, key, value) # Otherwise use the LazyTraceHeaderAttribDict. else: # Add the trace header as a new lazy attrib dictionary. header = LazyTraceHeaderAttribDict(tr.header.unpacked_header, tr.header.endian) trace.stats.segy.trace_header = header # The sampling rate should be set for every trace. It is a sample # interval in microseconds. The only sanity check is that is should be # larger than 0. tr_header = trace.stats.segy.trace_header if tr_header.sample_interval_in_ms_for_this_trace > 0: trace.stats.delta = \ float(tr.header.sample_interval_in_ms_for_this_trace) / \ 1E6 # If the year is not zero, calculate the start time. The end time is # then calculated from the start time and the sampling rate. if tr_header.year_data_recorded > 0: year = tr_header.year_data_recorded # The SEG Y rev 0 standard specifies the year to be a 4 digit # number. Before that it was unclear if it should be a 2 or 4 # digit number. Old or wrong software might still write 2 digit # years. Every number <30 will be mapped to 2000-2029 and every # number between 30 and 99 will be mapped to 1930-1999. if year < 100: if year < 30: year += 2000 else: year += 1900 julday = tr_header.day_of_year hour = tr_header.hour_of_day minute = tr_header.minute_of_hour second = tr_header.second_of_minute trace.stats.starttime = UTCDateTime(year=year, julday=julday, hour=hour, minute=minute, second=second) return stream