-
Notifications
You must be signed in to change notification settings - Fork 39
/
catfill.py
144 lines (119 loc) · 5.13 KB
/
catfill.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
# REDPy - Repeating Earthquake Detector in Python
# Copyright (C) 2016-2020 Alicia Hotovec-Ellis (ahotovec-ellis@usgs.gov)
# Licensed under GNU GPLv3 (see LICENSE.txt)
import argparse
import redpy
import numpy as np
import obspy
from obspy import UTCDateTime
import time
import pandas as pd
"""
Run this script to fill the table with data from the past using a catalog of events.
usage: catfill.py [-h] [-v] [-c CONFIGFILE] csvfile
positional arguments:
csvfile catalog csv file with a 'Time UTC' column of event times
optional arguments:
-h, --help show this help message and exit
-v, --verbose increase written print statements
-c CONFIGFILE, --configfile CONFIGFILE
use configuration file named CONFIGFILE instead of
default settings.cfg
"""
t = time.time()
parser = argparse.ArgumentParser(description=
"Backfills table with data from the past")
parser.add_argument("csvfile",
help="catalog csv file with a 'Time UTC' column of event times")
parser.add_argument("-v", "--verbose", action="count", default=0,
help="increase written print statements")
parser.add_argument("-c", "--configfile",
help="use configuration file named CONFIGFILE instead of default settings.cfg")
args = parser.parse_args()
if args.configfile:
opt = redpy.config.Options(args.configfile)
if args.verbose: print("Using config file: {0}".format(args.configfile))
else:
opt = redpy.config.Options("settings.cfg")
if args.verbose: print("Using config file: settings.cfg")
if args.verbose: print("Opening hdf5 table: {0}".format(opt.filename))
h5file, rtable, otable, ttable, ctable, jtable, dtable, ftable = redpy.table.openTable(opt)
# Check for MPL version mismatch
redpy.table.checkMPL(rtable, ftable, ttable, otable, dtable, opt)
# Read in csv file using pandas
df = pd.read_csv(args.csvfile)
# Grab event times from 'Time UTC' column, convert to datetimes also
eventlist = pd.to_datetime(df['Time UTC']).tolist()
# Sort so events are processed in order of occurrence
eventlist.sort()
for event in eventlist:
etime = UTCDateTime(event)
if len(ttable) > 0:
ttimes = ttable.cols.startTimeMPL[:]
else:
ttimes = 0
if args.verbose: print(etime)
# Download and trigger
try:
st, stC = redpy.trigger.getData(etime-5*opt.atrig, etime+5*opt.atrig, opt)
alltrigs = redpy.trigger.trigger(st, stC, rtable, opt)
# Reset ptime for refilling later
rtable.attrs.ptime = []
except (TypeError, obspy.fdsn.header.FDSNException, Exception):
print('Could not download or trigger data... moving on')
alltrigs = []
# Clean out data spikes etc.
trigs, junk, junkFI, junkKurt = redpy.trigger.dataClean(alltrigs, opt, flag=1)
# Save junk triggers in separate table for quality checking purposes
for i in range(len(junk)):
redpy.table.populateJunk(jtable, junk[i], 2, opt)
for i in range(len(junkKurt)):
redpy.table.populateJunk(jtable, junkKurt[i], 1, opt)
for i in range(len(junkFI)):
redpy.table.populateJunk(jtable, junkFI[i], 0, opt)
# Append times of triggers to ttable to compare total seismicity later
redpy.table.populateTriggers(ttable, trigs, ttimes, opt)
# Check triggers against deleted events
if len(dtable) > 0:
trigs = redpy.correlation.compareDeleted(trigs, dtable, opt)
if len(trigs) > 0:
id = rtable.attrs.previd
if len(trigs) == 1:
ostart = 0
if len(otable) == 0:
# First trigger goes to orphans table
redpy.table.populateOrphan(otable, 0, trigs[0], opt)
ostart = 1
else:
id = id + 1
redpy.correlation.runCorrelation(rtable, otable, ctable, ftable,
ttimes, trigs[0], id, opt)
else:
ostart = 0
if len(otable) == 0:
# First trigger goes to orphans table
redpy.table.populateOrphan(otable, 0, trigs[0], opt)
ostart = 1
# Loop through remaining triggers
for i in range(ostart,len(trigs)):
id = id + 1
redpy.correlation.runCorrelation(rtable, otable, ctable, ftable,
ttimes, trigs[i], id, opt)
rtable.attrs.previd = id
# Don't expire orphans in the catalog?
# redpy.table.clearExpiredOrphans(otable, opt, tstart+(n+1)*opt.nsec)
# Print some stats
if args.verbose:
print("Length of Orphan table: {}".format(len(otable)))
if len(rtable) > 1:
print("Number of repeaters: {}".format(len(rtable)))
print("Number of clusters: {}".format(ftable.attrs.nClust))
if len(rtable) > 1:
if args.verbose: print("Creating plots...")
redpy.plotting.createPlots(rtable, ftable, ttable, ctable, otable, opt)
else:
print("No repeaters to plot.")
if args.verbose: print("Closing table...")
h5file.close()
if args.verbose: print("Total time spent: {} minutes".format((time.time()-t)/60))
if args.verbose: print("Done")