def test_explosion_source(self): target = gf.Target(interpolation='nearest_neighbor') ex = gf.ExplosionSource(magnitude=5., volume_change=4., depth=5 * km) with self.assertRaises(gf.DerivedMagnitudeError): ex.validate() ex = gf.ExplosionSource(depth=5 * km) ex.validate() self.assertEqual(ex.get_moment(), 1.0) ex = gf.ExplosionSource(magnitude=3.0, depth=5 * km) store = self.dummy_store() with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change() volume_change = ex.get_volume_change(store, target) with self.assertRaises(TypeError): ex.get_volume_change(store, gf.Target(interpolation='nearest_neighbour')) ex = gf.ExplosionSource(volume_change=volume_change, depth=5 * km) self.assertAlmostEqual(ex.get_magnitude(store, target), 3.0) ex = gf.ExplosionSource(magnitude=3.0, depth=-5.) with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change(store, target)
def test_explosion_source(self): ex = gf.ExplosionSource(magnitude=5., volume_change=4., depth=5 * km) with self.assertRaises(gf.DerivedMagnitudeError): ex.validate() ex = gf.ExplosionSource(depth=5 * km) ex.validate() self.assertEqual(ex.get_moment(), 1.0) ex = gf.ExplosionSource(magnitude=3.0, depth=5 * km) store = self.dummy_store() with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change() volume_change = ex.get_volume_change(store) ex = gf.ExplosionSource(volume_change=volume_change, depth=5 * km) self.assertAlmostEqual(ex.get_magnitude(store), 3.0) ex = gf.ExplosionSource(magnitude=3.0, depth=-5.) with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change(store)
def test_explosion_source(self): target = gf.Target(interpolation='nearest_neighbor') ex = gf.ExplosionSource(magnitude=5., volume_change=4., depth=5 * km) with self.assertRaises(gf.DerivedMagnitudeError): ex.validate() ex = gf.ExplosionSource(depth=5 * km) ex.validate() self.assertEqual(ex.get_moment(), 1.0) # magnitude input magnitude = 3. ex = gf.ExplosionSource(magnitude=magnitude, depth=5 * km) store = self.dummy_store() with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change() volume_change = ex.get_volume_change(store, target) self.assertAlmostEqual(ex.get_magnitude(store, target), magnitude) # validate with MT source moment = ex.get_moment(store, target) * float(num.sqrt(2. / 3)) mt = gf.MTSource(mnn=moment, mee=moment, mdd=moment) self.assertAlmostEqual(ex.get_magnitude(store, target), mt.get_magnitude(store=store, target=target)) # discretized sources d_ex = ex.discretize_basesource(store=store, target=target) d_mt = mt.discretize_basesource(store=store, target=target) d_ex_m6s = d_ex.get_source_terms('elastic10') d_mt_m6s = d_mt.get_source_terms('elastic10') numeq(d_ex_m6s, d_mt_m6s, 1e-20) # interpolation method with self.assertRaises(TypeError): ex.get_volume_change(store, gf.Target(interpolation='nearest_neighbour')) # volume change input ex = gf.ExplosionSource(volume_change=volume_change, depth=5 * km) self.assertAlmostEqual(ex.get_magnitude(store, target), 3.0) ex = gf.ExplosionSource(magnitude=3.0, depth=-5.) with self.assertRaises(gf.DerivedMagnitudeError): ex.get_volume_change(store, target)
def test_stf_pre_post(self): store_dir = self.get_pulse_store_dir() engine = gf.LocalEngine(store_dirs=[store_dir]) store = engine.get_store('pulse') for duration in [0., 0.05, 0.1]: trs = [] for mode in ['pre', 'post']: source = gf.ExplosionSource( time=store.config.deltat * 0.5, depth=200., moment=1.0, stf=gf.BoxcarSTF(duration=duration), stf_mode=mode) target = gf.Target(codes=('', 'STA', '', 'Z'), north_shift=500., east_shift=0., store_id='pulse') xtrs = engine.process(source, target).pyrocko_traces() for tr in xtrs: tr.set_codes(location='%3.1f_%s' % (duration, mode)) trs.append(tr) tmin = max(tr.tmin for tr in trs) tmax = min(tr.tmax for tr in trs) for tr in trs: tr.chop(tmin, tmax) amax = max(num.max(num.abs(tr.ydata)) for tr in trs) perc = num.max(num.abs(trs[0].ydata - trs[1].ydata) / amax) * 100. if perc > 0.1: logger.warn('test_stf_pre_post: max difference of %.1f %%' % perc)
def test_target_source_timing(self): store_dir = self.get_pulse_store_dir() engine = gf.LocalEngine(store_dirs=[store_dir]) for stime in [0., -160000., time.time()]: source = gf.ExplosionSource( depth=200., magnitude=4., time=stime) targets = [ gf.Target( codes=('', 'STA', '', component), north_shift=500., tmin=source.time-300., tmax=source.time+300., east_shift=500.) for component in 'ZNE' ] response = engine.process(source, targets) synthetic_traces = response.pyrocko_traces() data = num.zeros(num.shape(synthetic_traces[0].ydata)) for tr in synthetic_traces: data += tr.ydata sum_data = num.sum(abs(tr.ydata)) assert sum_data > 1.0
def test_pulse(self): store_dir = self.get_pulse_store_dir() engine = gf.LocalEngine(store_dirs=[store_dir]) sources = [ gf.ExplosionSource( time=0.0, depth=depth, moment=moment) for moment in (1.0, 2.0, 3.0) for depth in [100., 200., 300.] ] targets = [ gf.Target( codes=('', 'STA', '', component), north_shift=500., east_shift=0.) for component in 'ZNE' ] pulse = engine.get_store_extra(None, 'pulse') store = engine.get_store('pulse') response = engine.process(sources=sources, targets=targets) for source, target, tr in response.iter_results(): t = tr.get_xdata() dist = math.sqrt((source.depth - target.depth)**2 + source.distance_to(target)**2) data = pulse.evaluate(dist, t-source.time) phi = math.atan2((source.depth - target.depth), source.distance_to(target)) * r2d azi, bazi = source.azibazi_to(target) data *= source.get_moment(store) * math.sqrt(2./3.) if tr.channel.endswith('N'): data *= math.cos(phi*d2r) * math.cos(azi*d2r) elif tr.channel.endswith('E'): data *= math.cos(phi*d2r) * math.sin(azi*d2r) elif tr.channel.endswith('Z'): data *= math.sin(phi*d2r) tr2 = tr.copy(data=False) tr2.set_ydata(data) tr2.set_codes(location='X') num.testing.assert_almost_equal(data, tr.ydata, 2)
def test_process_timeseries(self): engine = gf.LocalEngine(use_config=True) sources = [ gf.ExplosionSource( time=0.0, depth=depth, moment=moment) for moment in [2., 4., 8.] for depth in [3000., 6000., 12000.] ] targets = [ gf.Target( codes=('', 'ST%d' % i, '', component), north_shift=shift*km, east_shift=0., tmin=tmin, store_id='global_2s', tmax=None if tmin is None else tmin+40.) for component in 'ZNE' for i, shift in enumerate([100]) for tmin in [None, 5., 20.] ] response_sum = engine.process(sources=sources, targets=targets, calc_timeseries=False, nthreads=1) response_calc = engine.process(sources=sources, targets=targets, calc_timeseries=True, nthreads=1) for (source, target, tr), (source_n, target_n, tr_n) in zip( response_sum.iter_results(), response_calc.iter_results()): assert source is source_n assert target is target_n t1 = tr.get_xdata() t2 = tr_n.get_xdata() num.testing.assert_equal(t1, t2) disp1 = tr.get_ydata() disp2 = tr_n.get_ydata() num.testing.assert_equal(disp1, disp2)
def test_pulse_decimate(self): store_dir = self.get_pulse_store_dir() store = gf.Store(store_dir) store.make_decimated(2) engine = gf.LocalEngine(store_dirs=[store_dir]) # pulse = engine.get_store_extra(None, 'pulse') source = gf.ExplosionSource( time=0.0, depth=100., moment=1.0) targets = [ gf.Target( codes=('', 'STA', '%s' % sample_rate, component), sample_rate=sample_rate, north_shift=500., east_shift=0.) for component in 'N' for sample_rate in [None, store.config.sample_rate / 2.0] ] response = engine.process(source, targets) trs = [] for source, target, tr in response.iter_results(): tr.extend(0., 1.) if target.sample_rate is None: tr.downsample_to(2./store.config.sample_rate, snap=True) trs.append(tr) tmin = max(tr.tmin for tr in trs) tmax = min(tr.tmax for tr in trs) for tr in trs: tr.chop(tmin, tmax) num.testing.assert_almost_equal( trs[0].ydata, trs[1].ydata, 2)
def test_pulse(self): store_dir = self.get_pulse_store_dir() engine = gf.LocalEngine(store_dirs=[store_dir]) sources = [ gf.ExplosionSource( time=0.0, depth=depth, moment=moment) for moment in (1.0, 2.0, 3.0) for depth in [100., 200., 300.] ] targets = [ gf.Target( quantity=quantity, codes=('', 'STA', quantity, component), north_shift=500., east_shift=0.) for component in 'ZNE' for quantity in ['displacement', 'velocity', 'acceleration'] ] pulse = engine.get_store_extra(None, 'pulse') store = engine.get_store('pulse') response = engine.process(sources=sources, targets=targets) for source, target, tr in response.iter_results(): t = tr.get_xdata() dist = math.sqrt((source.depth - target.depth)**2 + source.distance_to(target)**2) data = pulse.evaluate(dist, t-source.time) phi = math.atan2((source.depth - target.depth), source.distance_to(target)) * r2d azi, bazi = source.azibazi_to(target) data *= source.get_moment(store) * math.sqrt(2./3.) if tr.channel.endswith('N'): data *= math.cos(phi*d2r) * math.cos(azi*d2r) elif tr.channel.endswith('E'): data *= math.cos(phi*d2r) * math.sin(azi*d2r) elif tr.channel.endswith('Z'): data *= math.sin(phi*d2r) tr2 = tr.copy(data=False) tr2.set_ydata(data) tr2.set_codes(location='X') if target.quantity == 'velocity': tr2 = tr2.transfer( transfer_function=trace.DifferentiationResponse(), demean=False) elif target.quantity == 'acceleration': tr2 = tr2.transfer( transfer_function=trace.DifferentiationResponse(2), demean=False) # trace.snuffle([tr, tr2]) amax = num.max(num.abs(tr.ydata)) if amax > 1e-10: # print(num.max(num.abs(tr2.ydata - tr.ydata) / amax)) assert num.all(num.abs(tr2.ydata - tr.ydata) < 0.05 * amax)
def test_calc_timeseries(self): from pyrocko.gf import store_ext benchmark.show_factor = True engine = gf.LocalEngine(use_config=True) def test_timeseries( store, source, targets, interpolation, nthreads, random_itmin=False, random_nsamples=False): ntargets = len(targets) dsource = source.discretize_basesource(store, targets[0]) source_coords_arr = dsource.coords5() scheme = store.config.component_scheme source_terms = dsource.get_source_terms(scheme) receiver_coords_arr = num.empty((len(targets), 5)) for itarget, target in enumerate(targets): receiver = target.receiver(store) receiver_coords_arr[itarget, :] = \ [receiver.lat, receiver.lon, receiver.north_shift, receiver.east_shift, receiver.depth] delays = dsource.times itmin = num.zeros(ntargets, dtype=num.int32) nsamples = num.full(ntargets, -1, dtype=num.int32) if random_itmin: itmin = num.random.randint(-20, 5, size=ntargets, dtype=num.int32) if random_nsamples: nsamples = num.random.randint(10, 100, size=ntargets, dtype=num.int32) @benchmark.labeled('calc_timeseries-%s' % interpolation) def calc_timeseries(): return store_ext.store_calc_timeseries( store.cstore, source_coords_arr, source_terms, delays, receiver_coords_arr, scheme, interpolation, itmin, nsamples, nthreads) @benchmark.labeled('sum_timeseries-%s' % interpolation) def sum_timeseries(): results = [] for itarget, target in enumerate(targets): params = store_ext.make_sum_params( store.cstore, source_coords_arr, source_terms, target.coords5[num.newaxis, :].copy(), scheme, interpolation, nthreads) for weights, irecords in params: neach = irecords.size // dsource.times.size delays2 = num.repeat(dsource.times, neach) r = store_ext.store_sum( store.cstore, irecords, delays2, weights, int(itmin[itarget]), int(nsamples[itarget])) results.append(r) return results res_calc = calc_timeseries() res_sum = sum_timeseries() for c, s in zip(res_calc, res_sum): num.testing.assert_equal(c[0], s[0], verbose=True) for cc, cs in zip(c[1:-1], s[1:]): assert cc == cs source_store_targets = [ ( gf.ExplosionSource(time=0.0, depth=100., moment=1.0), gf.Store(self.get_pulse_store_dir()), [ gf.Target( codes=('', 'STA', '', component), north_shift=500., east_shift=500., tmin=-300., tmax=+300.) for component in 'ZNE' ] ), ( gf.RectangularSource( lat=0., lon=0., depth=5*km, length=1*km, width=1*km, anchor='top'), engine.get_store('global_2s'), [ gf.Target( codes=('', 'STA%02i' % istation, '', component), lat=lat, lon=lon, store_id='global_2s') for component in 'ZNE' for istation, (lat, lon) in enumerate( num.random.random((1, 2))) ] ) ] for source, store, targets in source_store_targets: store.open() for interp in ['multilinear', 'nearest_neighbor']: for random_itmin in [True, False]: for random_nsamples in [True, False]: test_timeseries( store, source, targets, interpolation=interp, nthreads=1, random_itmin=random_itmin, random_nsamples=random_nsamples) print(benchmark) benchmark.clear()
def createMT_Isotropic(mag): return gf.ExplosionSource().pyrocko_moment_tensor()