def test_undefinedB(self): """ Test that an undefined B value (-12345.0) is not messing up the starttime """ # read in the test file an see that sac reference time and # starttime of seismogram are correct tr = read(self.file)[0] self.assertEqual(tr.stats.starttime.timestamp, 269596810.0) self.assertEqual(tr.stats.sac.b, 10.0) with open(self.file, 'rb') as fh: sac_ref_time = SacIO(fh).reftime self.assertEqual(sac_ref_time.timestamp, 269596800.0) # change b to undefined and write (same case as if b == 0.0) # now sac reference time and reftime of seismogram must be the # same tr.stats.sac.b = -12345.0 with NamedTemporaryFile() as tf: tmpfile = tf.name tr.write(tmpfile, format="SAC") tr2 = read(tmpfile)[0] self.assertEqual(tr2.stats.starttime.timestamp, 269596810.0) self.assertEqual(tr2.stats.sac.b, -12345.0) with open(tmpfile, "rb") as fh: sac_ref_time2 = SacIO(fh).reftime self.assertEqual(sac_ref_time2.timestamp, 269596810.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)
def test_issue171(self): """ Test for issue #171. """ tr = read()[0] with NamedTemporaryFile() as tf: tempfile = tf.name tr.write(tempfile, format="SAC") with open(tempfile, "rb") as fh: trace = SacIO(fh) trace.set_header_value('stel', 91.0) # Open with modification! with open(tempfile, "rb+") as fh: trace.write_sac_header(fh) with open(tempfile, "rb") as fh: SacIO(fh)
def test_is_sac(self): """ Assertion is raised if file is not a SAC file """ t = SacIO() with open(__file__, "rb") as fh: self.assertRaises(SacError, t.read_sac_file, fh)
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 ")
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)
def test_iztype11(self): # test that iztype 11 is read correctly sod_file = os.path.join(self.path, 'data', 'dis.G.SCZ.__.BHE_short') tr = read(sod_file)[0] with open(sod_file, "rb") as fh: sac = SacIO(fh) t1 = tr.stats.starttime - float(tr.stats.sac.b) t2 = sac.reftime self.assertAlmostEqual(t1.timestamp, t2.timestamp, 5) # see that iztype is written correctly with NamedTemporaryFile() as tf: tempfile = tf.name tr.write(tempfile, format="SAC") with open(tempfile, "rb") as fh: sac2 = SacIO(fh) self.assertEqual(sac2.iztype, 11) self.assertAlmostEqual(tr.stats.sac.b, sac2.b) self.assertAlmostEqual(t2.timestamp, sac2.reftime.timestamp, 5)
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 ")
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 ')
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'))
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])
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)
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)
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)
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 ')
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)