示例#1
0
 def on_seedlink_error(self):
     self._EasySeedLinkClient__streaming_started = False
     self.streams = self.conn.streams.copy()
     del self.conn
     self.conn = SeedLinkConnection(timeout=30)
     self.conn.set_sl_address('%s:%d' %
                              (self.server_hostname, self.server_port))
     self.conn.multistation = True
     self.conn.streams = self.streams.copy()
示例#2
0
    def test_issue777(self):
        """
        Regression tests for Github issue #777
        """
        conn = SeedLinkConnection()

        # Check adding multiple streams (#3)
        conn.add_stream('BW', 'RJOB', 'EHZ', seqnum=-1, timestamp=None)
        conn.add_stream('BW', 'RJOB', 'EHN', seqnum=-1, timestamp=None)
        self.assertFalse(isinstance(conn.streams[0].get_selectors()[1], list))

        # Check if the correct Exception is raised (#4)
        try:
            conn.negotiate_station(SLNetStation('BW', 'RJOB', None, None,
                                                None))
        except Exception as e:
            self.assertTrue(isinstance(e, SeedLinkException))

        # Test if calling add_stream() with selectors_str=None still raises
        # (#5)
        try:
            conn.add_stream('BW', 'RJOB', None, seqnum=-1, timestamp=None)
        except AttributeError:
            msg = 'Calling add_stream with selectors_str=None raised ' + \
                  'AttributeError'
            self.fail(msg)
示例#3
0
    def test_issue777(self):
        """
        Regression tests for Github issue #777
        """
        conn = SeedLinkConnection()

        # Check adding multiple streams (#3)
        conn.add_stream('BW', 'RJOB', 'EHZ', seqnum=-1, timestamp=None)
        conn.add_stream('BW', 'RJOB', 'EHN', seqnum=-1, timestamp=None)
        self.assertFalse(isinstance(conn.streams[0].get_selectors()[1], list))

        # Check if the correct Exception is raised (#4)
        try:
            conn.negotiate_station(SLNetStation('BW', 'RJOB', None,
                                                None, None))
        except Exception as e:
            self.assertTrue(isinstance(e, SeedLinkException))

        # Test if calling add_stream() with selectors_str=None still raises
        # (#5)
        try:
            conn.add_stream('BW', 'RJOB', None, seqnum=-1, timestamp=None)
        except AttributeError:
            msg = 'Calling add_stream with selectors_str=None raised ' + \
                  'AttributeError'
            self.fail(msg)
