Beispiel #1
0
 def test_readWriteXY(self):
     """
     Tests for ascii sac io
     """
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         with open(tempfile, "wb") as fh:
             t.write_sac_xy(fh)
         with open(tempfile, "rb") as fh:
             d = SacIO(fh, alpha=True)
         e = SacIO()
         with open(tempfile, "rb") as fh:
             e.read_sac_xy(fh)
         self.assertEqual(e.get_header_value('npts'),
                          d.get_header_value('npts'))
         with open(tempfile, "rb") as fh:
             self.assertEqual(e.is_valid_xy_sac_file(fh), True)
         with open(tempfile, "rb") as fh:
             self.assertEqual(e.is_valid_sac_file(fh), False)
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tempfile, "wb") as fh:
             d.write_sac_binary(fh)
         size1 = os.stat(tempfile)[6]
         size2 = os.stat(tfile)[6]
     self.assertEqual(size1, size2)
     np.testing.assert_array_almost_equal(t.seis, d.seis, decimal=5)
Beispiel #2
0
 def test_readWriteXY(self):
     """
     Tests for ascii sac io
     """
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         with open(tempfile, "wb") as fh:
             t.write_sac_xy(fh)
         with open(tempfile, "rb") as fh:
             d = SacIO(fh, alpha=True)
         e = SacIO()
         with open(tempfile, "rb") as fh:
             e.read_sac_xy(fh)
         self.assertEqual(e.get_header_value('npts'),
                          d.get_header_value('npts'))
         with open(tempfile, "rb") as fh:
             self.assertEqual(e.is_valid_xy_sac_file(fh), True)
         with open(tempfile, "rb") as fh:
             self.assertEqual(e.is_valid_sac_file(fh), False)
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tempfile, "wb") as fh:
             d.write_sac_binary(fh)
         size1 = os.stat(tempfile)[6]
         size2 = os.stat(tfile)[6]
     self.assertEqual(size1, size2)
     np.testing.assert_array_almost_equal(t.seis, d.seis, decimal=5)
Beispiel #3
0
 def test_read(self):
     """
     Tests for SacIO read and write
     """
     data = np.array([-8.7422776573475858e-08, -0.30901697278022766,
                      -0.58778536319732666, -0.8090171217918396,
                      -0.95105659961700439, -1.0, -0.95105630159378052,
                      -0.80901658535003662, -0.5877845287322998,
                      -0.30901604890823364, 1.1285198979749111e-06],
                     dtype=native_str('<f4'))
     sacfile = os.path.join(self.path, 'test.sac')
     t = SacIO()
     with open(sacfile, "rb") as fh:
         t.read_sac_file(fh)
     np.testing.assert_array_equal(t.seis[0:11], data)
     self.assertEqual(t.get_header_value('npts'), 100)
     self.assertEqual(t.get_header_value("kstnm"), "STA     ")
Beispiel #4
0
 def test_read(self):
     """
     Tests for SacIO read and write
     """
     data = np.array([
         -8.7422776573475858e-08, -0.30901697278022766,
         -0.58778536319732666, -0.8090171217918396, -0.95105659961700439,
         -1.0, -0.95105630159378052, -0.80901658535003662,
         -0.5877845287322998, -0.30901604890823364, 1.1285198979749111e-06
     ],
                     dtype=native_str('<f4'))
     sacfile = os.path.join(self.path, 'test.sac')
     t = SacIO()
     with open(sacfile, "rb") as fh:
         t.read_sac_file(fh)
     np.testing.assert_array_equal(t.seis[0:11], data)
     self.assertEqual(t.get_header_value('npts'), 100)
     self.assertEqual(t.get_header_value("kstnm"), "STA     ")
Beispiel #5
0
 def test_Date(self):
     """
     Test for SacIO '_get_date_'-function to calculate timestamp
     """
     fn = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with open(fn, "rb") as fh:
         t = SacIO(fh)
     self.assertEqual(t.reftime.timestamp, 269596800.0)
     diff = t.get_header_value('npts')
     self.assertEqual(int(t.endtime - t.starttime), diff)
