Coverage for /opt/obspy/update-docs/src/obspy/obspy/sh/core : 89%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- SH bindings to ObsPy core module.
:copyright: The ObsPy Development Team (devs@obspy.org) :license: GNU Lesser General Public License, Version 3 (http://www.gnu.org/copyleft/lesser.html) """
'OCT', 'NOV', 'DEC']
'LENGTH': 'L001', 'SIGN': 'I011', 'EVENTNO': 'I012', 'MARK': 'I014', 'DELTA': 'R000', 'CALIB': 'R026', 'DISTANCE': 'R011', 'AZIMUTH': 'R012', 'SLOWNESS': 'R018', 'INCI': 'R013', 'DEPTH': 'R014', 'MAGNITUDE': 'R015', 'LAT': 'R016', 'LON': 'R017', 'SIGNOISE': 'R022', 'PWDW': 'R023', 'DCVREG': 'R024', 'DCVINCI': 'R025', 'COMMENT': 'S000', 'STATION': 'S001', 'OPINFO': 'S002', 'FILTER': 'S011', 'QUAL': 'S012', 'COMP': 'C000', 'CHAN1': 'C001', 'CHAN2': 'C002', 'BYTEORDER': 'C003', 'START': 'S021', 'P-ONSET': 'S022', 'S-ONSET': 'S023', 'ORIGIN': 'S024' }
""" Checks whether a file is a Seismic Handler ASCII file or not.
:type filename: str :param filename: Name of the ASCII file to be checked. :rtype: bool :return: ``True`` if a Seismic Handler ASCII file.
.. rubric:: Example
>>> isASC("/path/to/QFILE-TEST-ASC.ASC") #doctest: +SKIP True """ # first six chars should contain 'DELTA:' except: return False
**kwargs): # @UnusedVariable """ Reads a Seismic Handler ASCII 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: ASCII 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 skip: int, optional :param skip: Number of lines to be skipped from top of file. If defined only one trace is read from file. :type delta: float, optional :param delta: If "skip" is used, "delta" defines sample offset in seconds. :type length: int, optional :param length: If "skip" is used, "length" defines the number of values to be read. :rtype: :class:`~obspy.core.stream.Stream` :return: A ObsPy Stream object.
.. rubric:: Example
>>> from obspy import read >>> st = read("/path/to/QFILE-TEST-ASC.ASC") >>> st # doctest: +ELLIPSIS <obspy.core.stream.Stream object at 0x...> >>> print(st) # doctest: +ELLIPSIS 3 Trace(s) in Stream: .TEST..BHN | 2009-10-01T12:46:01.000000Z - ... | 20.0 Hz, 801 samples .TEST..BHE | 2009-10-01T12:46:01.000000Z - ... | 20.0 Hz, 801 samples .WET..HHZ | 2010-01-01T01:01:05.999000Z - ... | 100.0 Hz, 4001 samples """ # read file and split text into channels # blank line # check if any data fetched yet # append current channel # create new channel # if skip is set only one trace is read, everything else makes # no sense. continue # header entry # data entry - may be written in multiple columns # create ObsPy stream object # custom header
# create Stats # generate headers # 01-JAN-2009_01:01:01.0 # 1-OCT-2009_12:46:01.000 else: # everything else gets stored into sh entry header['sh'][key] = int(value) header['sh'][key] = float(value) else: # set channel code # skip data stream.append(Trace(header=header)) else: # read data
# cut data if requested
# use correct value in any case
custom_format="%-.6e", append=False, **kwargs): # @UnusedVariable """ Writes a Seismic Handler ASCII 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 the ASCII file to write. :type npl: int, optional :param npl: Number of data columns in file, default to four. :type included_headers: list or None, optional :param included_headers: If set to a list, only these header entries will be written to file. DELTA and LENGTH are written in any case. If it's set to None, a basic set will be included. :type custom_format: string, optional :param custom_format: Parameter for number formatting of samples, defaults to "%-.6e". :type append: bool, optional :param append: If filename exists append all data to file, default False. """
fh = open(filename, 'ab') else: # write headers # additional headers continue # special format for start time # component must be split # write data in npl columns
""" Checks whether a file is a Seismic Handler Q file or not.
:type filename: str :param filename: Name of the Q file to be checked. :rtype: bool :return: ``True`` if a Seismic Handler Q file.
.. rubric:: Example
>>> isQ("/path/to/QFILE-TEST.QHD") #doctest: +SKIP True """ # file must start with magic number 43981 except: return False
**kwargs): # @UnusedVariable """ Reads a Seismic Handler Q 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: Q header file to be read. Must have a `QHD` file extension. :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 data_directory: str, optional :param data_directory: Data directory where the corresponding QBN file can be found. :type byteorder: ``'<'``, ``'>'``, or ``'='``, optional :param byteorder: Enforce byte order for data file. This is important for Q files written in older versions of Seismic Handler, which don't explicit state the `BYTEORDER` flag within the header file. Defaults to ``'='`` (local byte order). :rtype: :class:`~obspy.core.stream.Stream` :return: A ObsPy Stream object.
Q files consists of two files per data set:
* a ASCII header file with file extension `QHD` and the * binary data file with file extension `QBN`.
The read method only accepts header files for the ``filename`` parameter. ObsPy assumes that the corresponding data file is within the same directory if the ``data_directory`` parameter is not set. Otherwise it will search in the given ``data_directory`` for a file with the `QBN` file extension. This function should NOT be called directly, it registers via the ObsPy :func:`~obspy.core.stream.read` function, call this instead.
.. rubric:: Example
>>> from obspy import read >>> st = read("/path/to/QFILE-TEST.QHD") >>> st #doctest: +ELLIPSIS <obspy.core.stream.Stream object at 0x...> >>> print(st) # doctest: +ELLIPSIS 3 Trace(s) in Stream: .TEST..BHN | 2009-10-01T12:46:01.000000Z - ... | 20.0 Hz, 801 samples .TEST..BHE | 2009-10-01T12:46:01.000000Z - ... | 20.0 Hz, 801 samples .WET..HHZ | 2010-01-01T01:01:05.999000Z - ... | 100.0 Hz, 4001 samples """ else: data_file = os.path.basename(os.path.splitext(filename)[0]) data_file = os.path.join(data_directory, data_file + '.QBN') msg = "Can't find corresponding QBN file at %s." raise IOError(msg % data_file) # loop through read header file # comment lines comments += [fh.readline()] # trace lines # create stream object # fetch headers "FROMQ": True, "FILE": os.path.splitext(os.path.split(filename)[1])[0], } if value == '<' or value == '>': byteorder = header['sh']['BYTEORDER'] = value # 01-JAN-2009_01:01:01.0 # 1-OCT-2009_12:46:01.000 header['sh']['P-ONSET'] = toUTCDateTime(value) header['sh']['S-ONSET'] = toUTCDateTime(value) header['sh']['ORIGIN'] = toUTCDateTime(value) header['sh'][key] = int(value) header['sh'][key] = float(value) else: # set channel code # remember record number # skip data stream.append(Trace(header=header)) else: # read data # convert to system byte order
**kwargs): # @UnusedVariable """ Writes a Seismic Handler Q 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 the Q file to write. :type data_directory: str, optional :param data_directory: Data directory where the corresponding QBN will be written. :type byteorder: ``'<'``, ``'>'``, or ``'='``, optional :param byteorder: Enforce byte order for data file. Defaults to ``'='`` (local byte order). :type append: bool, optional :param append: If filename exists append all data to file, default False. """ temp = os.path.basename(filename) filename_data = os.path.join(data_directory, temp) else:
# if the header file exists its assumed that the data is also there try: trcs = readQ(filename_header, headonly=True) mode = 'ab' count_offset = len(trcs) except: raise Exception("Target filename '%s' not readable!" % filename) else:
# build up header strings trace.stats.delta, trace.stats.calib) # component must be split # special format for start time # skip unknown keys # convert UTCDateTimes into strings value = fromUTCDateTime(value) # get maximal number of trclines minnol = nol # first line: magic number, cmtlines, trclines # XXX: comment lines are ignored
# write headers # write data in given byte order
""" Converts time string used within Seismic Handler into a UTCDateTime.
:type value: str :param value: A Date time string. :return: Converted :class:`~obspy.core.UTCDateTime` object.
.. rubric:: Example
>>> toUTCDateTime(' 2-JAN-2008_03:04:05.123') UTCDateTime(2008, 1, 2, 3, 4, 5, 123000) >>> toUTCDateTime('2-JAN-2008') UTCDateTime(2008, 1, 2, 0, 0) >>> toUTCDateTime('2-JAN-08') UTCDateTime(2008, 1, 2, 0, 0) >>> toUTCDateTime('2-JAN-99') UTCDateTime(1999, 1, 2, 0, 0) >>> toUTCDateTime('2-JAN-2008_1') UTCDateTime(2008, 1, 2, 1, 0) >>> toUTCDateTime('2-JAN-2008_1:1') UTCDateTime(2008, 1, 2, 1, 1) """ else:
""" Converts UTCDateTime object into a time string used within Seismic Handler.
:type dt: :class:`~obspy.core.UTCDateTime` :param dt: A UTCDateTime object. :return: Converted date time string usable by Seismic Handler.
.. rubric:: Example
>>> from obspy import UTCDateTime >>> dt = UTCDateTime(2008, 1, 2, 3, 4, 5, 123456) >>> fromUTCDateTime(dt) ' 2-JAN-2008_03:04:05.123' """
dt.minute, dt.second, dt.microsecond / 1000)
if __name__ == '__main__': import doctest doctest.testmod(exclude_empty=True) |