示例#4
0
    def run(self):

        if self.data_retrieval is False:
            while True:
                try:
                    with open(BUFFER_DIR+'/streams.data', 'r') as file:
                        new_streams = file.read().splitlines()
                        if self.streams != new_streams:
                            self._EasySeedLinkClient__streaming_started = False
                            # streams = self.conn.streams.copy()
                            del self.conn
                            self.conn = SeedLinkConnection(timeout=30)
                            self.conn.set_sl_address('%s:%d' %
                                                     (self.server_hostname, self.server_port))
                            self.conn.multistation = True
                            for station in new_streams[1:]:
                                full_sta_name = station.split('.')
                                net = full_sta_name[0]
                                sta = full_sta_name[1]
                                cha = full_sta_name[2] + full_sta_name[3]
                                self.select_stream(net, sta, cha)

                            self.streams = new_streams.copy()

                except FileNotFoundError:
                    print('WAITING FOR STREAM FILE OF MONA-LISA')
                    time.sleep(5)
                    if self.data_retrieval:
                        self.on_terminate()
                        break
                else:
                    if self.data_retrieval:
                        self.on_terminate()
                        break

                    data = self.conn.collect()

                    if data == SLPacket.SLTERMINATE:
                        self.on_terminate()
                        continue
                    elif data == SLPacket.SLERROR:
                        self.on_seedlink_error()
                        continue

                    # At this point the received data should be a SeedLink packet
                    # XXX In SLClient there is a check for data == None, but I think
                    #     there is no way that self.conn.collect() can ever return None
                    assert(isinstance(data, SLPacket))

                    packet_type = data.get_type()

                    # Ignore in-stream INFO packets (not supported)
                    if packet_type not in (SLPacket.TYPE_SLINF, SLPacket.TYPE_SLINFT):
                        # The packet should be a data packet
                        trace = data.get_trace()
                        # Pass the trace to the on_data callback
                        self.on_data(trace)
        elif self.begin_time is not None and self.end_time is not None:
            try:
                with open(BUFFER_DIR + '/streams.data', 'r') as file:
                    new_streams = file.read().splitlines()
                    self.on_terminate()
                    for station in new_streams[1:]:
                        full_sta_name = station.split('.')
                        net = full_sta_name[0]
                        sta = full_sta_name[1]
                        cha = full_sta_name[2] + full_sta_name[3]
                        self.select_stream(net, sta, cha)

            except FileNotFoundError:
                print('WAITING FOR STREAM FILE OF MONA-LISA')
                time.sleep(5)
            else:
                self.conn.set_begin_time(self.begin_time)
                self.conn.set_end_time(self.end_time)
                data = self.conn.collect()
                while data is not None:
                    if data == SLPacket.SLTERMINATE:
                        self.on_terminate()
                        continue

                    elif data == SLPacket.SLERROR:
                        self.on_seedlink_error()
                        continue
                    else:

                    # At this point the received data should be a SeedLink packet
                    # XXX In SLClient there is a check for data == None, but I think
                    #     there is no way that self.conn.collect() can ever return None
                        assert (isinstance(data, SLPacket))

                        packet_type = data.get_type()

                        # Ignore in-stream INFO packets (not supported)
                        if packet_type not in (SLPacket.TYPE_SLINF, SLPacket.TYPE_SLINFT):
                            # The packet should be a data packet
                            trace = data.get_trace()
                            # Pass the trace to the on_data callback
                            self.on_data(trace)
                        data = self.conn.collect()