Beispiel #6
0
 def test_Date(self):
     """
     Test for SacIO '_get_date_'-function to calculate timestamp
     """
     fn = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with open(fn, "rb") as fh:
         t = SacIO(fh)
     self.assertEqual(t.reftime.timestamp, 269596800.0)
     diff = t.get_header_value('npts')
     self.assertEqual(int(t.endtime - t.starttime), diff)
Beispiel #7
0
 def test_getdist(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         t.set_header_value('evla', 48.15)
         t.set_header_value('evlo', 11.58333)
         t.set_header_value('stla', -41.2869)
         t.set_header_value('stlo', 174.7746)
         t.set_header_value('lcalda', 1)
         with open(tempfile, "wb") as fh:
             t.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t2 = SacIO(fh)
     b = np.array([18486532.5788 / 1000., 65.654154562, 305.975459869],
                  dtype=native_str('>f4'))
     self.assertEqual(t2.get_header_value('dist'), b[0])
     self.assertEqual(t2.get_header_value('az'), b[1])
     self.assertEqual(t2.get_header_value('baz'), b[2])
Beispiel #8
0
 def test_getdist(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         t.set_header_value('evla', 48.15)
         t.set_header_value('evlo', 11.58333)
         t.set_header_value('stla', -41.2869)
         t.set_header_value('stlo', 174.7746)
         t.set_header_value('lcalda', 1)
         with open(tempfile, "wb") as fh:
             t.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t2 = SacIO(fh)
     b = np.array([18486532.5788 / 1000., 65.654154562, 305.975459869],
                  dtype=native_str('>f4'))
     self.assertEqual(t2.get_header_value('dist'), b[0])
     self.assertEqual(t2.get_header_value('az'), b[1])
     self.assertEqual(t2.get_header_value('baz'), b[2])
Beispiel #9
0
 def test_readWrite(self):
     """
     Tests for SacIO read and write
     """
     sacfile = os.path.join(self.path, 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         t = SacIO()
         with open(sacfile, "rb") as fh:
             t.read_sac_file(fh)
         self.assertEqual(t.get_header_value('npts'), 100)
         self.assertEqual(t.get_header_value("kcmpnm"), "Q       ")
         self.assertEqual(t.get_header_value("kstnm"), "STA     ")
         t.set_header_value("kstnm", "spiff")
         self.assertEqual(t.get_header_value('kstnm'), 'spiff   ')
         with open(tempfile, "wb") as fh:
             t.write_sac_binary(fh)
         self.assertEqual(os.stat(sacfile)[6], os.stat(tempfile)[6])
         self.assertEqual(os.path.exists(tempfile), True)
         with open(tempfile, "rb") as fh:
             t.read_sac_header(fh)
         self.assertEqual((t.hf is not None), True)
         t.set_header_value("kstnm", "spoff")
         self.assertEqual(t.get_header_value('kstnm'), 'spoff   ')
         # Open with modification!
         with open(tempfile, "rb+") as fh:
             t.write_sac_header(fh)
         t.set_header_value_in_file(tempfile, "kcmpnm", 'Z       ')
         self.assertEqual(t.get_header_value_from_file(tempfile, "kcmpnm"),
                          'Z       ')
         with open(tempfile, "rb") as fh:
             self.assertEqual(
                 SacIO(fh, headonly=True).get_header_value('kcmpnm'),
                 'Z       ')
         with open(tempfile, "rb") as fh:
             self.assertEqual(t.is_valid_sac_file(fh), True)
         self.assertEqual(t.is_valid_xy_sac_file(tempfile), False)
         self.assertEqual(
             SacIO().get_header_value_from_file(sacfile, 'npts'), 100)
         with open(sacfile, "rb") as fh:
             self.assertEqual(SacIO(fh).get_header_value('npts'), 100)
Beispiel #10
0
 def test_readWrite(self):
     """
     Tests for SacIO read and write
     """
     sacfile = os.path.join(self.path, 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         t = SacIO()
         with open(sacfile, "rb") as fh:
             t.read_sac_file(fh)
         self.assertEqual(t.get_header_value('npts'), 100)
         self.assertEqual(t.get_header_value("kcmpnm"), "Q       ")
         self.assertEqual(t.get_header_value("kstnm"), "STA     ")
         t.set_header_value("kstnm", "spiff")
         self.assertEqual(t.get_header_value('kstnm'), 'spiff   ')
         with open(tempfile, "wb") as fh:
             t.write_sac_binary(fh)
         self.assertEqual(os.stat(sacfile)[6], os.stat(tempfile)[6])
         self.assertEqual(os.path.exists(tempfile), True)
         with open(tempfile, "rb") as fh:
             t.read_sac_header(fh)
         self.assertEqual((t.hf is not None), True)
         t.set_header_value("kstnm", "spoff")
         self.assertEqual(t.get_header_value('kstnm'), 'spoff   ')
         # Open with modification!
         with open(tempfile, "rb+") as fh:
             t.write_sac_header(fh)
         t.set_header_value_in_file(tempfile, "kcmpnm", 'Z       ')
         self.assertEqual(t.get_header_value_from_file(tempfile, "kcmpnm"),
                          'Z       ')
         with open(tempfile, "rb") as fh:
             self.assertEqual(
                 SacIO(fh, headonly=True).get_header_value('kcmpnm'),
                 'Z       ')
         with open(tempfile, "rb") as fh:
             self.assertEqual(t.is_valid_sac_file(fh), True)
         self.assertEqual(t.is_valid_xy_sac_file(tempfile), False)
         self.assertEqual(
             SacIO().get_header_value_from_file(sacfile, 'npts'), 100)
         with open(sacfile, "rb") as fh:
             self.assertEqual(SacIO(fh).get_header_value('npts'), 100)
Beispiel #11
0
 def test_readXYheader(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         with open(tempfile, 'wb') as fh:
             t.write_sac_xy(fh)
         with open(tempfile, "rb") as fh:
             d = SacIO(fh, alpha=True)
         e = SacIO()
         with open(tempfile, "rb") as fh:
             e.read_sac_xy_header(fh)
         self.assertEqual(e.get_header_value('npts'),
                          d.get_header_value('npts'))
         self.assertEqual(e.get_header_value('depmen'),
                          d.get_header_value('depmen'))
         self.assertEqual(e.starttime, d.starttime)
         self.assertNotEqual(e.seis.size, d.seis.size)
         with open(tempfile, "rb") as fh:
             c = SacIO(fh, alpha=True, headonly=True)
     self.assertEqual(e.seis.size, c.seis.size)
Beispiel #12
0
 def test_read_with_fsize(self):
     """
     Testing fsize option on SacIO.read_sac_file()
     """
     # reading sac file with wrong file size should raise error
     longer_file = os.path.join(self.path, 'seism-longer.sac')
     shorter_file = os.path.join(self.path, 'seism-shorter.sac')
     t = SacIO()
     # default
     with open(longer_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh)
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh)
     # fsize=True
     with open(longer_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh, fsize=True)
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh, fsize=True)
     # using fsize=False should not work for shorter file
     # (this is not supported by SAC) ...
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacIOError, t.read_sac_file, fh, fsize=False)
     # ...but it should work for longer file
     with open(longer_file, "rb") as fh:
         t.read_sac_file(fh, fsize=False)
     # checking trace
     self.assertEqual(t.get_header_value('nzyear'), 1981)
     self.assertEqual(t.get_header_value('nzjday'), 88)
     self.assertEqual(t.get_header_value('nzhour'), 10)
     self.assertEqual(t.get_header_value('nzmin'), 38)
     self.assertEqual(t.get_header_value('nzsec'), 14)
     self.assertEqual(t.get_header_value('nzmsec'), 0)
     # we should never test equality for float values:
     self.assertLessEqual(abs(t.get_header_value('delta') - 0.01), 1e-9)
     self.assertEqual(t.get_header_value('scale'), -12345.0)
     self.assertEqual(t.get_header_value('npts'), 998)
     self.assertEqual(t.get_header_value('knetwk'), '-12345  ')
     self.assertEqual(t.get_header_value('kstnm'), 'CDV     ')
     self.assertEqual(t.get_header_value('kcmpnm'), 'Q       ')
Beispiel #13
0
 def test_readXYheader(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfile, "rb") as fh:
             t = SacIO(fh)
         with open(tempfile, 'wb') as fh:
             t.write_sac_xy(fh)
         with open(tempfile, "rb") as fh:
             d = SacIO(fh, alpha=True)
         e = SacIO()
         with open(tempfile, "rb") as fh:
             e.read_sac_xy_header(fh)
         self.assertEqual(e.get_header_value('npts'),
                          d.get_header_value('npts'))
         self.assertEqual(e.get_header_value('depmen'),
                          d.get_header_value('depmen'))
         self.assertEqual(e.starttime, d.starttime)
         self.assertNotEqual(e.seis.size, d.seis.size)
         with open(tempfile, "rb") as fh:
             c = SacIO(fh, alpha=True, headonly=True)
     self.assertEqual(e.seis.size, c.seis.size)
Beispiel #14
0
 def test_read_with_fsize(self):
     """
     Testing fsize option on SacIO.read_sac_file()
     """
     # reading sac file with wrong file size should raise error
     longer_file = os.path.join(self.path, 'seism-longer.sac')
     shorter_file = os.path.join(self.path, 'seism-shorter.sac')
     t = SacIO()
     # default
     with open(longer_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh)
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh)
     # fsize=True
     with open(longer_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh, fsize=True)
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacError, t.read_sac_file, fh, fsize=True)
     # using fsize=False should not work for shorter file
     # (this is not supported by SAC) ...
     with open(shorter_file, "rb") as fh:
         self.assertRaises(SacIOError, t.read_sac_file, fh, fsize=False)
     # ...but it should work for longer file
     with open(longer_file, "rb") as fh:
         t.read_sac_file(fh, fsize=False)
     # checking trace
     self.assertEqual(t.get_header_value('nzyear'), 1981)
     self.assertEqual(t.get_header_value('nzjday'), 88)
     self.assertEqual(t.get_header_value('nzhour'), 10)
     self.assertEqual(t.get_header_value('nzmin'), 38)
     self.assertEqual(t.get_header_value('nzsec'), 14)
     self.assertEqual(t.get_header_value('nzmsec'), 0)
     # we should never test equality for float values:
     self.assertTrue(abs(t.get_header_value('delta') - 0.01) <= 1e-9)
     self.assertEqual(t.get_header_value('scale'), -12345.0)
     self.assertEqual(t.get_header_value('npts'), 998)
     self.assertEqual(t.get_header_value('knetwk'), '-12345  ')
     self.assertEqual(t.get_header_value('kstnm'), 'CDV     ')
     self.assertEqual(t.get_header_value('kcmpnm'), 'Q       ')
