cangyeone commited on
Commit
10914e4
·
verified ·
1 Parent(s): d622586

Upload scripts/example_query_waveform.py

Browse files
Files changed (1) hide show
  1. scripts/example_query_waveform.py +106 -1
scripts/example_query_waveform.py CHANGED
@@ -9,7 +9,6 @@ python example_query_waveform.py \
9
  --endtime 2019-07-01T01:00:00
10
  """
11
  import argparse
12
- import json
13
  import sys
14
  from pathlib import Path
15
 
@@ -20,6 +19,28 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
20
  from utils.waveform_index_api import query_and_merge, save_merged_to_npz
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def main():
24
  parser = argparse.ArgumentParser(
25
  description="Example API client for querying and merging waveform data."
@@ -92,8 +113,72 @@ def main():
92
  action="store_true",
93
  help="Print segment details.",
94
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  args = parser.parse_args()
 
97
 
98
  merged, rows = query_and_merge(
99
  db_file=args.db,
@@ -106,6 +191,17 @@ def main():
106
  fill_value=args.fill_value,
107
  dtype=np.float32,
108
  limit=args.limit,
 
 
 
 
 
 
 
 
 
 
 
109
  )
110
 
111
  print("=" * 80)
@@ -153,6 +249,15 @@ def main():
153
  f"filled_ratio={item['filled_ratio']:.4f}, "
154
  f"segments={len(item['segments'])}"
155
  )
 
 
 
 
 
 
 
 
 
156
 
157
  if args.output_npz:
158
  save_merged_to_npz(merged, args.output_npz)
 
9
  --endtime 2019-07-01T01:00:00
10
  """
11
  import argparse
 
12
  import sys
13
  from pathlib import Path
14
 
 
19
  from utils.waveform_index_api import query_and_merge, save_merged_to_npz
20
 
21
 
22
+ def parse_optional_float(value):
23
+ if value is None:
24
+ return None
25
+ text = str(value).strip().lower()
26
+ if text in {"none", "null", ""}:
27
+ return None
28
+ out = float(value)
29
+ return None if out < 0 else out
30
+
31
+
32
+ def build_simulation_selector(args):
33
+ fields = {
34
+ "network": args.simulation_network,
35
+ "station": args.simulation_station,
36
+ "location": args.simulation_location,
37
+ "channel": args.simulation_channel,
38
+ "time": args.simulation_time,
39
+ }
40
+ selector = {key: value for key, value in fields.items() if value not in (None, "")}
41
+ return selector or None
42
+
43
+
44
  def main():
45
  parser = argparse.ArgumentParser(
46
  description="Example API client for querying and merging waveform data."
 
113
  action="store_true",
114
  help="Print segment details.",
115
  )
116
+ parser.add_argument(
117
+ "--response_json",
118
+ default="data/response/instrument_responses.json",
119
+ help="Instrument-response JSON used by --remove_response or --simulate_response.",
120
+ )
121
+ parser.add_argument(
122
+ "--remove_response",
123
+ action="store_true",
124
+ help="Remove the native instrument response with ObsPy before returning arrays.",
125
+ )
126
+ parser.add_argument(
127
+ "--response_output",
128
+ default="VEL",
129
+ help="Physical output unit for response removal: DISP, VEL, or ACC.",
130
+ )
131
+ parser.add_argument(
132
+ "--response_pre_filt",
133
+ nargs=4,
134
+ type=float,
135
+ default=None,
136
+ metavar=("F1", "F2", "F3", "F4"),
137
+ help="Four-corner frequency pre-filter passed to ObsPy remove_response.",
138
+ )
139
+ parser.add_argument(
140
+ "--response_water_level",
141
+ default="60",
142
+ help='ObsPy water level. Use "none" or a negative value to pass None.',
143
+ )
144
+ parser.add_argument(
145
+ "--response_error_behavior",
146
+ choices=("raise", "warn", "skip"),
147
+ default="raise",
148
+ help="How to handle response-processing failures.",
149
+ )
150
+ parser.add_argument(
151
+ "--simulate_response",
152
+ action="store_true",
153
+ help="Simulate a target instrument response after optional native-response removal.",
154
+ )
155
+ parser.add_argument(
156
+ "--simulation_response_json",
157
+ default=None,
158
+ help="Response JSON containing the target response; defaults to --response_json.",
159
+ )
160
+ parser.add_argument(
161
+ "--simulation_response_id",
162
+ default=None,
163
+ help="Exact target response_id to simulate.",
164
+ )
165
+ parser.add_argument("--simulation_network", default=None)
166
+ parser.add_argument("--simulation_station", default=None)
167
+ parser.add_argument("--simulation_location", default=None)
168
+ parser.add_argument("--simulation_channel", default=None)
169
+ parser.add_argument(
170
+ "--simulation_time",
171
+ default=None,
172
+ help="Time used with station-channel target-response selection.",
173
+ )
174
+ parser.add_argument(
175
+ "--simulation_output",
176
+ default=None,
177
+ help="Physical unit used when evaluating the target response; defaults to --response_output.",
178
+ )
179
 
180
  args = parser.parse_args()
181
+ simulation_selector = build_simulation_selector(args)
182
 
183
  merged, rows = query_and_merge(
184
  db_file=args.db,
 
191
  fill_value=args.fill_value,
192
  dtype=np.float32,
193
  limit=args.limit,
194
+ instrument_response_json=args.response_json,
195
+ remove_instrument_response=args.remove_response,
196
+ response_output=args.response_output,
197
+ response_pre_filt=tuple(args.response_pre_filt) if args.response_pre_filt else None,
198
+ response_water_level=parse_optional_float(args.response_water_level),
199
+ response_error_behavior=args.response_error_behavior,
200
+ simulate_instrument_response=args.simulate_response,
201
+ simulation_response_json=args.simulation_response_json,
202
+ simulation_response_id=args.simulation_response_id,
203
+ simulation_response_selector=simulation_selector,
204
+ simulation_output=args.simulation_output,
205
  )
206
 
207
  print("=" * 80)
 
249
  f"filled_ratio={item['filled_ratio']:.4f}, "
250
  f"segments={len(item['segments'])}"
251
  )
252
+ if "instrument_processing" in item:
253
+ proc = item["instrument_processing"]
254
+ print(
255
+ " instrument_processing: "
256
+ f"processed={proc.get('processed')} "
257
+ f"response_id={proc.get('response_id', '')} "
258
+ f"simulation_response_id={proc.get('simulation_response_id', '')} "
259
+ f"error={proc.get('error', '')}"
260
+ )
261
 
262
  if args.output_npz:
263
  save_merged_to_npz(merged, args.output_npz)