def test_read_with_wrong_checksum(self): """ """ # read original file gse2file = os.path.join(self.path, 'loc_RJOB20050831023349.z.wrong_chksum') # should fail fp = open(gse2file, 'rb') self.assertRaises(ChksumError, libgse2.read, fp, verify_chksum=True) # should not fail fp.seek(0) libgse2.read(fp, verify_chksum=False) fp.close()
def test_read_and_write(self): """ Writes, reads and compares files created via libgse2. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: header, data = libgse2.read(f) with NamedTemporaryFile() as f: libgse2.write(header, data, f) f.flush() with open(f.name, 'rb') as f2: newheader, newdata = libgse2.read(f2) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_readAndWrite(self): """ Writes, reads and compares files created via libgse2. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: header, data = libgse2.read(f) with NamedTemporaryFile() as f: libgse2.write(header, data, f) f.flush() with open(f.name, 'rb') as f2: newheader, newdata = libgse2.read(f2) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_read_and_write(self): """ Writes, reads and compares files created via libgse2. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: header, data = libgse2.read(f) with NamedTemporaryFile() as f: # raises "UserWarning: Bad value in GSE2 header field" with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) libgse2.write(header, data, f) f.flush() with open(f.name, 'rb') as f2: newheader, newdata = libgse2.read(f2) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_bytes_io(self): """ Checks that reading and writing works via BytesIO. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: fin = io.BytesIO(f.read()) header, data = libgse2.read(fin) # be sure something es actually read self.assertEqual(12000, header['npts']) self.assertEqual(1, data[-1]) fout = io.BytesIO() libgse2.write(header, data, fout) fout.seek(0) newheader, newdata = libgse2.read(fout) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_bytesIO(self): """ Checks that reading and writing works via BytesIO. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: fin = io.BytesIO(f.read()) header, data = libgse2.read(fin) # be sure something es actually read self.assertEqual(12000, header['npts']) self.assertEqual(1, data[-1]) fout = io.BytesIO() libgse2.write(header, data, fout) fout.seek(0) newheader, newdata = libgse2.read(fout) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_chk2_in_cm6(self): """ Tests a file which contains the "CHK2" string in the CM6 encoded string (line 13 of twiceCHK2.gse2). """ with open(os.path.join(self.path, 'twiceCHK2.gse2'), 'rb') as f: header, data = libgse2.read(f, verify_chksum=True) self.assertEqual(header['npts'], 750) np.testing.assert_array_equal(data[-4:], np.array([-139, -153, -169, -156]))
def test_bytes_io(self): """ Checks that reading and writing works via BytesIO. """ gse2file = os.path.join(self.path, 'loc_RNON20040609200559.z') with open(gse2file, 'rb') as f: fin = io.BytesIO(f.read()) header, data = libgse2.read(fin) # be sure something es actually read self.assertEqual(12000, header['npts']) self.assertEqual(1, data[-1]) fout = io.BytesIO() # raises "UserWarning: Bad value in GSE2 header field" with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) libgse2.write(header, data, fout) fout.seek(0) newheader, newdata = libgse2.read(fout) self.assertEqual(header, newheader) np.testing.assert_equal(data, newdata)
def test_read(self): """ Compares waveform data read by libgse2 with an ASCII dump. Checks the first 13 datasamples when reading loc_RJOB20050831023349.z. The values are assumed to be correct. The values were created using getevents. Only checks relative values. """ gse2file = os.path.join(self.path, 'loc_RJOB20050831023349.z') # list of known data samples datalist = [12, -10, 16, 33, 9, 26, 16, 7, 17, 6, 1, 3, -2] f = open(gse2file, 'rb') header, data = libgse2.read(f, verify_chksum=True) self.assertEqual('RJOB', header['station']) self.assertEqual('Z', header['channel']) self.assertEqual(200.0, header['sampling_rate']) self.assertEqual(UTCDateTime(2005, 8, 31, 2, 33, 49, 850000), header['starttime']) self.assertAlmostEqual(9.49e-02, header['calib']) self.assertEqual(1.0, header['gse2']['calper']) self.assertEqual(-1.0, header['gse2']['vang']) self.assertEqual(-1.0, header['gse2']['hang']) self.assertEqual(data[0:13].tolist(), datalist) f.close()