def test_readAndWriteTraces(self): """ Writes, reads and compares files created via obspy.mseed. This uses all possible encodings, record lengths and the byte order options. A re-encoded SEED file should still have the same values regardless of write options. Note: Test currently only tests the first trace """ mseed_file = os.path.join(self.path, 'data', 'test.mseed') stream = readMSEED(mseed_file) # Define test ranges record_length_values = [2**i for i in range(8, 21)] encoding_values = { "ASCII": "|S1", "INT16": "int16", "INT32": "int32", "FLOAT32": "float32", "FLOAT64": "float64", "STEIM1": "int32", "STEIM2": "int32" } byteorder_values = ['>', '<'] # Loop over every combination. for reclen in record_length_values: for byteorder in byteorder_values: for encoding in encoding_values.keys(): this_stream = copy.deepcopy(stream) this_stream[0].data = \ np.require(this_stream[0].data, dtype=encoding_values[encoding]) temp_file = NamedTemporaryFile().name writeMSEED(this_stream, temp_file, encoding=encoding, byteorder=byteorder, reclen=reclen) new_stream = readMSEED(temp_file) # Assert the new stream still has the chosen attributes. # This should mean that writing as well as reading them # works. self.assertEqual(new_stream[0].stats.mseed.byteorder, byteorder) self.assertEqual(new_stream[0].stats.mseed.record_length, reclen) self.assertEqual(new_stream[0].stats.mseed.encoding, encoding) np.testing.assert_array_equal(this_stream[0].data, new_stream[0].data) os.remove(temp_file)
def test_writeWithDateTimeBefore1970(self): """ Write an stream via libmseed with a datetime before 1970. This test depends on the platform specific localtime()/gmtime() function. """ # create trace tr = Trace(data=np.empty(1000)) tr.stats.starttime = UTCDateTime("1969-01-01T00:00:00") # write file with NamedTemporaryFile() as tf: tempfile = tf.name writeMSEED(Stream([tr]), tempfile, format="MSEED") # read again stream = readMSEED(tempfile) stream.verify()
def test_oneSampleOverlap(self): """ Both methods readMSTraces and readMSTracesViaRecords should recognize a single sample overlap. """ # create a stream with one sample overlapping trace1 = Trace(data=np.zeros(1000)) trace2 = Trace(data=np.zeros(10)) trace2.stats.starttime = UTCDateTime(999) st = Stream([trace1, trace2]) # write into MSEED with NamedTemporaryFile() as tf: tempfile = tf.name writeMSEED(st, tempfile, format="MSEED") # read it again new_stream = readMSEED(tempfile) self.assertEqual(len(new_stream), 2)
def test_writeIntegers(self): """ Write integer array via L{obspy.mseed.mseed.writeMSEED}. """ npts = 1000 # data array of integers - float won't work! np.random.seed(815) # make test reproducable data = np.random.randint(-1000, 1000, npts).astype('int32') st = Stream([Trace(data=data)]) with NamedTemporaryFile() as tf: tempfile = tf.name # write writeMSEED(st, tempfile, format="MSEED") # read again stream = readMSEED(tempfile) stream.verify() np.testing.assert_array_equal(stream[0].data, data)
def test_writeWithDateTimeBefore1970(self): """ Write an stream via libmseed with a datetime before 1970. This test depends on the platform specific localtime()/gmtime() function. """ # create trace tr = Trace(data=np.empty(1000)) tr.stats.starttime = UTCDateTime("1969-01-01T00:00:00") # write file tempfile = NamedTemporaryFile().name writeMSEED(Stream([tr]), tempfile, format="MSEED") # read again stream = readMSEED(tempfile) os.remove(tempfile) stream.verify()
def test_readAndWriteFileWithGaps(self): """ Tests reading and writing files with more than one trace. """ filename = os.path.join(self.path, 'data', 'gaps.mseed') # Read file and test if all traces are being read. stream = readMSEED(filename) self.assertEqual(len(stream), 4) # Write File to temporary file. with NamedTemporaryFile() as tf: outfile = tf.name writeMSEED(copy.deepcopy(stream), outfile) # Read the same file again and compare it to the original file. new_stream = readMSEED(outfile) self.assertEqual(len(stream), len(new_stream)) # Compare new_trace_list with trace_list for tr1, tr2 in zip(stream, new_stream): self.assertEqual(tr1.stats, tr2.stats) np.testing.assert_array_equal(tr1.data, tr2.data)
def test_readAndWriteTraces(self): """ Writes, reads and compares files created via obspy.mseed. This uses all possible encodings, record lengths and the byte order options. A re-encoded SEED file should still have the same values regardless of write options. Note: Test currently only tests the first trace """ mseed_file = os.path.join(self.path, 'data', 'test.mseed') stream = readMSEED(mseed_file) # Define test ranges record_length_values = [2 ** i for i in range(8, 21)] encoding_values = {"ASCII": "|S1", "INT16": "int16", "INT32": "int32", "FLOAT32": "float32", "FLOAT64": "float64", "STEIM1": "int32", "STEIM2": "int32"} byteorder_values = ['>', '<'] # Loop over every combination. for reclen in record_length_values: for byteorder in byteorder_values: for encoding in encoding_values.keys(): this_stream = copy.deepcopy(stream) this_stream[0].data = \ np.require(this_stream[0].data, dtype=encoding_values[encoding]) temp_file = NamedTemporaryFile().name writeMSEED(this_stream, temp_file, encoding=encoding, byteorder=byteorder, reclen=reclen) new_stream = readMSEED(temp_file) # Assert the new stream still has the chosen attributes. # This should mean that writing as well as reading them # works. self.assertEqual(new_stream[0].stats.mseed.byteorder, byteorder) self.assertEqual(new_stream[0].stats.mseed.record_length, reclen) self.assertEqual(new_stream[0].stats.mseed.encoding, encoding) np.testing.assert_array_equal(this_stream[0].data, new_stream[0].data) os.remove(temp_file)
def test_bugWriteReadFloat32SEEDWin32(self): """ Test case for issue #64. """ # create stream object data = np.array([395.07809448, 395.0782, 1060.28112793, -1157.37487793, -1236.56237793, 355.07028198, -1181.42175293], dtype=np.float32) st = Stream([Trace(data=data)]) tempfile = NamedTemporaryFile().name writeMSEED(st, tempfile, format="MSEED") # read temp file directly without libmseed bin_data = open(tempfile, "rb").read() bin_data = np.array(unpack(">7f", bin_data[56:84])) np.testing.assert_array_equal(data, bin_data) # read via ObsPy st2 = readMSEED(tempfile) os.remove(tempfile) # test results np.testing.assert_array_equal(data, st2[0].data)
def test_bugWriteReadFloat32SEEDWin32(self): """ Test case for issue #64. """ # create stream object data = np.array([395.07809448, 395.0782, 1060.28112793, -1157.37487793, -1236.56237793, 355.07028198, -1181.42175293], dtype=np.float32) st = Stream([Trace(data=data)]) with NamedTemporaryFile() as tf: tempfile = tf.name writeMSEED(st, tempfile, format="MSEED") # read temp file directly without libmseed with open(tempfile, 'rb') as fp: fp.seek(56) bin_data = np.fromfile(fp, dtype='>f4', count=7) np.testing.assert_array_equal(data, bin_data) # read via ObsPy st2 = readMSEED(tempfile) # test results np.testing.assert_array_equal(data, st2[0].data)
def test_bugWriteReadFloat32SEEDWin32(self): """ Test case for issue #64. """ # create stream object data = np.array([ 395.07809448, 395.0782, 1060.28112793, -1157.37487793, -1236.56237793, 355.07028198, -1181.42175293 ], dtype=np.float32) st = Stream([Trace(data=data)]) tempfile = NamedTemporaryFile().name writeMSEED(st, tempfile, format="MSEED") # read temp file directly without libmseed bin_data = open(tempfile, "rb").read() bin_data = np.array(unpack(">7f", bin_data[56:84])) np.testing.assert_array_equal(data, bin_data) # read via ObsPy st2 = readMSEED(tempfile) os.remove(tempfile) # test results np.testing.assert_array_equal(data, st2[0].data)
def test_bugWriteReadFloat32SEEDWin32(self): """ Test case for issue #64. """ # create stream object data = np.array([ 395.07809448, 395.0782, 1060.28112793, -1157.37487793, -1236.56237793, 355.07028198, -1181.42175293 ], dtype=np.float32) st = Stream([Trace(data=data)]) with NamedTemporaryFile() as tf: tempfile = tf.name writeMSEED(st, tempfile, format="MSEED") # read temp file directly without libmseed with open(tempfile, 'rb') as fp: fp.seek(56) bin_data = np.fromfile(fp, dtype='>f4', count=7) np.testing.assert_array_equal(data, bin_data) # read via ObsPy st2 = readMSEED(tempfile) # test results np.testing.assert_array_equal(data, st2[0].data)