Skip to content

Commit

Permalink
Merge pull request #224 from rmodrak/master
Browse files Browse the repository at this point in the history
Group velocity window selection
  • Loading branch information
rmodrak authored Aug 29, 2023
2 parents ce9aab7 + 74d4f75 commit 69a68a0
Show file tree
Hide file tree
Showing 21 changed files with 422 additions and 226 deletions.
20 changes: 11 additions & 9 deletions examples/GridSearch.DoubleCouple+Magnitude+Depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,26 @@

results = results_bw + results_sw

#
# Collect information about best-fitting source
#

origin_idx = results.origin_idxmin()
best_origin = origins[origin_idx]

source_idx = results.source_idxmin()
best_mt = grid.get(source_idx)

# dictionary of lune parameters
lune_dict = grid.get_dict(source_idx)

# dictionary of Mij parameters
mt_dict = best_mt.as_dict()

merged_dict = merge_dicts(
mt_dict, lune_dict, {'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()}, best_origin)


#
# Generate figures and save results
Expand All @@ -236,15 +247,6 @@

print('Saving results...\n')

# collect information about best-fitting source
merged_dict = merge_dicts(
mt_dict,
lune_dict,
{'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()},
best_origin,
)

# save best-fitting source
save_json(event_id+'DC+Z_solution.json', merged_dict)

Expand Down
20 changes: 11 additions & 9 deletions examples/GridSearch.DoubleCouple+Magnitude+Hypocenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,26 @@

results = results_bw + results_sw

#
# Collect information about best-fitting source
#

origin_idx = results.origin_idxmin()
best_origin = origins[origin_idx]

source_idx = results.source_idxmin()
best_mt = grid.get(source_idx)

# dictionary of lune parameters
lune_dict = grid.get_dict(source_idx)

# dictionary of Mij parameters
mt_dict = best_mt.as_dict()

merged_dict = merge_dicts(
mt_dict, lune_dict, {'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()}, best_origin)


#
# Generate figures and save results
Expand All @@ -255,15 +266,6 @@

print('Saving results...\n')

# collect information about best-fitting source
merged_dict = merge_dicts(
mt_dict,
lune_dict,
{'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()},
best_origin,
)

# save best-fitting source
save_json(event_id+'DC+XY_solution.json', merged_dict)

Expand Down
24 changes: 14 additions & 10 deletions examples/GridSearch.DoubleCouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,26 @@

results = results_bw + results_sw

# `grid` index corresponding to minimum misfit
#
# Collect information about best-fitting source
#

# index of best-fitting moment tensor
idx = results.source_idxmin()

# MomentTensor object
best_mt = grid.get(idx)

# dictionary of lune parameters
lune_dict = grid.get_dict(idx)

# dictionary of Mij parameters
mt_dict = best_mt.as_dict()

merged_dict = merge_dicts(
mt_dict, lune_dict, {'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()}, origin)


#
# Generate figures and save results
Expand All @@ -220,15 +233,6 @@

print('Saving results...\n')

# collect information about best-fitting source
merged_dict = merge_dicts(
mt_dict,
lune_dict,
{'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()},
origin,
)

# save best-fitting source
save_json(event_id+'DC_solution.json', merged_dict)

Expand Down
24 changes: 14 additions & 10 deletions examples/GridSearch.FullMomentTensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,26 @@

results = results_bw + results_sw

# `grid` index corresponding to minimum misfit
#
# Collect information about best-fitting source
#

# index of best-fitting moment tensor
idx = results.source_idxmin()

# MomentTensor object
best_mt = grid.get(idx)

# dictionary of lune parameters
lune_dict = grid.get_dict(idx)

# dictionary of Mij parameters
mt_dict = best_mt.as_dict()

merged_dict = merge_dicts(
mt_dict, lune_dict, {'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()}, origin)


#
# Generate figures and save results
Expand All @@ -212,15 +225,6 @@

print('Saving results...\n')

# collect information about best-fitting source
merged_dict = merge_dicts(
mt_dict,
lune_dict,
{'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()},
origin,
)

# save best-fitting source
save_json(event_id+'FMT_solution.json', merged_dict)

Expand Down
23 changes: 13 additions & 10 deletions examples/SerialGridSearch.DoubleCouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,25 @@

results = results_bw + results_sw

# `grid` index corresponding to minimum misfit
#
# Collect information about best-fitting source
#

# index of best-fitting moment tensor
idx = results.source_idxmin()

# MomentTensor object
best_mt = grid.get(idx)

# dictionary of lune parameters
lune_dict = grid.get_dict(idx)

# dictionary of Mij parameters
mt_dict = best_mt.as_dict()

merged_dict = merge_dicts(
mt_dict, lune_dict, {'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()}, origin)

#
# Generate figures and save results
Expand All @@ -193,15 +205,6 @@

print('Saving results...\n')

# collect information about best-fitting source
merged_dict = merge_dicts(
mt_dict,
lune_dict,
{'M0': best_mt.moment()},
{'Mw': best_mt.magnitude()},
origin,
)

# save best-fitting source
save_json(event_id+'DC_solution.json', merged_dict)

Expand Down
11 changes: 11 additions & 0 deletions mtuq/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ def __init__(self, array, convention='USE'):
# asarray(array), convention, 'USE')


def as_dict(self):
""" Returns dictionary in `up-south-east` convention
"""
array = self._array
return {
'Fr': array[0],
'Ft': array[1],
'Fp': array[2],
}


def as_vector(self):
""" Returns 1D NumPy array in `up-south-east` convention
"""
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 14 additions & 9 deletions mtuq/graphics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

import matplotlib
from os.path import dirname, join


from mtuq.graphics.attrs import\
plot_time_shifts, plot_amplitude_ratios, plot_log_amplitude_ratios,\
Expand Down Expand Up @@ -39,12 +42,14 @@
likelihood_analysis


# use Helvetica if available
for fontname in ['Helvetica', 'Arial']:
try:
import matplotlib
matplotlib.rcParams['font.sans-serif'] = fontname
matplotlib.rcParams['font.family'] = "sans-serif"
break
except:
continue
# use Nimbus Sans L as default font
try:
import matplotlib.font_manager as font_manager
path = join(dirname(__file__), '__fonts')
for font in font_manager.findSystemFonts(path):
font_manager.fontManager.addfont(font)
matplotlib.rcParams['font.family'] = "Nimbus Sans L"

except:
pass

92 changes: 92 additions & 0 deletions mtuq/graphics/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

import numpy as np

from matplotlib import pyplot
from obspy.geodetics import gps2dist_azimuth


def station_label_writer(ax, station, origin, units='km'):
""" Displays station id, distance, and azimuth to the left of current axes
"""
distance_in_m, azimuth, _ = gps2dist_azimuth(
origin.latitude,
origin.longitude,
station.latitude,
station.longitude)

# construct station label
if len(station.location) > 1:
label = station.network + '.' + station.station + '.' + station.location
else:
label = station.network + '.' + station.station

# display station label
pyplot.text(0.2,0.50, label, fontsize=11, transform=ax.transAxes)

# display distance
if units=='m':
label = '%d m' % round(distance_in_m)

elif units=='km':
label = '%d km' % round(distance_in_m/1000.)

elif units=='deg':
label = '%d%s' % (round(m_to_deg(distance_in_m)), u'\N{DEGREE SIGN}')

pyplot.text(0.2,0.35, label, fontsize=11, transform=ax.transAxes)

# display azimuth
azimuth = '%d%s' % (round(azimuth), u'\N{DEGREE SIGN}')
pyplot.text(0.2,0.20, azimuth, fontsize=11, transform=ax.transAxes)


def trace_label_writer(axis, dat, syn, total_misfit=1.):
""" Displays cross-correlation time shift and misfit information below trace
"""
ymin = axis.get_ylim()[0]

s = syn.data
d = dat.data

# display cross-correlation time shift
time_shift = 0.
time_shift += _getattr(syn, 'time_shift', np.nan)
time_shift += _getattr(dat, 'static_time_shift', 0)
axis.text(0.,(1/4.)*ymin, '%.2f' %time_shift, fontsize=11)

# display maximum cross-correlation coefficient
Ns = np.dot(s,s)**0.5
Nd = np.dot(d,d)**0.5
if Ns*Nd > 0.:
max_cc = np.correlate(s, d, 'valid').max()
max_cc /= (Ns*Nd)
axis.text(0.,(2/4.)*ymin, '%.2f' %max_cc, fontsize=11)
else:
max_cc = np.nan
axis.text(0.,(2/4.)*ymin, '%.2f' %max_cc, fontsize=11)

# display percent of total misfit
misfit = _getattr(syn, 'misfit', np.nan)
misfit /= total_misfit
if misfit >= 0.1:
axis.text(0.,(3/4.)*ymin, '%.1f' %(100.*misfit), fontsize=11)
else:
axis.text(0.,(3/4.)*ymin, '%.2f' %(100.*misfit), fontsize=11)


#
# utility functions
#

def _getattr(trace, name, *args):
if len(args)==1:
if not hasattr(trace, 'attrs'):
return args[0]
else:
return getattr(trace.attrs, name, args[0])
elif len(args)==0:
return getattr(trace.attrs, name)
else:
raise TypeError("Wrong number of arguments")


2 changes: 1 addition & 1 deletion mtuq/graphics/beachball.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _meca_pygmt(fig, mt):
region=PYGMT_REGION,
projection=PYGMT_PROJECTION,

G='grey50',
compressionfill='grey50',
no_clip=False,
M=True,
)
Expand Down
Loading

0 comments on commit 69a68a0

Please sign in to comment.