- ObsPy Documentation (1.0.0)
- Module code
Source code for obspy.io.gse2.libgse1
#!/usr/bin/env python
# -------------------------------------------------------------------
# Filename: libgse1.py
# Purpose: Python wrapper for reading GSE1 files
# Author: Moritz Beyreuther
# Email: moritz.beyreuther@geophysik.uni-muenchen.de
#
# Copyright (C) 2008-2012 Moritz Beyreuther
# ---------------------------------------------------------------------
"""
Low-level module internally used for handling GSE1 files
:copyright:
The ObsPy Development Team (devs@obspy.org)
:license:
GNU Lesser General Public License, Version 3
(https://www.gnu.org/copyleft/lesser.html)
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA
import doctest
import sys
import numpy as np
from obspy import UTCDateTime
from obspy.core.util.deprecation_helpers import \
DynamicAttributeImportRerouteModule
from .libgse2 import uncompress_cm6, verify_checksum
[docs]def read(fh, verify_chksum=True):
"""
Read GSE1 file and return header and data.
Currently supports only CM6 compressed and plain integer GSE1 files, this
should be sufficient for most cases. Data are in circular frequency counts,
for correction of calper multiply by 2PI and calper: data * 2 * pi *
header['calper'].
:type fh: file
:param fh: Open file pointer of GSE1 file to read, opened in binary mode,
e.g. fh = open('myfile','rb')
:type verify_chksum: bool
:param verify_chksum: If True verify Checksum and raise Exception if not
correct
:rtype: Dictionary, :class:`numpy.ndarray`, dtype=int32
:return: Header entries and data as numpy.ndarray of type int32.
"""
header = read_header(fh)
dtype = header['gse1']['datatype']
if dtype == 'CMP6':
data = uncompress_cm6(fh, header['npts'])
elif dtype == 'INTV':
data = read_integer_data(fh, header['npts'])
else:
raise Exception("Unsupported data type %s in GSE1 file" % dtype)
# test checksum only if enabled
if verify_chksum:
verify_checksum(fh, data, version=1)
return header, data
[docs]def read_integer_data(fh, npts):
"""
Reads npts points of uncompressed integers from given file handler.
"""
# find next DAT1 section within file
data = []
in_data_section = False
while len(data) < npts:
buf = fh.readline()
if buf.startswith(b"DAT1"):
in_data_section = True
continue
if not in_data_section:
continue
data.extend(buf.strip().split(b" "))
return np.array(data, dtype=np.int32)
sys.modules[__name__] = DynamicAttributeImportRerouteModule(
name=__name__, doc=__doc__, locs=locals(),
original_module=sys.modules[__name__],
import_map={},
function_map={
'readHeader': 'obspy.io.gse2.libgse1.read_header',
'readIntegerData': 'obspy.io.gse2.libgse1.read_integer_data'})
if __name__ == '__main__':
doctest.testmod(exclude_empty=True)