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