In [1]:
Copied!
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import obspy
import requests
sys.path.insert(0, os.path.abspath("../"))
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import obspy
import requests
sys.path.insert(0, os.path.abspath("../"))
In [2]:
Copied!
# PHASENET_API_URL = "http://127.0.0.1:8000"
# PHASENET_API_URL = "http://phasenet.quakeflow.com" ## gcp
# PHASENET_API_URL = "http://test.quakeflow.com:8001" ## local machine
PHASENET_API_URL = "https://ai4eps-eqnet.hf.space"
# PHASENET_API_URL = "http://131.215.74.195:8001" ## local machine
# PHASENET_API_URL = "http://127.0.0.1:8000"
# PHASENET_API_URL = "http://phasenet.quakeflow.com" ## gcp
# PHASENET_API_URL = "http://test.quakeflow.com:8001" ## local machine
PHASENET_API_URL = "https://ai4eps-eqnet.hf.space"
# PHASENET_API_URL = "http://131.215.74.195:8001" ## local machine
2. Prepare seismic waveforms¶
Find more details in obspy's tutorials:
In [3]:
Copied!
import obspy
stream = obspy.read()
stream.plot();
import obspy
stream = obspy.read()
stream.plot();
In [4]:
Copied!
## Extract 3-component data
stream = stream.sort()
assert(len(stream) == 3)
data = []
for trace in stream:
data.append(trace.data)
data = np.array(data).T
assert(data.shape[-1] == 3)
data_id = stream[0].get_id()[:-1]
timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
## Extract 3-component data
stream = stream.sort()
assert(len(stream) == 3)
data = []
for trace in stream:
data.append(trace.data)
data = np.array(data).T
assert(data.shape[-1] == 3)
data_id = stream[0].get_id()[:-1]
timestamp = stream[0].stats.starttime.datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
3. Predict P/S-phase picks using PhaseNet¶
In [5]:
Copied!
req = {"id": [data_id],
"timestamp": [timestamp],
"vec": [data.tolist()]}
resp = requests.post(f'{PHASENET_API_URL}/predict', json=req)
print(resp)
print('Picks', resp.json())
req = {"id": [data_id],
"timestamp": [timestamp],
"vec": [data.tolist()]}
resp = requests.post(f'{PHASENET_API_URL}/predict', json=req)
print(resp)
print('Picks', resp.json())
<Response [200]> Picks [{'station_id': 'B', 'phase_time': '2009-08-24T00:20:07.710', 'phase_score': 0.978, 'phase_type': 'P', 'dt': 0.01}, {'station_id': 'B', 'phase_time': '2009-08-24T00:20:08.680', 'phase_score': 0.924, 'phase_type': 'S', 'dt': 0.01}]
4. Get both picks and prediction (time series)¶
In [6]:
Copied!
resp = requests.post(f'{PHASENET_API_URL}/predict_prob', json=req)
print(resp)
picks, preds = resp.json()
preds = np.array(preds)
print('Picks', picks)
plt.figure()
plt.subplot(211)
plt.plot(data[:,-1], 'k', label="Z")
plt.subplot(212)
plt.plot(preds[0, :, 0, 1], label="P")
plt.plot(preds[0, :, 0, 2], label="S")
plt.legend()
plt.show();
resp = requests.post(f'{PHASENET_API_URL}/predict_prob', json=req)
print(resp)
picks, preds = resp.json()
preds = np.array(preds)
print('Picks', picks)
plt.figure()
plt.subplot(211)
plt.plot(data[:,-1], 'k', label="Z")
plt.subplot(212)
plt.plot(preds[0, :, 0, 1], label="P")
plt.plot(preds[0, :, 0, 2], label="S")
plt.legend()
plt.show();
<Response [200]> Picks [{'station_id': 'B', 'phase_time': '2009-08-24T00:20:07.710', 'phase_score': 0.978, 'phase_type': 'P', 'dt': 0.01}, {'station_id': 'B', 'phase_time': '2009-08-24T00:20:08.680', 'phase_score': 0.924, 'phase_type': 'S', 'dt': 0.01}]