Repository: obspy/obspy

Issue:
Fix stream trimming in FDSN client
This is a follow-up for https://github.com/obspy/obspy/pull/1779#issuecomment-327903626.

The FDSN client should trim the data to the requested time range, as it's not guaranteed that the underlying upstream FDSN servers do so.

Quoting from #1779:

---

> I tend to disagree. The user is making a request to the obspy client and specifies start and end time, so to the user it doesnt matter where the data is cut to these times, but it should be in any case.

The normal (non-federated) obspy.clients.fdsn.Client does not trim samples outside of the requested start and end times. I think this is also an issue with the normal Client and not the FederatedClient exclusively.

For example, the following dataselect request for 10 seconds of data returned a result containing data before and after the requested start and end times.

**GET Version:**
http://eida.ipgp.fr/fdsnws/dataselect/1/query?net=G&sta=PEL&cha=LHZ&starttime=2016-11-01T00:00:00&endtime=2016-11-01T00:00:10
```
   Source                Start sample             End sample        Gap  Hz  Samples
G_PEL_00_LHZ      2016,305,23:43:16.000000 2016,306,00:00:27.000000  ==  1   1032
Total: 1 trace(s) with 1 segment(s)
```

**ObsPy Client Version:**
i.e. running
```python
from obspy import UTCDateTime
from obspy.clients import fdsn
c = fdsn.client.Client(
                        service_mappings={
                            'dataselect': \
                                'http://eida.ipgp.fr/fdsnws/dataselect/1'
                        },
                        debug=True
                    )

stream = c.get_waveforms('G', 'PEL',
                         '*', 'LHZ',
                         UTCDateTime('2016-11-01T00:00:00'),
                         UTCDateTime('2016-11-01T00:00:10'))

print stream
```
produces this output
```
1 Trace(s) in Stream:
G.PEL.00.LHZ | 2016-10-31T23:43:16.000000Z - 2016-11-01T00:00:27.000000Z | 1.0 Hz, 1032 samples
```
---


Discussion:

Resolving patch:
diff --git a/obspy/clients/fdsn/client.py b/obspy/clients/fdsn/client.py
--- a/obspy/clients/fdsn/client.py
+++ b/obspy/clients/fdsn/client.py
@@ -839,6 +839,7 @@ def get_waveforms(self, network, station, location, channel, starttime,
             if attach_response:
                 self._attach_responses(st)
             self._attach_dataselect_url_to_stream(st)
+            st.trim(starttime, endtime)
             return st
 
     def _attach_responses(self, st):


Test patch:
diff --git a/obspy/clients/fdsn/tests/test_client.py b/obspy/clients/fdsn/tests/test_client.py
--- a/obspy/clients/fdsn/tests/test_client.py
+++ b/obspy/clients/fdsn/tests/test_client.py
@@ -304,6 +304,20 @@ def test_set_credentials(self):
         # queryauth)
         self.assertEqual(client.user, user)
 
+    def test_trim_stream_after_get_waveform(self):
+        """
+        Tests that stream is properly trimmed to user requested times after
+        fetching from datacenter, see #1887
+        """
+        c = Client(
+            service_mappings={'dataselect':
+                              'http://eida.ipgp.fr/fdsnws/dataselect/1'})
+        starttime = UTCDateTime('2016-11-01T00:00:00')
+        endtime = UTCDateTime('2016-11-01T00:00:10')
+        stream = c.get_waveforms('G', 'PEL', '*', 'LHZ', starttime, endtime)
+        self.assertEqual(starttime, stream[0].stats.starttime)
+        self.assertEqual(endtime, stream[0].stats.endtime)
+
     def test_service_discovery_iris(self):
         """
         Tests the automatic discovery of services with the IRIS endpoint. The