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 __init__(self): Snuffling.__init__(self) self.stf_types = ['half sin', 'triangular', 'boxcar', 'None'] self.stf_instances = [ gf.HalfSinusoidSTF(), gf.TriangularSTF(), gf.BoxcarSTF(), None ]
def createFocalMechanisms(n, depths, durations, target, engine, noisy=False, noise_factor=0.1): n_0 = int(n / 3) seismograms = [] moment_tensors = [createMT_DC(1) for i in range(n_0)] + \ [createMT_CLVD(1) for i in range(n_0)] + \ [createMT_Isotropic(1) for i in range(n_0)] focal_mechanisms = n_0 * ['DC'] + n_0 * ['CLVD'] + n_0 * ['Isotropic'] for i, mt in enumerate(moment_tensors): source = gf.MTSource(lat=0, lon=0, depth=depths[i], mnn=mt.mnn, mee=mt.mee, mdd=mt.mdd, mne=mt.mne, mnd=mt.mne, med=mt.med, stf=gf.BoxcarSTF(duration=durations[i])) if noisy: seismograms.append( create_noisy_seismogram(source, target, engine, noise_factor)) else: seismograms.append(create_noisy_seismogram(source, target, engine)) #seismograms = np.array(quakes) df = 20 #deltat_inverse = 1/seismogram[0].deltat spectrograms = [create_spectrogram(s, df) for s in seismograms] #spectrograms = np.array(spectrograms) #specshape = spectrograms.shape #spectrograms = spectrograms.reshape(specshape[2],specshape[0],specshape[1]) df = pd.DataFrame({ 'FocalMechanism': focal_mechanisms, 'MomentTensor': moment_tensors, 'Seismogram': seismograms, 'Spectrogram': spectrograms, 'Depth': depths, 'Duration': durations }) return df
def test_stf_boxcar(self, plot=False): from matplotlib import pyplot as plt for duration in [0., 1., 2., 3.]: tref = 10.02 stf = gf.BoxcarSTF(duration=duration, anchor=0.) t, a = stf.discretize_t(deltat=0.1, tref=tref) assert numeq(stf.centroid_time(tref), tref, 1e-5) if plot: plt.plot(t, a) plt.plot(t, a, 'o') if plot: plt.show()
def test_effective_durations(self): deltat = 1e-4 for stf in [ gf.HalfSinusoidSTF(duration=2.0), gf.TriangularSTF(duration=2.0, peak_ratio=0.), gf.TriangularSTF(duration=2.0, peak_ratio=1.), gf.TriangularSTF(duration=2.0, peak_ratio=0.5), gf.BoxcarSTF(duration=2.0) ]: t, a = stf.discretize_t(deltat, 0.0) t0 = stf.centroid_time(0.0) edur = num.sqrt(num.sum((t - t0)**2 * a)) * 2. * num.sqrt(3.) assert abs(edur - stf.effective_duration) < 1e-3
max_duration = 10 durations = np.random.randint(min_duration, max_duration + 1, len(moment_tensors)) # use a boxcar source-time function for i, mt in enumerate(moment_tensors): source = gf.MTSource(lat=0, lon=0, depth=depths[i], mnn=mt.mnn, mee=mt.mee, mdd=mt.mdd, mne=mt.mne, mnd=mt.mne, med=mt.med, stf=gf.BoxcarSTF(duration=durations[i])) quakes_by_mechanism.append(process(source, target)) seismograms = np.array([q.ydata[:1000] for q in quakes_by_mechanism ]) #trim to 1000 samples df = 20 deltat_inverse = 1 / quakes_by_mechanism[0].deltat spectrograms = [ spectrogram(s, fs=deltat_inverse, nperseg=df * 2, scaling='spectrum')[2] for s in seismograms ] spectrograms = np.dstack(spectrograms) specshape = spectrograms.shape
def test_against_kiwi(self): engine = gf.get_engine() store_id = 'chile_70km_crust' try: store = engine.get_store(store_id) except gf.NoSuchStore: logger.warn('GF Store %s not available - skipping test' % store_id) return base_source = gf.RectangularSource( depth=15*km, strike=0., dip=90., rake=0., magnitude=4.5, nucleation_x=-1., length=10*km, width=0*km, stf=gf.BoxcarSTF(duration=1.0)) base_event = base_source.pyrocko_event() channels = 'NEZ' nstations = 10 stations = [] targets = [] for istation in xrange(nstations): dist = rand(40.*km, 900*km) azi = rand(-180., 180.) north_shift = dist * math.cos(azi*d2r) east_shift = dist * math.sin(azi*d2r) lat, lon = od.ne_to_latlon(0., 0., north_shift, east_shift) sta = 'S%02i' % istation station = model.Station( '', sta, '', lat=lat, lon=lon) station.set_channels_by_name('N', 'E', 'Z') stations.append(station) for cha in channels: target = gf.Target( codes=station.nsl() + (cha,), lat=lat, lon=lon, quantity='displacement', interpolation='multilinear', optimization='enable', store_id=store_id) targets.append(target) from tunguska import glue nsources = 10 # nprocs_max = multiprocessing.cpu_count() nprocs = 1 try: seis = glue.start_seismosizer( gfdb_path=op.join(store.store_dir, 'db'), event=base_event, stations=stations, hosts=['localhost']*nprocs, balance_method='123321', effective_dt=0.5, verbose=False) ksource = to_kiwi_source(base_source) seis.set_source(ksource) recs = seis.get_receivers_snapshot(('syn',), (), 'plain') trs = [] for rec in recs: for tr in rec.get_traces(): tr.set_codes(channel=transchan[tr.channel]) trs.append(tr) trs2 = engine.process(base_source, targets).pyrocko_traces() trace.snuffle(trs + trs2) seis.set_synthetic_reference() for sourcetype in ['point', 'rect']: sources = [] for isource in xrange(nsources): m = pmt.MomentTensor.random_dc() strike, dip, rake = map(float, m.both_strike_dip_rake()[0]) if sourcetype == 'point': source = gf.RectangularSource( north_shift=rand(-20.*km, 20*km), east_shift=rand(-20.*km, 20*km), depth=rand(10*km, 20*km), nucleation_x=0.0, nucleation_y=0.0, strike=strike, dip=dip, rake=rake, magnitude=rand(4.0, 5.0), stf=gf.BoxcarSTF(duration=1.0)) elif sourcetype == 'rect': source = gf.RectangularSource( north_shift=rand(-20.*km, 20*km), east_shift=rand(-20.*km, 20*km), depth=rand(10*km, 20*km), length=10*km, width=5*km, nucleation_x=-1., nucleation_y=0, strike=strike, dip=dip, rake=rake, magnitude=rand(4.0, 5.0), stf=gf.BoxcarSTF(duration=1.0)) else: assert False sources.append(source) for temperature in ['cold', 'hot']: t0 = time.time() resp = engine.process(sources, targets, nprocs=nprocs) t1 = time.time() if temperature == 'hot': dur_pyrocko = t1 - t0 del resp ksources = map(to_kiwi_source, sources) for temperature in ['cold', 'hot']: t0 = time.time() seis.make_misfits_for_sources( ksources, show_progress=False) t1 = time.time() if temperature == 'hot': dur_kiwi = t1 - t0 print 'pyrocko %-5s %5.2fs %5.1fx' % ( sourcetype, dur_pyrocko, 1.0) print 'kiwi %-5s %5.2fs %5.1fx' % ( sourcetype, dur_kiwi, dur_pyrocko/dur_kiwi) finally: seis.close() del seis
save_dir = 'Experiment2' if not os.path.exists(save_dir): os.makedirs(save_dir) velocity_model = 'crust2_m5_hardtop_16Hz' # create random moment tensors print('Creating random moment tensors...') moment_tensors = [createMT_DC(1) for i in range(n_seismograms)] source_mechanisms = n_seismograms*['DC'] #initialize source time functions durations = np.random.randint(1,11,n_eachstf) stfs = [LognormalSTF(shape=2, duration=dur) for dur in durations] + \ [gf.BoxcarSTF(duration=dur) for dur in durations] + \ [gf.TriangularSTF(duration=dur) for dur in durations] stftypes = n_eachstf*['lognorm'] + n_eachstf*['boxcar'] +n_eachstf*['triangle'] # uniform depth of 20 km / 20,000m depths = n_seismograms*[20000] # seismometer target and the actual computation engine target = gf.Target( quantity='displacement', lat=0, lon=long, store_id=velocity_model, codes=('NET', 'STA', 'LOC', 'E'), tmin=tmin, tmax=tmax) engine = LocalEngine(store_dirs=[velocity_model])