def test_issue171(self): """ Test for issue #171. """ tr = read()[0] with NamedTemporaryFile() as tf: tempfile = tf.name tr.write(tempfile, format="SAC") with open(tempfile, "rb") as fh: trace = SacIO(fh) trace.set_header_value('stel', 91.0) # Open with modification! with open(tempfile, "rb+") as fh: trace.write_sac_header(fh) with open(tempfile, "rb") as fh: SacIO(fh)
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)