def test_read_with_fsize(self): """ Testing fsize option on SacIO.ReadSacFile() """ # 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 self.assertRaises(SacError, t.ReadSacFile, longer_file) self.assertRaises(SacError, t.ReadSacFile, shorter_file) # fsize=True self.assertRaises(SacError, t.ReadSacFile, longer_file, fsize=True) self.assertRaises(SacError, t.ReadSacFile, shorter_file, fsize=True) # using fsize=False should not work for shorter file # (this is not supported by SAC) ... self.assertRaises(SacIOError, t.ReadSacFile, shorter_file, fsize=False) # ...but it should work for longer file t.ReadSacFile(longer_file, fsize=False) # checking trace self.assertEqual(t.GetHvalue('nzyear'), 1981) self.assertEqual(t.GetHvalue('nzjday'), 88) self.assertEqual(t.GetHvalue('nzhour'), 10) self.assertEqual(t.GetHvalue('nzmin'), 38) self.assertEqual(t.GetHvalue('nzsec'), 14) self.assertEqual(t.GetHvalue('nzmsec'), 0) # we should never test equality for float values: self.assertTrue(abs(t.GetHvalue('delta') - 0.01) <= 1e-9) self.assertEqual(t.GetHvalue('scale'), -12345.0) self.assertEqual(t.GetHvalue('npts'), 998) self.assertEqual(t.GetHvalue('knetwk'), '-12345 ') self.assertEqual(t.GetHvalue('kstnm'), 'CDV ') self.assertEqual(t.GetHvalue('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() t.ReadSacFile(sacfile) self.assertEqual(t.GetHvalue('npts'), 100) self.assertEqual(t.GetHvalue("kcmpnm"), "Q ") self.assertEqual(t.GetHvalue("kstnm"), "STA ") t.SetHvalue("kstnm", "spiff") self.assertEqual(t.GetHvalue('kstnm'), 'spiff ') t.WriteSacBinary(tempfile) self.assertEqual(os.stat(sacfile)[6], os.stat(tempfile)[6]) self.assertEqual(os.path.exists(tempfile), True) t.ReadSacHeader(tempfile) self.assertEqual((t.hf is not None), True) t.SetHvalue("kstnm", "spoff") self.assertEqual(t.GetHvalue('kstnm'), 'spoff ') t.WriteSacHeader(tempfile) t.SetHvalueInFile(tempfile, "kcmpnm", 'Z ') self.assertEqual(t.GetHvalueFromFile(tempfile, "kcmpnm"), 'Z ') self.assertEqual( SacIO(tempfile, headonly=True).GetHvalue('kcmpnm'), 'Z ') self.assertEqual(t.IsValidSacFile(tempfile), True) self.assertEqual(t.IsValidXYSacFile(tempfile), False) self.assertEqual(SacIO().GetHvalueFromFile(sacfile, 'npts'), 100) self.assertEqual(SacIO(sacfile).GetHvalue('npts'), 100)
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') tl = SacIO(tfilel) tb = SacIO(tfileb) self.assertEqual(tl.GetHvalue('kevnm'), tb.GetHvalue('kevnm')) self.assertEqual(tl.GetHvalue('npts'), tb.GetHvalue('npts')) self.assertEqual(tl.GetHvalueFromFile(tfilel, 'kcmpnm'), tb.GetHvalueFromFile(tfileb, 'kcmpnm')) np.testing.assert_array_equal(tl.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 t = SacIO(tfile) t.WriteSacXY(tempfile) d = SacIO(tempfile, alpha=True) e = SacIO() e.ReadSacXYHeader(tempfile) self.assertEqual(e.GetHvalue('npts'), d.GetHvalue('npts')) self.assertEqual(e.GetHvalue('depmen'), d.GetHvalue('depmen')) self.assertEqual(e.starttime, d.starttime) self.assertNotEqual(e.seis.size, d.seis.size) c = SacIO(tempfile, alpha=True, headonly=True) self.assertEqual(e.seis.size, c.seis.size)
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() t.ReadSacFile(sacfile) np.testing.assert_array_equal(t.seis[0:11], data) self.assertEqual(t.GetHvalue('npts'), 100) self.assertEqual(t.GetHvalue("kstnm"), "STA ")
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 tb = SacIO(tfileb) tb.swap_byte_order() tb.WriteSacBinary(tempfile) tr1 = SacIO(tempfile) tl = SacIO(tfilel) np.testing.assert_array_equal(tl.seis, tr1.seis) self.assertEqual(tl.GetHvalue('kevnm'), tr1.GetHvalue('kevnm')) self.assertEqual(tl.GetHvalue('npts'), tr1.GetHvalue('npts')) self.assertEqual(tl.GetHvalueFromFile(tfilel, 'kcmpnm'), tr1.GetHvalueFromFile(tempfile, 'kcmpnm'))
def test_getdist(self): tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac') with NamedTemporaryFile() as tf: tempfile = tf.name t = SacIO(tfile) t.SetHvalue('evla', 48.15) t.SetHvalue('evlo', 11.58333) t.SetHvalue('stla', -41.2869) t.SetHvalue('stlo', 174.7746) t.SetHvalue('lcalda', 1) t.WriteSacBinary(tempfile) t2 = SacIO(tempfile) b = np.array([18486532.5788 / 1000., 65.654154562, 305.975459869], dtype=native_str('>f4')) self.assertEqual(t2.GetHvalue('dist'), b[0]) self.assertEqual(t2.GetHvalue('az'), b[1]) self.assertEqual(t2.GetHvalue('baz'), b[2])
def test_Date(self): """ Test for SacIO '_get_date_'-function to calculate timestamp """ fn = os.path.join(os.path.dirname(__file__), 'data', 'test.sac') t = SacIO(fn) self.assertEqual(t.reftime.timestamp, 269596800.0) diff = t.GetHvalue('npts') self.assertEqual(int(t.endtime - t.starttime), diff)
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') t = SacIO(tfile) t.WriteSacXY(tempfile) d = SacIO(tempfile, alpha=True) e = SacIO() e.ReadSacXY(tempfile) self.assertEqual(e.GetHvalue('npts'), d.GetHvalue('npts')) self.assertEqual(e.IsValidXYSacFile(tempfile), True) self.assertEqual(e.IsValidSacFile(tempfile), False) with NamedTemporaryFile() as tf: tempfile = tf.name d.WriteSacBinary(tempfile) 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_getattr(self): tfile = os.path.join(os.path.dirname(__file__), 'data', 'test.sac') tr = SacIO(tfile) self.assertEqual(tr.npts, tr.GetHvalue('npts')) self.assertEqual(tr.kstnm, tr.GetHvalue('kstnm'))