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_jupyter_example(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() temp_normalizer = XArrayTemporalNorm(t_norm_type='reduce_metric', freqmin=0.001, freqmax=0.1, time_window=2.0, rolling_procedure='mean', reduce_metric='max') xarray = converter(st) resampled_array = resampler(xarray) rmm_array = rmmean_trend(resampled_array) t_normed_array = temp_normalizer(rmm_array) assert True
def test_variable_type(self): # first, make a noise trace and shift it by tau * sampling rate\ file = source_file duration = 400 shift = 30 noise_loc_1 = create_random_trace(sampling_rate=40, duration=duration) noise_loc_1[0].data += create_random_trace(sampling_rate=40, duration=duration)[0].data noise_loc_1 = convert(noise_loc_1) noise_loc_2 = xr.apply_ufunc(shift_trace, noise_loc_1, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={ 'time': shift, 'delta': noise_loc_1.attrs['delta'] }, keep_attrs=True) # next, add an eq teleseism from file to both noise streams noise_loc_1_eq = noise_loc_1.copy() down = XArrayResample(10) t_norm = XArrayTemporalNorm(time_window=2.0) noise_loc_2_eq = down(noise_loc_1_eq) noise_loc_2_eq_tnorm = t_norm(noise_loc_2_eq) assert noise_loc_2_eq_tnorm.dtype == np.float64
def test_read_resample(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(XArrayResample(target_rate=10.0)) anxcor.save_at_process(target_dir,'resample:0') result = anxcor.process(times) anxcor = Anxcor() anxcor.set_window_length(120.0) anxcor.add_process(XArrayResample(target_rate=10.0)) bank = WavebankWrapper(source_dir) anxcor.add_dataset(bank, 'nodals') anxcor.load_at_process(target_dir, 'resample: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
def test_increase_earthquake_shift_time_xcorr_func(self): # first, make a noise trace and shift it by tau * sampling rate\ file = source_file duration = 400 shift = 3 noise_loc_1 = create_random_trace(sampling_rate=40, duration=duration) noise_loc_1[0].data+= create_random_trace(sampling_rate=40, duration=duration)[0].data noise_loc_1 = convert(noise_loc_1,starttime=0,station=0) noise_loc_2 = xr.apply_ufunc(shift_trace, noise_loc_1, input_core_dims=[['time']], output_core_dims=[['time']], kwargs={'time': shift, 'delta': noise_loc_1.attrs['delta']}, keep_attrs=True) # next, add an eq teleseism from file to both noise streams noise_loc_1_eq = noise_loc_1.copy() noise_loc_2_eq = noise_loc_2.copy() attrs = noise_loc_1.attrs source_eq = source_earthquake() noise_loc_1_eq[:, :, :source_eq.data.shape[2]] += source_eq.data[:, :, :] noise_loc_2_eq[:, :, :source_eq.data.shape[2]] += source_eq.data[:, :, :] noise_loc_1.attrs = attrs noise_loc_2.attrs = attrs noise_loc_2_eq.attrs = attrs noise_loc_1_eq.attrs = attrs # downsample both to 10hz sampling rate down = XArrayResample(10) t_norm =XArrayTemporalNorm(time_window=2.0) noise_loc_1_eq = down(noise_loc_1_eq) noise_loc_2_eq = down(noise_loc_2_eq) noise_loc_2_eq_tnorm = t_norm(noise_loc_2_eq.copy()) noise_loc_1_eq_tnorm = t_norm(noise_loc_1_eq.copy()) x_corr_eq = self.max_corr_norm(noise_loc_1_eq, noise_loc_2_eq) x_corr_eq_tnorm= self.max_corr_norm(noise_loc_1_eq_tnorm, noise_loc_2_eq_tnorm) zero_index = len(x_corr_eq)//2 assert x_corr_eq[zero_index+int(40*shift)] < x_corr_eq_tnorm[zero_index+int(40*shift)]
def test_phase_shift_not_introduced(self): target_rate = 20 process = XArrayResample(target_rate=target_rate) trace_initial = converter( create_sinsoidal_trace(sampling_rate=100, period=0.5, duration=0.5)) trace_processed = converter( create_sinsoidal_trace(sampling_rate=100, period=0.5, duration=0.5)) trace_processed = process(trace_processed) target = np.argmax( trace_initial.data.ravel()) * trace_initial.attrs['delta'] source = np.argmax( trace_processed.data.ravel()) * trace_processed.attrs['delta'] assert round(abs(target - source), int(np.log10( 1 / target_rate))) == 0, "filter introduced phase shift"