# Source code for obspy.clients.seedlink.basic_client

    # -*- coding: utf-8 -*-
    """
    SeedLink request client for ObsPy.

    :copyright:
        The ObsPy Development Team (devs@obspy.org)
    :license:
        GNU Lesser General Public License, Version 3
        (https://www.gnu.org/copyleft/lesser.html)
    """
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    from future.builtins import *  # NOQA @UnusedWildImport

    import warnings

    from obspy import Stream
    from .slclient import SLClient, SLPacket
    from .client.seedlinkconnection import SeedLinkConnection

    class Client(object):
        """
        SeedLink request client.

        This client is intended for requests of specific, finite time windows.
        To work with continuous realtime data streams please see
        :class:`~obspy.clients.seedlink.slclient.SLClient` and
        :class:`~obspy.clients.seedlink.easyseedlink.EasySeedLinkClient`.

        :type server: str
        :param server: Server name or IP address to connect to (e.g.
            "localhost", "rtserver.ipgp.fr")
        :type port: int
        :param port: Port at which the seedlink server is operating (default is
            `18000`).
        :type timeout: float
        :param timeout: Network timeout for low-level network connection in
            seconds.
        :type debug: bool
        :param debug: Switches on debugging output.
        """
        def __init__(self, server, port=18000, timeout=20, debug=False):
            """
            Initializes the SeedLink request client.
            """
            self.timeout = timeout
            self.debug = debug
            self._slclient = SLClient(loglevel=debug and "DEBUG" or "CRITICAL",
                                      timeout=self.timeout)
            self._server_url = "%s:%i" % (server, port)

        def _connect(self):
            """
            Open new connection to seedlink server.
            """
            self._slclient.slconn = SeedLinkConnection(timeout=self.timeout)
            self._slclient.slconn.set_sl_address(self._server_url)
            self._slclient.slconn.netto = self.timeout

        def get_waveforms(self, network, station, location, channel, starttime,
                          endtime):
            """
            Request waveform data from the seedlink server.

            >>> from obspy import UTCDateTime
            >>> client = Client('rtserver.ipgp.fr')
            >>> t = UTCDateTime() - 1500
            >>> st = client.get_waveforms("G", "FDF", "00", "BHZ", t, t + 5)
            >>> print(st)  # doctest: +ELLIPSIS
            1 Trace(s) in Stream:
            G.FDF.00.BHZ | 20... | 20.0 Hz, ... samples

            Most servers support '?' single-character wildcard in location and
            channel code fields:

            >>> st = client.get_waveforms("G", "FDF", "??", "B??", t, t + 5)
            >>> st = st.sort(reverse=True)
            >>> print(st)  # doctest: +ELLIPSIS
            3 Trace(s) in Stream:
            G.FDF.00.BHZ | 20... | 20.0 Hz, ... samples
            G.FDF.00.BHN | 20... | 20.0 Hz, ... samples
            G.FDF.00.BHE | 20... | 20.0 Hz, ... samples

            .. note::

                Support of wildcards strongly depends on the queried seedlink
                server. In general, '?' as single character wildcard seems to work
                well in location code and channel code fields for most servers.
                Usage of '*' for multiple characters in location and channel code
                field is not supported. No wildcards are supported in
                network and station code fields by ObsPy.

            :type network: str
            :param network: Network code. No wildcards supported by ObsPy.
            :type station: str
            :param station: Station code. No wildcards supported by ObsPy.
            :type location: str
            :param location: Location code. See note on wildcards above.
            :type channel: str
            :param channel: Channel code. See note on wildcards above.
            :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime`
            :param starttime: Start time of requested time window.
            :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime`
            :param endtime: End time of requested time window.
            """
            if len(location) > 2:
                msg = ("Location code ('%s') only supports a maximum of 2 "
                       "characters.") % location
                raise ValueError(msg)
            elif len(location) == 1:
                msg = "Single character location codes are untested."
                warnings.warn(msg)
            if location:
                loccha = "%2s%3s" % (location, channel)
            else:
                loccha = channel
            seedlink_id = "%s_%s:%s" % (network, station, loccha)
            self._slclient.multiselect = seedlink_id
            self._slclient.begin_time = starttime
            self._slclient.end_time = endtime
            self._connect()
            self._slclient.initialize()
            self.stream = Stream()
            self._slclient.run(packet_handler=self._packet_handler)
            stream = self.stream
            stream.trim(starttime, endtime)
            self.stream = None
            return stream

        def _packet_handler(self, count, slpack):
            """
            Custom packet handler that accumulates all waveform packets in a
            stream.
            """
            # check if not a complete packet
            if slpack is None or (slpack == SLPacket.SLNOPACKET) or \
                    (slpack == SLPacket.SLERROR):
                return False

            # get basic packet info
            type_ = slpack.get_type()
            if self.debug:
                print(type_)

            # process INFO packets here
            if type_ == SLPacket.TYPE_SLINF:
                if self.debug:
                    print(SLPacket.TYPE_SLINF)
                return False
            elif type_ == SLPacket.TYPE_SLINFT:
                if self.debug:
                    print("Complete INFO:" + self.slconn.get_info_string())
                return False

            # process packet data
            trace = slpack.get_trace()
            if trace is None:
                if self.debug:
                    print("Blockette contains no trace")
                return False

            # new samples add to the main stream which is then trimmed
            self.stream += trace
            self.stream.merge(-1)
            return False

    if __name__ == '__main__':
        import doctest
        doctest.testmod(exclude_empty=True)

