def test_convert_is_accurate(self): max_tau_shift = None correlator = XArrayXCorrelate(max_tau_shift=max_tau_shift) e2 = create_sinsoidal_trace_w_decay(decay=0.8, station='k', network='v', channel='e', duration=20) n2 = create_random_trace(station='k', network='v', channel='n', duration=20) z2 = create_sinsoidal_trace_w_decay(decay=0.3, station='k', network='v', channel='z', duration=20) b2 = create_random_trace(station='k', network='v', channel='b', duration=20) new_traces = e2.traces + n2.traces + z2.traces + b2.traces syth_trace2 = converter(new_traces) result_1 = syth_trace2.loc['e', :, :].data.ravel() - e2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['n', :, :].data.ravel() - n2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['z', :, :].data.ravel() - z2[0].data assert 0 == np.sum(result_1) result_1 = syth_trace2.loc['b', :, :].data.ravel() - b2[0].data assert 0 == np.sum(result_1)
def test_shift_trace_time_slice(self): max_tau_shift = 10.0 correlator = XArrayXCorrelate(max_tau_shift=max_tau_shift) time_shift = 4 synth_trace1 = converter( create_sinsoidal_trace_w_decay(decay=0.6, station='h', network='v', channel='z', duration=20)) synth_trace2 = converter( create_sinsoidal_trace_w_decay(decay=0.4, station='h', network='w', channel='z', duration=20)) synth_trace2 = xr.apply_ufunc(shift_trace, synth_trace2, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={ 'time': time_shift, 'delta': synth_trace2.attrs['delta'] }, keep_attrs=True) correlation = correlator(synth_trace1, synth_trace2) correlation /= correlation.max() time_array = correlation.coords['time'].values max_index = np.argmax(correlation.data) tau_shift = (time_array[max_index] - np.datetime64( UTCDateTime(0.0).datetime)) / np.timedelta64(1, 's') assert tau_shift == -time_shift, 'failed to correctly identify tau shift'
def create_example_xarrays_missing_channel(duration=20): e1 = create_sinsoidal_trace_w_decay(decay=0.9, station='h', network='v', channel='e', duration=duration) z1 = create_sinsoidal_trace_w_decay(decay=0.4, station='h', network='v', channel='z', duration=duration) e2 = create_sinsoidal_trace_w_decay(decay=0.8, station='k', network='v', channel='e', duration=duration) n2 = create_sinsoidal_trace_w_decay(decay=0.7, station='k', network='v', channel='n', duration=duration) z2 = create_sinsoidal_trace_w_decay(decay=0.6, station='k', network='v', channel='z', duration=duration) syth_trace1 = converter(e1.copy() + z1.copy()) syth_trace2 = converter(e2.copy() + z2.copy() + n2.copy()) return syth_trace1, syth_trace2
def test_into_freq_and_back_phase_shift(self): stream = synthfactory.create_sinsoidal_trace_w_decay( sampling_rate=40.0, duration=1000.0, period=0.5) xarray = convert(stream) xarray_freq = filt_ops.xarray_time_2_freq(xarray) xarray_time = filt_ops.xarray_freq_2_time(xarray_freq, xarray) a = xarray.data[0, 0, :] b = xarray_time.data[0, 0, :] 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_taper_phase_shift(self): stream = synthfactory.create_sinsoidal_trace_w_decay( sampling_rate=40.0, duration=1000.0, period=0.5) xarray = convert(stream) filtered_array = xr.apply_ufunc(filt_ops.taper_func, xarray, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={'taper': 0.1}, keep_attrs=True) a = xarray.data[0, 0, :] b = filtered_array.data[0, 0, :] 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