def test_float64(self): logging.debug('') logging.debug('test_float64') # Double precision. data = numpy.arange(0, 10, dtype=numpy.float64) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Unformatted scalar. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, unformatted=True) stream.write_float(1., full_record=True) self.assertEqual(os.path.getsize(self.filename), 16) with open(self.filename, 'rb') as inp: new_data = inp.read() self.assertEqual(new_data, UNF_R8) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_float() try: self.assertEqual(new_data, 1.) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) new_data = stream.read_float(full_record=True) self.assertEqual(new_data, 1.) # Unformatted array. data = numpy.arange(1, 9, dtype=numpy.float64) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, unformatted=True) stream.write_floats(data, full_record=True) self.assertEqual(os.path.getsize(self.filename), 72) with open(self.filename, 'rb') as inp: new_data = inp.read() self.assertEqual(new_data, UNF_R8A) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats(data.size) try: numpy.testing.assert_array_equal(new_data, data) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) new_data = stream.read_floats(data.size, full_record=True) numpy.testing.assert_array_equal(new_data, data) # Write as single precision. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 32) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Row-major. data = numpy.arange(0, 10, dtype=numpy.float64) arr2d = data.reshape((5, 2)) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(arr2d) self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats((5, 2)) numpy.testing.assert_array_equal(new_data, arr2d) # Column-major. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(arr2d, order='Fortran') self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats((5, 2)) try: numpy.testing.assert_array_equal(new_data, arr2d) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats((5, 2), order='Fortran') numpy.testing.assert_array_equal(new_data, arr2d) # Text. with open(self.filename, 'w') as out: stream = Stream(out) stream.write_floats(arr2d, order='Fortran', linecount=4) with open(self.filename, 'r') as inp: stream = Stream(inp) new_data = stream.read_floats((5, 2)) try: numpy.testing.assert_array_equal(new_data, arr2d) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'r') as inp: stream = Stream(inp) new_data = stream.read_floats((5, 2), order='Fortran') numpy.testing.assert_array_equal(new_data, arr2d)
def test_float32(self): logging.debug('') logging.debug('test_float32') # Single precision. data = numpy.arange(0, 10, dtype=numpy.float32) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 40) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Text scalar. with open(self.filename, 'w') as out: stream = Stream(out) stream.write_float(4., sep=' ') stream.write_float(2., full_record=True) size = 5 if sys.platform == 'win32' else 4 # CR LF self.assertEqual(os.path.getsize(self.filename), size) with open(self.filename, 'r') as inp: new_data = inp.read() self.assertEqual(new_data, '4 2\n') # Unformatted scalar. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True, unformatted=True) stream.write_float(1., full_record=True) self.assertEqual(os.path.getsize(self.filename), 12) with open(self.filename, 'rb') as inp: new_data = inp.read() self.assertEqual(new_data, UNF_R4) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_float() try: self.assertEqual(new_data, 1.) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True, unformatted=True) new_data = stream.read_float(full_record=True) self.assertEqual(new_data, 1.) # Unformatted array. data = numpy.arange(1, 9, dtype=numpy.float32) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True, unformatted=True) stream.write_floats(data, full_record=True) self.assertEqual(os.path.getsize(self.filename), 40) with open(self.filename, 'rb') as inp: new_data = inp.read() self.assertEqual(new_data, UNF_R4A) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_floats(data.size) try: numpy.testing.assert_array_equal(new_data, data) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True, unformatted=True) new_data = stream.read_floats(data.size, full_record=True) numpy.testing.assert_array_equal(new_data, data) # Byteswapped. swap_endian = sys.byteorder == 'little' wrong_endian = not swap_endian data = numpy.arange(0, 10, dtype=numpy.float32) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True, big_endian=swap_endian) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 40) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True, big_endian=wrong_endian) new_data = stream.read_floats(data.size) try: numpy.testing.assert_array_equal(new_data, data) except AssertionError: pass else: self.fail('Expected AssertionError') with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True, big_endian=swap_endian) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Write as double precision. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Write from list. data = list(data) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 40) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_floats(len(data)) numpy.testing.assert_array_equal(new_data, data)