Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
seisman opened this issue Sep 2, 2023 · 0 comments
Open

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

seisman opened this issue Sep 2, 2023 · 0 comments

Comments

@seisman
Copy link
Member

seisman commented Sep 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant