Skip to content

Instantly share code, notes, and snippets.

@calum-chamberlain
Last active January 24, 2018 04:21
Show Gist options
  • Save calum-chamberlain/9f2dc5483903f8587ebaa36f5ff7ca0c to your computer and use it in GitHub Desktop.
Save calum-chamberlain/9f2dc5483903f8587ebaa36f5ff7ca0c to your computer and use it in GitHub Desktop.
Simple function to attach available moment-tensor information to a given catalogue by downloading and integrating John Ristau's mt solutions.
def get_geonet_moment_tensor(catalog):
"""
Get the moment tensor info for a given set of events, and add them to the
events.
Use John Ristau's MT catalog here:
`https://raw.githubusercontent.com/GeoNet/data/master/moment-tensor/GeoNet_CMT_solutions.csv`
which does not include all events, if events are not in the database they
will warn that they are not found.
:param catalog: obspy.core.event.Event to get moment tensor for.
.. Note::
Works in-place on given event.
"""
import urllib.request
import csv
from obspy.core.event import (
FocalMechanism, MomentTensor, Origin, CreationInfo, ResourceIdentifier,
NodalPlane, NodalPlanes, PrincipalAxes, Axis, Magnitude, Tensor)
from obspy import UTCDateTime
# Build local database
response = urllib.request.urlopen(
"https://raw.githubusercontent.com/GeoNet/data/master/moment-tensor/"
"GeoNet_CMT_solutions.csv")
reader = csv.DictReader(response.read().decode('utf-8').splitlines())
moment_tensors = [line for line in reader]
for event in catalog:
mt = [_mt for _mt in moment_tensors
if _mt['PublicID'] == event.resource_id.id.split('/')[-1]]
if len(mt) == 0:
print("Event %s not found in database, skipping" %
event.resource_id.id.split('/')[-1])
continue
mt = mt[0]
fm_origin = Origin(
latitude=float(mt['Latitude']), longitude=float(mt['Longitude']),
depth=float(mt['CD']) * 1000,
time=UTCDateTime.strptime(mt['Date'], "%Y%m%d%H%M%S"),
origin_type="centroid", evaluation_mode="automatic",
creation_info=CreationInfo(agency_id="GNS", author="John Ristau"),
method_id=ResourceIdentifier("GNS_MT_solution"))
local_mag = Magnitude(mag=float(mt['ML']), magnitude_type="ML",
origin_id=fm_origin.resource_id)
moment_mag = Magnitude(mag=float(mt['Mw']), magnitude_type="MW",
origin_id=fm_origin.resource_id)
event.origins.append(fm_origin)
event.magnitudes.append(local_mag)
event.magnitudes.append(moment_mag)
fm = FocalMechanism(
triggering_origin_id=fm_origin.resource_id,
nodal_planes=NodalPlanes(
nodal_plane_1=NodalPlane(strike=float(mt['strike1']),
dip=float(mt['dip1']),
rake=float(mt['rake1'])),
nodal_plane_2=NodalPlane(strike=float(mt['strike2']),
dip=float(mt['dip2']),
rake=float(mt['rake2'])),
preferred_plane=1),
principal_axes=PrincipalAxes(
t_axis=Axis(azimuth=float(mt['Taz']), plunge=float(mt['Tpl']),
length=mt['Tva']),
p_axis=Axis(azimuth=float(mt['Paz']), plunge=float(mt['Ppl']),
length=mt['Pva']),
n_axis=Axis(azimuth=float(mt['Naz']), plunge=float(mt['Npl']),
length=mt['Nva'])),
station_polarity_count=int(mt['NS']),
method_id=ResourceIdentifier("GNS_MT_solution"),
moment_tensor=MomentTensor(
derived_origin_id=fm_origin.resource_id,
moment_magnitude_id=moment_mag.resource_id,
scalar_moment=_dyne_cm_to_nm(float(mt['Mo'])),
variance_reduction=float(mt['VR']),
double_couple=float(mt['DC']) / 100,
tensor=Tensor(
m_rr=_dyne_cm_to_nm(float(mt['Mxx']) * 1e20),
m_tt=_dyne_cm_to_nm(float(mt['Myy']) * 1e20),
m_pp=_dyne_cm_to_nm(float(mt['Mzz']) * 1e20),
m_rt=_dyne_cm_to_nm(float(mt['Mxy']) * 1e20),
m_rp=_dyne_cm_to_nm(float(mt['Mxz']) * 1e20),
m_tp=_dyne_cm_to_nm(float(mt['Myz']) * 1e20)),
creation_info=CreationInfo(agency_id="GNS",
author="John Ristau")))
event.focal_mechanisms.append(fm)
return catalog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment