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