Seismic phase association

From XuShuwenn/RedAct · View interactive version

Seismic phase association is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Run deep-learning phase picking (SeisBench) and robustly associate picks into earthquakes with GaMMA, with ID alignment, timestamp, projection, and config sanity checks.

How to use this skill

Skill file

Seismic Phase Association

This skill turns continuous multi-station waveform data into an event list by:

When to Use

Core Workflow

1) Load data

2) Prepare station table for association

Example projection fallback:

import numpy as np
lon0 = stations['longitude'].mean()
lat0 = stations['latitude'].mean()
km_per_deg_lat = 110.574
km_per_deg_lon = 111.320 * np.cos(np.deg2rad(lat0))
stations['x(km)'] = (stations['longitude'] - lon0) * km_per_deg_lon
stations['y(km)'] = (stations['latitude'] - lat0) * km_per_deg_lat
stations['z(km)'] = -stations['elevation_m'] / 1000.0
stations = stations[['id','x(km)','y(km)','z(km)']]

3) Phase picking with SeisBench

Example pick extraction:

co = model.classify(stream, P_threshold=0.3, S_threshold=0.3)
rows = []
for p in co.picks:
    parts = p.trace_id.split('.')
    net, sta = parts[0], parts[1]
    rows.append({
        'id': f"{net}.{sta}.",
        'timestamp': pd.to_datetime(p.peak_time.datetime, utc=True),
        'type': p.phase.lower(),
        'prob': float(p.peak_value),
    })
picks = pd.DataFrame(rows)

4) Align IDs between picks and stations

5) Configure GaMMA

Example config skeleton:

x_min, x_max = stations['x(km)'].min()-10, stations['x(km)'].max()+10
y_min, y_max = stations['y(km)'].min()-10, stations['y(km)'].max()+10
z_min, z_max = 0.0, 30.0
try:
    from gamma.utils import estimate_eps
    eps = estimate_eps(stations, vp)
except Exception:
    eps = 20.0
config = {
  'dims': ['x(km)','y(km)','z(km)'],
  'vel': {'p': vp, 's': vs},
  'min_picks_per_eq': 5,
  'use_dbscan': True,
  'dbscan_eps': eps,
  'dbscan_min_samples': 3,
  'x(km)': (x_min, x_max),
  'y(km)': (y_min, y_max),
  'z(km)': (z_min, z_max),
  'bfgs_bounds': ((x_min, x_max),(y_min, y_max),(z_min, z_max),(None,None)),
  'use_amplitude': False,
  'oversample_factor': 5,  # int
  'ncpu': 1,
}

6) Run association and write output

out = []
for ev in events:
    t = pd.to_datetime(ev.get('time'))
    if getattr(t, 'tzinfo', None) is not None:
        t = t.tz_convert(None)
    out.append({'time': t.isoformat(timespec='milliseconds')})
pd.DataFrame(out).drop_duplicates().sort_values('time').to_csv(output_path, index=False)

Verification

Common Pitfalls and Fixes

Optional Script Usage

python scripts/assoc_sanity_check.py \
  --picks picks.csv \
  --stations stations.csv \
  --out-picks picks_norm.csv \
  --out-stations stations_gamma.csv

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.