Beispiel #1
0
 def test_mergePreviews2(self):
     """
     Test case for issue #84.
     """
     # Note: explicitly creating np.ones instead of np.empty in order to
     # prevent NumPy warnings related to max function
     tr1 = Trace(data=np.ones(2880))
     tr1.stats.starttime = UTCDateTime("2010-01-01T00:00:00.670000Z")
     tr1.stats.delta = 30.0
     tr1.stats.preview = True
     tr1.verify()
     tr2 = Trace(data=np.ones(2881))
     tr2.stats.starttime = UTCDateTime("2010-01-01T23:59:30.670000Z")
     tr2.stats.delta = 30.0
     tr2.stats.preview = True
     tr2.verify()
     st1 = Stream([tr1, tr2])
     st1.verify()
     # merge
     st2 = merge_previews(st1)
     st2.verify()
     # check
     self.assertTrue(st2[0].stats.preview)
     self.assertEqual(st2[0].stats.starttime, tr1.stats.starttime)
     self.assertEqual(st2[0].stats.endtime, tr2.stats.endtime)
     self.assertEqual(st2[0].stats.npts, 5760)
     self.assertEqual(len(st2[0]), 5760)
Beispiel #2
0
 def test_merge_previews_2(self):
     """
     Test case for issue #84.
     """
     # Note: explicitly creating np.ones instead of np.empty in order to
     # prevent NumPy warnings related to max function
     tr1 = Trace(data=np.ones(2880))
     tr1.stats.starttime = UTCDateTime("2010-01-01T00:00:00.670000Z")
     tr1.stats.delta = 30.0
     tr1.stats.preview = True
     tr1.verify()
     tr2 = Trace(data=np.ones(2881))
     tr2.stats.starttime = UTCDateTime("2010-01-01T23:59:30.670000Z")
     tr2.stats.delta = 30.0
     tr2.stats.preview = True
     tr2.verify()
     st1 = Stream([tr1, tr2])
     st1.verify()
     # merge
     st2 = merge_previews(st1)
     st2.verify()
     # check
     assert st2[0].stats.preview
     assert st2[0].stats.starttime == tr1.stats.starttime
     assert st2[0].stats.endtime == tr2.stats.endtime
     assert st2[0].stats.npts == 5760
     assert len(st2[0]) == 5760
Beispiel #3
0
    def test_rtrim_with_padding(self):
        """
        Tests the _rtrim() method of the Trace class with padding. It has
        already been tested in the two sided trimming tests. This is just to
        have an explicit test. Also tests issue #429.
        """
        # set up
        trace = Trace(data=np.arange(10))
        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
        trace.stats.starttime = start
        trace.stats.sampling_rate = 1.0
        trace.verify()

        # Pad with no fill_value will mask the additional values.
        tr = trace.copy()
        end = tr.stats.endtime
        tr._rtrim(end + 10, pad=True)
        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10)
        np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
        # Check that the first couple of entries are not masked.
        self.assertFalse(tr.data[0:10].mask.any())
        # All the other entries should be masked.
        self.assertTrue(tr.data[10:].mask.all())

        # Pad with fill_value.
        tr = trace.copy()
        end = tr.stats.endtime
        tr._rtrim(end + 10, pad=True, fill_value=-33)
        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10)
        # The first ten entries should not have changed.
        np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
        # The rest should be filled with the fill_value.
        np.testing.assert_array_equal(tr.data[10:], np.ones(10) * -33)
Beispiel #4
0
 def test_addTraceWithGap(self):
     """
     Tests __add__ method of the Trace class.
     """
     # set up
     tr1 = Trace(data=np.arange(1000))
     tr1.stats.sampling_rate = 200
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     tr1.stats.starttime = start
     tr2 = Trace(data=np.arange(0, 1000)[::-1])
     tr2.stats.sampling_rate = 200
     tr2.stats.starttime = start + 10
     # verify
     tr1.verify()
     tr2.verify()
     # add
     trace = tr1 + tr2
     # stats
     self.assertEqual(trace.stats.starttime, start)
     self.assertEqual(trace.stats.endtime, start + 14.995)
     self.assertEqual(trace.stats.sampling_rate, 200)
     self.assertEqual(trace.stats.npts, 3000)
     # data
     self.assertEqual(len(trace), 3000)
     self.assertEqual(trace[0], 0)
     self.assertEqual(trace[999], 999)
     self.assertTrue(is_masked(trace[1000]))
     self.assertTrue(is_masked(trace[1999]))
     self.assertEqual(trace[2000], 999)
     self.assertEqual(trace[2999], 0)
     # verify
     trace.verify()