### Thank you!

We would like to thank our contributors, whose efforts make this software what it is. These people have helped by writing code and documentation, and by testing. They have created and maintained this product, its associated libraries and applications, our build tools and our web sites.

#### Contributors

- Ackerley, Nick
- Allgeyer, Sebastien
- Ammon, Charles J.
- Antunes, Emanuel
- Arnarsson, Ólafur St.
- Bank, Markus
- Barsch, Robert
- Behr, Yannik
- Bernardi, Fabrizio
- Bernauer, Felix
- Bes de Berc, Maxime
- Beyreuther, Moritz
- Boltz, Shawn
- Bonaimé, Sébastien
- Carothers, Lloyd
- Chamberlain, Calum
- Chambers, Derrick
- Chen, Zhao
- Clark, Adam
- Danecek, Peter
- Egdorf, Sven
- Engels, Fabian
- Ermert, Laura
- Eulenfeld, Tom

- Fabbri, Tommaso
- Falco, Nicholas
- Fee, Jeremy
- Grellier, Clément
- Grunberg, Marc
- Hammer, Conny
- Heimann, Sebastian
- Heiniger, Lukas
- Hope, Gaute
- Igel, Heiner
- Inza, Adolfo
- Isken, Marius
- Ketchum, David
- Koymans, Mathijs
- Kremers, Simon
- Kress, Victor
- Krieger, Lars
- Krischer, Lion
- Kula, Damian
- Käufl, Paul
- Köhler, Andreas
- Lecocq, Thomas
- Leeman, John
- Legovini, Paride

- Lesage, Philippe
- Li, Yulin
- Lomax, Anthony
- Lopes, Rui L.
- MacCarthy, Jonathan
- Maggi, Alessia
- Martin, Henri
- Medlin, Andrew
- Megies, Tobias
- Meschede, Matthias
- Michelini, Alberto
- Miller, Nathaniel C.
- Morgenstern, Bernhard
- Nof, Ran Novitsky
- Panning, Mark P.
- Parker, Tom
- Pestourie, Romain
- Rapagnani, Giovanni
- Reyes, Celso
- Ringler, Adam
- Rothenhäusler, Nicolas
- Russo, Emiliano
- Sales de Andrade, Elliott

- Satriano, Claudio
- Saul, Joachim
- Scheingraber, Chris
- Schneider, Simon
- Sippl, Christian
- Snoke, Arthur
- Stange, Stefan
- Stähler, Simon C.
- Sullivan, Benjamin
- Tian, Dongdong
- Trabant, Chad
- Trani, Luca
- Uieda, Leonardo
- Walker, Andrew
- Walther, Marcus
- Wassermann, Joachim
- Williams, Mark C.
- Winkelman, Andrew
- Zaccarelli, Riccardo
- Zad, Seyed Kasra Hosseini
- Zhu, Lijun
- van Driel, Martin
- van Wijk, Kasper

#### Funds

ObsPy was partially funded by the

- German Science Foundation (DFG) via grant DFG IG 16/9-1
- German Ministry for Education and Research (BMBF), GEOTECHNOLOGIEN grant 03G0646H.
- NERA project (Network of European Research Infrastructures for Earthquake Risk Assessment and Mitigation) under the European Community's Seventh Framework Programme (FP7/2007-2013) grant agreement n° 262330
- Leibniz Institute for Applied Geophysics (LIAG)
- VERCE EU-FP7 project (no. 283543)