Coverage for /opt/obspy/update-docs/src/obspy/obspy/gse2/libgse2 : 94%

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
#!/usr/bin/env python #------------------------------------------------------------------- # Filename: libgse2.py # Purpose: Python wrapper for gse_functions of Stefan Stange # Author: Moritz Beyreuther # Email: moritz.beyreuther@geophysik.uni-muenchen.de # # Copyright (C) 2008-2012 Moritz Beyreuther #--------------------------------------------------------------------- Lowlevel module internally used for handling GSE2 files
Python wrappers for gse_functions - The GSE2 library of Stefan Stange. Currently CM6 compressed GSE2 files are supported, this should be sufficient for most cases. Gse_functions is written in C and interfaced via python-ctypes.
See: http://www.orfeus-eu.org/Software/softwarelib.html#gse
:copyright: The ObsPy Development Team (devs@obspy.org) :license: GNU Lesser General Public License, Version 3 (http://www.gnu.org/copyleft/lesser.html) """
# Import shared libgse2 # create library names # platform specific library name 'libgse2-%s-%s-py%s' % (platform.system(), platform.architecture()[0], ''.join([str(i) for i in platform.python_version_tuple()[:2]])), # fallback for pre-packaged libraries 'libgse2'] # get default file extension for shared objects # initialize library 'lib', lib_name + lib_extension)) except Exception, e: pass else: msg = 'Could not load shared library for obspy.gse2.\n\n %s' % (e) raise ImportError(msg)
""" Exception type for mismatching checksums """
""" Exception type for other errors in GSE_UTI """
# gse2 header struct """ Ctypes based GSE2 header structure for internal usage. """ ('d_year', C.c_int), ('d_mon', C.c_int), ('d_day', C.c_int), ('t_hour', C.c_int), ('t_min', C.c_int), ('t_sec', C.c_float), ('station', C.c_char * 6), ('channel', C.c_char * 4), ('auxid', C.c_char * 5), ('datatype', C.c_char * 4), ('n_samps', C.c_int), ('samp_rate', C.c_float), ('calib', C.c_float), ('calper', C.c_float), ('instype', C.c_char * 7), ('hang', C.c_float), ('vang', C.c_float), ]
# ctypes, PyFile_AsFile: convert python file pointer/ descriptor to C file # pointer descriptor
# reading C memory into buffer which can be converted to numpy array
## gse_functions read_header
## gse_functions decomp_6b c_file_p, C.c_int, np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS')]
# gse_functions rem_2nd_diff np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), C.c_int]
# gse_functions check_sum np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), C.c_int, C.c_int32]
# gse_functions buf_init
# gse_functions diff_2nd np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), C.c_int, C.c_int]
# gse_functions compress_6b np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), C.c_int]
## gse_functions write_header
## gse_functions buf_dump
# gse_functions buf_free
# module wide variable, can be imported by: # >>> from obspy.gse2 import gse2head
""" Checks whether a file is GSE2 or not. Returns True or False.
:type f : file pointer :param f : file pointer to start of GSE2 file to be checked. """
""" Rewriting the write_header Function of gse_functions.c
Different operating systems are delivering different output for the scientific format of floats (fprinf libc6). Here we ensure to deliver in a for GSE2 valid format independent of the OS. For speed issues we simple cut any number ending with E+0XX or E-0XX down to E+XX or E-XX. This fails for numbers XX>99, but should not occur.
:type f: File pointer :param f: File pointer to to GSE2 file to write :type head: Ctypes struct :param head: Ctypes structure to write """ "%11.6f %s %7.3f %-6s %5.1f %4.1f\n" head.d_year, head.d_mon, head.d_day, head.t_hour, head.t_min, head.t_sec, head.station, head.channel, head.auxid, head.datatype, head.n_samps, head.samp_rate, calib, head.calper, head.instype, head.hang, head.vang))
""" Uncompress n_samps of CM6 compressed data from file pointer fp.
:type f: File Pointer :param f: File Pointer :type n_samps: Int :param n_samps: Number of samples """ # transform to a C file pointer raise GSEUtiError("Mismatching length in lib.decomp_6b")
""" Calculate checksum from data, as in gse_driver.c line 60
:type fh: File Pointer :param fh: File Pointer :type version: Int :param version: GSE version, either 1 or 2, defaults to 2. """ # find checksum within file # 2012-02-12, should be deleted in a year from now msg = "Checksum differs only in absolute value. If this file " + \ "was written with ObsPy GSE2, this is due to a bug in " + \ "the obspy.gse2.write routine (resolved with [3431]), " + \ "and thus this message can be safely ignored." warnings.warn(msg, UserWarning) return
""" Read GSE2 file and return header and data.
Currently supports only CM6 compressed GSE2 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 f: File Pointer :param f: Open file pointer of GSE2 file to read, opened in binary mode, e.g. f = open('myfile','rb') :type test_chksum: Bool :param verify_chksum: If True verify Checksum and raise Exception if it is not correct :rtype: Dictionary, Numpy.ndarray int32 :return: Header entries and data as numpy.ndarray of type int32. """ raise GSEUtiError("Error in lib.read_header") # test checksum only if enabled # cleaning up
""" Write GSE2 file, given the header and data.
Currently supports only CM6 compressed GSE2 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'].
Warning: The data are actually compressed in place for performance issues, if you still want to use the data afterwards use data.copy()
:note: headdict dictionary entries C{'datatype', 'n_samps', 'samp_rate'} are absolutely necessary :type data: numpy.ndarray dtype int32 :param data: Contains the data. :type f: File Pointer :param f: Open file pointer of GSE2 file to write, opened in binary mode, e.g. f = open('myfile','wb') :type inplace: Bool :param inplace: If True, do compression not on a copy of the data but on the data itself --- note this will change the data values and make them therefore unusable :type headdict: Dictionary :param headdict: Header containing the following entries:: 'd_year': int, 'd_mon': int, 'd_mon': int, 'd_day': int, 't_hour': int, 't_min': int, 't_sec': float, 'station': char*6, 'station': char*6, 'channel': char*4, 'auxid': char*5, 'datatype': char*4, 'n_samps': int, 'samp_rate': float, 'calib': float, 'calper': float, 'instype': char*7, 'hang': float, 'vang': float """ # # Maximum values above 2^26 will result in corrupted/wrong data! # do this after chksum as chksum does the type checking for numpy array # for you # set some defaults if not available and convert header entries # This is the actual function where the header is written. It avoids # the different format of 10.4e with fprintf on Windows and Linux. # For further details, see the __doc__ of writeHeader
""" Return (and read) only the header of gse2 file as dictionary.
Currently supports only CM6 compressed GSE2 files, this should be sufficient for most cases.
:type file: File Pointer :param file: Open file pointer of GSE2 file to read, opened in binary mode, e.g. f = open('myfile','rb') :rtype: Dictionary :return: Header entries. """
""" Return start and endtime/date of GSE2 file
Currently supports only CM6 compressed GSE2 files, this should be sufficient for most cases.
:type f: File Pointer :param f: Open file pointer of GSE2 file to read, opened in binary mode, e.g. f = open('myfile','rb') :rtype: List :return: C{[startdate,stopdate,startime,stoptime]} Start and Stop time as Julian seconds and as date string. """ head.t_hour, head.t_min, seconds, microseconds) head.n_samps / float(head.samp_rate))
if __name__ == '__main__': doctest.testmod(exclude_empty=True) |