ObsPy

1.5.0 · active · verified Fri Apr 17

ObsPy is an open-source Python framework for seismological observatories, providing tools for data acquisition, processing, and analysis. It is designed to work with various seismological data formats and interact with data centers. The current stable version is 1.5.0, with new releases typically occurring a few times per year, focusing on bug fixes, performance improvements, and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read seismic data using `obspy.read()`, access `Stream` and `Trace` objects, and perform a basic filter operation. It also includes error handling for network issues and creates a dummy stream if needed. Note that plotting functionality requires the `matplotlib` library.

from obspy import read, UTCDateTime
import os

# ObsPy often requires C-compiled dependencies for full functionality and speed.
# It is highly recommended to install via Anaconda/Miniconda: 
# conda install -c conda-forge obspy

# Example: Reading a MiniSEED file from a URL
try:
    # This URL provides a small example MiniSEED file.
    # For local files, replace with your path: st = read("path/to/your/file.mseed")
    st = read("https://examples.obspy.org/BW.KW1..EHZ.D.2010.010.mseed")
    print(f"\nSuccessfully loaded stream: {st}")
    
    # Accessing the first trace in the stream
    trace = st[0]
    print(f"First trace metadata: {trace.stats}")
    print(f"Start time: {trace.stats.starttime}, End time: {trace.stats.endtime}")
    print(f"Sampling rate: {trace.stats.sampling_rate} Hz")
    print(f"Number of samples: {trace.stats.npts}")
    
    # Accessing the data array (NumPy array)
    print(f"First 5 data points: {trace.data[:5]}")
    
    # Apply a basic filter (requires scipy)
    st.filter('lowpass', freq=0.5)
    print(f"\nStream after lowpass filter: {st}")
    
    # For plotting, matplotlib is required:
    # try:
    #     st.plot()
    #     print("Plot generated (if matplotlib installed).")
    # except ImportError:
    #     print("Install 'matplotlib' (pip install matplotlib) to enable plotting.")

except Exception as e:
    print(f"\nFailed to read example data. Error: {e}")
    print("Please ensure you have an active internet connection or try a local file.")
    # Fallback to creating a dummy stream if download fails
    from obspy.core import Stream, Trace
    import numpy as np
    
    print("Creating a dummy stream for demonstration...")
    UTC_DT = UTCDateTime("2023-01-01T00:00:00.000Z")
    stats = {'network': 'XX', 'station': 'DUM', 'location': '00',
             'channel': 'BHZ', 'starttime': UTC_DT, 'delta': 0.01,
             'sampling_rate': 100.0, 'npts': 1000}
    dummy_trace = Trace(data=np.random.rand(stats['npts']), header=stats)
    dummy_st = Stream([dummy_trace])
    print(f"Dummy stream created: {dummy_st}")

view raw JSON →