def writeSAC(stream, filename, **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. :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. .. rubric:: Example >>> from obspy import read >>> st = read() >>> st.write("test.sac", format="SAC") #doctest: +SKIP """ # Translate the common (renamed) entries base, ext = os.path.splitext(filename) for i, trace in enumerate(stream): t = SacIO(trace) if len(stream) != 1: filename = "%s%02d%s" % (base, i + 1, ext) t.WriteSacBinary(filename) return
def readSACXY(filename, headonly=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. :type filename: str :param filename: Alphanumeric SAC file to be read. :type headonly: bool, optional :param headonly: If set to True, read only the head. This is most useful for scanning available data in huge (temporary) data sets. :rtype: :class:`~obspy.core.stream.Stream` :return: A ObsPy Stream object. .. rubric:: Example >>> from obspy.core import read # doctest: +SKIP >>> st = read("/path/to/testxy.sac") # doctest: +SKIP """ t = SacIO() if headonly: t.ReadSacXYHeader(filename) else: t.ReadSacXY(filename) # assign all header entries to a new dictionary compatible with ObsPy header = t.get_obspy_header() if headonly: tr = Trace(header=header) else: tr = Trace(header=header, data=t.seis) return Stream([tr])
def readSAC(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. :type filename: str :param filename: SAC file to be read. :type headonly: bool, optional :param headonly: If set to True, read only the head. This is most useful for scanning available data in huge (temporary) data sets. :type debug_headers: bool, optional :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 fsize: bool, optional :param fsize: Check if file size is consistent with theoretical size from header. Defaults to ``True``. :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 """ # read SAC file t = SacIO(debug_headers=debug_headers) if headonly: t.ReadSacHeader(filename) else: t.ReadSacFile(filename, fsize) # assign all header entries to a new dictionary compatible with an ObsPy header = t.get_obspy_header() if headonly: tr = Trace(header=header) else: tr = Trace(header=header, data=t.seis) return Stream([tr])
def writeSAC(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. :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 byteorder: int or str, optional :param byteorder: Must be either ``0`` or ``'<'`` for LSBF or little-endian, ``1`` or ``'>'`` for MSBF or big-endian. Defaults to little endian. .. rubric:: Example >>> from obspy import read >>> st = read() >>> st.write("test.sac", format="SAC") #doctest: +SKIP """ if byteorder in ("<", 0, "0"): byteorder = 0 elif byteorder in (">", 1, "1"): byteorder = 1 else: msg = "Invalid byte order. It must be either '<', '>', 0 or 1" raise ValueError(msg) # Translate the common (renamed) entries base, ext = os.path.splitext(filename) for i, trace in enumerate(stream): t = SacIO(trace) if len(stream) != 1: filename = "%s%02d%s" % (base, i + 1, ext) if (byteorder == 1 and t.byteorder == 'little') or \ (byteorder == 0 and t.byteorder == 'big'): t.swap_byte_order() t.WriteSacBinary(filename) return