Skip to content

Commit 38a203a

Browse files
authored
Merge pull request #39 from JunhaoSong/patch-1
Update run_skhash.py
2 parents f7d1181 + 350fe33 commit 38a203a

File tree

1 file changed

+24
-64
lines changed

1 file changed

+24
-64
lines changed

scripts/run_skhash.py

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# %%
21
import argparse
32
import os
43
from glob import glob
@@ -11,7 +10,6 @@
1110
from sklearn.cluster import DBSCAN
1211
from args import parse_args
1312

14-
1513
args = parse_args()
1614
root_path = args.root_path
1715
region = args.region
@@ -27,9 +25,12 @@
2725
if not os.path.exists(f"{root_path}/{result_path}/OUT"):
2826
os.makedirs(f"{root_path}/{result_path}/OUT")
2927

28+
3029
# %%
3130
if not os.path.exists(f"../SKHASH"):
32-
os.system(f"git clone [email protected]:AI4EPS/SKHASH.git ../SKHASH")
31+
os.system(f"git clone https://code.usgs.gov/esc/SKHASH.git ../SKHASH")
32+
# os.system(f"git clone [email protected]:AI4EPS/SKHASH.git ../SKHASH")
33+
3334

3435
# %%
3536
stations = pd.read_json(f"{root_path}/{region}/obspy/stations.json", orient="index")
@@ -48,7 +49,6 @@
4849

4950
# %%
5051
events = pd.read_csv(f"{root_path}/{region}/adloc/ransac_events.csv", parse_dates=["time"])
51-
# events = pd.read_csv(f"{root_path}/{region}/adloc/debug_events.csv", parse_dates=["time"])
5252
if "magnitude" not in events.columns:
5353
events["magnitude"] = pd.NA
5454
events = events[["event_index", "time", "latitude", "longitude", "depth_km", "magnitude"]]
@@ -59,37 +59,14 @@
5959
events = events[events["depth"] >= 0]
6060
events.to_csv(f"{root_path}/{result_path}/IN/catalog.csv", index=None)
6161

62-
# %%
63-
picks = pd.read_csv(f"{root_path}/{region}/adloc/ransac_picks.csv", parse_dates=["phase_time"])
62+
##
63+
picks = pd.read_csv(f"{root_path}/{region}/adloc_plus/ransac_picks.csv", parse_dates=["phase_time"])
64+
# picks = picks[picks['phase_time'] < pd.to_datetime("2019-07-05 00:00:00")].copy()
6465
if "adloc_mask" in picks.columns:
6566
picks = picks[picks["adloc_mask"] == 1.0]
66-
# picks = pd.read_csv(f"{root_path}/{region}/adloc/debug_picks.csv", parse_dates=["phase_time"])
6767
picks.sort_values("phase_index", inplace=True)
6868
picks.rename(columns={"event_index": "event_id"}, inplace=True)
69-
70-
# TODO: check why ADLOC keep picks that are not in events
7169
picks = picks.merge(events[["event_id"]], on="event_id", how="right")
72-
73-
74-
# TODO: do the filtering in EQNet
75-
def filter_duplicates(picks):
76-
picks_filt = []
77-
picks["t"] = (picks["phase_time"] - picks["phase_time"].min()).dt.total_seconds()
78-
print(f"before {len(picks)} picks")
79-
dbscan = DBSCAN(eps=0.10, min_samples=1)
80-
for station_id, picks_ in picks.groupby("station_id"):
81-
dbscan.fit(picks_[["t"]])
82-
picks_["label"] = dbscan.labels_
83-
picks_ = picks_.groupby("label").first().reset_index()
84-
picks_.drop(columns=["label", "t"], inplace=True)
85-
picks_filt.append(picks_)
86-
picks_filt = pd.concat(picks_filt)
87-
print(f"after {len(picks_filt)} picks")
88-
return picks_filt
89-
90-
91-
picks = filter_duplicates(picks)
92-
9370
picks = picks[picks["event_id"] != -1]
9471
picks = picks.merge(
9572
stations[["station_id", "network", "station", "location", "channel"]],
@@ -105,36 +82,16 @@ def filter_duplicates(picks):
10582
ppicks.sort_values(["event_id", "network", "station", "location", "channel"], inplace=True)
10683
ppicks.to_csv(f"{root_path}/{result_path}/IN/pol.csv", index=None)
10784

108-
# %%
109-
picks["station_id"] = picks["network"] + "." + picks["station"] + "." + picks["location"] + "." + picks["channel"]
110-
stations["station_id"] = (
111-
stations["network"] + "." + stations["station"] + "." + stations["location"] + "." + stations["channel"]
112-
)
113-
sp_ratio = []
114-
for (event_id, staton_id), picks_ in picks.groupby(["event_id", "station_id"]):
115-
if len(picks_["phase_type"].unique()) < 2: ## assume P and S
116-
continue
117-
if picks_["phase_score"].max() < 0.3:
118-
continue
119-
ratio = (
120-
picks_[picks_["phase_type"] == "S"]["phase_amplitude"].values[0]
121-
/ picks_[picks_["phase_type"] == "P"]["phase_amplitude"].values[0]
122-
)
123-
network, station, location, channel = staton_id.split(".")
124-
sp_ratio.append(
125-
{
126-
"event_id": event_id,
127-
"network": network,
128-
"station": station,
129-
"location": location,
130-
"channel": channel,
131-
"sp_ratio": ratio,
132-
}
133-
)
134-
sp_ratio = pd.DataFrame(sp_ratio)
135-
sp_ratio.to_csv(f"{root_path}/{result_path}/IN/amp.csv", index=None)
13685

137-
# %%
86+
##
87+
amps = picks.drop_duplicates(subset=['event_id', 'station_id', 'sp_ratio']).copy()
88+
amps = amps.drop_duplicates(subset=["event_id", "station_id"]).copy()
89+
amps = amps[["event_id", "network", "station", "location", "channel", "sp_ratio"]]
90+
amps.sort_values(["event_id", "network", "station", "location", "channel"], inplace=True)
91+
amps.to_csv(f"{root_path}/{result_path}/IN/amp.csv", index=None)
92+
93+
94+
13895
# 1D model from Shelly (2020)
13996
velocity_model = """# 1D model from Shelly (2020)
14097
# Depth(km), Vp(km/s)
@@ -193,7 +150,7 @@ def filter_duplicates(picks):
193150
0.1
194151
195152
$nmc # number of trials (e.g., 30)
196-
50
153+
30
197154
198155
$maxout # max num of acceptable focal mech. outputs (e.g., 500)
199156
500
@@ -205,13 +162,13 @@ def filter_duplicates(picks):
205162
0.2
206163
207164
$delmax # maximum allowed source-receiver distance in km.
208-
100
165+
120
209166
210167
$prob_max # probability threshold for multiples (e.g., 0.1)
211168
0.2
212169
213170
$max_agap # maximum azimuthal gap between stations in degree
214-
150
171+
180
215172
216173
$max_pgap # maximum "plungal" gap between stations in degree
217174
90
@@ -220,12 +177,15 @@ def filter_duplicates(picks):
220177
45
221178
222179
$num_cpus # number of cpus for computing
223-
50
180+
30
181+
182+
$use_fortran
183+
True
224184
"""
185+
225186
with open(f"{root_path}/{result_path}/control_file.txt", "w") as fp:
226187
fp.writelines(control_params)
227188

228-
# %%
229189
# ! python SKHASH.py control_file.txt
230190
os.system(f"python ../SKHASH/SKHASH/SKHASH.py {root_path}/{result_path}/control_file.txt")
231191

0 commit comments

Comments
 (0)