Beispiel #5
0
    def test_rtrim_with_padding(self):
        """
        Tests the _rtrim() method of the Trace class with padding. It has
        already been tested in the two sided trimming tests. This is just to
        have an explicit test. Also tests issue #429.
        """
        # set up
        trace = Trace(data=np.arange(10))
        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
        trace.stats.starttime = start
        trace.stats.sampling_rate = 1.0
        trace.verify()

        # Pad with no fill_value will mask the additional values.
        tr = trace.copy()
        end = tr.stats.endtime
        tr._rtrim(end + 10, pad=True)
        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10)
        np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
        # Check that the first couple of entries are not masked.
        self.assertFalse(tr.data[0:10].mask.any())
        # All the other entries should be masked.
        self.assertTrue(tr.data[10:].mask.all())

        # Pad with fill_value.
        tr = trace.copy()
        end = tr.stats.endtime
        tr._rtrim(end + 10, pad=True, fill_value=-33)
        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10)
        # The first ten entries should not have changed.
        np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
        # The rest should be filled with the fill_value.
        np.testing.assert_array_equal(tr.data[10:], np.ones(10) * -33)
Beispiel #6
0
 def test_addTraceWithGap(self):
     """
     Tests __add__ method of the Trace class.
     """
     # set up
     tr1 = Trace(data=np.arange(1000))
     tr1.stats.sampling_rate = 200
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     tr1.stats.starttime = start
     tr2 = Trace(data=np.arange(0, 1000)[::-1])
     tr2.stats.sampling_rate = 200
     tr2.stats.starttime = start + 10
     # verify
     tr1.verify()
     tr2.verify()
     # add
     trace = tr1 + tr2
     # stats
     self.assertEquals(trace.stats.starttime, start)
     self.assertEquals(trace.stats.endtime, start + 14.995)
     self.assertEquals(trace.stats.sampling_rate, 200)
     self.assertEquals(trace.stats.npts, 3000)
     # data
     self.assertEquals(len(trace), 3000)
     self.assertEquals(trace[0], 0)
     self.assertEquals(trace[999], 999)
     self.assertTrue(is_masked(trace[1000]))
     self.assertTrue(is_masked(trace[1999]))
     self.assertEquals(trace[2000], 999)
     self.assertEquals(trace[2999], 0)
     # verify
     trace.verify()
