跳转到主要内容

为obspy提供HDF5读写支持

项目描述

目标:

https://doi.org/10.5281/zenodo.3953668

保存并写入OBSPy流到hdf5文件。如果它们是数字、字符串、UTCDateTime对象或NumPy数组,则保留统计属性。它可以作为obspy的读取函数的插件来读取整个hdf5文件。或者,您可以使用iterh5函数遍历hdf5文件中的轨迹。

安装

安装h5py和obspy。然后使用pip通过以下方式安装obspyh5:

pip install obspyh5

使用conda,可以将包安装到一个新的环境中:

conda config --add channels conda-forge
conda create -n obsh5 numpy obspy h5py
conda activate obsh5
pip install obspyh5

用法

使用obspy插件的基本示例

>>> from obspy import read
>>> stream = read()  # load example stream
>>> print(stream)
..3 Trace(s) in Stream:
BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
BW.RJOB..EHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
BW.RJOB..EHE | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
>>> stream.write('test.h5', 'H5')  # declare 'H5' as format
>>> print(read('test.h5'))  # order is preserved only for default index
3 Trace(s) in Stream:
BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
BW.RJOB..EHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
BW.RJOB..EHE | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples

示例遍历大型hdf5文件中的轨迹。每次迭代后,轨迹不会保留在内存中,因此在PC上处理大型hdf5文件时不会出现问题。

>>> from obspyh5 import iterh5
>>> for trace in iterh5('huge_in.h5')
        trace.do_something()
        trace.write('huge_out.h5', 'H5', mode='a')  # append mode to write into file

替代索引

obspyh5支持替代索引。

>>> from obspy import read
>>> import obspyh5
>>> print(obspyh5._INDEX)  # default index
waveforms/{trc_num:03d}_{id}_{starttime.datetime:%Y-%m-%dT%H:%M:%S}_{duration:.1f}s

索引在写入轨迹时由stats对象和轨迹号填充,例如。

'waveforms/000_BW.RJOB..EHZ/2009-08-24T00:20:03_30.0s'

要更改索引,请使用set_index。

>>> obspyh5.set_index('flat')  # flat index wihtout trace number, writing a trace with the same metadata twice will overwrite
>>> obspyh5.set_index('nested')  # nested index
>>> obspyh5.set_index('xcorr')  # xcorr indexing
>>> obspyh5.set_index('waveforms/{network}.{station}/{distance}')  # custom indexing
>>> obspyh5.set_index('waveforms/{trc_num:03d}_{station}')  # use of the trace number
>>> obspyh5.set_index()  # default index

当使用“xcorr”索引时,stats需要第一个和第二个台站的第一和第二站点的“network1”、“station1”、“location1”、“channel1”、“network2”、“station2”、“location2”和“channel2”条目。例如。

>>> from obspy import read
>>> import obspyh5
>>> obspyh5.set_index('xcorr')  # activate xcorr indexing
>>> stream = read()
>>> for i, tr in enumerate(stream):  # manipulate stats object
        station1, station2 = 'ST1', 'ST%d' % i
        channel1, channel2 = 'HHZ', 'HHN'
        s = tr.stats
        # we manipulate seed id so that important information gets
        # printed by obspy
        s.network, s.station = s.station1, s.channel1 = station1, channel1
        s.location, s.channel = s.station2, s.channel2 = station2, channel2
        s.network1 = s.network2 = 'BW'
        s.location1 = s.location2 = ''
>>> print(stream)
ST1.HHZ.ST0.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
ST1.HHZ.ST1.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
ST1.HHZ.ST2.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
>>> stream.write('test_xcorr.h5', 'H5')
>>> print(read('test_xcorr.h5'))
ST1.HHZ.ST0.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
ST1.HHZ.ST1.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples
ST1.HHZ.ST2.HHN | 2009-08-24T00:20:03.000000Z - 2009-08-24T00:20:32.990000Z | 100.0 Hz, 3000 samples

注意

有关更全面的方法,请参阅ASDF

用例:晚于鄂霍次克半岛尾波的相关性(notebook)。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

obspyh5-0.6.0.tar.gz (9.8 kB 查看哈希值)

上传时间 源代码

由以下机构支持