def test_jupyter_tutorial_tapers(self): client = Client("IRIS") t = UTCDateTime("2018-12-25 12:00:00").timestamp st = client.get_waveforms("UU", "SPU", "*", "H*", t, t + 10 * 60, attach_response=True) pre_filt = (0.01, 0.03, 40.0, 45.0) st = anxcor_utils.remove_response(st, output='DISP', pre_filt=pre_filt, zero_mean=True, taper=True) converter = XArrayConverter() resampler = XArrayResample(target_rate=10.0) rmmean_trend = XArrayRemoveMeanTrend() xarray = converter(st) resampled_array = resampler(xarray) rmm_array = rmmean_trend(resampled_array) whitening_op = XArrayWhiten(taper=0.05, whiten_type='cross_component', freqmax=3.0, freqmin=0.01, smoothing_window_ratio=0.01) whitened_array = whitening_op(rmm_array) assert whitened_array.data[0, 0, 0] == pytest.approx(0, abs=1e-2)
def test_phase_shift(self): stream = create_sinsoidal_trace(sampling_rate=40.0, duration=1000.0, period=20) stream[0].data += np.random.uniform(-0.01, 0.01, stream[0].data.shape) converter = XArrayConverter() xarray = converter(stream) taper = 0.1 center = False ratio = 0.01 whitening_op = XArrayWhiten(taper=0.1, window=0.05, whiten_type='cross_component', freqmax=20.0, freqmin=0.01, center=center, order=2) whitened_array = whitening_op(xarray) a = whitened_array.data[0, 0, :].squeeze() b = xarray.data[0, 0, :].squeeze() xcorr = correlate(a, b) # delta time array to match xcorr dt = np.arange(1 - a.shape[-1], a.shape[-1]) recovered_time_shift = dt[xcorr.argmax()] assert recovered_time_shift == 0
def test_symmetric_output(self): converter = XArrayConverter() signal_length = 1000 sampling_rate = 20.0 center_index = int(signal_length * sampling_rate) // 2 stream = create_sinsoidal_trace(sampling_rate=sampling_rate, duration=signal_length, period=2) stream1 = create_sinsoidal_trace(sampling_rate=sampling_rate, duration=signal_length, period=0.5) stream[0].data += stream1[0].data stream[0].data += np.random.uniform(-0.01, 0.01, stream[0].data.shape) converter = XArrayConverter() xarray = converter(stream) center = True whitening_op = XArrayWhiten(taper=0.1, window=0.05, whiten_type='cross_component', freqmax=20.0, freqmin=0.01, center=center, order=2) whitened_array = whitening_op(xarray) xarray_squeezed = whitened_array[0, 0, :].data.squeeze() difference = xarray_squeezed[:center_index] - xarray_squeezed[ -center_index:] cumdiff = abs( np.cumsum(difference)[-1] / (signal_length * sampling_rate)) assert pytest.approx(0, abs=1e-4) == cumdiff
def test_read_whitening(self): anxcor = Anxcor() anxcor.set_window_length(120.0) times = anxcor.get_starttimes(starttime_stamp,endtime_stamp, 0.5) bank = WavebankWrapper(source_dir) anxcor.add_dataset(bank, 'nodals') anxcor.add_process(XArrayWhiten()) anxcor.save_at_process(target_dir, 'whiten:0') result = anxcor.process(times) anxcor = Anxcor() anxcor.set_window_length(120.0) bank = WavebankWrapper(source_dir) anxcor.add_dataset(bank, 'nodals') anxcor.add_process(XArrayWhiten()) anxcor.load_at_process(target_dir, 'whiten:0') result = anxcor.process(times) how_many_nc = _how_many_fmt(target_dir, format='.nc') _clean_files_in_dir(target_dir) assert 20 == how_many_nc
from anxcor.xarray_routines import XArrayWhiten, XArrayConverter, XArrayResample, XArrayRemoveMeanTrend # for travis build try: from tests.synthetic_trace_factory import create_sinsoidal_trace except: from synthetic_trace_factory import create_sinsoidal_trace from scipy.signal import correlate from obspy.clients.fdsn import Client import pytest from obspy.core import UTCDateTime, Stream import numpy as np import anxcor.filters as filts whiten = XArrayWhiten(window=0.1, freqmax=20.0, freqmin=0.001, order=2, rolling_metric='mean') convert = XArrayConverter() def plot_spectrum(xarray): import matplotlib.pyplot as plt freq_array = filts.xarray_time_2_freq(xarray) plt.figure() abs(freq_array.squeeze()).plot.line(x='frequency', xscale='log', yscale='log') plt.show()
from anxcor import anxcor_utils from anxcor.xarray_routines import XArrayWhiten, XArrayConverter, XArrayResample, XArrayRemoveMeanTrend # for travis build try: from tests.synthetic_trace_factory import create_sinsoidal_trace except: from synthetic_trace_factory import create_sinsoidal_trace from scipy.signal import correlate from obspy.clients.fdsn import Client import pytest from obspy.core import UTCDateTime, Stream import numpy as np import anxcor.filters as filts whiten = XArrayWhiten(window=0.1, upper_frequency=20.0, lower_frequency=0.001, order=2,rolling_metric='mean') convert = XArrayConverter() def plot_spectrum(xarray): import matplotlib.pyplot as plt freq_array = filts.xarray_time_2_freq(xarray) plt.figure() abs(freq_array.squeeze()).plot.line(x='frequency',xscale='log', yscale='log') plt.show() class TestSpectralWhitening(unittest.TestCase): def test_whitened_success(self): trace = convert(create_sinsoidal_trace(sampling_rate=100,period=0.5, duration=3))
try: from tests.synthetic_trace_factory import create_random_trace, create_sinsoidal_trace except Exception: from synthetic_trace_factory import create_random_trace, create_sinsoidal_trace # relative import #from synthetic_trace_factory import create_random_trace, create_sinsoidal_trace from obspy.core import read from scipy.signal import correlate from anxcor.xarray_routines import XArrayRemoveMeanTrend import pytest from obspy.clients.fdsn import Client from obspy.core import UTCDateTime import numpy as np import xarray as xr whiten = XArrayWhiten(smoothing_window_ratio=0.025, freqmax=25.0, freqmin=0.001, order=2) convert = XArrayConverter() xarraycorrelate = XArrayXCorrelate(max_tau_shift=40) source_file = 'tests/test_data/test_teleseism/test_teleseism.BHE.SAC' def source_earthquake(): earthquake_trace = read(source_file, format='sac')[0] earthquake_trace.stats.data_type = 'eq' earthquake_trace.data /= max(earthquake_trace.data) earthquake_trace.stats.starttime = 0 earthquake_trace.detrend(type='linear') earthquake_trace.detrend(type='constant') earthquake_trace.taper(max_percentage=0.05) earthquake_trace.stats.channel = 'Z'
# travis import try: from tests.synthetic_trace_factory import create_random_trace, create_sinsoidal_trace except Exception: from synthetic_trace_factory import create_random_trace, create_sinsoidal_trace # relative import #from synthetic_trace_factory import create_random_trace, create_sinsoidal_trace from obspy.core import read from scipy.signal import correlate from anxcor.xarray_routines import XArrayRemoveMeanTrend import pytest from obspy.clients.fdsn import Client from obspy.core import UTCDateTime import numpy as np import xarray as xr whiten = XArrayWhiten(smoothing_window_ratio=0.025, upper_frequency=25.0, lower_frequency=0.001, order=2) convert = XArrayConverter() xarraycorrelate = XArrayXCorrelate(max_tau_shift=40) source_file = 'tests/test_data/test_teleseism/test_teleseism.BHE.SAC' def source_earthquake(): earthquake_trace = read(source_file, format='sac')[0] earthquake_trace.stats.data_type='eq' earthquake_trace.data /= max(earthquake_trace.data) earthquake_trace.stats.starttime=0 earthquake_trace.stats.channel='Z' earthquake_trace.stats.station = 'test' earthquake_trace.stats.network = ''