Beispiel #15
0
 def test_readBigEnd(self):
     """
     Test reading big endian binary files
     """
     tfilel = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     tfileb = os.path.join(os.path.dirname(__file__), 'data',
                           'test.sac.swap')
     with open(tfilel, "rb") as fh:
         tl = SacIO(fh)
     with open(tfileb, "rb") as fh:
         tb = SacIO(fh)
     self.assertEqual(tl.get_header_value('kevnm'),
                      tb.get_header_value('kevnm'))
     self.assertEqual(tl.get_header_value('npts'),
                      tb.get_header_value('npts'))
     self.assertEqual(tl.get_header_value('delta'),
                      tb.get_header_value('delta'))
     np.testing.assert_array_equal(tl.seis, tb.seis)
Beispiel #16
0
 def test_readBigEnd(self):
     """
     Test reading big endian binary files
     """
     tfilel = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     tfileb = os.path.join(os.path.dirname(__file__), 'data',
                           'test.sac.swap')
     with open(tfilel, "rb") as fh:
         tl = SacIO(fh)
     with open(tfileb, "rb") as fh:
         tb = SacIO(fh)
     self.assertEqual(tl.get_header_value('kevnm'),
                      tb.get_header_value('kevnm'))
     self.assertEqual(tl.get_header_value('npts'),
                      tb.get_header_value('npts'))
     self.assertEqual(tl.get_header_value('delta'),
                      tb.get_header_value('delta'))
     np.testing.assert_array_equal(tl.seis, tb.seis)
