Source code for obspy.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
(http://www.gnu.org/copyleft/lesser.html)
"""
from obspy import UTCDateTime
from obspy.gse2.libgse2 import uncompress_CM6, verifyChecksum
import doctest
import numpy as np
[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 Pointer
: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, Numpy.ndarray int32
:return: Header entries and data as numpy.ndarray of type int32.
"""
header = readHeader(fh)
dtype = header['gse1']['datatype']
if dtype == 'CMP6':
data = uncompress_CM6(fh, header['npts'])
elif dtype == 'INTV':
data = readIntegerData(fh, header['npts'])
else:
raise Exception("Unsupported data type %s in GSE1 file" % dtype)
# test checksum only if enabled
if verify_chksum:
verifyChecksum(fh, data, version=1)
return header, data
[docs]def readIntegerData(fh, npts):
"""
Reads npts points of uncompressed integers from given file handler.
"""
# find next DAT1 section within file
buf = fh.readline()
while buf:
if buf.startswith("DAT1"):
data = np.fromfile(fh, dtype=np.int32, count=npts, sep=' ')
break
buf = fh.readline()
return data
if __name__ == '__main__':
doctest.testmod(exclude_empty=True)