コード例 #1
0
ファイル: core.py プロジェクト: jshridha/obspy
def _write_sac(stream, filename, byteorder="<", **kwargs):  # @UnusedVariable
    """
    Writes a SAC file.

    .. 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.

    :param stream: The ObsPy Stream object to write.
    :type stream: :class:`~obspy.core.stream.Stream`
    :param filename: Name of file to write. In case an open file or
        file-like object is passed, this function only supports writing
        Stream objects containing a single Trace. This is a limitation of
        the SAC file format. An exception will be raised in case it's
        necessary.
    :type filename: str, open file, or file-like object
    :param byteorder: Must be either ``0`` or ``'<'`` for LSBF or
        little-endian, ``1`` or ``'>'`` for MSBF or big-endian.
        Defaults to little endian.
    :type byteorder: int or str

    .. rubric:: Example

    >>> from obspy import read
    >>> st = read()
    >>> st.write("test.sac", format="SAC")  #doctest: +SKIP
    """
    # Bytes buffer are ok, but only if the Stream object contains only one
    # Trace. SAC can only store one Trace per file.
    if is_bytes_buffer(filename):
        if len(stream) > 1:
            raise ValueError("If writing to a file-like object in the SAC "
                             "format, the Stream object can only contain "
                             "one Trace.")
        __write_sac(stream[0], filename, byteorder=byteorder, **kwargs)
        return
    elif isinstance(filename, (str, bytes)):
        # Otherwise treat it as a filename
        # Translate the common (renamed) entries
        base, ext = os.path.splitext(filename)
        for i, trace in enumerate(stream):
            if len(stream) != 1:
                filename = "%s%02d%s" % (base, i + 1, ext)
            with open(filename, "wb") as fh:
                __write_sac(trace, fh, byteorder=byteorder, **kwargs)
    else:
        raise ValueError("Cannot open '%s'." % filename)
コード例 #2
0
ファイル: core.py プロジェクト: Qigaoo/obspy
def _read_sac(filename, headonly=False, debug_headers=False, fsize=True,
              **kwargs):  # @UnusedVariable
    """
    Reads an SAC 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.

    :param filename: SAC file to be read.
    :type filename: str, open file, or file-like object
    :param headonly: If set to True, read only the head. This is most useful
        for scanning available data in huge (temporary) data sets.
    :type headonly: bool
    :param debug_headers: Extracts also the SAC headers ``'nzyear', 'nzjday',
        'nzhour', 'nzmin', 'nzsec', 'nzmsec', 'delta', 'scale', 'npts',
        'knetwk', 'kstnm', 'kcmpnm'`` which are usually directly mapped to the
        :class:`~obspy.core.stream.Stream` object if set to ``True``. Those
        values are not synchronized with the Stream object itself and won't
        be used during writing of a SAC file! Defaults to ``False``.
    :type debug_headers: bool
    :param fsize: Check if file size is consistent with theoretical size
        from header. Defaults to ``True``.
    :type fsize: bool
    :rtype: :class:`~obspy.core.stream.Stream`
    :return: A ObsPy Stream object.

    .. rubric:: Example

    >>> from obspy import read # doctest: +SKIP
    >>> st = read("/path/to/test.sac") # doctest: +SKIP
    """
    # Only byte buffers for binary SAC.
    if is_bytes_buffer(filename):
        return _internal_read_sac(buf=filename, headonly=headonly,
                                  debug_headers=debug_headers, fsize=fsize,
                                  **kwargs)
    elif isinstance(filename, (str, bytes)):
        with open(filename, "rb") as fh:
            return _internal_read_sac(buf=fh, headonly=headonly,
                                      debug_headers=debug_headers, fsize=fsize,
                                      **kwargs)
    else:
        raise ValueError("Cannot open '%s'." % filename)
コード例 #3
0
ファイル: core.py プロジェクト: jshridha/obspy
def _is_sac(filename):
    """
    Checks whether a file is a SAC file or not.

    :param filename: SAC file to be checked.
    :type filename: str, open file, or file-like object
    :rtype: bool
    :return: ``True`` if a SAC file.

    .. rubric:: Example

    >>> _is_sac('/path/to/test.sac')  #doctest: +SKIP
    """
    if is_bytes_buffer(filename):
        return __is_sac(filename)
    elif isinstance(filename, (str, bytes)):
        with open(filename, "rb") as fh:
            return __is_sac(fh)
    else:
        raise ValueError("Cannot open '%s'." % filename)