Beispiel #17
0
 def test_getattr(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with open(tfile, "rb") as fh:
         tr = SacIO(fh)
     self.assertEqual(tr.npts, tr.get_header_value('npts'))
     self.assertEqual(tr.kstnm, tr.get_header_value('kstnm'))
Beispiel #18
0
 def test_swapbytes(self):
     tfilel = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     tfileb = os.path.join(os.path.dirname(__file__), 'data',
                           'test.sac.swap')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfileb, "rb") as fh:
             tb = SacIO(fh)
         tb.swap_byte_order()
         with open(tempfile, "wb") as fh:
             tb.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t = SacIO(fh)
         with open(tfilel, "rb") as fh:
             tl = SacIO(fh)
         self.assertEqual(t.get_header_value('kevnm'),
                          tl.get_header_value('kevnm'))
         self.assertEqual(t.get_header_value('npts'),
                          tl.get_header_value('npts'))
         self.assertEqual(t.get_header_value('delta'),
                          tl.get_header_value('delta'))
         np.testing.assert_array_equal(t.seis, tl.seis)
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfilel, "rb") as fh:
             tl = SacIO(fh)
         tl.swap_byte_order()
         with open(tempfile, "wb") as fh:
             tl.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t = SacIO(fh)
         with open(tfileb, "rb") as fh:
             tb = SacIO(fh)
         self.assertEqual(t.get_header_value('kevnm'),
                          tb.get_header_value('kevnm'))
         self.assertEqual(t.get_header_value('npts'),
                          tb.get_header_value('npts'))
         self.assertEqual(t.get_header_value('delta'),
                          tb.get_header_value('delta'))
         np.testing.assert_array_equal(t.seis, tb.seis)
