Пример #1
0
 def test_readAndWriteBinaryFileHeader(self):
     """
     Reading and writing should not change the binary file header.
     """
     for file, attribs in self.files.items():
         endian = attribs['endian']
         file = os.path.join(self.path, file)
         # Read the file.
         with open(file, 'rb') as f:
             f.seek(3200)
             org_header = f.read(400)
         header = SEGYBinaryFileHeader(header=org_header, endian=endian)
         # The header writes to a file like object.
         new_header = io.BytesIO()
         header.write(new_header)
         new_header.seek(0, 0)
         new_header = new_header.read()
         # Assert the correct length.
         self.assertEqual(len(new_header), 400)
         # Assert the actual header.
         self.assertEqual(org_header, new_header)
Пример #2
0
def writeSEGY(stream, filename, data_encoding=None, byteorder=None,
              textual_header_encoding=None, **kwargs):  # @UnusedVariable
    """
    Writes a SEG Y file from given ObsPy Stream object.

    .. warning::
        This function should NOT be called directly, it registers via the
        the :meth:`~obspy.core.stream.Stream.write` method of an
        ObsPy :class:`~obspy.core.stream.Stream` object, call this instead.

    :type stream: :class:`~obspy.core.stream.Stream`
    :param stream: The ObsPy Stream object to write.
    :type filename: str
    :param filename: Name of file to write.
    :type data_encoding: int
    :param data_encoding: The data encoding is an integer with the following
        currently supported meaning:

            ``1``
                4 byte IBM floating points (float32)
            ``2``
                4 byte Integers (int32)
            ``3``
                2 byte Integer (int16)
            ``5``
                4 byte IEEE floating points (float32)

        The value in the brackets is the necessary dtype of the data. ObsPy
        will now automatically convert the data because data might change/loose
        precision during the conversion so the user has to take care of the
        correct dtype.

        If it is ``None``, the value of the first Trace will be used for all
        consecutive Traces. If it is None for the first Trace, 1 (IBM floating
        point numbers) will be used. Different data encodings for different
        traces are currently not supported because these will most likely not
        be readable by other software.
    :type byteorder: str or ``None``
    :param byteorder: Determines the endianness of the file. Either ``'>'`` for
        big endian or ``'<'`` for little endian. If is ``None``, it will either
        be the endianness of the first Trace or if that is also not set, it
        will be big endian. A mix between little and big endian for the headers
        and traces is currently not supported.
    :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``, the
        textual_file_header_encoding attribute in the stats.segy dictionary of
        the first Trace is used and if that is not set, ASCII will be used.

    This function will automatically set the data encoding field of the binary
    file header so the user does not need to worry about it.

    The starttime of every trace is not a required field in the SEG Y
    specification. If the starttime of a trace is UTCDateTime(0) it will be
    interpreted as a not-set starttime and no time is written to the trace
    header. Every other time will be written.

    SEG Y supports a sample interval from 1 to 65535 microseconds in steps of 1
    microsecond. Larger intervals cannot be supported due to the definition of
    the SEG Y format. Therefore the smallest possible sampling rate is ~ 15.26
    Hz. Please keep that in mind.
    """
    # Some sanity checks to catch invalid arguments/keyword arguments.
    if data_encoding is not None and data_encoding not in VALID_FORMATS:
        msg = "Invalid data encoding."
        raise SEGYCoreWritingError(msg)
    # Figure out the data encoding if it is not set.
    if data_encoding is None:
        if hasattr(stream, 'stats') and hasattr(stream.stats, 'data_encoding'):
            data_encoding = stream.stats.data_encoding
        if hasattr(stream, 'stats') and hasattr(stream.stats,
                                                'binary_file_header'):
            data_encoding = \
                stream.stats.binary_file_header.data_sample_format_code
        # Set it to float if it in not given.
        else:
            data_encoding = 1

    # Create empty file wide headers if they do not exist.
    if not hasattr(stream, 'stats'):
        stream.stats = AttribDict()
    if not hasattr(stream.stats, 'textual_file_header'):
        stream.stats.textual_file_header = b""
    if not hasattr(stream.stats, 'binary_file_header'):
        stream.stats.binary_file_header = SEGYBinaryFileHeader()

    # Valid dtype for the data encoding.
    valid_dtype = DATA_SAMPLE_FORMAT_CODE_DTYPE[data_encoding]
    # Makes sure that the dtype is for every Trace is correct.
    for trace in stream:
        # Check the dtype.
        if trace.data.dtype != valid_dtype:
            msg = """
            The dtype of the data and the chosen data_encoding do not match.
            You need to manually convert the dtype if you want to use that
            data_encoding. Please refer to the obspy.segy manual for more
            details.
            """.strip()
            raise SEGYCoreWritingError(msg)
        # Check the sample interval.
        if trace.stats.delta > MAX_INTERVAL_IN_SECONDS:
            msg = """
            SEG Y supports a maximum interval of %s seconds in between two
            samples (trace.stats.delta value).
            """.strip()
            msg = msg % MAX_INTERVAL_IN_SECONDS
            raise SEGYSampleIntervalError(msg)

    # Figure out endianness and the encoding of the textual file header.
    if byteorder is None:
        if hasattr(stream, 'stats') and hasattr(stream.stats, 'endian'):
            byteorder = stream.stats.endian
        else:
            byteorder = '>'
    # Map the byteorder.
    byteorder = ENDIAN[byteorder]
    if textual_header_encoding is None:
        if hasattr(stream, 'stats') and hasattr(
                stream.stats, 'textual_file_header_encoding'):
            textual_header_encoding = \
                stream.stats.textual_file_header_encoding
        else:
            textual_header_encoding = 'ASCII'

    # Loop over all Traces and create a SEGY File object.
    segy_file = SEGYFile()
    # Set the file wide headers.
    segy_file.textual_file_header = stream.stats.textual_file_header
    segy_file.textual_header_encoding = \
        textual_header_encoding
    binary_header = SEGYBinaryFileHeader()
    this_binary_header = stream.stats.binary_file_header
    # Loop over all items and if they exists set them. Ignore all other
    # attributes.
    for _, item, _ in BINARY_FILE_HEADER_FORMAT:
        if hasattr(this_binary_header, item):
            setattr(binary_header, item, getattr(this_binary_header, item))
    # Set the data encoding.
    binary_header.data_sample_format_code = data_encoding
    segy_file.binary_file_header = binary_header
    # Add all traces.
    for trace in stream:
        new_trace = SEGYTrace()
        new_trace.data = trace.data
        # Create empty trace header if none is there.
        if not hasattr(trace.stats, 'segy'):
            warnings.warn("CREATING TRACE HEADER")
            trace.stats.segy = {}
            trace.stats.segy.trace_header = SEGYTraceHeader(endian=byteorder)
        elif not hasattr(trace.stats.segy, 'trace_header'):
            warnings.warn("CREATING TRACE HEADER")
            trace.stats.segy.trace_header = SEGYTraceHeader()
        this_trace_header = trace.stats.segy.trace_header
        new_trace_header = new_trace.header
        # Again loop over all field of the trace header and if they exists, set
        # them. Ignore all additional attributes.
        for _, item, _, _ in TRACE_HEADER_FORMAT:
            if hasattr(this_trace_header, item):
                setattr(new_trace_header, item,
                        getattr(this_trace_header, item))
        starttime = trace.stats.starttime
        # Set the date of the Trace if it is not UTCDateTime(0).
        if starttime == UTCDateTime(0):
            new_trace.header.year_data_recorded = 0
            new_trace.header.day_of_year = 0
            new_trace.header.hour_of_day = 0
            new_trace.header.minute_of_hour = 0
            new_trace.header.second_of_minute = 0
        else:
            new_trace.header.year_data_recorded = starttime.year
            new_trace.header.day_of_year = starttime.julday
            new_trace.header.hour_of_day = starttime.hour
            new_trace.header.minute_of_hour = starttime.minute
            new_trace.header.second_of_minute = starttime.second
        # Set the sampling rate.
        new_trace.header.sample_interval_in_ms_for_this_trace = \
            int(trace.stats.delta * 1E6)
        # Set the data encoding and the endianness.
        new_trace.data_encoding = data_encoding
        new_trace.endian = byteorder
        # Add the trace to the SEGYFile object.
        segy_file.traces.append(new_trace)
    # Write the file
    segy_file.write(filename, data_encoding=data_encoding, endian=byteorder)
