seismo-learn / software

Tutorials for popular seismological software
https://seismo-learn.org/software
10 stars 3 forks source link

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

Open seisman opened 1 year ago

seisman commented 1 year ago
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