Beispiel #7
0
 def test_trim(self):
     """
     Tests the trim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1001))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 5, 0)
     trace.verify()
     # rtrim 100 samples
     trace.trim(0.5, 0.5)
     trace.verify()
     np.testing.assert_array_equal(trace.data[-5:],
                                   np.array([896, 897, 898, 899, 900]))
     np.testing.assert_array_equal(trace.data[:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEqual(len(trace.data), 801)
     self.assertEqual(trace.stats.npts, 801)
     self.assertEqual(trace.stats.sampling_rate, 200.0)
     self.assertEqual(trace.stats.starttime, start + 0.5)
     self.assertEqual(trace.stats.endtime, end - 0.5)
Beispiel #8
0
 def test_trim(self):
     """
     Tests the trim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1001))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 5, 0)
     trace.verify()
     # rtrim 100 samples
     trace.trim(0.5, 0.5)
     trace.verify()
     np.testing.assert_array_equal(trace.data[-5:],
                                   np.array([896, 897, 898, 899, 900]))
     np.testing.assert_array_equal(trace.data[:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEquals(len(trace.data), 801)
     self.assertEquals(trace.stats.npts, 801)
     self.assertEquals(trace.stats.sampling_rate, 200.0)
     self.assertEquals(trace.stats.starttime, start + 0.5)
     self.assertEquals(trace.stats.endtime, end - 0.5)
Beispiel #9
0
 def test_verify(self):
     """
     Tests verify method.
     """
     # empty Trace
     tr = Trace()
     tr.verify()
     # Trace with a single sample (issue #357)
     tr = Trace(data=np.array([1]))
     tr.verify()
     # example Trace
     tr = read()[0]
     tr.verify()
Beispiel #10
0
 def test_verify(self):
     """
     Tests verify method.
     """
     # empty Trace
     tr = Trace()
     tr.verify()
     # Trace with a single sample (issue #357)
     tr = Trace(data=np.array([1]))
     tr.verify()
     # example Trace
     tr = read()[0]
     tr.verify()
Beispiel #11
0
 def test_ltrim(self):
     """
     Tests the ltrim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1000))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000)
     # verify
     trace.verify()
     # ltrim 100 samples
     tr = deepcopy(trace)
     tr._ltrim(0.5)
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEqual(len(tr.data), 900)
     self.assertEqual(tr.stats.npts, 900)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start + 0.5)
     self.assertEqual(tr.stats.endtime, end)
     # ltrim 202 samples
     tr = deepcopy(trace)
     tr._ltrim(1.010)
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([202, 203, 204, 205, 206]))
     self.assertEqual(len(tr.data), 798)
     self.assertEqual(tr.stats.npts, 798)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start + 1.010)
     self.assertEqual(tr.stats.endtime, end)
     # ltrim to UTCDateTime
     tr = deepcopy(trace)
     tr._ltrim(UTCDateTime(2000, 1, 1, 0, 0, 1, 10000))
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([202, 203, 204, 205, 206]))
     self.assertEqual(len(tr.data), 798)
     self.assertEqual(tr.stats.npts, 798)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start + 1.010)
     self.assertEqual(tr.stats.endtime, end)
     # some sanity checks
     # negative start time as datetime
     tr = deepcopy(trace)
     tr._ltrim(start - 1, pad=True)
     tr.verify()
     self.assertEqual(tr.stats.starttime, start - 1)
     np.testing.assert_array_equal(trace.data, tr.data[200:])
     self.assertEqual(tr.stats.endtime, trace.stats.endtime)
     # negative start time as integer
     tr = deepcopy(trace)
     tr._ltrim(-100, pad=True)
     tr.verify()
     self.assertEqual(tr.stats.starttime, start - 100)
     delta = 100 * trace.stats.sampling_rate
     np.testing.assert_array_equal(trace.data, tr.data[delta:])
     self.assertEqual(tr.stats.endtime, trace.stats.endtime)
     # start time > end time
     tr = deepcopy(trace)
     tr._ltrim(trace.stats.endtime + 100)
     tr.verify()
     self.assertEqual(tr.stats.starttime, trace.stats.endtime + 100)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEqual(tr.stats.endtime, tr.stats.starttime)
     # start time == end time
     tr = deepcopy(trace)
     tr._ltrim(5)
     tr.verify()
     self.assertEqual(tr.stats.starttime, trace.stats.starttime + 5)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEqual(tr.stats.endtime, tr.stats.starttime)
     # start time == end time
     tr = deepcopy(trace)
     tr._ltrim(5.1)
     tr.verify()
     self.assertEqual(tr.stats.starttime, trace.stats.starttime + 5.1)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEqual(tr.stats.endtime, tr.stats.starttime)
Beispiel #12
0
 def test_rtrim(self):
     """
     Tests the rtrim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1000))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000)
     trace.verify()
     # rtrim 100 samples
     tr = deepcopy(trace)
     tr._rtrim(0.5)
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([895, 896, 897, 898, 899]))
     self.assertEqual(len(tr.data), 900)
     self.assertEqual(tr.stats.npts, 900)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start)
     self.assertEqual(tr.stats.endtime, end - 0.5)
     # rtrim 202 samples
     tr = deepcopy(trace)
     tr._rtrim(1.010)
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([793, 794, 795, 796, 797]))
     self.assertEqual(len(tr.data), 798)
     self.assertEqual(tr.stats.npts, 798)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start)
     self.assertEqual(tr.stats.endtime, end - 1.010)
     # rtrim 1 minute via UTCDateTime
     tr = deepcopy(trace)
     tr._rtrim(UTCDateTime(2000, 1, 1, 0, 0, 3, 985000))
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([793, 794, 795, 796, 797]))
     self.assertEqual(len(tr.data), 798)
     self.assertEqual(tr.stats.npts, 798)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start)
     self.assertEqual(tr.stats.endtime, end - 1.010)
     # some sanity checks
     # negative end time
     tr = deepcopy(trace)
     t = UTCDateTime(1999, 12, 31)
     tr._rtrim(t)
     tr.verify()
     self.assertEqual(tr.stats.endtime, t)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     # negative end time with given seconds
     tr = deepcopy(trace)
     tr._rtrim(100)
     tr.verify()
     self.assertEqual(tr.stats.endtime, trace.stats.endtime - 100)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEqual(tr.stats.endtime, tr.stats.starttime)
     # end time > start time
     tr = deepcopy(trace)
     t = UTCDateTime(2001)
     tr._rtrim(t)
     tr.verify()
     self.assertEqual(tr.stats.endtime, t)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEqual(tr.stats.endtime, tr.stats.starttime)
     # end time > start time given seconds
     tr = deepcopy(trace)
     tr._rtrim(5.1)
     tr.verify()
     delta = int(math.floor(round(5.1 * trace.stats.sampling_rate, 7)))
     endtime = trace.stats.starttime + trace.stats.delta * \
               (trace.stats.npts - delta - 1)
     self.assertEqual(tr.stats.endtime, endtime)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     # end time == start time
     # returns one sample!
     tr = deepcopy(trace)
     tr._rtrim(4.995)
     #XXX I do not understand why this fails!!!
     #tr.verify()
     np.testing.assert_array_equal(tr.data, np.array([0]))
     self.assertEqual(len(tr.data), 1)
     self.assertEqual(tr.stats.npts, 1)
     self.assertEqual(tr.stats.sampling_rate, 200.0)
     self.assertEqual(tr.stats.starttime, start)
     self.assertEqual(tr.stats.endtime, start)