Пример #3
0
def writeSEGY(stream,
              filename,
              data_encoding=None,
              byteorder=None,
              textual_header_encoding=None,
              **kwargs):  # @UnusedVariable
    """
    Writes a SEG Y file from given ObsPy Stream object.

    .. warning::
        This function should NOT be called directly, it registers via the
        the :meth:`~obspy.core.stream.Stream.write` method of an
        ObsPy :class:`~obspy.core.stream.Stream` object, call this instead.

    :type stream: :class:`~obspy.core.stream.Stream`
    :param stream: The ObsPy Stream object to write.
    :type filename: str
    :param filename: Name of file to write.
    :type data_encoding: int
    :param data_encoding: The data encoding is an integer with the following
        currently supported meaning:

            ``1``
                4 byte IBM floating points (float32)
            ``2``
                4 byte Integers (int32)
            ``3``
                2 byte Integer (int16)
            ``5``
                4 byte IEEE floating points (float32)

        The value in the brackets is the necessary dtype of the data. ObsPy
        will now automatically convert the data because data might change/loose
        precision during the conversion so the user has to take care of the
        correct dtype.

        If it is ``None``, the value of the first Trace will be used for all
        consecutive Traces. If it is None for the first Trace, 1 (IBM floating
        point numbers) will be used. Different data encodings for different
        traces are currently not supported because these will most likely not
        be readable by other software.
    :type byteorder: ``'<'``, ``'>'``, or ``None``
    :param byteorder: Determines the endianness of the file. Either ``'>'`` for
        big endian or ``'<'`` for little endian. If is ``None``, it will either
        be the endianness of the first Trace or if that is also not set, it
        will be big endian. A mix between little and big endian for the headers
        and traces is currently not supported.
    :type textual_header_encoding: ``'EBCDIC'``, ``'ASCII'`` or ``None``
    :param textual_header_encoding: The encoding of the textual header. If it
        is ``None``, the textual_file_header_encoding attribute in the
        stats.segy dictionary of the first Trace is used and if that is not
        set, ASCII will be used.

    This function will automatically set the data encoding field of the binary
    file header so the user does not need to worry about it.

    The starttime of every trace is not a required field in the SEG Y
    specification. If the starttime of a trace is UTCDateTime(0) it will be
    interpreted as a not-set starttime and no time is written to the trace
    header. Every other time will be written.

    SEG Y supports a sample interval from 1 to 65535 microseconds in steps of 1
    microsecond. Larger intervals cannot be supported due to the definition of
    the SEG Y format. Therefore the smallest possible sampling rate is ~ 15.26
    Hz. Please keep that in mind.
    """
    # Some sanity checks to catch invalid arguments/keyword arguments.
    if data_encoding is not None and data_encoding not in VALID_FORMATS:
        msg = "Invalid data encoding."
        raise SEGYCoreWritingError(msg)
    # Figure out the data encoding if it is not set.
    if data_encoding is None:
        if hasattr(stream, 'stats') and hasattr(stream.stats, 'data_encoding'):
            data_encoding = stream.stats.data_encoding
        if hasattr(stream, 'stats') and hasattr(stream.stats,
                                                'binary_file_header'):
            data_encoding = \
                stream.stats.binary_file_header.data_sample_format_code
        # Set it to float if it in not given.
        else:
            data_encoding = 1

    # Create empty file wide headers if they do not exist.
    if not hasattr(stream, 'stats'):
        stream.stats = AttribDict()
    if not hasattr(stream.stats, 'textual_file_header'):
        stream.stats.textual_file_header = ""
    if not hasattr(stream.stats, 'binary_file_header'):
        stream.stats.binary_file_header = SEGYBinaryFileHeader()

    # Valid dtype for the data encoding.
    valid_dtype = DATA_SAMPLE_FORMAT_CODE_DTYPE[data_encoding]
    # Makes sure that the dtype is for every Trace is correct.
    for trace in stream:
        # Check the dtype.
        if trace.data.dtype != valid_dtype:
            msg = """
            The dtype of the data and the chosen data_encoding do not match.
            You need to manually convert the dtype if you want to use that
            data_encoding. Please refer to the obspy.segy manual for more
            details.
            """.strip()
            raise SEGYCoreWritingError(msg)
        # Check the sample interval.
        if trace.stats.delta > MAX_INTERVAL_IN_SECONDS:
            msg = """
            SEG Y supports a maximum interval of %s seconds in between two
            samples (trace.stats.delta value).
            """.strip()
            msg = msg % MAX_INTERVAL_IN_SECONDS
            raise SEGYSampleIntervalError(msg)

    # Figure out endianness and the encoding of the textual file header.
    if byteorder is None:
        if hasattr(stream, 'stats') and hasattr(stream.stats, 'endian'):
            byteorder = stream.stats.endian
        else:
            byteorder = '>'
    # Map the byteorder.
    byteorder = ENDIAN[byteorder]
    if textual_header_encoding is None:
        if hasattr(stream, 'stats') and hasattr(
                stream.stats, 'textual_file_header_encoding'):
            textual_header_encoding = \
                stream.stats.textual_file_header_encoding
        else:
            textual_header_encoding = 'ASCII'

    # Loop over all Traces and create a SEGY File object.
    segy_file = SEGYFile()
    # Set the file wide headers.
    segy_file.textual_file_header = stream.stats.textual_file_header
    segy_file.textual_header_encoding = \
            textual_header_encoding
    binary_header = SEGYBinaryFileHeader()
    this_binary_header = stream.stats.binary_file_header
    # Loop over all items and if they exists set them. Ignore all other
    # attributes.
    for _, item, _ in BINARY_FILE_HEADER_FORMAT:
        if hasattr(this_binary_header, item):
            setattr(binary_header, item, getattr(this_binary_header, item))
    # Set the data encoding.
    binary_header.data_sample_format_code = data_encoding
    segy_file.binary_file_header = binary_header
    # Add all traces.
    for trace in stream:
        new_trace = SEGYTrace()
        new_trace.data = trace.data
        # Create empty trace header if none is there.
        if not hasattr(trace.stats, 'segy'):
            print "CREATING TRACE HEADER"
            trace.stats.segy = {}
            trace.stats.segy.trace_header = SEGYTraceHeader(endian=byteorder)
        elif not hasattr(trace.stats.segy, 'trace_header'):
            print "CREATING TRACE HEADER"
            trace.stats.segy.trace_header = SEGYTraceHeader()
        this_trace_header = trace.stats.segy.trace_header
        new_trace_header = new_trace.header
        # Again loop over all field of the trace header and if they exists, set
        # them. Ignore all additional attributes.
        for _, item, _, _ in TRACE_HEADER_FORMAT:
            if hasattr(this_trace_header, item):
                setattr(new_trace_header, item,
                        getattr(this_trace_header, item))
        starttime = trace.stats.starttime
        # Set the date of the Trace if it is not UTCDateTime(0).
        if starttime == UTCDateTime(0):
            new_trace.header.year_data_recorded = 0
            new_trace.header.day_of_year = 0
            new_trace.header.hour_of_day = 0
            new_trace.header.minute_of_hour = 0
            new_trace.header.second_of_minute = 0
        else:
            new_trace.header.year_data_recorded = starttime.year
            new_trace.header.day_of_year = starttime.julday
            new_trace.header.hour_of_day = starttime.hour
            new_trace.header.minute_of_hour = starttime.minute
            new_trace.header.second_of_minute = starttime.second
        # Set the sampling rate.
        new_trace.header.sample_interval_in_ms_for_this_trace = \
            int(trace.stats.delta * 1E6)
        # Set the data encoding and the endianness.
        new_trace.data_encoding = data_encoding
        new_trace.endian = byteorder
        # Add the trace to the SEGYFile object.
        segy_file.traces.append(new_trace)
    # Write the file
    segy_file.write(filename, data_encoding=data_encoding, endian=byteorder)