コード例 #4
0
def _is_sac(filename):
    """
    Checks whether a file is a SAC file or not.

    :param filename: SAC file to be checked.
    :type filename: str, open file, or file-like object
    :rtype: bool
    :return: ``True`` if a SAC file.

    .. rubric:: Example

    >>> from obspy.core.util import get_example_file
    >>> _is_sac(get_example_file('test.sac'))
    True
    >>> _is_sac(get_example_file('test.mseed'))
    False
    """
    if is_bytes_buffer(filename):
        return _internal_is_sac(filename)
    elif isinstance(filename, (str, bytes)):
        with open(filename, "rb") as fh:
            return _internal_is_sac(fh)
    else:
        raise ValueError("Cannot open '%s'." % filename)
コード例 #5
0
ファイル: core.py プロジェクト: QuLogic/obspy
def _is_sac(filename):
    """
    Checks whether a file is a SAC file or not.

    :param filename: SAC file to be checked.
    :type filename: str, open file, or file-like object
    :rtype: bool
    :return: ``True`` if a SAC file.

    .. rubric:: Example

    >>> from obspy.core.util import get_example_file
    >>> _is_sac(get_example_file('test.sac'))
    True
    >>> _is_sac(get_example_file('test.mseed'))
    False
    """
    if is_bytes_buffer(filename):
        return _internal_is_sac(filename)
    elif isinstance(filename, (str, bytes)):
        with open(filename, "rb") as fh:
            return _internal_is_sac(fh)
    else:
        raise ValueError("Cannot open '%s'." % filename)
コード例 #6
0
ファイル: core.py プロジェクト: QuLogic/obspy
def _read_sac_xy(filename, headonly=False, debug_headers=False,
                 **kwargs):  # @UnusedVariable
    """
    Reads an alphanumeric SAC 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.

    :param filename: Alphanumeric SAC file to be read.
    :type filename: str, open file, or file-like object
    :param headonly: If set to True, read only the head. This is most useful
        for scanning available data in huge (temporary) data sets.
    :type headonly: bool
    :param debug_headers: Extracts also the SAC headers ``'nzyear', 'nzjday',
        'nzhour', 'nzmin', 'nzsec', 'nzmsec', 'delta', 'scale', 'npts',
        'knetwk', 'kstnm', 'kcmpnm'`` which are usually directly mapped to the
        :class:`~obspy.core.stream.Stream` object if set to ``True``. Those
        values are not synchronized with the Stream object itself and won't
        be used during writing of a SAC file! Defaults to ``False``.
    :type debug_headers: bool
    :rtype: :class:`~obspy.core.stream.Stream`
    :return: A ObsPy Stream object.

    .. rubric:: Example

    >>> from obspy import read
    >>> st = read("/path/to/testxy.sac")
    """
    if is_bytes_buffer(filename):
        return _internal_read_sac_xy(buf=filename, headonly=headonly,
                                     debug_headers=debug_headers, **kwargs)
    else:
        with open(filename, "rb") as fh:
            return _internal_read_sac_xy(buf=fh, headonly=headonly,
                                         debug_headers=debug_headers, **kwargs)
コード例 #7
0
def _read_sac_xy(filename, headonly=False, debug_headers=False,
                 **kwargs):  # @UnusedVariable
    """
    Reads an alphanumeric SAC 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.

    :param filename: Alphanumeric SAC file to be read.
    :type filename: str, open file, or file-like object
    :param headonly: If set to True, read only the head. This is most useful
        for scanning available data in huge (temporary) data sets.
    :type headonly: bool
    :param debug_headers: Extracts also the SAC headers ``'nzyear', 'nzjday',
        'nzhour', 'nzmin', 'nzsec', 'nzmsec', 'delta', 'scale', 'npts',
        'knetwk', 'kstnm', 'kcmpnm'`` which are usually directly mapped to the
        :class:`~obspy.core.stream.Stream` object if set to ``True``. Those
        values are not synchronized with the Stream object itself and won't
        be used during writing of a SAC file! Defaults to ``False``.
    :type debug_headers: bool
    :rtype: :class:`~obspy.core.stream.Stream`
    :return: A ObsPy Stream object.

    .. rubric:: Example

    >>> from obspy import read # doctest: +SKIP
    >>> st = read("/path/to/testxy.sac") # doctest: +SKIP
    """
    if is_bytes_buffer(filename):
        return _internal_read_sac_xy(buf=filename, headonly=headonly,
                                     debug_headers=debug_headers, **kwargs)
    else:
        with open(filename, "rb") as fh:
            return _internal_read_sac_xy(buf=fh, headonly=headonly,
                                         debug_headers=debug_headers, **kwargs)