def test_xcorr(trace_length, sampling_rate, other_sampling_rate): # Testing x-correlation of traces with different sampling rates tr1 = Trace(data=np.sin( np.linspace(0, 20 * np.pi, trace_length * sampling_rate)), header=Stats( header={ 'sampling_rate': sampling_rate, 'npts': trace_length * sampling_rate, 'network': 'AU', 'station': 'A' })) tr2 = Trace(data=np.cos( np.linspace(0, 200 * np.pi, trace_length * other_sampling_rate)), header=Stats( header={ 'sampling_rate': other_sampling_rate, 'npts': trace_length * other_sampling_rate, 'network': 'AU', 'station': 'B' })) arr, _, _, _, _ = xcorr2(tr1, tr2, window_seconds=trace_length / 2, interval_seconds=trace_length) # Mean of the x-correlation function should be close to zero assert np.allclose(np.abs(np.mean(arr)), 0, atol=1e-2)
def test_casted_stats_nscl_writes_to_mseed(self): """ Ensure a Stream object that has had its nslc types cast to str can still be written. """ st = Stream(traces=read()[0]) # Get a new stats object with just the basic items in it stats_items = set(Stats()) new_stats = Stats() new_stats.__dict__.update({x: st[0].stats[x] for x in stats_items}) new_stats.network = 1 new_stats.station = 1.1 new_stats.channel = 'Non' st[0].stats = new_stats # try writing stream to bytes buffer bio = io.BytesIO() st.write(bio, 'mseed') bio.seek(0) # read bytes and compare stt = read(bio) # remove _mseed so streams can compare equal stt[0].stats.pop('mseed') del stt[0].stats._format # format gets added upon writing self.assertEqual(st, stt)
def test_update(self): """ Tests update method of Stats object. """ x = Stats({'a': 5}) assert 'a' in dir(x) x.update({'b': 5}) assert 'b' in dir(x) y = {'a': 5} y.update({'b': 5}) x = Stats(y) assert 'b' in dir(x)
def test_update(self): """ Tests update method of Stats object. """ x = Stats({'a': 5}) self.assertTrue('a' in dir(x)) x.update({'b': 5}) self.assertTrue('b' in dir(x)) y = {'a': 5} y.update({'b': 5}) x = Stats(y) self.assertTrue('b' in dir(x))
def read_ascii(path, NR, nt): from numpy import loadtxt from obspy.core import Stream, Stats, Trace dat_type = 'semd' comp1 = 'FXX' comp2 = 'FXY' stream = Stream() for rec_x in range(0,NR): file_name_in1 = path + 'P.R' + str(int(rec_x+1)) + '.' + comp1 + '.' + dat_type file_name_in2 = path + 'P.R' + str(int(rec_x+1)) + '.' + comp2 + '.' + dat_type xz1 = np.genfromtxt(file_name_in1) xz2 = np.genfromtxt(file_name_in2) deg = 0.0 alpha = np.arctan(xz2[:nt,1]/(1.0e-40 + xz1[:nt,1])) # angle of projection direction = np.sign(np.cos(deg*np.pi/180.0)*xz1[:nt,1]*np.cos(alpha) + np.sin(deg*np.pi/180.0)*xz2[:nt,1]*np.cos(alpha)) data = direction*np.sqrt(xz1[:nt,1]**2 + xz2[:nt,1]**2)*np.cos(alpha) # scalar radial component stats = Stats() stats.filename = path + 'P.R' + str(int(rec_x+1)) stats.starttime = xz1[0,0] stats.delta = xz1[1,0] - xz1[0,0] stats.npts = len(xz1[:nt,0]) try: parts = filename.split('.') stats.network = parts[0] stats.station = parts[1] stats.channel = temp[2] except: pass stream.append(Trace(data=data[:], header=stats)) return stream
def ascii(path, filename): """ Reads SPECFEM3D-style ASCII data :type path: str :param path: path to datasets :type filenames: list :param filenames: files to read """ st = Stream() stats = Stats() time, data = loadtxt(os.path.join(path, filename)).T stats.filename = filename stats.starttime = time[0] stats.delta = time[1] - time[0] stats.npts = len(data) try: parts = filename.split(".") stats.network = parts[0] stats.station = parts[1] stats.channel = parts[2] except: pass st.append(Trace(data=data, header=stats)) return st
def test_component(self): """ Test setting and getting of component. """ stats = Stats() # Channel with 3 characters stats.channel = 'HHZ' self.assertEqual(stats.component, 'Z') stats.component = 'L' self.assertEqual(stats.component, 'L') self.assertEqual(stats.channel, 'HHL') stats['component'] = 'Q' self.assertEqual(stats['component'], 'Q') self.assertEqual(stats.channel, 'HHQ') # Channel with 1 character as component stats.channel = 'N' stats.component = 'E' self.assertEqual(stats.channel, 'E') self.assertEqual(stats.component, 'E') # Channel with 0 characters stats.channel = '' self.assertEqual(stats.component, '') stats.component = 'Z' self.assertEqual(stats.channel, 'Z') # Components must be single character stats.channel = 'HHZ' with self.assertRaises(ValueError): stats.component = '' self.assertEqual(stats.channel, 'HHZ') with self.assertRaises(ValueError): stats.component = 'ZZ' self.assertEqual(stats.channel, 'HHZ')
def test_raw_input_client(): """edge_test.RawInputClient_test.test_raw_input_client()""" network = "NT" station = "BOU" channel = "MVH" location = "R0" data = [0, 1, 2, 3, 4, 5] starttime = UTCDateTime("2019-12-01") trace = Trace( numpy.array(data, dtype=numpy.float64), Stats({ "channel": channel, "delta": 60.0, "location": location, "network": network, "npts": len(data), "starttime": starttime, "station": station, }), ) client = MockRawInputClient( tag="tag", host="host", port="port", station=station, channel=channel, location=location, network=network, ) trace_send = EdgeFactory()._convert_trace_to_int(trace.copy()) client.send_trace("minute", trace_send) # verify data was sent assert_equal(len(client.last_send), 1)
def __create_trace( data, network="NT", station="BOU", channel="H", location="R0", data_interval="second", data_type="interval", ): """ Utility to create a trace containing the given numpy array. Parameters ---------- data: array The array to be inserted into the trace. Returns ------- obspy.core.Stream Stream containing the channel. """ stats = Stats() stats.starttime = UTCDateTime("2019-12-01") stats.delta = TimeseriesUtility.get_delta_from_interval(data_interval) stats.channel = channel stats.station = station stats.npts = len(data) stats.data_interval = data_interval stats.data_type = data_type numpy_data = numpy.array(data, dtype=numpy.float64) return Trace(numpy_data, stats)
def test_component(self): """ Test setting and getting of component. """ stats = Stats() # Channel with 3 characters stats.channel = 'HHZ' assert stats.component == 'Z' stats.component = 'L' assert stats.component == 'L' assert stats.channel == 'HHL' stats['component'] = 'Q' assert stats['component'] == 'Q' assert stats.channel == 'HHQ' # Channel with 1 character as component stats.channel = 'N' stats.component = 'E' assert stats.channel == 'E' assert stats.component == 'E' # Channel with 0 characters stats.channel = '' assert stats.component == '' stats.component = 'Z' assert stats.channel == 'Z' # Components must be single character stats.channel = 'HHZ' with pytest.raises(ValueError): stats.component = '' assert stats.channel == 'HHZ' with pytest.raises(ValueError): stats.component = 'ZZ' assert stats.channel == 'HHZ'
def test_deepcopy(self): """ Tests deepcopy method of Stats object. """ stats = Stats() stats.network = 'BW' stats['station'] = 'ROTZ' stats['other1'] = {'test1': '1'} stats['other2'] = AttribDict({'test2': '2'}) stats['other3'] = 'test3' stats2 = copy.deepcopy(stats) stats.network = 'CZ' stats.station = 'RJOB' assert stats2.__class__ == Stats assert stats2.network == 'BW' assert stats2.station == 'ROTZ' assert stats2.other1.test1 == '1' assert stats2.other1.__class__ == AttribDict assert len(stats2.other1) == 1 assert stats2.other2.test2 == '2' assert stats2.other2.__class__ == AttribDict assert len(stats2.other2) == 1 assert stats2.other3 == 'test3' assert stats.network == 'CZ' assert stats.station == 'RJOB'
def process_step(self, step, stream): """Filters stream for one step. Filters all traces in stream. Parameters ---------- step : array element step holding variables for one filtering operation stream : obspy.core.Stream stream of data to filter Returns ------- out : obspy.core.Stream stream containing 1 trace per original trace. """ # gather variables from step input_sample_period = step["input_sample_period"] output_sample_period = step["output_sample_period"] window = np.array(step["window"]) decimation = int(output_sample_period / input_sample_period) numtaps = len(window) window = window / sum(window) out = Stream() for trace in stream: filtered = self.firfilter(trace.data, window, decimation) stats = Stats(trace.stats) stats.starttime = stats.starttime + input_sample_period * (numtaps // 2) stats.delta = output_sample_period stats.npts = len(filtered) trace_out = self.create_trace(stats.channel, stats, filtered) out += trace_out return out
def create_trace(self, channel, stats, data): """Utility to create a new trace object. Parameters ---------- channel : str channel name. stats : obspy.core.Stats channel metadata to clone. data : numpy.array channel data. Returns ------- obspy.core.Trace trace containing data and metadata. """ stats = Stats(stats) if self.data_type is None: stats.data_type = 'adjusted' else: stats.data_type = self.data_type if self.data_type is None: stats.location = 'A0' else: stats.location = self.location trace = super(AdjustedAlgorithm, self).create_trace(channel, stats, data) return trace
def read_hdf5_header(dataset: h5py.Dataset) -> Stats: """ Takes an hdf5 dataset as input and returns the header of the CorrTrace. :param dataset: The dataset to be read from :type dataset: h5py.Dataset :return: The trace's header :rtype: Stats """ attrs = dataset.attrs time_keys = ['starttime', 'endtime', 'onset', 'event_time'] header = {} for key in attrs: if key in time_keys: try: header[key] = UTCDateTime(attrs[key]) except ValueError as e: # temporary fix of obspy's UTCDateTime issue. SHould be removed # as soon as they release version 1.23 if attrs[key][4:8] == '360T': new = list(attrs[key]) new[6] = '1' header[key] = UTCDateTime(''.join(new)) - 86400 else: raise e elif key == 'processing': header[key] = list(attrs[key]) else: header[key] = attrs[key] return Stats(header)
def test_deepcopy(self): """ Tests deepcopy method of Stats object. """ stats = Stats() stats.network = 'BW' stats['station'] = 'ROTZ' stats['other1'] = {'test1': '1'} stats['other2'] = AttribDict({'test2': '2'}) stats['other3'] = 'test3' stats2 = copy.deepcopy(stats) stats.network = 'CZ' stats.station = 'RJOB' self.assertEqual(stats2.__class__, Stats) self.assertEqual(stats2.network, 'BW') self.assertEqual(stats2.station, 'ROTZ') self.assertEqual(stats2.other1.test1, '1') self.assertEqual(stats2.other1.__class__, AttribDict) self.assertEqual(len(stats2.other1), 1) self.assertEqual(stats2.other2.test2, '2') self.assertEqual(stats2.other2.__class__, AttribDict) self.assertEqual(len(stats2.other2), 1) self.assertEqual(stats2.other3, 'test3') self.assertEqual(stats.network, 'CZ') self.assertEqual(stats.station, 'RJOB')
def process(self, stream): """Run algorithm for a stream. Processes all traces in the stream. Parameters ---------- stream : obspy.core.Stream stream of data to process Returns ------- out : obspy.core.Stream stream containing 1 trace per original trace. """ out = Stream() for trace in stream: data = trace.data step = self.decimation filtered = self.firfilter(data, self.window, step) stats = Stats(trace.stats) # stats.channel = trace_chan_dict2[stats.channel] stats.starttime = trace.stats.starttime + \ self.numtaps * self.sample_period // 2 stats.delta = stats.delta * step # stats.processing.append('[Gaussian Filter]') stats.npts = filtered.shape[0] trace_out = self.create_trace(stats.channel, stats, filtered) out += trace_out return out
def ascii(path, filenames): from numpy import loadtxt from obspy.core import Stream, Stats, Trace stream = Stream() for filename in filenames: stats = Stats() data = loadtxt(path + '/' + filename) stats.filename = filename stats.starttime = data[0, 0] stats.sampling_rate = data[0, 1] - data[0, 0] stats.npts = len(data[:, 0]) try: parts = filename.split('.') stats.network = parts[0] stats.station = parts[1] stats.channel = temp[2] except: pass stream.append(Trace(data=data[:, 1], header=stats)) return stream
def process(self, stream): """ Run algorithm for a stream. Processes all traces in the stream. Parameters ---------- stream : obspy.core.Stream stream of data to process Returns ------- out : obspy.core.Stream stream containing 1 trace per original trace. """ out = Stream() for trace in stream: dbdt = np.around(np.diff(trace.data), decimals=6) stats = Stats(trace.stats) stats.channel = "{}_DT".format(stats.channel) trace_out = create_empty_trace( starttime=stats.starttime + stats.delta, endtime=stats.endtime, observatory=stats.station, type=stats.location, interval=get_interval_from_delta(stats.delta), channel=stats.channel, network=stats.network, station=stats.station, location=stats.location, ) trace_out.data = dbdt out += trace_out return out
def test_nice_ringlaser_metadata_error_msg(self): with self.assertRaises(TypeError) as e: PPSD(stats=Stats(), metadata=Inventory(networks=[], source=""), special_handling='ringlaser') expected = ("When using `special_handling='ringlaser'`, `metadata` " "must be a plain dictionary with key 'sensitivity' " "stating the overall sensitivity`.") self.assertEqual(str(e.exception), expected)
def _create_trace(data, channel, starttime, delta=60.0): stats = Stats() stats.channel = channel stats.delta = delta stats.starttime = starttime stats.npts = len(data) data = numpy.array(data, dtype=numpy.float64) return Trace(data, stats)
def test_nice_ringlaser_metadata_error_msg(self): expected = ("When using `special_handling='ringlaser'`, `metadata` " "must be a plain dictionary with key 'sensitivity' " "stating the overall sensitivity`.") with pytest.raises(TypeError, match=re.escape(expected)): PPSD(stats=Stats(), metadata=Inventory(networks=[], source=""), special_handling='ringlaser')
def test_nestedStats(self): """ Various setter and getter tests. """ #1 stats = Stats() stats.test = dict() stats.test['test2'] = 'muh' self.assertEqual(stats.test.test2, 'muh') self.assertEqual(stats.test['test2'], 'muh') self.assertEqual(stats['test'].test2, 'muh') self.assertEqual(stats['test']['test2'], 'muh') stats.test['test2'] = 'maeh' self.assertEqual(stats.test.test2, 'maeh') self.assertEqual(stats.test['test2'], 'maeh') self.assertEqual(stats['test'].test2, 'maeh') self.assertEqual(stats['test']['test2'], 'maeh') #2 - multiple initialization stats = Stats({'muh': 'meah'}) stats2 = Stats(Stats(Stats(stats))) self.assertEqual(stats2.muh, 'meah') #3 - check conversion to AttribDict stats = Stats() stats.sub1 = {'muh': 'meah'} stats.sub2 = AttribDict({'muh2': 'meah2'}) stats2 = Stats(stats) self.assertTrue(isinstance(stats.sub1, AttribDict)) self.assertTrue(isinstance(stats.sub2, AttribDict)) self.assertEqual(stats2.sub1.muh, 'meah') self.assertEqual(stats2.sub2.muh2, 'meah2')
def test_nested_stats(self): """ Various setter and getter tests. """ # 1 stats = Stats() stats.test = dict() stats.test['test2'] = 'muh' assert stats.test.test2 == 'muh' assert stats.test['test2'] == 'muh' assert stats['test'].test2 == 'muh' assert stats['test']['test2'] == 'muh' stats.test['test2'] = 'maeh' assert stats.test.test2 == 'maeh' assert stats.test['test2'] == 'maeh' assert stats['test'].test2 == 'maeh' assert stats['test']['test2'] == 'maeh' # 2 - multiple initialization stats = Stats({'muh': 'meah'}) stats2 = Stats(Stats(Stats(stats))) assert stats2.muh == 'meah' # 3 - check conversion to AttribDict stats = Stats() stats.sub1 = {'muh': 'meah'} stats.sub2 = AttribDict({'muh2': 'meah2'}) stats2 = Stats(stats) assert isinstance(stats.sub1, AttribDict) assert isinstance(stats.sub2, AttribDict) assert stats2.sub1.muh == 'meah' assert stats2.sub2.muh2 == 'meah2'
def test_nscl_cannot_be_none(self): """ Ensure the nslc values can't be assigned to None but rather None gets converted to a str """ stats = Stats() for val in self.nslc: setattr(stats, val, None) self.assertEqual(getattr(stats, val), 'None')
def test_nscl_cannot_be_none(self): """ Ensure the nslc values can't be assigned to None but rather None gets converted to a str """ stats = Stats() for val in self.nslc: with warnings.catch_warnings(record=True): setattr(stats, val, None) assert getattr(stats, val) == 'None'
def test_nscl_cannot_be_none(self): """ Ensure the nslc values can't be assigned to None but rather None gets converted to a str """ stats = Stats() for val in self.nslc: with pytest.warns(UserWarning): setattr(stats, val, None) assert getattr(stats, val) == 'None'
def test_ppsd_time_checks(self): """ Some tests that make sure checking if a new PSD slice to be addded to existing PPSD has an invalid overlap or not works as expected. """ ppsd = PPSD(Stats(), Response()) one_second = 1000000000 t0 = 946684800000000000 # 2000-01-01T00:00:00 time_diffs = [ 0, one_second, one_second * 2, one_second * 3, one_second * 8, one_second * 9, one_second * 10 ] ppsd._times_processed = [t0 + td for td in time_diffs] ppsd.ppsd_length = 2 ppsd.overlap = 0.5 # valid time stamps to insert data for (i.e. data that overlaps with # existing data at most "overlap" times "ppsd_length") ns_ok = [ t0 - 3 * one_second, t0 - 1.01 * one_second, t0 - one_second, t0 + 4 * one_second, t0 + 4.01 * one_second, t0 + 6 * one_second, t0 + 7 * one_second, t0 + 6.99 * one_second, t0 + 11 * one_second, t0 + 11.01 * one_second, t0 + 15 * one_second, ] for ns in ns_ok: t = UTCDateTime(ns=int(ns)) # getting False means time is not present yet and a PSD slice would # be added to the PPSD data self.assertFalse(ppsd._PPSD__check_time_present(t)) # invalid time stamps to insert data for (i.e. data that overlaps with # existing data more than "overlap" times "ppsd_length") ns_bad = [ t0 - 0.99 * one_second, t0 - 0.5 * one_second, t0, t0 + 1.1 * one_second, t0 + 3.99 * one_second, t0 + 7.01 * one_second, t0 + 7.5 * one_second, t0 + 8 * one_second, t0 + 8.8 * one_second, t0 + 10 * one_second, t0 + 10.99 * one_second, ] for ns in ns_bad: t = UTCDateTime(ns=int(ns)) # getting False means time is not present yet and a PSD slice would # be added to the PPSD data self.assertTrue(ppsd._PPSD__check_time_present(t))
def test_ppsd_add_npz(self): """ Test PPSD.add_npz(). """ # set up a bogus PPSD, with fixed random psds but with real start times # of psd pieces, to facilitate testing the stack selection. ppsd = PPSD(stats=Stats(dict(sampling_rate=150)), metadata=None, db_bins=(-200, -50, 20.), period_step_octaves=1.4) _times_processed = np.load( os.path.join(self.path, "ppsd_times_processed.npy")).tolist() # change data to nowadays used nanoseconds POSIX timestamp _times_processed = [UTCDateTime(t)._ns for t in _times_processed] np.random.seed(1234) _binned_psds = [ arr for arr in np.random.uniform(-200, -50, ( len(_times_processed), len(ppsd.period_bin_centers))) ] with NamedTemporaryFile(suffix=".npz") as tf1, \ NamedTemporaryFile(suffix=".npz") as tf2, \ NamedTemporaryFile(suffix=".npz") as tf3: # save data split up over three separate temporary files ppsd._times_processed = _times_processed[:200] ppsd._binned_psds = _binned_psds[:200] ppsd.save_npz(tf1.name) ppsd._times_processed = _times_processed[200:400] ppsd._binned_psds = _binned_psds[200:400] ppsd.save_npz(tf2.name) ppsd._times_processed = _times_processed[400:] ppsd._binned_psds = _binned_psds[400:] ppsd.matplotlib_version = "X.X.X" ppsd.save_npz(tf3.name) # now load these saved npz files and check if all data is present ppsd = PPSD.load_npz(tf1.name, metadata=None) ppsd.add_npz(tf2.name) # we changed a version number so this should emit a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ppsd.add_npz(tf3.name) self.assertEqual(len(w), 1) np.testing.assert_array_equal(_binned_psds, ppsd._binned_psds) np.testing.assert_array_equal(_times_processed, ppsd._times_processed) # adding data already present should also emit a warning and the # PPSD should not be changed with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ppsd.add_npz(tf2.name) self.assertEqual(len(w), 1) np.testing.assert_array_equal(_binned_psds, ppsd._binned_psds) np.testing.assert_array_equal(_times_processed, ppsd._times_processed)
def stats(self): return Stats({ 'network': self.net, 'location': self.location, 'station': self.station, 'channel': self.channel, 'starttime': self.starttime, 'sampling_rate': self.rate, 'npts': self.num_samples, 'calib': self.calib })
def test_compare_with_dict(self): """ Checks if Stats is still comparable to a dict object. """ adict = { 'network': '', 'sampling_rate': 1.0, 'test': 1, 'station': '', 'location': '', 'starttime': UTCDateTime(1970, 1, 1, 0, 0), 'delta': 1.0, 'calib': 1.0, 'npts': 0, 'endtime': UTCDateTime(1970, 1, 1, 0, 0), 'channel': ''} ad = Stats(adict) self.assertEqual(ad, adict) self.assertEqual(adict, ad)