Пример #4
0
#synthchannel.id=
streamtot = Stream()

for i in range(50):
    dato = np.sin(x + i * .1)
    dato = np.require(dato, dtype=np.float32)

    traces = Trace(data=dato)
    traces.stats.delta = 0.01
    traces.stats.starttime = UTCDateTime(2011, 11, 11, 11, 11, 11)
    traces.stats.sampling_rate = 20
    if not hasattr(traces.stats, 'segy.trace_header'):
        traces.stats.segy = {}
    traces.stats.segy.trace_header = SEGYTraceHeader()
    traces.stats.segy.trace_header.trace_sequence_number_within_line = i + 1
    traces.stats.segy.trace_header.receiver_group_elevation = 444

    streamtemp = Stream(traces)

    streamtot += streamtemp

streamtot.stats = AttribDict()
streamtot.stats.textual_file_header = 'Textual Header'
streamtot.stats.binary_file_header = SEGYBinaryFileHeader()
streamtot.stats.binary_file_header.trace_sorting_code = 5

print("Stream object before writing...")
#streamtot.write('file.sgy', format='SEGY', data_encoding=1, byteorder=sys.byteorder)
#streamtot.write('file.segy', format='SEGY')
streamtot.write('file.segy', format='SEGY', data_encoding=1, byteorder=None)