Skip to content

ObsPy: Example to create a simplest working QuakeML from scratch #289

@seisman

Description

@seisman
import pandas as pd
from obspy import Catalog, UTCDateTime, read_events
from obspy.core.event import Event, Magnitude, Origin, Pick, Arrival, WaveformStreamID

# Create an empty Catalog object
cat = Catalog()

# Read events from a CSV file
#
# The CSV file should contain the following columns:
# - time
# - longitude
# - latitude
# - depth (in km)
# - magnitude
df = pd.read_csv("catalog.csv")
for _, row in df.iterrows():  # loop over events
    # Create the Origin object
    origin = Origin(
        time=UTCDateTime(row["time"]),
        longitude=row["longitude"],
        latitude=row["latitude"],
        depth=row["depth"] * 1000.0,  # depth is in meter in ObsPy
    )
    # Create the Magnitude object
    magnitude = Magnitude(mag=row["magnitude"])

    # picks are stored in separate TXT files
    pickfile = origin.time.strftime("%Y%m%d%H%M%S") + ".txt"

    dfpick = pd.read_csv(pickfile)
    picks, arrivals = [], []
    for _, rowp in dfpick.iterrows(): # loop over picks
        pick = Pick(
            time=rowp["time"],
            waveform_id=WaveformStreamID(seed_string=rowp["seed_id"])
        )
        arrival = Arrival(phase=rowp["phase"], pick_id=pick.resource_id)
        picks.append(pick)
        arrivals.append(arrival)

    # Associate arrivals to the Origin object
    origin.arrivals = arrivals

    # Create the Event object
    event = Event(origins=[origin], magnitudes=[magnitude], picks=picks)

    # Append the Event object to the Catalog object
    cat.append(event)

# Save as QUAKEML format
cat.write("catalog.quakeml", format="QUAKEML")

data files:
20190101050556.txt
20190101061544.txt
catalog.csv

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions