def test_getWaveform(self): """ Tests getWaveform method. """ client = Client("pele.ess.washington.edu", 16017) start = UTCDateTime() - 24 * 3600 end = start + 30 # example 1 -- 1 channel, cleanup stream = client.getWaveform('UW', 'TUCA', '', 'BHZ', start, end) self.assertEquals(len(stream), 1) delta = stream[0].stats.delta trace = stream[0] self.assertTrue(len(trace) == 1201) self.assertTrue(trace.stats.starttime >= start - delta) self.assertTrue(trace.stats.starttime <= start + delta) self.assertTrue(trace.stats.endtime >= end - delta) self.assertTrue(trace.stats.endtime <= end + delta) self.assertEquals(trace.stats.network, 'UW') self.assertEquals(trace.stats.station, 'TUCA') self.assertEquals(trace.stats.location, '') self.assertEquals(trace.stats.channel, 'BHZ') # example 2 -- 1 channel, no cleanup stream = client.getWaveform('UW', 'TUCA', '', 'BHZ', start, end, cleanup=False) self.assertTrue(len(stream) >= 2) summed_length = array([len(trace) for trace in stream]).sum() self.assertTrue(summed_length == 1201) self.assertTrue(stream[0].stats.starttime >= start - delta) self.assertTrue(stream[0].stats.starttime <= start + delta) self.assertTrue(stream[-1].stats.endtime >= end - delta) self.assertTrue(stream[-1].stats.endtime <= end + delta) for trace in stream: self.assertEquals(trace.stats.network, 'UW') self.assertEquals(trace.stats.station, 'TUCA') self.assertEquals(trace.stats.location, '') self.assertEquals(trace.stats.channel, 'BHZ') # example 3 -- component wildcarded with '?' stream = client.getWaveform('UW', 'TUCA', '', 'BH?', start, end) self.assertEquals(len(stream), 3) for trace in stream: self.assertTrue(len(trace) == 1201) self.assertTrue(trace.stats.starttime >= start - delta) self.assertTrue(trace.stats.starttime <= start + delta) self.assertTrue(trace.stats.endtime >= end - delta) self.assertTrue(trace.stats.endtime <= end + delta) self.assertEquals(trace.stats.network, 'UW') self.assertEquals(trace.stats.station, 'TUCA') self.assertEquals(trace.stats.location, '') self.assertEquals(stream[0].stats.channel, 'BHZ') self.assertEquals(stream[1].stats.channel, 'BHN') self.assertEquals(stream[2].stats.channel, 'BHE')
def test_getWaveform(self): """ Tests getWaveform method. """ client = Client("pele.ess.washington.edu", 16017) start = UTCDateTime(2013, 1, 17) end = start + 30 # example 1 -- 1 channel, cleanup stream = client.getWaveform('UW', 'TUCA', '', 'BHZ', start, end) self.assertEqual(len(stream), 1) delta = stream[0].stats.delta trace = stream[0] self.assertTrue(len(trace) == 1201) self.assertTrue(trace.stats.starttime >= start - delta) self.assertTrue(trace.stats.starttime <= start + delta) self.assertTrue(trace.stats.endtime >= end - delta) self.assertTrue(trace.stats.endtime <= end + delta) self.assertEqual(trace.stats.network, 'UW') self.assertEqual(trace.stats.station, 'TUCA') self.assertEqual(trace.stats.location, '') self.assertEqual(trace.stats.channel, 'BHZ') # example 2 -- 1 channel, no cleanup stream = client.getWaveform('UW', 'TUCA', '', 'BHZ', start, end, cleanup=False) self.assertTrue(len(stream) >= 2) summed_length = array([len(tr) for tr in stream]).sum() self.assertTrue(summed_length == 1201) self.assertTrue(stream[0].stats.starttime >= start - delta) self.assertTrue(stream[0].stats.starttime <= start + delta) self.assertTrue(stream[-1].stats.endtime >= end - delta) self.assertTrue(stream[-1].stats.endtime <= end + delta) for trace in stream: self.assertEqual(trace.stats.network, 'UW') self.assertEqual(trace.stats.station, 'TUCA') self.assertEqual(trace.stats.location, '') self.assertEqual(trace.stats.channel, 'BHZ') # example 3 -- component wildcarded with '?' stream = client.getWaveform('UW', 'TUCA', '', 'BH?', start, end) self.assertEqual(len(stream), 3) for trace in stream: self.assertTrue(len(trace) == 1201) self.assertTrue(trace.stats.starttime >= start - delta) self.assertTrue(trace.stats.starttime <= start + delta) self.assertTrue(trace.stats.endtime >= end - delta) self.assertTrue(trace.stats.endtime <= end + delta) self.assertEqual(trace.stats.network, 'UW') self.assertEqual(trace.stats.station, 'TUCA') self.assertEqual(trace.stats.location, '') self.assertEqual(stream[0].stats.channel, 'BHZ') self.assertEqual(stream[1].stats.channel, 'BHN') self.assertEqual(stream[2].stats.channel, 'BHE')
class EarthwormWaveclient(WaveClient): ''' The earthworm waveserver client. This class provides the connector to a Earthworm waveserver. The client uses the :class:`obspy.earthworm.Client` class. ''' def __init__(self, name='earthworm waveserver client', host='localhost', port=16022, **kwargs): WaveClient.__init__(self, name=name, **kwargs) # The Earthworm waveserver host to which the client should connect. self.host = host # The port on which the Eartworm waveserver is running on host. self.port = port # The obspy earthworm waveserver client instance. self.client = Client(self.host, self.port, timeout=2) @property def pickle_attributes(self): ''' The attributes which can be pickled. ''' d = super(EarthwormWaveclient, self).pickle_attributes d['host'] = self.host d['port'] = self.port return d def getWaveform(self, startTime, endTime, scnl): ''' Get the waveform data for the specified parameters. Parameters ---------- startTime : UTCDateTime The begin datetime of the data to fetch. endTime : UTCDateTime The end datetime of the data to fetch. scnl : List of tuples The SCNL codes of the data to request. Returns ------- stream : :class:`obspy.core.Stream` The requested waveform data. All traces are packed into one stream. ''' from obspy.core import Stream self.logger.debug("Querying...") self.logger.debug('startTime: %s', startTime) self.logger.debug('endTime: %s', endTime) self.logger.debug("%s", scnl) stream = Stream() for curScnl in scnl: curStation = curScnl[0] curChannel = curScnl[1] curNetwork = curScnl[2] curLocation = curScnl[3] stock_stream = self.get_from_stock(station=curStation, channel=curChannel, network=curNetwork, location=curLocation, start_time=startTime, end_time=endTime) if len(stock_stream) > 0: cur_trace = stock_stream.traces[0] cur_start_time = cur_trace.stats.starttime cur_end_time = cur_trace.stats.starttime + cur_trace.stats.npts / cur_trace.stats.sampling_rate stream += stock_stream if startTime < cur_start_time: curStream = self.request_from_server( station=curStation, channel=curChannel, network=curNetwork, location=curLocation, start_time=startTime, end_time=cur_start_time) stream += curStream if cur_end_time < endTime: curStream = self.request_from_server( station=curStation, channel=curChannel, network=curNetwork, location=curLocation, start_time=cur_end_time, end_time=endTime) stream += curStream else: curStream = self.request_from_server(station=curStation, channel=curChannel, network=curNetwork, location=curLocation, start_time=startTime, end_time=endTime) stream += curStream stream.merge() self.add_to_stock(stream) return stream def request_from_server(self, station, network, channel, location, start_time, end_time): stream = Stream() try: self.logger.debug('Before getWaveform....') stream = self.client.getWaveform(network, station, location, channel, start_time, end_time) for cur_trace in stream: cur_trace.stats.unit = 'counts' self.logger.debug('got waveform: %s', stream) self.logger.debug('leave try') except Exception as e: self.logger.exception("Error connecting to waveserver: %s", e) return stream def preload(self, start_time, end_time, scnl): ''' Preload the data for the given timespan and the scnl. Preloading is done as a thread. ''' t = PreloadThread(name='daemon', start_time=start_time, end_time=end_time, scnl=scnl, target=self.getWaveform) t.setDaemon(True) t.start() self.preload_threads.append(t) return t