示例#5
0
class EasySLC(EasySeedLinkClient):
    def __init__(self, server_url, network_list, network_list_values,
                 data_retrieval=False, begin_time=None, end_time=None):
        self.network_list_values = network_list_values
        try:
            super(EasySLC, self).__init__(server_url, autoconnect=True)
            self.conn.timeout = 30
            self.data_retrieval = data_retrieval
            self.begin_time = begin_time
            self.end_time = end_time
            self.connected = 0

            self.connected = get_network_list('server', network_list, network_list_values,
                                              server_hostname=self.server_hostname, server_port=self.server_port)

            print(network_list_values)

            self.streams = []

        except SeedLinkException:
            pass

    def on_data(self, tr):
        print(tr)
        t_start = UTCDateTime()
        if tr is not None and t_start - tr.stats.starttime <= 300:

            tr.resample(sampling_rate=SAMPLING_RATE)
            tr.detrend(type='constant')

            if tr.stats.location == '':
                station = tr.stats.network + '.' + tr.stats.station + '..' + tr.stats.channel
            else:
                station = tr.stats.network + '.' + tr.stats.station + '.' + tr.stats.location + '.' + tr.stats.channel

            x = pd.to_datetime(tr.times('timestamp'), unit='s')
            new_data_sta = pd.DataFrame({'Date': x, 'Data_Sta': tr.data})
            try:
                data_sta = pd.read_feather(BUFFER_DIR + '/' + station + '.data')
                if len(data_sta) <= 4500:
                    data_sta = pd.concat([data_sta, new_data_sta])
                else:
                    data_sta = pd.concat([data_sta[round(-len(data_sta) / 2):], new_data_sta])
            except FileNotFoundError:
                data_sta = new_data_sta
            data_sta = data_sta.reset_index(drop=True)
            data_sta.to_feather(BUFFER_DIR + '/' + station + '.data')

        elif t_start - tr.stats.starttime > 300:
            print(f'blockette is too old ({(t_start - tr.stats.starttime) / 60} min).')
        else:
            print("blockette contains no trace")

    def run(self):

        if self.data_retrieval is False:
            while True:
                try:
                    with open(BUFFER_DIR+'/streams.data', 'r') as file:
                        new_streams = file.read().splitlines()
                        if self.streams != new_streams:
                            self._EasySeedLinkClient__streaming_started = False
                            # streams = self.conn.streams.copy()
                            del self.conn
                            self.conn = SeedLinkConnection(timeout=30)
                            self.conn.set_sl_address('%s:%d' %
                                                     (self.server_hostname, self.server_port))
                            self.conn.multistation = True
                            for station in new_streams[1:]:
                                full_sta_name = station.split('.')
                                net = full_sta_name[0]
                                sta = full_sta_name[1]
                                cha = full_sta_name[2] + full_sta_name[3]
                                self.select_stream(net, sta, cha)

                            self.streams = new_streams.copy()

                except FileNotFoundError:
                    print('WAITING FOR STREAM FILE OF MONA-LISA')
                    time.sleep(5)
                    if self.data_retrieval:
                        self.on_terminate()
                        break
                else:
                    if self.data_retrieval:
                        self.on_terminate()
                        break

                    data = self.conn.collect()

                    if data == SLPacket.SLTERMINATE:
                        self.on_terminate()
                        continue
                    elif data == SLPacket.SLERROR:
                        self.on_seedlink_error()
                        continue

                    # At this point the received data should be a SeedLink packet
                    # XXX In SLClient there is a check for data == None, but I think
                    #     there is no way that self.conn.collect() can ever return None
                    assert(isinstance(data, SLPacket))

                    packet_type = data.get_type()

                    # Ignore in-stream INFO packets (not supported)
                    if packet_type not in (SLPacket.TYPE_SLINF, SLPacket.TYPE_SLINFT):
                        # The packet should be a data packet
                        trace = data.get_trace()
                        # Pass the trace to the on_data callback
                        self.on_data(trace)
        elif self.begin_time is not None and self.end_time is not None:
            try:
                with open(BUFFER_DIR + '/streams.data', 'r') as file:
                    new_streams = file.read().splitlines()
                    self.on_terminate()
                    for station in new_streams[1:]:
                        full_sta_name = station.split('.')
                        net = full_sta_name[0]
                        sta = full_sta_name[1]
                        cha = full_sta_name[2] + full_sta_name[3]
                        self.select_stream(net, sta, cha)

            except FileNotFoundError:
                print('WAITING FOR STREAM FILE OF MONA-LISA')
                time.sleep(5)
            else:
                self.conn.set_begin_time(self.begin_time)
                self.conn.set_end_time(self.end_time)
                data = self.conn.collect()
                while data is not None:
                    if data == SLPacket.SLTERMINATE:
                        self.on_terminate()
                        continue

                    elif data == SLPacket.SLERROR:
                        self.on_seedlink_error()
                        continue
                    else:

                    # At this point the received data should be a SeedLink packet
                    # XXX In SLClient there is a check for data == None, but I think
                    #     there is no way that self.conn.collect() can ever return None
                        assert (isinstance(data, SLPacket))

                        packet_type = data.get_type()

                        # Ignore in-stream INFO packets (not supported)
                        if packet_type not in (SLPacket.TYPE_SLINF, SLPacket.TYPE_SLINFT):
                            # The packet should be a data packet
                            trace = data.get_trace()
                            # Pass the trace to the on_data callback
                            self.on_data(trace)
                        data = self.conn.collect()

    # ADAPT
    def on_terminate(self):
        self._EasySeedLinkClient__streaming_started = False
        streams = self.conn.streams.copy()
        del self.conn
        self.conn = SeedLinkConnection(timeout=30)
        self.conn.set_sl_address('%s:%d' %
                                 (self.server_hostname, self.server_port))
        self.conn.multistation = True
        self.conn.streams = streams.copy()
        # if self.data_retrieval is False:
        #     self.connect()

        # self.conn.begin_time = UTCDateTime()

    def on_seedlink_error(self):
        self._EasySeedLinkClient__streaming_started = False
        self.streams = self.conn.streams.copy()
        del self.conn
        self.conn = SeedLinkConnection(timeout=30)
        self.conn.set_sl_address('%s:%d' %
                                 (self.server_hostname, self.server_port))
        self.conn.multistation = True
        self.conn.streams = self.streams.copy()