def __get_sample(self, coef, datas): sample = [] if (coef == 2): sample = np.frombuffer(datas, dtype=np.int16) elif (coef == 4): if (self.order == "big"): if (self.type_float == "IBM"): dt = np.dtype(np.uint32) dt = dt.newbyteorder(">") sample = np.frombuffer(datas, dtype=dt) sample = ibm2ieee.ibm2float32(sample) else: dt = np.dtype(np.float32) dt = dt.newbyteorder(">") sample = np.frombuffer(datas, dtype=dt) else: if (self.type_float == "IBM"): dt = np.dtype(np.uint32) dt = dt.newbyteorder("<") sample = np.frombuffer(datas, dtype=dt) sample = ibm2ieee.ibm2float32(sample) else: dt = np.dtype(np.float32) dt = dt.newbyteorder("<") sample = np.frombuffer(datas, dtype=dt) elif (coef == 1): sample = np.frombuffer(datas, dtype=np.int8) return sample
def test_double_to_single(self): for input, expected in double_to_single_pairs: pos_input = np.uint64(input) pos_expected = np.float32(expected) self.assertFloatsIdentical(ibm2float32(pos_input), pos_expected) neg_input = np.uint64(input ^ 0x8000000000000000) neg_expected = -np.float32(expected) self.assertFloatsIdentical(ibm2float32(neg_input), neg_expected)
def test_single_to_single(self): # Inputs with known outputs. for input, expected in single_to_single_pairs: pos_input = np.uint32(input) pos_expected = np.float32(expected) self.assertFloatsIdentical(ibm2float32(pos_input), pos_expected) neg_input = np.uint32(input ^ 0x80000000) neg_expected = -np.float32(expected) self.assertFloatsIdentical(ibm2float32(neg_input), neg_expected)
def test_read_trace_data_ibm(self): from ibm2ieee import ibm2float32 import numpy as np from rss.api import read_trace_data with open(self.segy_file, "rb") as fp: fp.seek(3600 + 240) trace = ibm2float32(np.frombuffer(fp.read(1001 * 4), dtype=">u4")) read_trace_data(self.segy_file, binary_meta, scalco=-100) output_file = os.path.join( os.path.splitext(os.path.basename(self.segy_file))[0], "inlines", "1253", "traces.bin", ) with open(output_file, "rb") as fp: test_trace = np.frombuffer(fp.read(1001 * 4), "<f") np.testing.assert_array_equal(trace, test_trace) try: np.testing.assert_array_equal(trace, test_trace) shutil.rmtree( os.path.splitext(os.path.basename(self.segy_file))[0]) except: shutil.rmtree( os.path.splitext(os.path.basename(self.segy_file))[0]) raise
def test_double_to_single_random_inputs(self): # Random inputs inputs = self.random.randint(2**64, size=32768, dtype=np.uint64) for input in inputs: actual = ibm2float32(input) expected = ibm64ieee32(input) self.assertFloatsIdentical(actual, expected)
def parse_trace(trace_as_bytes, binary_format, override_byteswap=False): trace_data = trace_as_bytes[trace_header_size:] if binary_format == 1: trace_data = ibm2float32(np.frombuffer(trace_data, dtype=">u4")) elif binary_format == 5: trace_data = np.frombuffer(trace_data, dtype=">f") if not override_byteswap: trace_data = trace_data.byteswap() else: fmt = segy_format[float_format] raise RuntimeError(f"binary format {fmt} not supported.") return trace_data