Beispiel #13
0
 def test_ltrim(self):
     """
     Tests the ltrim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1000))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000)
     # verify
     trace.verify()
     # ltrim 100 samples
     tr = deepcopy(trace)
     tr._ltrim(0.5)
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([100, 101, 102, 103, 104]))
     self.assertEquals(len(tr.data), 900)
     self.assertEquals(tr.stats.npts, 900)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start + 0.5)
     self.assertEquals(tr.stats.endtime, end)
     # ltrim 202 samples
     tr = deepcopy(trace)
     tr._ltrim(1.010)
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([202, 203, 204, 205, 206]))
     self.assertEquals(len(tr.data), 798)
     self.assertEquals(tr.stats.npts, 798)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start + 1.010)
     self.assertEquals(tr.stats.endtime, end)
     # ltrim to UTCDateTime
     tr = deepcopy(trace)
     tr._ltrim(UTCDateTime(2000, 1, 1, 0, 0, 1, 10000))
     tr.verify()
     np.testing.assert_array_equal(tr.data[0:5],
                                   np.array([202, 203, 204, 205, 206]))
     self.assertEquals(len(tr.data), 798)
     self.assertEquals(tr.stats.npts, 798)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start + 1.010)
     self.assertEquals(tr.stats.endtime, end)
     # some sanity checks
     # negative start time as datetime
     tr = deepcopy(trace)
     tr._ltrim(start - 1, pad=True)
     tr.verify()
     self.assertEquals(tr.stats.starttime, start - 1)
     np.testing.assert_array_equal(trace.data, tr.data[200:])
     self.assertEquals(tr.stats.endtime, trace.stats.endtime)
     # negative start time as integer
     tr = deepcopy(trace)
     tr._ltrim(-100, pad=True)
     tr.verify()
     self.assertEquals(tr.stats.starttime, start - 100)
     delta = 100 * trace.stats.sampling_rate
     np.testing.assert_array_equal(trace.data, tr.data[delta:])
     self.assertEquals(tr.stats.endtime, trace.stats.endtime)
     # start time > end time
     tr = deepcopy(trace)
     tr._ltrim(trace.stats.endtime + 100)
     tr.verify()
     self.assertEquals(tr.stats.starttime,
                       trace.stats.endtime + 100)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEquals(tr.stats.endtime, tr.stats.starttime)
     # start time == end time
     tr = deepcopy(trace)
     tr._ltrim(5)
     tr.verify()
     self.assertEquals(tr.stats.starttime,
                       trace.stats.starttime + 5)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEquals(tr.stats.endtime, tr.stats.starttime)
     # start time == end time
     tr = deepcopy(trace)
     tr._ltrim(5.1)
     tr.verify()
     self.assertEquals(tr.stats.starttime,
                       trace.stats.starttime + 5.1)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEquals(tr.stats.endtime, tr.stats.starttime)
Beispiel #14
0
 def test_rtrim(self):
     """
     Tests the rtrim method of the Trace class.
     """
     # set up
     trace = Trace(data=np.arange(1000))
     start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0)
     trace.stats.starttime = start
     trace.stats.sampling_rate = 200.0
     end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000)
     trace.verify()
     # rtrim 100 samples
     tr = deepcopy(trace)
     tr._rtrim(0.5)
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([895, 896, 897, 898, 899]))
     self.assertEquals(len(tr.data), 900)
     self.assertEquals(tr.stats.npts, 900)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start)
     self.assertEquals(tr.stats.endtime, end - 0.5)
     # rtrim 202 samples
     tr = deepcopy(trace)
     tr._rtrim(1.010)
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([793, 794, 795, 796, 797]))
     self.assertEquals(len(tr.data), 798)
     self.assertEquals(tr.stats.npts, 798)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start)
     self.assertEquals(tr.stats.endtime, end - 1.010)
     # rtrim 1 minute via UTCDateTime
     tr = deepcopy(trace)
     tr._rtrim(UTCDateTime(2000, 1, 1, 0, 0, 3, 985000))
     tr.verify()
     np.testing.assert_array_equal(tr.data[-5:],
                                   np.array([793, 794, 795, 796, 797]))
     self.assertEquals(len(tr.data), 798)
     self.assertEquals(tr.stats.npts, 798)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start)
     self.assertEquals(tr.stats.endtime, end - 1.010)
     # some sanity checks
     # negative end time
     tr = deepcopy(trace)
     t = UTCDateTime(1999, 12, 31)
     tr._rtrim(t)
     tr.verify()
     self.assertEquals(tr.stats.endtime, t)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     # negative end time with given seconds
     tr = deepcopy(trace)
     tr._rtrim(100)
     tr.verify()
     self.assertEquals(tr.stats.endtime, trace.stats.endtime - 100)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEquals(tr.stats.endtime, tr.stats.starttime)
     # end time > start time
     tr = deepcopy(trace)
     t = UTCDateTime(2001)
     tr._rtrim(t)
     tr.verify()
     self.assertEquals(tr.stats.endtime, t)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     self.assertEquals(tr.stats.endtime, tr.stats.starttime)
     # end time > start time given seconds
     tr = deepcopy(trace)
     tr._rtrim(5.1)
     tr.verify()
     delta = int(math.floor(round(5.1 * trace.stats.sampling_rate, 7)))
     endtime = trace.stats.starttime + trace.stats.delta * \
               (trace.stats.npts - delta - 1)
     self.assertEquals(tr.stats.endtime, endtime)
     np.testing.assert_array_equal(tr.data, np.empty(0))
     # end time == start time
     # returns one sample!
     tr = deepcopy(trace)
     tr._rtrim(4.995)
     #XXX I do not understand why this fails!!!
     #tr.verify()
     np.testing.assert_array_equal(tr.data, np.array([0]))
     self.assertEquals(len(tr.data), 1)
     self.assertEquals(tr.stats.npts, 1)
     self.assertEquals(tr.stats.sampling_rate, 200.0)
     self.assertEquals(tr.stats.starttime, start)
     self.assertEquals(tr.stats.endtime, start)
Beispiel #15
0
def data2obspy(dates, data, name):
    """
    Converts data to an obspy stream object.
    
    Parameters
    ----------
    dates: list
        List of list of Dates in UTCDateTime format.  Each list is assumed
        to be continuous and evenly sampled.
    data: list
        List of list of floats of data corresponding to the dates above.  Each
        list is assumed to be continuous and evenly sampled.
    name: string
        Channel name separated by $ in station$channel$network order
        
    Outputs
    ---------
    streamData: Stream
        Obspy stream, with each list converted to a trace with the appropriate
        metadata.
    """
    from numpy import round, array
    from obspy import Trace, Stream
    streamData = Stream()
    # Get ID
    try:
        sta, chan, net, loc = name.split('$', 4)
    except:
        try:
            sta, chan, net = name.split('$', 3)
            loc = ''
        except:
            try:
                sta, chan = name.split('$', 2)
                net = ''
                loc = ''
            except:
                sta = name
                chan = ''
                net = ''
                loc = ''

    # Loop over lists to get metadata and convert to obspy
    for nowdates, nowdata in zip(dates, data):
        # Build metadata string, then punch the data into a Trace and save in the stream
        starttime = nowdates[0]
        npts = len(nowdata)
        delta = round(nowdates[1] - nowdates[0])
        meta = {
            'station': sta,
            'network': net,
            'channel': chan,
            'location': loc,
            'delta': delta,
            'starttime': starttime,
            'npts': npts
        }
        npdata = array(nowdata, dtype='float64')
        T = Trace(data=npdata, header=meta)
        T.verify()
        streamData.append(T)

    return streamData