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