Skip to content

Commit

Permalink
Merge pull request #236 from Lachlan-Adams/develop
Browse files Browse the repository at this point in the history
Minor update to database communication tools in seismic/ssst_relocation/relocation/Relocation.py
  • Loading branch information
Lachlan-Adams committed Jan 28, 2022
2 parents 0e1fd29 + 9238e23 commit 55a05f9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
73 changes: 49 additions & 24 deletions seismic/ssst_relocation/relocation/Relocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,12 @@ def push_time_corrections_to_database(picks):
return
#end func

def extract_hypocentres_from_database(events, picks, config,
unstable_events=list()):
def update_hypocentres_from_database(events, picks, hypo_dict, config,
unstable_events=list()):
"""
Retrieves updated hypocentres from SeisComp3 database after relocation has
been performed. Attaches these new hypocentres to their corresponding
picks, or if the hypocentre has changed by more than 'thr_dist' or
'thr_time', event ID is added to 'unstable_events'.
Attaches new hypocentres to their corresponding picks, or if the hypocentre
has changed by more than 'thr_dist' or 'thr_time', event ID is added to
'unstable_events'.
Parameters
Expand All @@ -213,6 +212,9 @@ def extract_hypocentres_from_database(events, picks, config,
('domFreq', 'half'), ('qualityMeasureSlope', 'half'),
('bandIndex', 'uint8'), ('nSigma', 'uint8')]
hypo_dict : dictionary
Dictionary of updated event hypocentres.
config : configparser.SectionProxy object
Information from config file.
Expand All @@ -231,28 +233,10 @@ def extract_hypocentres_from_database(events, picks, config,
"""
import MySQLdb
from obspy import UTCDateTime

thr_dist = float(config['hypo_thr_dist_deg'])
thr_time = float(config['hypo_thr_time_sec'])

db = MySQLdb.connect(host="localhost", user="sysop", passwd="sysop",
db="seiscomp3")
c = db.cursor()
c.execute(str('select ep.publicID, o.longitude_value, ' + \
'o.latitude_value, o.depth_value, o.time_value, ' + \
'o.time_value_ms, o._oid from Origin o, ' + \
'PublicObject op, Event e, PublicObject ep where ' + \
'o._oid=op._oid and e._oid=ep._oid and ' + \
'e.preferredOriginID=op.publicID'))
rows = c.fetchall()
hypo_dict = {row[0]: (float(row[1]), float(row[2]), float(row[3])*1e3,
UTCDateTime(row[4]).timestamp + int(row[5])/1e6) \
for row in rows if row[0] in events}
c.close()
db.close()

for event in events:
inds = picks['event_id'] == event
picks_temp = picks[inds]
Expand All @@ -276,6 +260,47 @@ def extract_hypocentres_from_database(events, picks, config,
picks['origin_time'][inds] = time2
#end for
return picks, unstable_events
#end func

def extract_hypocentres_from_database(events):
"""
Retrieves updated hypocentres from SeisComp3 database after relocation has
been performed.
Parameters
----------
events : list
List of event IDs for which to retrieve updated hypocentres.
Returns
-------
hypo_dict : dictionary
Dictionary of updated event hypocentres.
"""
import MySQLdb
from obspy import UTCDateTime

db = MySQLdb.connect(host="localhost", user="sysop", passwd="sysop",
db="seiscomp3")
c = db.cursor()
c.execute(str('select ep.publicID, o.longitude_value, ' + \
'o.latitude_value, o.depth_value, o.time_value, ' + \
'o.time_value_ms, o._oid from Origin o, ' + \
'PublicObject op, Event e, PublicObject ep where ' + \
'o._oid=op._oid and e._oid=ep._oid and ' + \
'e.preferredOriginID=op.publicID'))
rows = c.fetchall()
hypo_dict = {row[0]: (float(row[1]), float(row[2]), float(row[3])*1e3,
UTCDateTime(row[4]).timestamp + int(row[5])/1e6) \
for row in rows if row[0] in events}
c.close()
db.close()

return hypo_dict
#end func

def compute_new_hypocentre(events, picks, TT_dict, ellipcorr_dict, output_path,
Expand Down
50 changes: 32 additions & 18 deletions seismic/ssst_relocation/relocation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ def process():
"--elcordir", '/g/data/ha3/la8536/SSST/TT_tables/',
"--iteration", '0',
"--output_path", '/g/data/ha3/la8536/SSST/test/']
sys.argv = ['/home/centos/ladams/ssst_relocation/relocation/main.py',
"--config_file",
'/home/centos/ladams/ssst_relocation/relocation/relocation_config.txt',
"--tt_table_path",
'/home/centos/ladams/ssst_relocation/TT_tables/',
"--input_path",
'/home/centos/ladams/ssst_relocation/output_events/ssst_iloc/',
"--elcordir", '/home/centos/ladams/ssst_relocation/TT_tables/',
"--iteration", '0', "--output_path",
'/home/centos/ladams/ssst_relocation/output_events/ssst_iloc/',
"--relocation_algorithm", 'iloc']
"""

args = parser.parse_args()
Expand Down Expand Up @@ -410,11 +421,17 @@ def process():
if relocation_algorithm == 'iloc':
"""
If relocation algorithm is iloc:
1. Partition event ID between processors.
1. Partition pick array by event ID between processors.
2. Push time corrections to database.
3. Save pick arrays to file.
"""
from Relocation import push_time_corrections_to_database
push_time_corrections_to_database(picks)
for i in range(nproc):
np.save(os.path.join(output_path,
'%s.npy'%str(i).zfill(3)),
picks_split[i])
#end for
write(outfile, 'Relocating events using iLoc, time = ',
time.time() - t0)
else:
Expand Down Expand Up @@ -444,33 +461,30 @@ def process():
If relocation algorithm is iloc:
1. Compute new hypocentres using iloc.
2. Extract hypocentres from database.
3. Split pick array randomly and save to file for calculation
of residuals with respect to new hypocentre.
4. Load pick save file.
3. Load pick save file.
4. Update picks wiht new hypocentres.
"""
from Relocation import compute_new_hypocentre_iloc
compute_new_hypocentre_iloc(events_split, output_path, rank,
config)
from Relocation import compute_new_hypocentre_iloc, \
update_hypocentres_from_database
compute_new_hypocentre_iloc(events_split, output_path, config,
rank)
comm.barrier()

if rank == 0:
from Relocation import extract_hypocentres_from_database
events = list(np.unique(picks['event_id']))
picks, unstable_events = \
extract_hypocentres_from_database(events, picks, config,
unstable_events=\
unstable_events)
picks_split = list(np.array_split(picks, nproc))
for i in range(nproc):
np.save(os.path.join(output_path,
'%s.npy'%str(i).zfill(3)),
picks_split[i])
#end for
hypo_dict = extract_hypocentres_from_database(events)
#end if

unstable_events = comm.bcast(unstable_events, root=0)
hypo_dict = comm.bcast(hypo_dict, root=0)
picks_split = np.load(os.path.join(output_path,
'%s.npy'%str(rank).zfill(3)))

picks_split, unstable_events = \
update_hypocentres_from_database(events, picks_split,
hypo_dict, config,
unstable_events=\
unstable_events)
else:
"""
If relocation algorithm is not iloc:
Expand Down

0 comments on commit 55a05f9

Please sign in to comment.