def moment_to_magnitude_hack( conf, data, xy ): icomp = {'x':0,'y':1}[xy] if conf[xy+'label'].lower() == 'moment': for i,dset in enumerate(data): data[i] = list(dset) data[i][icomp] = moment_to_magnitude(num.asarray(dset[icomp])) conf[xy+'label'] = 'M@-W@-' if xy+'unit' in conf: del conf[xy+'unit'] if xy+'inc' in conf: del conf[xy+'inc'] if xy+'limits' in conf: conf[xy+'limits'] = [ moment_to_magnitude(x) for x in conf[xy+'limits'] ]
def test_vs_mtpar(self): try: import mtpar except (ImportError): logger.warning('This test needs mtpar to be installed: ' 'https://github.com/rmodrak/mtpar/') import sys sys.exit() reference = { 'magnitude': 4.8, 'mnn': 0.84551376, 'mee': -0.75868967, 'mdd': -0.08682409, 'mne': 0.51322155, 'mnd': 0.14554675, 'med': -0.25767963, 'east_shift': 10., 'north_shift': 20., 'depth': 8.00, 'time': -2.7, 'duration': 5., } m6s = [] for var in ['mnn', 'mee', 'mdd', 'mne', 'mnd', 'med']: m6s.append(reference[var]) m6ned = num.array(m6s) # mT in NED m6use = mtpar.change_basis(m6ned, i1=2, i2=1) # rotate to USE rho, v, w, kappa_deg, sigma_deg, h = mtpar.cmt2tt15(m6use) magnitude = mtm.moment_to_magnitude(rho / num.sqrt(2.)) mtqt = MTQTSource(w=w, v=v, kappa=kappa_deg * num.pi / 180., sigma=sigma_deg * num.pi / 180., h=h, magnitude=magnitude) mt_TT = mtpar.tt152cmt(rho, v, w, kappa_deg, sigma_deg, h) # MT in USE # convert from USE to NED mt_TT_ned = mtpar.change_basis(mt_TT, 1, 2) print('MTQTSource NED: \n', mtqt.m6) print(mtqt) print('TT15, USE: \n', mt_TT) print('Input NED: \n', m6ned) print('MTTT15: \n', mt_TT_ned) assert_allclose(mt_TT_ned, m6ned, atol=1e-3, rtol=0.) assert_allclose(mtqt.m6, m6ned, atol=1e-3, rtol=0.) assert_allclose(mt_TT_ned, mtqt.m6, atol=1e-3, rtol=0.)
def testMomentGetterSetter(self): m1 = MomentTensor() want_mom = 2e7 m1.moment = want_mom sm2 = m1.scalar_moment() assert sm2 == m1.moment assert abs(sm2 - want_mom) < 1e-8 mag = moment_to_magnitude(want_mom) assert m1.magnitude == mag want_mag = 3.5 m1.magnitude = want_mag mom = magnitude_to_moment(want_mag) assert m1.moment == mom
def testMomentGetterSetter(self): m1 = MomentTensor() want_mom = 2E7 m1.moment = want_mom sm2 = m1.scalar_moment() assert (sm2 == m1.moment) assert (abs(sm2 - want_mom) < 1E-8) mag = moment_to_magnitude(want_mom) assert (m1.magnitude == mag) want_mag = 3.5 m1.magnitude = want_mag mom = magnitude_to_moment(want_mag) assert (m1.moment == mom)
def testMomentGetterSetter(self): m1 = MomentTensor() want_mom = 2E7 m1.moment = want_mom sm2 = m1.scalar_moment() assert(sm2 == m1.moment) assert(abs(sm2 - want_mom) < 1E-8) mag = moment_to_magnitude(want_mom) assert(m1.magnitude == mag) want_mag = 3.5 m1.magnitude = want_mag mom = magnitude_to_moment(want_mag) assert(m1.moment == mom)
def mechanism_from_event(self): event = self.get_viewer().get_active_event() if event is None: self.fail('No active event set.') if event.moment_tensor is not None: strike, dip, slip_rake = event.moment_tensor.both_strike_dip_rake()[0] moment = event.moment_tensor.scalar_moment() self.set_parameter('magnitude', moment_tensor.moment_to_magnitude(moment)) self.set_parameter('strike', strike) self.set_parameter('dip', dip) self.set_parameter('rake', slip_rake) else: self.warn('No source mechanism available for event %s. Only setting location' % event.name) self.set_parameter('lat', event.lat) self.set_parameter('lon', event.lon) self.set_parameter('depth_km', event.depth/km)
def test_MTSourceQT(self): # from Tape & Tape 2015 Appendix A: (u, v, kappa, sigma, h) = (3. / 8. * pi, -1. / 9., 4. / 5. * pi, -pi / 2., 3. / 4.) magnitude = mtm.moment_to_magnitude(1. / num.sqrt(2.)) w = 3. / 8. * pi - u mt = MTQTSource(w=w, v=v, kappa=kappa, sigma=sigma, h=h, magnitude=magnitude) assert_allclose(mt.rho, 1.) # check rho reference_colatlon = num.array([1.571, -0.113]) reference_theta = num.array([0.723]) reference_U = num.array([[-0.587, -0.809, 0.037], [0.807, -0.588, -0.051], [0.063, 0., 0.998]]) reference_lambda = num.array([0.749, -0.092, -0.656]) reference_m9_nwu = num.array([[0.196, -0.397, -0.052], [-0.397, 0.455, 0.071], [-0.052, 0.071, -0.651]]) reference_m9_ned = num.array([[0.196, 0.397, 0.052], [0.397, 0.455, 0.071], [0.052, 0.071, -0.651]]) assert_allclose(num.array([mt.beta, mt.gamma]), reference_colatlon, atol=1e-3, rtol=0.) assert_allclose(mt.theta, reference_theta, atol=1e-3, rtol=0.) assert_allclose(mt.rot_U, reference_U, atol=1e-3, rtol=0.) assert_allclose(mt.lune_lambda, reference_lambda, atol=1e-3, rtol=0.) assert_allclose(mt.m9_nwu, reference_m9_nwu, atol=1e-3, rtol=0.) assert_allclose(mt.m9, reference_m9_ned, atol=1e-3, rtol=0.) print('M9 NED', mt.m9) print('M9 NWU', mt.m9_nwu)
def testMagnitudeMoment(self): for i in range(1, 10): mag = float(i) assert ( abs(mag - moment_to_magnitude(magnitude_to_moment(mag))) < 1e-6 ), "Magnitude to moment to magnitude test failed."
def call(self): '''Main work routine of the snuffling.''' self.cleanup() olat = 0. olon = 0. f = (0., 0., 0.) deltat = 1./self.fsampling if self.stf == 'Gauss': stf = Gauss(self.tau) elif self.stf == 'Impulse': stf = Impulse() viewer = self.get_viewer() event = viewer.get_active_event() if event: event, stations = self.get_active_event_and_stations(missing='warn') else: event = model.Event(lat=olat, lon=olon) stations = [] if not stations: s = model.Station(lat=olat, lon=olon, station='AFG') stations = [s] viewer.add_stations(stations) source = gf.DCSource( time=event.time+self.time, lat=event.lat, lon=event.lon, north_shift=self.north_km*km, east_shift=self.east_km*km, depth=self.depth_km*km, magnitude=moment_tensor.moment_to_magnitude(self.moment), strike=self.strike, dip=self.dip, rake=self.rake) source.regularize() m = EventMarker(source.pyrocko_event()) self.add_marker(m) targets = [] mt = moment_tensor.MomentTensor( strike=source.strike, dip=source.dip, rake=source.rake, moment=self.moment) traces = [] for station in stations: xyz = (self.north_km*km, self.east_km*km, self.depth_km*km) r = num.sqrt(xyz[0]**2 + xyz[1]**2 + xyz[2]**2) ns = math.ceil(r/self.vs/1.6)*2 outx = num.zeros(int(ns)) outy = num.zeros(int(ns)) outz = num.zeros(int(ns)) nsl = station.nsl() quantity = self.quantity.split()[0].lower() add_seismogram( self.vp*km, self.vs*km, self.density, self.qp, self.qs, xyz, f, mt.m6(), quantity, deltat, 0., outx, outy, outz, stf=stf, want_near=self.want_near, want_intermediate=self.want_intermediate, want_far=self.want_far) for channel, out in zip('NEZ', [outx, outy, outz]): tr = trace.Trace('', station.station, '', channel, deltat=deltat, tmin=source.time, ydata=out) traces.append(tr) self.add_traces(traces)
def moment(self, value): self.magnitude = mt.moment_to_magnitude(value)
def __init__(self, **kwargs): if 'moment' in kwargs: kwargs['magnitude'] = mt.moment_to_magnitude(kwargs.pop('moment')) Source.__init__(self, **kwargs)
def testMagnitudeMoment(self): for i in range(1, 10): mag = float(i) assert abs(mag - moment_to_magnitude(magnitude_to_moment(mag))) \ < 1e-6, 'Magnitude to moment to magnitude test failed.'
def gen_induced_event(scenario_id, magmin=1., magmax=3., depmin=3.5, depmax=14, latmin=48.9586, latmax=49.2, lonmin=8.0578, lonmax=8.1578, radius_min=0.1, radius_max=20.2, stress_drop_min=4.e06, stress_drop_max=17e6, velocity=3000., timemin=util.str_to_time('2007-01-01 16:10:00.000'), timemax=util.str_to_time('2020-01-01 16:10:00.000'), well="insheim", simple_induced=True): name = "scenario" + str(scenario_id) depth = rand(depmin, depmax) * km # source time function (STF) based on Brune source model, to get # spectra roughly realistic radius = randlat(radius_min, radius_max) stress_drop = rand(stress_drop_min, stress_drop_max) magnitude = float( pmt.moment_to_magnitude(16. / 7. * stress_drop * radius**3)) rupture_velocity = 0.9 * velocity duration = 1.5 * radius / rupture_velocity choice_in_ex = num.random.choice(3, 1) if well is "insheim": if choice_in_ex == 1: # injection latmin = 49.149488448967226 - 0.01 latmax = 49.15474362306716 + 0.01 lonmin = 8.154522608466785 - 0.01 lonmax = 8.160353517676578 + 0.01 elif choice_in_ex == 0: # extraction latmin = 49.15452255594506 - 0.01 latmax = 49.16078760284185 + 0.01 lonmin = 8.154355077151537 - 0.01 lonmax = 8.160920669126998 + 0.01 else: latmin = 49.12452255594506 latmax = 49.17078760284185 lonmin = 8.110355077151537 lonmax = 8.190920669126998 depth_min = 1500. depth_max = 7000. else: if choice_in_ex == 1: # injection latmin = 49.187282830892414 - 0.01 latmax = 49.1876069158743 + 0.01 lonmin = 8.110819065944598 - 0.01 lonmax = 8.12401755104507 + 0.01 elif choice_in_ex == 0: # extraction latmin = 49.18755938553847 - 0.01 latmax = 49.187687407341066 + 0.01 lonmin = 8.123798045195171 - 0.01 lonmax = 8.130836557156785 + 0.01 else: latmin = 49.17752255594506 latmax = 49.21278760284185 lonmin = 8.060355077151537 lonmax = 8.150920669126998 depth_min = 1500. depth_max = 7000. lat, lon, depth_ell = get_random_point_in_ellipse(latmin, latmax, lonmin, lonmax, depth_min, depth_max) time = rand(timemin, timemax) depth = rand(depth_min, depth_max) if choice_in_ex > 1: lat = rand(latmin, latmax) lon = rand(lonmin, lonmax) if simple_induced is True: event = model.Event(name=name, lat=lat, lon=lon, magnitude=magnitude, depth=depth, time=time, duration=duration, tags=["stress:" + str(stress_drop)]) return event