-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathatacr_correct_event.py
520 lines (447 loc) · 19.4 KB
/
atacr_correct_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python
# Copyright 2019 Pascal Audet & Helen Janiszewski
#
# This file is part of OBStools.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Import modules and functions
import numpy as np
from obspy import UTCDateTime, Stream
import pickle
import stdb
from obstools.atacr import EventStream
from obstools.atacr import utils, plotting
from pathlib import Path
from argparse import ArgumentParser
from os.path import exists as exist
from numpy import nan
def get_correct_arguments(argv=None):
"""
Get Options from :class:`~optparse.OptionParser` objects.
Calling options for the script `obs_correct_event.py` that accompanies this
package.
"""
parser = ArgumentParser(
usage="%(prog)s [options] <indb>",
description="Script used "
"to extract transfer functions between various " +
"components, and use them to clean vertical " +
"component of OBS data for selected events. The " +
"noise data can be those obtained from the daily " +
"spectra (i.e., from `obs_daily_spectra.py`) "
"or those obtained from the averaged noise spectra " +
"(i.e., from `obs_clean_spectra.py`). Flags are " +
"available to specify the source of data to use as " +
"well as the time range for given events. "
"The stations are processed one by one and the " +
"data are stored to disk in a new 'CORRECTED' folder.")
parser.add_argument(
"indb",
help="Station Database to process from.",
type=str)
# General Settings
parser.add_argument(
"--keys",
action="store",
type=str,
dest="stkeys",
default="",
help="Specify a comma separated list of station " +
"keys for which to perform the analysis. These must be "
"contained within the station database. Partial keys " +
"will be used to match against those in the "
"dictionary. For instance, providing IU will match with " +
"all stations in the IU network. [Default processes "
"all stations in the database]")
parser.add_argument(
"-O", "--overwrite",
action="store_true",
dest="ovr",
default=False,
help="Force the overwriting of pre-existing data. " +
"[Default False]")
# Event Selection Criteria
DaysGroup = parser.add_argument_group(
title="Time Search Settings",
description="Time settings associated with " +
"searching for specific event-related seismograms")
DaysGroup.add_argument(
"--start",
action="store",
type=str,
dest="startT",
default="",
help="Specify a UTCDateTime compatible string " +
"representing the start day for the event search. "
"This will override any station start times. " +
"[Default start date of each station in database]")
DaysGroup.add_argument(
"--end",
action="store",
type=str,
dest="endT",
default="",
help="Specify a UTCDateTime compatible string " +
"representing the start time for the event search. "
"This will override any station end times. [Default " +
"end date of each station in database]")
# Constants Settings
ConstGroup = parser.add_argument_group(
title='Parameter Settings',
description="Miscellaneous default " +
"values and settings")
ConstGroup.add_argument(
"--skip-daily",
action="store_true",
dest="skip_daily",
default=False,
help="Skip daily spectral averages in application " +
"of transfer functions. [Default False]")
ConstGroup.add_argument(
"--skip-clean",
action="store_true",
dest="skip_clean",
default=False,
help="Skip cleaned spectral averages in " +
"application of transfer functions. " +
"[Default False]")
ConstGroup.add_argument(
"--fmin",
action="store",
type=float,
dest="fmin",
default="0.006666666666666667",
help="Low frequency corner (in Hz) for " +
"plotting the raw (un-corrected) seismograms. "
"Filter is a 2nd order, zero phase butterworth " +
"filter. [Default 1./150.]")
ConstGroup.add_argument(
"--fmax",
action="store",
type=float,
dest="fmax",
default="0.1",
help="High frequency corner (in Hz) for " +
"plotting the raw (un-corrected) seismograms. "
"Filter is a 2nd order, zero phase butterworth " +
"filter. [Default 1./10.]")
# Constants Settings
FigureGroup = parser.add_argument_group(
title='Figure Settings',
description="Flags for plotting figures")
FigureGroup.add_argument(
"--figRaw",
action="store_true",
dest="fig_event_raw",
default=False,
help="Plot raw seismogram figure. " +
"[Default does not plot figure]")
FigureGroup.add_argument(
"--figClean",
action="store_true",
dest="fig_plot_corrected",
default=False,
help="Plot cleaned vertical seismogram figure. " +
"[Default does not plot figure]")
FigureGroup.add_argument(
"--save-fig",
action="store_true",
dest="saveplot",
default=False,
help="Set this option if you wish to save the figure(s). [Default " +
"does not save figure]")
FigureGroup.add_argument(
"--format",
action="store",
type=str,
dest="form",
default="png",
help="Specify format of figure. Can be any one of the valid" +
"matplotlib formats: 'png', 'jpg', 'eps', 'pdf'. [Default 'png']")
args = parser.parse_args(argv)
# Check inputs
if not exist(args.indb):
parser.error("Input file " + args.indb + " does not exist")
# create station key list
if len(args.stkeys) > 0:
args.stkeys = args.stkeys.split(',')
# construct start time
if len(args.startT) > 0:
try:
args.startT = UTCDateTime(args.startT)
except Exception:
parser.error(
"Error: Cannot construct UTCDateTime from " +
"start time: " + args.startT)
else:
args.startT = None
# construct end time
if len(args.endT) > 0:
try:
args.endT = UTCDateTime(args.endT)
except Exception:
parser.error(
"Error: Cannot construct UTCDateTime from " +
"end time: " + args.endT)
else:
args.endT = None
if args.skip_clean and args.skip_daily:
parser.error(
"Error: cannot skip both daily and clean averages")
return args
def main(args=None):
if args is None:
# Run Input Parser
args = get_correct_arguments()
# Load Database
# stdb>0.1.3
try:
db, stkeys = stdb.io.load_db(fname=args.indb, keys=args.stkeys)
# stdb=0.1.3
except Exception:
db = stdb.io.load_db(fname=args.indb)
# Construct station key loop
allkeys = db.keys()
sorted(allkeys)
# Extract key subset
if len(args.stkeys) > 0:
stkeys = []
for skey in args.stkeys:
stkeys.extend([s for s in allkeys if skey in s])
else:
stkeys = db.keys()
sorted(stkeys)
# Loop over station keys
for stkey in list(stkeys):
# Extract station information from dictionary
sta = db[stkey]
# Path where transfer functions will be located
transpath = Path('TF_STA') / stkey
if not transpath.is_dir():
raise(Exception("Path to "+str(transpath) +
" doesn`t exist - aborting"))
# Path where event data are located
eventpath = Path('EVENTS') / stkey
if not eventpath.is_dir():
raise(Exception("Path to "+str(eventpath) +
" doesn`t exist - aborting"))
# Path where plots will be saved
if args.saveplot:
plotpath = eventpath / 'PLOTS'
if not plotpath.is_dir():
plotpath.mkdir(parents=True)
else:
plotpath = False
# Get catalogue search start time
if args.startT is None:
tstart = sta.startdate
else:
tstart = args.startT
# Get catalogue search end time
if args.endT is None:
tend = sta.enddate
else:
tend = args.endT
if tstart > sta.enddate or tend < sta.startdate:
continue
# Temporary print locations
tlocs = sta.location
if len(tlocs) == 0:
tlocs = ['']
for il in range(0, len(tlocs)):
if len(tlocs[il]) == 0:
tlocs[il] = "--"
sta.location = tlocs
# Update Display
print(" ")
print(" ")
print("|===============================================|")
print("|===============================================|")
print("| {0:>8s} |".format(
sta.station))
print("|===============================================|")
print("|===============================================|")
print("| Station: {0:>2s}.{1:5s} |".format(
sta.network, sta.station))
print("| Channel: {0:2s}; Locations: {1:15s} |".format(
sta.channel, ",".join(tlocs)))
print("| Lon: {0:7.2f}; Lat: {1:6.2f} |".format(
sta.longitude, sta.latitude))
print("| Start time: {0:19s} |".format(
sta.startdate.strftime("%Y-%m-%d %H:%M:%S")))
print("| End time: {0:19s} |".format(
sta.enddate.strftime("%Y-%m-%d %H:%M:%S")))
print("|-----------------------------------------------|")
# Get all components
trE1, trE2, trEZ, trEP = utils.get_event(eventpath, tstart, tend)
# Find all TF files in directory
p = list(transpath.glob('*.*'))
trans_files = [x for x in p if x.is_file()]
# Check if folders contain anything
if not trans_files:
raise(Exception("There are no transfer functions in folder " +
str(transpath)))
# Cycle through available data
for tr1, tr2, trZ, trP in zip(trE1, trE2, trEZ, trEP):
eventstream = EventStream(tr1, tr2, trZ, trP)
# Check if Trace is from SAC file with event info
evlo = None
evla = None
if hasattr(trZ.stats, 'sac'):
if hasattr(trZ.stats.sac, 'evlo'):
evlo = trZ.stats.sac.evlo
evla = trZ.stats.sac.evla
if args.fig_event_raw:
fname = stkey + '.' + eventstream.tstamp + '.raw'
plot = plotting.fig_event_raw(
eventstream,
fmin=args.fmin, fmax=args.fmax)
if plotpath:
plot.savefig(
plotpath / (fname + '.' + args.form),
dpi=300, bbox_inches='tight', format=args.form)
else:
plot.show()
# Cycle through corresponding TF files
for transfile in trans_files:
# Skip hidden files and folders
if transfile.name[0] == '.':
continue
tfprefix = transfile.name.split('transfunc')[0]
print(tfprefix)
# This case refers to the "cleaned" spectral averages
if len(tfprefix) > 9:
if not args.skip_clean:
yr1 = tfprefix.split('-')[0].split('.')[0]
jd1 = tfprefix.split('-')[0].split('.')[1]
yr2 = tfprefix.split('-')[1].split('.')[0]
jd2 = tfprefix.split('-')[1].split('.')[1]
date1 = UTCDateTime(yr1+'-'+jd1)
date2 = UTCDateTime(yr2+'-'+jd2)
dateev = eventstream.evtime
if dateev >= date1 and dateev <= date2:
print(str(transfile) +
" file found - applying transfer functions")
try:
file = open(transfile, 'rb')
tfaverage = pickle.load(file)
file.close()
except Exception:
print("File "+str(transfile) +
" exists but cannot be loaded")
continue
# List of possible transfer functions for station
# average files
eventstream.correct_data(tfaverage)
correct_sta = eventstream.correct
if args.fig_plot_corrected:
fname = eventstream.prefix + '.sta_corrected'
plot = plotting.fig_event_corrected(
eventstream, tfaverage.tf_list)
# Save or show figure
if plotpath:
plot.savefig(
plotpath / (fname + '.' + args.form),
dpi=300, bbox_inches='tight',
format=args.form)
else:
plot.show()
# Save corrected data to disk
correctpath = eventpath / 'CORRECTED'
if not correctpath.is_dir():
correctpath.mkdir(parents=True)
file = correctpath / eventstream.prefix
eventstream.save(str(file) + '.sta.pkl')
# Now save as SAC files
for key, value in tfaverage.tf_list.items():
if value and eventstream.ev_list[key]:
# Postfix
nameZ = '.sta.' + key + '.'
nameZ += sta.channel + 'Z.SAC'
# Add Prefix and Postfix
fileZ = str(file) + nameZ
# Select Z component and update trace
trZ = eventstream.trZ.copy()
trZ.data = eventstream.correct[key]
trZ = utils.update_stats(
trZ, sta.latitude, sta.longitude,
sta.elevation, sta.channel+'Z',
evla=evla,
evlo=evlo)
# Save as SAC file
trZ.write(str(fileZ), format='SAC')
# This case refers to the "daily" spectral averages
else:
if not args.skip_daily:
evprefix = eventstream.tstamp.split('.')
evstamp = evprefix[0]+'.'+evprefix[1]+'.'
if tfprefix == evstamp:
print(str(transfile) +
" file found - applying transfer functions")
try:
file = open(transfile, 'rb')
tfaverage = pickle.load(file)
file.close()
except Exception:
print("File "+str(transfile) +
" exists but cannot be loaded")
continue
# List of possible transfer functions for station
# average files
eventstream.correct_data(tfaverage)
correct_day = eventstream.correct
if args.fig_plot_corrected:
fname = eventstream.prefix + '.day_corrected'
plot = plotting.fig_event_corrected(
eventstream, tfaverage.tf_list)
# Save or show figure
if plotpath:
plot.savefig(
plotpath / (fname + '.' + args.form),
dpi=300, bbox_inches='tight',
format=args.form)
else:
plot.show()
# Save corrected data to disk
correctpath = eventpath / 'CORRECTED'
if not correctpath.is_dir():
correctpath.mkdir(parents=True)
file = correctpath / eventstream.prefix
eventstream.save(str(file) + '.day.pkl')
# Now save as SAC files
for key, value in tfaverage.tf_list.items():
if value and eventstream.ev_list[key]:
# Postfix
nameZ = '.day.' + key + '.'
nameZ += sta.channel+'Z.SAC'
# Add Prefix and Postfix
fileZ = str(file) + nameZ
# Select Z component and update trace
trZ = eventstream.trZ.copy()
trZ.data = eventstream.correct[key]
trZ = utils.update_stats(
trZ, sta.latitude, sta.longitude,
sta.elevation, sta.channel+'Z',
evla=evla,
evlo=evlo)
# Save as SAC file
trZ.write(str(fileZ), format='SAC')
if __name__ == "__main__":
# Run main program
main()