Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)