1- # %%
21import argparse
32import os
43from glob import glob
1110from sklearn .cluster import DBSCAN
1211from args import parse_args
1312
14-
1513args = parse_args ()
1614root_path = args .root_path
1715region = args .region
2725if not os .path .exists (f"{ root_path } /{ result_path } /OUT" ):
2826 os .makedirs (f"{ root_path } /{ result_path } /OUT" )
2927
28+
3029# %%
3130if 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# %%
3536stations = pd .read_json (f"{ root_path } /{ region } /obspy/stations.json" , orient = "index" )
4849
4950# %%
5051events = 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"])
5252if "magnitude" not in events .columns :
5353 events ["magnitude" ] = pd .NA
5454events = events [["event_index" , "time" , "latitude" , "longitude" , "depth_km" , "magnitude" ]]
5959events = events [events ["depth" ] >= 0 ]
6060events .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()
6465if "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"])
6767picks .sort_values ("phase_index" , inplace = True )
6868picks .rename (columns = {"event_index" : "event_id" }, inplace = True )
69-
70- # TODO: check why ADLOC keep picks that are not in events
7169picks = 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-
9370picks = picks [picks ["event_id" ] != - 1 ]
9471picks = picks .merge (
9572 stations [["station_id" , "network" , "station" , "location" , "channel" ]],
@@ -105,36 +82,16 @@ def filter_duplicates(picks):
10582ppicks .sort_values (["event_id" , "network" , "station" , "location" , "channel" ], inplace = True )
10683ppicks .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)
13996velocity_model = """# 1D model from Shelly (2020)
14097# Depth(km), Vp(km/s)
@@ -193,7 +150,7 @@ def filter_duplicates(picks):
1931500.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)
199156500
@@ -205,13 +162,13 @@ def filter_duplicates(picks):
2051620.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)
2111680.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
21717490
@@ -220,12 +177,15 @@ def filter_duplicates(picks):
22017745
221178
222179$num_cpus # number of cpus for computing
223- 50
180+ 30
181+
182+ $use_fortran
183+ True
224184"""
185+
225186with 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
230190os .system (f"python ../SKHASH/SKHASH/SKHASH.py { root_path } /{ result_path } /control_file.txt" )
231191
0 commit comments