def test_source_depth_error_handling(all_remote_dbs): """ Test the seismogram extraction from local and remote databases. """ db = all_remote_dbs # Skip forward databases. if "100s_db_fwd" in db._client.filepath: return # Mock responses to get the tornado testing to work. _add_callback(db._client) # 900 km is deeper than any test database. src = instaseis.Source( latitude=4.0, longitude=3.0, depth_in_m=900000, m_rr=4.71e17, m_tt=3.81e17, m_pp=-4.74e17, m_rt=3.99e17, m_rp=-8.05e17, m_tp=-1.23e17, ) rec = instaseis.Receiver(latitude=10.0, longitude=20.0, depth_in_m=0) with pytest.raises(ValueError) as err: db.get_seismograms(source=src, receiver=rec) assert err.value.args[0] == ( "Source too deep. Source would be located at a radius of 5471000.0 " "meters. The database supports source radii from 6000000.0 to " "6371000.0 meters.") # Too shallow. src = instaseis.Source( latitude=4.0, longitude=3.0, depth_in_m=-10000, m_rr=4.71e17, m_tt=3.81e17, m_pp=-4.74e17, m_rt=3.99e17, m_rp=-8.05e17, m_tp=-1.23e17, ) rec = instaseis.Receiver(latitude=10.0, longitude=20.0, depth_in_m=0) with pytest.raises(ValueError) as err: db.get_seismograms(source=src, receiver=rec) assert err.value.args[0] == ( "Source is too shallow. Source would be located at a radius of " "6381000.0 meters. The database supports source radii from " "6000000.0 to 6371000.0 meters.")
def get_receiver(self): receiver = instaseis.Receiver(latitude=self.prior['la_r'], longitude=self.prior['lo_r'], network=self.prior['network'], station=self.prior['station'], location=self.prior['location'], depth_in_m=self.prior['rec_depth']) return receiver
def prepare(self): # initialize parameters self.Fs = self.config['wavefield_sampling_rate'] self.npts = int(self.config['wavefield_duration'] * self.Fs) self.ntraces = self.sourcegrid.shape[-1] self.data_quantity = self.config['synt_data'] if self.config['wavefield_domain'] == 'fourier': self.fdomain = True if self.npts % 2 == 1: self.npad = 2 * self.npts - 2 else: self.npad = 2 * self.npts elif self.config['wavefield_domain'] == 'time': self.fdomain = False self.npad = next_fast_len(2 * self.npts - 1) else: raise ValueError('Unknown domain {}.'.format( self.config['wavefield_domain'])) self.freq = np.fft.rfftfreq(self.npad, d=1.0 / self.Fs) # Apply a filter if self.config['wavefield_filter'] is not None: freq_nyq = self.Fs / 2.0 # Nyquist if freq_nyq < self.config['wavefield_filter'][1]: warn("Selected upper freq > Nyquist, \ reset to 95\% of Nyquist freq.") freq_minres = 1. / self.config['wavefield_duration'] # lowest resolved freq_max = min(0.999 * freq_nyq, self.config['wavefield_filter'][1]) freq_min = max(freq_minres, self.config['wavefield_filter'][0]) f0 = freq_min / freq_nyq f1 = freq_max / freq_nyq self.filter = butter(4, [f0, f1], 'bandpass') else: self.filter = None # if using instaseis: Find and open database if self.config['wavefield_type'] == 'instaseis': path_to_db = self.config['wavefield_path'] self.db = instaseis.open_db(path_to_db) if self.db.info['length'] < self.npts / self.Fs: warn("Resetting wavefield duration to axisem database length.") fsrc = instaseis.ForceSource(latitude=0.0, longitude=0.0, f_r=1.0) rec = instaseis.Receiver(latitude=0.0, longitude=0.0) test = self.db.get_seismograms(source=fsrc, receiver=rec, dt=1. / self.Fs) self.npts = test[0].stats.npts
def test_get_grf6(): db = instaseis.open_db('syngine://prem_a_20s') reclat = 10.0 reclon = 10.0 cat = obspy.read_events('./stfinv/data/virginia.xml') # Define Moment tensor tensor = cat[0].focal_mechanisms[0].moment_tensor.tensor # tensor = obspy.core.event.Tensor(m_rr=1e20, m_pp=-2e20, m_tt=0.5e20, # m_rt=5e19, m_rp=-7e19, m_tp=-1e20) # Get a reference trace with normal instaseis rec = instaseis.Receiver(latitude=reclat, longitude=reclon, network='XX', station='YY', location='00') # src = instaseis.Source(latitude=evlat, longitude=evlon, # m_rr=tensor.m_rr, # m_tt=tensor.m_tt, # m_pp=tensor.m_pp, # m_tp=tensor.m_tp, # m_rt=tensor.m_rt, # m_rp=tensor.m_rp, # depth_in_m=evdepth) src = instaseis.Source.parse(cat[0]) st_ref = db.get_seismograms(src, rec, dt=0.1, components='Z') # Get a synthetic st_grf6 = Stream() st_grf6 += get_grf6(db, origin=cat[0].origins[0], rec_lat=reclat, rec_lon=reclon, dt=0.1, stats=st_ref[0].stats, depth_in_m=cat[0].origins[0].depth) st_synth = st_grf6.calc_synthetic_from_grf6(st_ref, tensor=tensor, stf=[1]) # st_synth.plot(outfile='synth.png') # st_ref.plot(outfile='ref.png') npt.assert_allclose(st_ref[0].data, st_synth[0].data, rtol=5e-1, atol=1e-7, err_msg='Synthetic not the same')
def _calc(self): db = instaseis.open_db(self.db_name) nazi = 8 dt = min([self.dt, db.info.dt]) lats = np.linspace(start=-90., stop=90., num=self.ndist) lons = np.linspace(start=-180, stop=180., num=nazi, endpoint=False) src = instaseis.Source(latitude=90.0, longitude=0.0, depth_in_m=self.depth_in_m, m_rr=-1.670000e+28 / 1e7, m_tt=3.820000e+27 / 1e7, m_pp=1.280000e+28 / 1e7, m_rt=-7.840000e+27 / 1e7, m_rp=-3.570000e+28 / 1e7, m_tp=1.550000e+27 / 1e7) npts = _get_npts(db, dt) stack_R = np.zeros(shape=(self.ndist, nazi, npts)) stack_T = np.zeros(shape=(self.ndist, nazi, npts)) stack_Z = np.zeros(shape=(self.ndist, nazi, npts)) for ilat in tqdm(range(0, int(self.ndist))): lat = lats[ilat] for ilon in range(0, nazi): lon = lons[ilon] rec = instaseis.Receiver(latitude=lat, longitude=lon, network="AB", station="%d" % lon) st = db.get_seismograms(src, rec, components='RTZ', kind='velocity', dt=dt, remove_source_shift=True) stack_R[ilat, ilon, :] = st.select(channel='*R')[0].data stack_T[ilat, ilon, :] = st.select(channel='*T')[0].data stack_Z[ilat, ilon, :] = st.select(channel='*Z')[0].data return stack_R, stack_T, stack_Z, st[0].stats, db.info
def select_and_add(st, db_HF, db_LF, M, dist): winlen_hours = 4 tstart_total = float(st[0].stats.starttime) + 3600 tend_total = float(st[0].stats.endtime) - 3600 * (winlen_hours + 1) tstart_win = tstart_total + (tend_total - tstart_total) * np.random.rand(1) tend_win = tstart_win + 3600 * winlen_hours t0 = tstart_win + 600 + (tend_win - 4200 - tstart_win) * np.random.rand(1) st_LF = st.slice(starttime=utct(tstart_win - 1800), endtime=utct(tend_win + 1800)) st_HF = st.slice(starttime=utct(tstart_win - 1800), endtime=utct(tend_win + 1800)) st_HF.filter('highpass', freq=0.9) st_LF.filter('bandpass', freqmin=1./60, freqmax=1.2) st_LF.trim(starttime=utct(tstart_win), endtime=utct(tend_win)) st_HF.trim(starttime=utct(tstart_win), endtime=utct(tend_win)) src = instaseis.Source(latitude=90.0-dist, longitude=0.0, depth_in_m=1e3 + np.random.rand(1) * 2.9e4, m_rr=instaseis.source.magnitude2moment(M)[0] * np.sqrt(2.), origin_time=utct(t0) ) rec = instaseis.Receiver(latitude=90.0, longitude=0.0, network='XB', station='ELYSE', location='58') for db, flims in zip([db_HF, db_LF], [(1./10, 4.), (1./100., 1./10.)]): st_inst = db.get_seismograms(source=src, receiver=rec, kind='velocity', components='Z', dt=0.1) st_inst.trim(endtime=st_HF[0].stats.endtime-2) st_inst[0].stats.channel = 'BZC' #st_inst.filter('lowpass', freq=fmin) st_inst.filter('highpass', freq=flims[0]) st_inst.filter('lowpass', freq=flims[1]) i0 = int((st_inst[0].stats.starttime - st_LF[0].stats.starttime) * 10) for s in (st_LF, st_HF): s[0].data[i0:i0+len(st_inst[0].data)] += st_inst[0].data return st_LF, st_HF, t0
def create_insta_from_invcat(network, event, database): """ This function creates synthetic data using the given network and event information, with the database of instaseis :param network: Desired Network, for which the data is generated :type network: obspy.core.inventory.Network :param event: Event, for wich the data is generated. The event must have stored the moment tensor (e.g. given by glogalcmt.org) :type event: obspy.core.event.Event :param database: Link to the database, e.g. the path on your harddrive :type database: str """ db = instaseis.open_db(database) tofe = event.origins[0].time lat = event.origins[0].latitude lon = event.origins[0].longitude depth = event.origins[0].depth source = instaseis.Source(latitude=lat, longitude=lon, depth_in_m=depth, m_rr=event.MomentTensor.m_rr, m_tt=event.MomentTensor.m_tt, m_pp=event.MomentTensor.m_pp, m_rt=event.MomentTensor.m_rt, m_rp=event.MomentTensor.m_rp, m_tp=event.MomentTensor.m_tp, origin_time=tofe) stream = Stream() tmp = [] for station in network: rec = instaseis.Receiver(latitude=str(station.latitude), longitude=str(station.longitude), network=str(network.code), station=str(station.code)) tmp.append(db.get_seismograms(source=source, receiver=rec)) for x in tmp: stream += x return stream
def compare_dbs(seed, databases): if seed: random.seed(seed) reference = instaseis.open_db(databases[0]) others = [instaseis.open_db(_i) for _i in databases[1:]] max_depth = reference.info.max_radius - reference.info.min_radius while True: receiver = instaseis.Receiver( latitude=random.random() * 180.0 - 90.0, longitude=random.random() * 360.0 - 180.0, network="AB", station="CED", ) source = instaseis.Source( latitude=random.random() * 180.0 - 90.0, longitude=random.random() * 360.0 - 180.0, depth_in_m=random.random() * max_depth, m_rr=4.710000e24 / 1e7, m_tt=3.810000e22 / 1e7, m_pp=-4.740000e24 / 1e7, m_rt=3.990000e23 / 1e7, m_rp=-8.050000e23 / 1e7, m_tp=-1.230000e24 / 1e7, origin_time=obspy.UTCDateTime(2011, 1, 2, 3, 4, 5), ) print("======") ref = reference.get_seismograms(source=source, receiver=receiver, components="ZNERT") oth = [ _i.get_seismograms(source=source, receiver=receiver, components="ZNERT") for _i in others ] for _i, _j in zip(oth, others): print(_j.info.directory, ":", ref == _i) assert ref == _i, str(source) + "\n" + str(receiver)
def on_open_instaseis_button_released(self): cwd = os.getcwd() self.folder = str( QtGui.QFileDialog.getExistingDirectory( self, "choose instaseis database folder", cwd)) if not self.folder: return self.instaseis_db = instaseis.open_db(self.folder) self.source = instaseis.Source(latitude=self.ui.evla.value(), longitude=self.ui.evlo.value(), depth_in_m=self.ui.evdp.value() * 1000., m_rr=self.ui.m_rr.value(), m_tt=self.ui.m_tt.value(), m_pp=self.ui.m_pp.value(), m_rt=self.ui.m_rt.value(), m_rp=self.ui.m_rp.value(), m_tp=self.ui.m_pp.value()) self.receiver = instaseis.Receiver(latitude=self.ui.stla.value(), longitude=self.ui.stlo.value()) self.stream = self.instaseis_db.get_seismograms( source=self.source, receiver=self.receiver, components=str(self.ui.component.currentText()), kind=str(self.ui.motion_type.currentText()), remove_source_shift=True) self.stream[0].stats.sac = {} self.stream[0].stats.sac['o'] = 0.0 self.stream[0].stats.sac['gcarc'] = geodetics.locations2degrees( float(self.ui.evla.value()), float(self.ui.evlo.value()), float(self.ui.stla.value()), float(self.ui.stlo.value())) self.stream_copy = self.stream.copy() time = self.get_time_axis self.ui.window_start.setMaximum(time[-1]) self.ui.window_end.setMaximum(time[-1]) self.ui.window_start.setValue(time[0]) self.ui.window_end.setValue(time[-1]) self.instaseis = True self.plot_map() self.update()
def _get_npts(db, dt): src = instaseis.Source(latitude=90.0, longitude=0.0, depth_in_m=0.0, m_rr=-1.670000e+28 / 1e7, m_tt=3.820000e+27 / 1e7, m_pp=1.280000e+28 / 1e7, m_rt=-7.840000e+27 / 1e7, m_rp=-3.570000e+28 / 1e7, m_tp=1.550000e+27 / 1e7) rec = instaseis.Receiver(latitude=10, longitude=10) st = db.get_seismograms(src, rec, components='RTZ', kind='displacement', dt=dt, remove_source_shift=True) return st[0].stats.npts
def get_ns(wf1, source_conf, insta): # Nr of time steps in traces if insta: # get path to instaseis db #ToDo: ugly. dbpath = json.load( open(os.path.join(source_conf['project_path'], 'config.json')))['wavefield_path'] # open db = instaseis.open_db(dbpath) # get a test seismogram to determine... stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0, longitude=0.0), receiver=instaseis.Receiver(latitude=10., longitude=0.0), dt=1. / source_conf['sampling_rate'])[0] nt = stest.stats.npts Fs = stest.stats.sampling_rate else: with WaveField(wf1) as wf1: nt = int(wf1.stats['nt']) Fs = round(wf1.stats['Fs'], 8) # Necessary length of zero padding for carrying out # frequency domain correlations/convolutions n = next_fast_len(2 * nt - 1) # Number of time steps for synthetic correlation n_lag = int(source_conf['max_lag'] * Fs) if nt - 2 * n_lag <= 0: click.secho('Resetting maximum lag to %g seconds: Synthetics are too\ short for a maximum lag of %g seconds.' % (nt // 2 / Fs, n_lag / Fs)) n_lag = nt // 2 n_corr = 2 * n_lag + 1 return nt, n, n_corr, Fs
def get_ns(all_conf, insta=False): # Nr of time steps in traces if insta: # get path to instaseis db dbpath = all_conf.config['wavefield_path'] # open db = instaseis.open_db(dbpath) # get a test seismogram to determine... stest = db.get_seismograms(source=instaseis.ForceSource(latitude=0.0, longitude=0.), receiver=instaseis.Receiver(latitude=10., longitude=0.), dt=1. / all_conf.config ['wavefield_sampling_rate'])[0] nt = stest.stats.npts Fs = stest.stats.sampling_rate else: any_wavefield = glob(os.path.join(all_conf.config['project_path'], 'greens', '*.h5'))[-1] with WaveField(any_wavefield) as wf1: nt = int(wf1.stats['nt']) Fs = round(wf1.stats['Fs'], 8) n = wf1.stats['npad'] # # Necessary length of zero padding # # for carrying out frequency domain correlations/convolutions # n = next_fast_len(2 * nt - 1) # Number of time steps for synthetic correlation n_lag = int(all_conf.source_config['max_lag'] * Fs) if nt - 2 * n_lag <= 0: n_lag_old = n_lag n_lag = nt // 2 warn('Resetting maximum lag to %g seconds:\ Synthetics are too short for %g seconds.' % (n_lag / Fs, n_lag_old / Fs)) n_corr = 2 * n_lag + 1 return nt, n, n_corr, Fs
def green_from_instaseis(self, station): # set some parameters lat_sta = station['lat'] lon_sta = station['lon'] lat_sta = geograph_to_geocent(float(lat_sta)) lon_sta = float(lon_sta) rec = instaseis.Receiver(latitude=lat_sta, longitude=lon_sta) point_f = float(self.config['wavefield_point_force']) channel = self.config['wavefield_channel'] station_id = station['net'] + '.' + station['sta'] + '..MX' + channel if self.config['verbose']: print(station_id) if channel == 'Z': c_index = 0 elif channel == 'R': c_index = 1 elif channel == 'T': c_index = 2 else: raise ValueError("Unknown channel: %s, choose R, T, Z" % channel) # initialize the file f_out = os.path.join(self.wf_path, station_id + '.h5') with h5py.File(f_out, "w") as f: # DATASET NR 1: STATS stats = f.create_dataset('stats', data=(0,)) stats.attrs['reference_station'] = station['sta'] stats.attrs['data_quantity'] = self.data_quantity stats.attrs['ntraces'] = self.ntraces stats.attrs['Fs'] = self.Fs stats.attrs['nt'] = int(self.npts) stats.attrs['npad'] = self.npad if self.fdomain: stats.attrs['fdomain'] = True else: stats.attrs['fdomain'] = False # DATASET NR 2: Source grid f.create_dataset('sourcegrid', data=self.sourcegrid) # DATASET Nr 3: Seismograms itself if self.fdomain: traces = f.create_dataset('data', (self.ntraces, self.npts + 1), dtype=np.complex64) else: traces = f.create_dataset('data', (self.ntraces, self.npts), dtype=np.float32) for i in range(self.ntraces): if i % 1000 == 0 and i > 0 and self.config['verbose']: print('Converted %g of %g traces' % (i, self.ntraces)) lat_src = geograph_to_geocent(self.sourcegrid[1, i]) lon_src = self.sourcegrid[0, i] fsrc = instaseis.ForceSource(latitude=lat_src, longitude=lon_src, f_r=point_f) if self.config['synt_data'] == 'DIS': values = self.db.get_seismograms(source=fsrc, receiver=rec, dt=1. / self.Fs) elif self.config['synt_data'] == 'VEL': values = self.db.get_seismograms(source=fsrc, receiver=rec, dt=1. / self.Fs, kind='velocity') elif self.config['synt_data'] == 'ACC': values = self.db.get_seismograms(source=fsrc, receiver=rec, dt=1. / self.Fs, kind='acceleration') else: raise ValueError('Unknown data quantity. \ Choose DIS, VEL or ACC in configuration.') if channel in ['R', 'T']: baz = gps2dist_azimuth(lat_src, lon_src, lat_sta, lon_sta)[2] values.rotate('NE->RT', baz) if self.filter is not None: trace = lfilter(*self.filter, x=values[c_index].data) else: trace = values[c_index].data if self.fdomain: trace_fd = np.fft.rfft(trace[0: self.npts], n=self.npad) traces[i, :] = trace_fd else: traces[i, :] = trace[0: self.npts] return()
print('delete channel ' + tr.stats['channel']) else: for tr in seis.select(channel='BX90*'): seis.remove(tr) print('delete channel ' + tr.stats['channel']) #While we are at it there is a mistake in data_processing_2 where the stats of seis[0] (vertical component) get overwritten by those of a horizontal component... fixing this here. seis[0].stats['channel'] = 'BHZ' # print(seis[0].stats['channel']) #Calculate receiver at 90 distance origin = geopy.Point(srcdict['latitude'], srcdict['longitude']) destination = VincentyDistance(kilometers=90 * (6371 * np.pi / 180.)).destination( origin, seis[0].stats['az']) receiver = instaseis.Receiver(latitude=destination.latitude, longitude=destination.longitude, network=seis[0].stats['network'], station=seis[0].stats['station']) eventtime = seis[0].stats['eventtime'] starttime = seis[0].stats['starttime'] endtime = seis[0].stats['endtime'] st = db.get_seismograms(source=source, receiver=receiver, kind='displacement', dt=0.1) # Rotate synthetics stE = st.select(channel='BXE') stN = st.select(channel='BXN') stZ = st.select(channel='BXZ') [stRtmp, stTtmp] = obspy.signal.rotate.rotate_ne_rt(stN[0].data, stE[0].data,
def compute_correlation(input_files, all_conf, nsrc, all_ns, taper, insta=False): """ Compute noise cross-correlations from two .h5 'wavefield' files. Noise source distribution and spectrum is given by starting_model.h5 It is assumed that noise sources are delta-correlated in space. Metainformation: Include the reference station names for both stations from wavefield files, if possible. Do not include geographic information from .csv file as this might be error-prone. Just add the geographic info later if needed. """ wf1, wf2 = input_files ntime, n, n_corr, Fs = all_ns ntraces = nsrc.src_loc[0].shape[0] correlation = np.zeros(n_corr) if insta: # open database dbpath = all_conf.config['wavefield_path'] # open db = instaseis.open_db(dbpath) # get receiver locations station1 = wf1[0] station2 = wf2[0] lat1 = geograph_to_geocent(float(wf1[2])) lon1 = float(wf1[3]) rec1 = instaseis.Receiver(latitude=lat1, longitude=lon1) lat2 = geograph_to_geocent(float(wf2[2])) lon2 = float(wf2[3]) rec2 = instaseis.Receiver(latitude=lat2, longitude=lon2) else: wf1 = WaveField(wf1) wf2 = WaveField(wf2) station1 = wf1.stats['reference_station'] station2 = wf2.stats['reference_station'] # Make sure all is consistent if False in (wf1.sourcegrid[1, 0:10] == wf2.sourcegrid[1, 0:10]): raise ValueError("Wave fields not consistent.") if False in (wf1.sourcegrid[1, -10:] == wf2.sourcegrid[1, -10:]): raise ValueError("Wave fields not consistent.") if False in (wf1.sourcegrid[0, -10:] == nsrc.src_loc[0, -10:]): raise ValueError("Wave field and source not consistent.") # Loop over source locations print_each_n = max(5, round(max(ntraces // 5, 1), -1)) for i in range(ntraces): # noise source spectrum at this location S = nsrc.get_spect(i) if S.sum() == 0.: # If amplitude is 0, continue. (Spectrum has 0 phase anyway.) continue if insta: # get source locations lat_src = geograph_to_geocent(nsrc.src_loc[1, i]) lon_src = nsrc.src_loc[0, i] fsrc = instaseis.ForceSource(latitude=lat_src, longitude=lon_src, f_r=1.e12) Fs = all_conf.config['wavefield_sampling_rate'] s1 = db.get_seismograms(source=fsrc, receiver=rec1, dt=1. / Fs)[0].data * taper s2 = db.get_seismograms(source=fsrc, receiver=rec2, dt=1. / Fs)[0].data * taper s1 = np.ascontiguousarray(s1) s2 = np.ascontiguousarray(s2) spec1 = np.fft.rfft(s1, n) spec2 = np.fft.rfft(s2, n) else: if not wf1.fdomain: # read Green's functions s1 = np.ascontiguousarray(wf1.data[i, :] * taper) s2 = np.ascontiguousarray(wf2.data[i, :] * taper) # Fourier transform for greater ease of convolution spec1 = np.fft.rfft(s1, n) spec2 = np.fft.rfft(s2, n) else: spec1 = np.ascontiguousarray(wf1.data[i, :]) spec2 = np.ascontiguousarray(wf2.data[i, :]) # convolve G1G2 g1g2_tr = np.multiply(np.conjugate(spec1), spec2) # convolve noise source c = np.multiply(g1g2_tr, S) # transform back correlation += my_centered(np.fft.fftshift(np.fft.irfft(c, n)), n_corr) * nsrc.surf_area[i] # occasional info if i % print_each_n == 0 and all_conf.config['verbose']: print("Finished {} of {} source locations.".format(i, ntraces)) # end of loop over all source locations ####################################### return(correlation, station1, station2)
# From this you can already glance a couple of aspects of the database used for this tutorial: # # * uses anisotropic prem as its 1D model # * is accurate for periods down to 10 seconds # * includes vertical and horizontal components # * sources can have depths ranging from 0 to 700 km # * five hour long seismograms # # To avoid delays that become relevant when requesting very many seismograms, IRIS also offers the databases for download. # ### Receivers and Sources # # Instaseis calculates seismograms for any source and receiver pair. A receiver has coordinates and optionally network and station codes. Using a reciprocal database, all receivers are assumed to be at the same depth, i.e. usually at the Earth surface. rec = instaseis.Receiver(latitude=44.06238, longitude=10.59698, network="IV", station="BDI") print(rec) # Sources are naturally a bit more complex and Instaseis offers a variety of ways to define them. A straightforward way for earthquakes is to pass coordinates, moment as well as strike, dip and rake. src = instaseis.Source.from_strike_dip_rake(latitude=27.77, longitude=85.37, depth_in_m=12000.0, M0=1e+21, strike=32., dip=62., rake=90.) print(src) # Note that origin time was not provided and hence defaults to 1970. A non double-couple source can directly be specified in terms of its moment tensor (note that instaseis uses SI units, i.e. NM, while GCMT uses dyn cm).
def addSynthetics(name, clean): print(instaseis.__path__) # Input directory with PICKLE data as argument dir = 'Data/' + name + '/' # Load database with Greens functions db = instaseis.open_db("/raid2/sc845/Instaseis/DB/10s_PREM_ANI_FORCES/") # Directory needs to contain a CMT source text file with open(dir + 'cmtsource.txt', 'r') as inf: srcdict = eval(inf.read()) # Unpack dictionary latitude = srcdict['latitude'] longitude = srcdict['longitude'] depth_in_m = srcdict['depth'] * 1.e3 m_rr = srcdict['Mrr'] / 1E7 m_tt = srcdict['Mtt'] / 1E7 m_pp = srcdict['Mpp'] / 1E7 m_rt = srcdict['Mrt'] / 1E7 m_rp = srcdict['Mrp'] / 1E7 m_tp = srcdict['Mtp'] / 1E7 origin_time = obspy.UTCDateTime(srcdict['year'], srcdict['month'], srcdict['day'], srcdict['hour'], srcdict['min'], srcdict['sec'], srcdict['msec']) # Read in source source = instaseis.Source(latitude=latitude, longitude=longitude, depth_in_m=depth_in_m, m_rr=m_rr, m_tt=m_tt, m_pp=m_pp, m_rt=m_rt, m_rp=m_rp, m_tp=m_tp, origin_time=origin_time) # Read and loop through station list stalist = glob.glob(dir + '/*PICKLE') for s in range(len(stalist)): seis = read(stalist[s], format='PICKLE') for tr in seis.select(channel='BX*'): seis.remove(tr) # Fixing some old mistake (not sure if still present, check with Sanne) seis[0].stats['channel'] = 'BHZ' print(seis[0].stats['channel']) receiver = instaseis.Receiver(latitude=seis[0].stats['stla'], longitude=seis[0].stats['stlo'], network=seis[0].stats['network'], station=seis[0].stats['station']) start = seis[0].stats['starttime'] end = seis[0].stats['endtime'] st = db.get_seismograms(source=source, receiver=receiver, kind='displacement', dt=0.1) # Rotate synthetics stE = st.select(channel='BXE') stN = st.select(channel='BXN') stZ = st.select(channel='BXZ') [stRtmp, stTtmp] = obspy.signal.rotate.rotate_ne_rt(stN[0].data, stE[0].data, seis[0].stats['baz']) stR = stN[0].copy() stR.stats['channel'] = 'BXR' stR.data = stRtmp stT = stN[0].copy() stT.stats['channel'] = 'BXT' stT.data = stTtmp seis += stR seis += stT seis += stZ for x in seis: print(x.stats['channel']) # Overwrites previous PICKLE with synthetics included seis.write(stalist[s], format='PICKLE') print('Synthetics for this event have been successfully added')
print(source) # Read and loop through stationlist stalist = glob.glob(dr + '*PICKLE') for s in range(0, len(stalist)): print(str(s) + '/' + str(len(stalist)) + ' of ' + event) seis = read(stalist[s], format='PICKLE') for tr in seis.select(channel='BX*'): seis.remove(tr) #While we are at it there is a mistake in data_processing_2 where the stats of seis[0] (vertical component) get overwritten by those of a horizontal component... fixing this here. seis[0].stats['channel'] = 'BHZ' # print(seis[0].stats['channel']) receiver = instaseis.Receiver(latitude=seis[0].stats['stla'], longitude=seis[0].stats['stlo'], network=seis[0].stats['network'], station=seis[0].stats['station']) eventtime = seis[0].stats['eventtime'] starttime = seis[0].stats['starttime'] endtime = seis[0].stats['endtime'] st = db.get_seismograms(source=source, receiver=receiver, kind='displacement', dt=0.1) # displacement, velocity, acceleration # Rotate synthetics stE = st.select(channel='BXE') stN = st.select(channel='BXN') stZ = st.select(channel='BXZ') [stRtmp, stTtmp] = obspy.signal.rotate.rotate_ne_rt(stN[0].data, stE[0].data,
m_pp = 3.98e13 planet_radius = 1565.0 #planet_radius = 6371.0 distaz = gps2dist_azimuth(lat1=evla, lon1=evlo, lat2=stla, lon2=stlo, a=planet_radius*1000.0, f=0.0) # no flattening gcarc_m = distaz[0] dist_km = gcarc_m / 1000.0 #get instaseis seismogram instaseis_db = instaseis.open_db(insta_db_name) source = instaseis.Source(latitude=evla, longitude=evlo, depth_in_m=evdp*1000., m_rr = m_rr, m_pp = m_rr) receiver = instaseis.Receiver(latitude=stla, longitude=stlo) stream = instaseis_db.get_seismograms(source=source, receiver=receiver, components=components, kind = kind, remove_source_shift=True) stream = stream.slice(stream[0].stats.starttime + window_min, stream[0].stats.starttime + window_max) #stream.plot() time = np.linspace(window_min, window_min+(stream[0].stats.npts * stream[0].stats.delta), stream[0].stats.npts) #make ensemble of dispersion curves w/ different noise realizations
inv = SS_MTI.DataGetter.read_inv(inv_path=f_in["DATA"]["inventory_filepath"]) # Inventory file cat = SS_MTI.DataGetter.read_cat(cat_path=f_in["DATA"]["catalog_filepath"]) # Catalog file events = SS_MTI.DataGetter.read_events_from_cat( event_params=f_in["EVENTS"], cat=cat, inv=inv, local_folder=f_in["DATA"]["waveform_filepath"], host_name=f_in["SERVER"]["host_name"], user_name=f_in["SERVER"]["username"], remote_folder=f_in["SERVER"]["remote_folder"], save_file_name=cat_save_name, ) event = event[0] rec = instaseis.Receiver( latitude=f_in["PARAMETERS"]["RECEIVER"]["la_r"], longitude=f_in["PARAMETERS"]["RECEIVER"]["lon_r"], ) ## Step 3: """ Define forward modeler """ forward_method = f_in["FORWARD"]["METHOD"] forward_dict = f_in["FORWARD"][forward_method] if forward_method == "INSTASEIS": fwd = SS_MTI.Forward.Instaseis( instaseis_db=instaseis.open_db(forward_dict["VELOC"]), taup_model=forward_dict["VELOC_taup"], or_time=event.origin_time, dt=event.delta, start_cut=f_in["PARAMETERS"]["start_cut"],
datadir = "./" + evnum + "/" fils = os.listdir(datadir) for fil in fils: print(fil) ## Get station information mat = spio.loadmat(datadir + fil, squeeze_me=True) # load *.mat files latitude = mat['traces'][0]['latitude'] longitude = mat['traces'][0]['longitude'] network = mat['traces'][0]['network'] station = mat['traces'][0]['station'] elevation = mat['traces'][0]['elevation'] # CALCULATE SYNTHETICS rec = instaseis.Receiver(latitude=latitude, longitude=longitude, network=network, station=station) # tr = db.get_seismograms(source=cat_ev, receiver=rec, components=["Z","R","T"], kind='velocity') tr = db.get_seismograms(source=cat_ev, receiver=rec, components=["Z", "R", "T"], kind='displacement') mat_synth = mat channels = ["BHZ", "BHR", "BHT"] for icomp in np.arange(0, 3): channel = channels[icomp] sampleCount = len(tr[icomp].data) sampleRate = 1. / db.info.dt mat_synth['traces'][icomp]['channel'] = channel
def test_seismogram_extraction(syngine_client): """ Test the seismogram extraction from local and syngine databases. """ # syngine and local database. s_db = syngine_client l_db = instaseis.open_db(db_path) source = instaseis.Source( latitude=4.0, longitude=3.0, depth_in_m=0, m_rr=4.71e17, m_tt=3.81e17, m_pp=-4.74e17, m_rt=3.99e17, m_rp=-8.05e17, m_tp=-1.23e17, ) receiver = instaseis.Receiver(latitude=10.0, longitude=20.0, depth_in_m=None) kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], } _compare_streams(s_db, l_db, kwargs) # Test velocity and acceleration. kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], "kind": "velocity", } _compare_streams(s_db, l_db, kwargs) kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], "kind": "acceleration", } _compare_streams(s_db, l_db, kwargs) # Test remove source shift. kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], "remove_source_shift": False, } _compare_streams(s_db, l_db, kwargs) # Test resampling. kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], "dt": 1.0, "kernelwidth": 6, } _compare_streams(s_db, l_db, kwargs) # Force sources currently raise an error. source = instaseis.ForceSource( latitude=89.91, longitude=0.0, depth_in_m=12000, f_r=1.23e10, f_t=2.55e10, f_p=1.73e10, ) kwargs = {"source": source, "receiver": receiver} with pytest.raises(ValueError) as e: _compare_streams(s_db, l_db, kwargs) assert e.value.args[0] == ( "The Syngine Instaseis client does currently not " "support force sources. You can still download " "data from the Syngine service for force " "sources manually.") # Test less components and a latitude of 45 degrees to have the maximal # effect of geocentric vs geographic coordinates. source = instaseis.Source( latitude=45.0, longitude=3.0, depth_in_m=0, m_rr=4.71e17, m_tt=3.81e17, m_pp=-4.74e17, m_rt=3.99e17, m_rp=-8.05e17, m_tp=-1.23e17, ) receiver = instaseis.Receiver(latitude=-45.0, longitude=20.0, depth_in_m=None) kwargs = { "source": source, "receiver": receiver, "components": ["Z", "N", "E", "R", "T"], } _compare_streams(s_db, l_db, kwargs)
def g1g2_kern(wf1str, wf2str, kernel, adjt, src, source_conf, insta): measr_conf = json.load( open(os.path.join(source_conf['source_path'], 'measr_config.json'))) bandpass = measr_conf['bandpass'] if bandpass == None: filtcnt = 1 elif type(bandpass) == list: if type(bandpass[0]) != list: filtcnt = 1 else: filtcnt = len(bandpass) ntime, n, n_corr, Fs = get_ns(wf1str, source_conf, insta) # use a one-sided taper: The seismogram probably has a non-zero end, # being cut off whereever the solver stopped running. taper = cosine_taper(ntime, p=0.01) taper[0:ntime // 2] = 1.0 ######################################################################## # Prepare filenames and adjoint sources ######################################################################## filenames = [] adjt_srcs = [] adjt_srcs_cnt = 0 for ix_f in range(filtcnt): filename = kernel + '.{}.npy'.format(ix_f) filenames.append(filename) #if os.path.exists(filename): # continue f = Stream() for a in adjt: adjtfile = a + '*.{}.sac'.format(ix_f) adjtfile = glob(adjtfile) try: f += read(adjtfile[0])[0] f[-1].data = my_centered(f[-1].data, n_corr) adjt_srcs_cnt += 1 except IndexError: print('No adjoint source found: {}\n'.format(a)) break adjt_srcs.append(f) ######################################################################## # Compute the kernels ######################################################################## with NoiseSource(src) as nsrc: ntraces = nsrc.src_loc[0].shape[0] if insta: # open database dbpath = json.load( open(os.path.join(source_conf['project_path'], 'config.json')))['wavefield_path'] # open and determine Fs, nt db = instaseis.open_db(dbpath) # get receiver locations lat1 = geograph_to_geocent(float(wf1[2])) lon1 = float(wf1[3]) rec1 = instaseis.Receiver(latitude=lat1, longitude=lon1) lat2 = geograph_to_geocent(float(wf2[2])) lon2 = float(wf2[3]) rec2 = instaseis.Receiver(latitude=lat2, longitude=lon2) else: wf1 = WaveField(wf1str) wf2 = WaveField(wf2str) kern = np.zeros((filtcnt, ntraces, len(adjt))) ######################################################################## # Loop over locations ######################################################################## for i in range(ntraces): # noise source spectrum at this location # For the kernel, this contains only the basis functions of the # spectrum without weights; might still be location-dependent, # for example when constraining sensivity to ocean S = nsrc.get_spect(i) if S.sum() == 0.: # The spectrum has 0 phase so only checking absolute value here continue #################################################################### # Get synthetics #################################################################### if insta: # get source locations lat_src = geograph_to_geocent(nsrc.src_loc[1, i]) lon_src = nsrc.src_loc[0, i] fsrc = instaseis.ForceSource(latitude=lat_src, longitude=lon_src, f_r=1.e12) s1 = np.ascontiguousarray( db.get_seismograms( source=fsrc, receiver=rec1, dt=1. / source_conf['sampling_rate'])[0].data * taper) s2 = np.ascontiguousarray( db.get_seismograms( source=fsrc, receiver=rec2, dt=1. / source_conf['sampling_rate'])[0].data * taper) else: s1 = np.ascontiguousarray(wf1.data[i, :] * taper) s2 = np.ascontiguousarray(wf2.data[i, :] * taper) spec1 = np.fft.rfft(s1, n) spec2 = np.fft.rfft(s2, n) g1g2_tr = np.multiply(np.conjugate(spec1), spec2) c = np.multiply(g1g2_tr, S) ####################################################################### # Get Kernel at that location ####################################################################### corr_temp = my_centered(np.fft.ifftshift(np.fft.irfft(c, n)), n_corr) ####################################################################### # Apply the 'adjoint source' ####################################################################### for ix_f in range(filtcnt): f = adjt_srcs[ix_f] if f == None: continue for j in range(len(f)): delta = f[j].stats.delta kern[ix_f, i, j] = np.dot(corr_temp, f[j].data) * delta #elif measr_conf['mtype'] in ['envelope']: # if j == 0: # corr_temp_h = corr_temp # print(corr_temp_h) # if j == 1: # corr_temp_h = hilbert(corr_temp) # print(corr_temp_h) # # kern[ix_f,i,j] = np.dot(corr_temp,f[j].data) * delta if i % 50000 == 0: print("Finished {} source locations.".format(i)) if not insta: wf1.file.close() wf2.file.close() for ix_f in range(filtcnt): filename = filenames[ix_f] if kern[ix_f, :, :].sum() != 0: np.save(filename, kern[ix_f, :, :]) return ()
import matplotlib.pyplot as plt from datetime import datetime plt.rcParams['figure.figsize'] = (12, 8) db = instaseis.open_db( "http://instaseis.ethz.ch/icy_ocean_worlds/Tit124km-33pNH-hQ_2s") ntwk = 'ST' # Define M0 from shear modulus (mu), area of rupture along the fault (A), # and the average slip along the fault (D). # Adapted from Stein & Wysession (2003). mu = 2.0e+9 A = 6.66e+8 D = 0.5 Mzero = mu * A * D R01=instaseis.Receiver(latitude=-10.0, longitude=-165.0, network=ntwk,\ station='01') R02=instaseis.Receiver(latitude=-8.8, longitude=-167.4, network=ntwk,\ station='02') R03=instaseis.Receiver(latitude=-7.6, longitude=-169.8, network=ntwk,\ station='03') R04=instaseis.Receiver(latitude=-6.4, longitude=-172.3, network=ntwk,\ station='04') R05=instaseis.Receiver(latitude=-5.2, longitude=-174.7, network=ntwk,\ station='05') R06=instaseis.Receiver(latitude=-3.9, longitude=-177.1, network=ntwk,\ station='06') R07=instaseis.Receiver(latitude=-2.7, longitude=-179.5, network=ntwk,\ station='07') R08=instaseis.Receiver(latitude=-1.5, longitude=178.1, network=ntwk,\ station='08') R09=instaseis.Receiver(latitude=-0.3, longitude=175.6, network=ntwk,\
def love_pick(self, T_trace, la_s, lo_s, depth, save_directory, time_at_rec, npts, filter=True, plot_modus=False): if plot_modus == True: dir_L = save_directory + '/Love_waves' if not os.path.exists(dir_L): os.makedirs(dir_L) Love_st = Stream() evla = la_s evlo = lo_s rec = instaseis.Receiver(latitude=self.prior['la_r'], longitude=self.prior['lo_r']) dist, az, baz = gps2dist_azimuth(lat1=evla, lon1=evlo, lat2=self.prior['la_r'], lon2=self.prior['lo_r'], a=self.prior['radius'], f=0) # For now I am just using the Z-component, because this will have the strongest Rayleigh signal: T_comp = T_trace.copy() if plot_modus == True: T_comp.plot(outfile=dir_L + '/sw_entire_waveform.pdf') phases = self.get_L_phases(time_at_rec) for i in range(len(phases)): if plot_modus == True: dir_phases = dir_L + '/%s' % phases[i]['name'] if not os.path.exists(dir_phases): os.makedirs(dir_phases) trial = T_trace.copy() if filter == True: trial.detrend(type="demean") trial.filter('highpass', freq=phases[i]['fmin'], zerophase=True) trial.filter('lowpass', freq=phases[i]['fmax'], zerophase=True) trial.detrend() if plot_modus == True: start_vline = int( (phases[i]['starttime'](dist, depth).timestamp - time_at_rec.timestamp) / trial.stats.delta) end_vline = int((phases[i]['endtime'](dist, depth).timestamp - time_at_rec.timestamp) / trial.stats.delta) plt.figure(1) ax = plt.subplot(111) plt.plot(trial.data, alpha=0.5) ymin, ymax = ax.get_ylim() # plt.plot(trial.data) plt.vlines([start_vline, end_vline], ymin, ymax) plt.xlabel(time_at_rec.strftime('%Y-%m-%dT%H:%M:%S + sec')) plt.savefig(dir_phases + '/sw_with_Love_windows.pdf') plt.tight_layout() plt.close() if filter == True: trial.detrend(type="demean") env = envelope(trial.data) trial.data = env trial.trim(starttime=phases[i]['starttime'](dist, depth), endtime=phases[i]['endtime'](dist, depth)) else: env = trial.data if plot_modus == True: plt.figure(2) plt.plot(trial, label='%s' % phases[i]['name']) plt.legend() plt.tight_layout() plt.savefig(dir_phases + '/Love_envelope_filter_%s.pdf' % phases[i]['name']) plt.close() zero_trace = Trace(np.zeros(npts), header={ "starttime": phases[i]['starttime'](dist, depth), 'delta': trial.meta.delta, "station": trial.meta.station, "network": trial.meta.network, "location": trial.meta.location, "channel": phases[i]['name'] }) total_trace = zero_trace.__add__(trial, method=0, interpolation_samples=0, fill_value=trial.data, sanity_checks=False) Love_st.append(total_trace) if plot_modus == True: plt.figure(3) plt.plot(Love_st.traces[0].data, label='%s' % Love_st.traces[0].meta.channel) plt.plot(Love_st.traces[1].data, label='%s' % Love_st.traces[1].meta.channel) plt.plot(Love_st.traces[2].data, label='%s' % Love_st.traces[2].meta.channel) plt.plot(Love_st.traces[3].data, label='%s' % Love_st.traces[3].meta.channel) plt.legend() plt.tight_layout() plt.savefig(dir_L + '/diff_Love_freq.pdf') plt.close() return Love_st
def test_finite_source_retrieval(reciprocal_clients, usgs_param): """ Tests if the finite sources requested from the server are identical to the one requested with the local instaseis client with some required tweaks. """ client = reciprocal_clients db = instaseis.open_db(client.filepath) basic_parameters = { "receiverlongitude": 11, "receiverlatitude": 22, "receiverdepthinmeters": 0, "format": "miniseed" } with io.open(usgs_param, "rb") as fh: body = fh.read() # default parameters params = copy.deepcopy(basic_parameters) request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) for tr in st_server: assert tr.stats._format == "MSEED" # Parse the finite source. fs = _parse_finite_source(usgs_param) rec = instaseis.Receiver(latitude=22, longitude=11, network="XX", station="SYN", location="SE") st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec) # The origin time is the time of the first sample in the route. for tr in st_db: # Cut away the first ten samples as they have been previously added. tr.data = tr.data[10:] tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1) for tr_db, tr_server in zip(st_db, st_server): # Sample spacing is very similar but not equal due to floating point # accuracy. np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta) tr_server.stats.delta = tr_db.stats.delta del tr_server.stats._format del tr_server.stats.mseed assert tr_db.stats == tr_server.stats np.testing.assert_allclose(tr_db.data, tr_server.data) # Once again but this time request a SAC file. params = copy.deepcopy(basic_parameters) params["format"] = "saczip" request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.Stream() zip_obj = zipfile.ZipFile(request.buffer) for name in zip_obj.namelist(): st_server += obspy.read(io.BytesIO(zip_obj.read(name))) for tr in st_server: assert tr.stats._format == "SAC" for tr_db, tr_server in zip(st_db, st_server): # Sample spacing is very similar but not equal due to floating point # accuracy. np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta) tr_server.stats.delta = tr_db.stats.delta del tr_server.stats._format del tr_server.stats.sac assert tr_db.stats == tr_server.stats np.testing.assert_allclose(tr_db.data, tr_server.data) # One with a label. params = copy.deepcopy(basic_parameters) params["label"] = "random_things" request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 cd = request.headers["Content-Disposition"] assert cd.startswith("attachment; filename=random_things_") assert cd.endswith(".mseed") # One simulating a crash in the underlying function. params = copy.deepcopy(basic_parameters) with mock.patch("instaseis.database_interfaces.base_instaseis_db" ".BaseInstaseisDB.get_seismograms_finite_source") as p: p.side_effect = ValueError("random crash") request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 400 assert request.reason == ("Could not extract finite source seismograms. " "Make sure, the parameters are valid, and the " "depth settings are correct.") # Simulating a logic error that should not be able to happen. params = copy.deepcopy(basic_parameters) with mock.patch("instaseis.database_interfaces.base_instaseis_db" ".BaseInstaseisDB.get_seismograms_finite_source") as p: # Longer than the database returned stream thus the endtime is out # of bounds. st = obspy.read() p.return_value = st request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 500 assert request.reason.startswith("Endtime larger than the extracted " "endtime") # One more with resampling parameters and different units. params = copy.deepcopy(basic_parameters) # We must have a sampling rate that cleanly fits in the existing one, # otherwise we cannot fake the cutting. dt_new = 24.724845445855724 / 10 params["dt"] = dt_new params["kernelwidth"] = 2 params["units"] = "acceleration" st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec, dt=dt_new, kernelwidth=2, kind="acceleration") # The origin time is the time of the first sample in the route. for tr in st_db: # Cut away the first ten samples as they have been previously added. tr.data = tr.data[100:] tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1) request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) # Cut some parts in the middle to avoid any potential boundary effects. st_db.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4), obspy.UTCDateTime(1900, 1, 1, 0, 14)) st_server.trim(obspy.UTCDateTime(1900, 1, 1, 0, 4), obspy.UTCDateTime(1900, 1, 1, 0, 14)) for tr_db, tr_server in zip(st_db, st_server): # Sample spacing and times are very similar but not identical due to # floating point inaccuracies in the arithmetics. np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta) np.testing.assert_allclose(tr_server.stats.starttime.timestamp, tr_db.stats.starttime.timestamp) tr_server.stats.delta = tr_db.stats.delta tr_server.stats.starttime = tr_db.stats.starttime del tr_server.stats._format del tr_server.stats.mseed del tr_server.stats.processing del tr_db.stats.processing np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta) assert tr_db.stats == tr_server.stats np.testing.assert_allclose(tr_db.data, tr_server.data, rtol=1E-7, atol=tr_db.data.ptp() * 1E-7) # Testing network and station code parameters. # Default values. params = copy.deepcopy(basic_parameters) request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) for tr in st_server: assert tr.stats.network == "XX" assert tr.stats.station == "SYN" assert tr.stats.location == "SE" # Setting all three. params = copy.deepcopy(basic_parameters) params["networkcode"] = "AA" params["stationcode"] = "BB" params["locationcode"] = "CC" request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) for tr in st_server: assert tr.stats.network == "AA" assert tr.stats.station == "BB" assert tr.stats.location == "CC" # Setting only the location code. params = copy.deepcopy(basic_parameters) params["locationcode"] = "AA" request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) for tr in st_server: assert tr.stats.network == "XX" assert tr.stats.station == "SYN" assert tr.stats.location == "AA" # Test the scale parameter. params = copy.deepcopy(basic_parameters) params["scale"] = 33.33 request = client.fetch(_assemble_url('finite_source', **params), method="POST", body=body) assert request.code == 200 st_server = obspy.read(request.buffer) for tr in st_server: assert tr.stats._format == "MSEED" # Parse the finite source. fs = _parse_finite_source(usgs_param) rec = instaseis.Receiver(latitude=22, longitude=11, network="XX", station="SYN", location="SE") st_db = db.get_seismograms_finite_source(sources=fs, receiver=rec) # The origin time is the time of the first sample in the route. for tr in st_db: # Multiply with scale parameter. tr.data *= 33.33 # Cut away the first ten samples as they have been previously added. tr.data = tr.data[10:] tr.stats.starttime = obspy.UTCDateTime(1900, 1, 1) for tr_db, tr_server in zip(st_db, st_server): # Sample spacing is very similar but not equal due to floating point # accuracy. np.testing.assert_allclose(tr_server.stats.delta, tr_db.stats.delta) tr_server.stats.delta = tr_db.stats.delta del tr_server.stats._format del tr_server.stats.mseed assert tr_db.stats == tr_server.stats np.testing.assert_allclose(tr_db.data, tr_server.data, atol=1E-6 * tr_db.data.ptp())
# print(ev.name) """ Collect the corresponding data that belongs to the events """ events = SS_MTI.DataGetter.read_events_from_cat( event_params=Event_names, cat=cat, inv=inv, local_folder="/mnt/marshost/", host_name="marshost.ethz.ch", user_name="sysop", remote_folder="/data/", save_file_name=pjoin(save_folder, "event.mseed"), ) """ Specify receiver """ lat_rec = 4.5 # 02384 lon_rec = 135.623447 rec = instaseis.Receiver(latitude=lat_rec, longitude=lon_rec) for event in events: st = event.waveforms_VBB.copy() # epi, az, baz = _PreProcess.Get_location( # la_s=event.latitude, lo_s=event.longitude, la_r=rec.latitude, lo_r=rec.longitude # ) # st = st.rotate("NE->RT", back_azimuth=baz) P_arr = utct(event.picks["P"]) - utct(event.origin_time) + P_shift S_arr = utct(event.picks["S"]) - utct(event.origin_time) + S_shift """ Plot the waveforms and spectra """ fig, ax = plt.subplots(nrows=3, ncols=2, sharex="col", sharey="col",
delta_id = 2 baz_id = 3 depth_id = 4 strike_id = 5 rake_id = 6 dip_id = 7 nstations = len(lons) * len(lats) n = 0 for lon in lons: for lat in lats: n = n + 1 print('Station ' + str(n) + ' of ' + str(nstations) + ' lat ' + str(lat) + ' lon ' + str(lat)) receiver = instaseis.Receiver(latitude=90.0, longitude=0.0, network="XX", station="TITN") for evt in tqdm(range(0, nevents)): if (setmin and gr_obj.catalog.data[evt, mag_id] < min_Mw): continue latitude = 90.0 - gr_obj.catalog.data[evt, delta_id] longitude = gr_obj.catalog.data[evt, baz_id] if longitude > 180.0: longitude -= 360.0 depth = limit_depth(db, gr_obj.catalog.data[evt, depth_id] * 1000.) strike = gr_obj.catalog.data[evt, strike_id] rake = gr_obj.catalog.data[evt, rake_id] dip = gr_obj.catalog.data[evt, dip_id] M0 = gr.calc_m0(gr_obj.catalog.data[evt, mag_id]) source = instaseis.Source.from_strike_dip_rake(latitude=latitude, longitude=longitude,
stf = 2 * np.exp(-(t / t_half)**2) * t / t_half**2 nsample = len(t) f = open('../stf_test.txt', 'w') f.write('%d %f\n' % (nsample, dt)) for y in stf: f.write('%f\n' % (y)) f.close() print sum(stf * dt) db = instaseis.open_db('../wavefield/bwd') receiver = instaseis.Receiver(latitude=30, longitude=0, network='MC', station='kerner') source = instaseis.Source(latitude=90.0, longitude=0.0, depth_in_m=0.0, m_rr=1.0e13, m_tt=1.0e13, m_pp=1.0e13, m_rt=0.0, m_rp=0.0, m_tp=0.0, time_shift=None, sliprate=stf, dt=dt) source.resample_sliprate(db.info.dt, db.info.npts)
def test_seismogram_extraction(all_remote_dbs): """ Test the seismogram extraction from local and remote databases. """ # Remote and local database. r_db = all_remote_dbs l_db = instaseis.open_db(r_db._client.filepath) # Mock responses to get the tornado testing to work. _add_callback(r_db._client) source = instaseis.Source( latitude=4.0, longitude=3.0, depth_in_m=0, m_rr=4.71e17, m_tt=3.81e17, m_pp=-4.74e17, m_rt=3.99e17, m_rp=-8.05e17, m_tp=-1.23e17, ) receiver = instaseis.Receiver(latitude=10.0, longitude=20.0, depth_in_m=None) components = r_db.available_components kwargs = {"source": source, "receiver": receiver, "components": components} _compare_streams(r_db, l_db, kwargs) # Test velocity and acceleration. kwargs = { "source": source, "receiver": receiver, "components": components, "kind": "velocity", } _compare_streams(r_db, l_db, kwargs) kwargs = { "source": source, "receiver": receiver, "components": components, "kind": "acceleration", } _compare_streams(r_db, l_db, kwargs) # Test remove source shift. kwargs = { "source": source, "receiver": receiver, "components": components, "remove_source_shift": False, } _compare_streams(r_db, l_db, kwargs) # Test resampling. kwargs = { "source": source, "receiver": receiver, "components": components, "dt": 1.0, "kernelwidth": 6, } _compare_streams(r_db, l_db, kwargs) # Test force source. if "displ_only" in r_db._client.filepath: source = instaseis.ForceSource( latitude=89.91, longitude=0.0, depth_in_m=12000, f_r=1.23e10, f_t=2.55e10, f_p=1.73e10, ) kwargs = {"source": source, "receiver": receiver} _compare_streams(r_db, l_db, kwargs) # Fix receiver depth, network, and station codes. receiver = instaseis.Receiver( latitude=10.0, longitude=20.0, depth_in_m=0.0, station="ALTM", network="BW", ) kwargs = {"source": source, "receiver": receiver, "components": components} _compare_streams(r_db, l_db, kwargs)