GithubHelp home page GithubHelp logo

obspyh5's Introduction

obspyh5

HDF5 write/read support for obspy

buildstatus coverage version pyversions zenodo

Writes and reads ObsPy streams to/from hdf5 files. Stats attributes are preserved if they are numbers, strings, UTCDateTime objects or numpy arrays. It can be used as a plugin to obspy's read function to read a whole hdf5 file. Alternatively you can iterate over the traces in a hdf5 file with the iterh5 function.

Installation

Install h5py and obspy. After that install obspyh5 using pip by:

pip install obspyh5

With conda the package can be installed into a fresh environment with:

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

Usage

Basic example using the obspy plugin:

>>> 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

Example iterating over traces in a huge hdf5 file. After each iteration the trace is not kept in memory and therefore it is possible to process a huge hdf5 file on a PC without problems.

>>> 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

Alternative indexing

obspyh5 supports alternative indexing.

>>> 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

The index gets populated by the stats object and the trace number when writing a trace, e.g.

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

To change the index use 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

When using the 'xcorr' indexing stats needs the entries 'network1', 'station1', 'location1', 'channel1', 'network2', 'station2', 'location2' and 'channel2' of the first and second station. An example:

>>> 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

Note

See also ASDF for a more comprehensive approach.

Use case: Cross-correlation of late Okhotsk coda (notebook).

obspyh5's People

Contributors

trichter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.