Beispiel #19
0
 def test_getattr(self):
     tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     with open(tfile, "rb") as fh:
         tr = SacIO(fh)
     self.assertEqual(tr.npts, tr.get_header_value('npts'))
     self.assertEqual(tr.kstnm, tr.get_header_value('kstnm'))
Beispiel #20
0
 def test_swapbytes(self):
     tfilel = os.path.join(os.path.dirname(__file__), 'data', 'test.sac')
     tfileb = os.path.join(os.path.dirname(__file__), 'data',
                           'test.sac.swap')
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfileb, "rb") as fh:
             tb = SacIO(fh)
         tb.swap_byte_order()
         with open(tempfile, "wb") as fh:
             tb.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t = SacIO(fh)
         with open(tfilel, "rb") as fh:
             tl = SacIO(fh)
         self.assertEqual(t.get_header_value('kevnm'),
                          tl.get_header_value('kevnm'))
         self.assertEqual(t.get_header_value('npts'),
                          tl.get_header_value('npts'))
         self.assertEqual(t.get_header_value('delta'),
                          tl.get_header_value('delta'))
         np.testing.assert_array_equal(t.seis, tl.seis)
     with NamedTemporaryFile() as tf:
         tempfile = tf.name
         with open(tfilel, "rb") as fh:
             tl = SacIO(fh)
         tl.swap_byte_order()
         with open(tempfile, "wb") as fh:
             tl.write_sac_binary(fh)
         with open(tempfile, "rb") as fh:
             t = SacIO(fh)
         with open(tfileb, "rb") as fh:
             tb = SacIO(fh)
         self.assertEqual(t.get_header_value('kevnm'),
                          tb.get_header_value('kevnm'))
         self.assertEqual(t.get_header_value('npts'),
                          tb.get_header_value('npts'))
         self.assertEqual(t.get_header_value('delta'),
                          tb.get_header_value('delta'))
         np.testing.assert_array_equal(t.seis, tb.seis)