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_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)