From 3386bd61c7acf253a30d8584998c382ae95b863a Mon Sep 17 00:00:00 2001 From: Marius Lie Winger Date: Mon, 21 Mar 2022 15:35:19 +0100 Subject: [PATCH] Fix representation of bools Changed "True" from 1 to -1 to match Eclipse binary file format. --- src/ecl_data_io/_unformatted/read.py | 2 +- src/ecl_data_io/_unformatted/write.py | 8 ++++---- tests/test_unformatted_write.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ecl_data_io/_unformatted/read.py b/src/ecl_data_io/_unformatted/read.py index 348ce80..5998df6 100644 --- a/src/ecl_data_io/_unformatted/read.py +++ b/src/ecl_data_io/_unformatted/read.py @@ -121,7 +121,7 @@ def _read(self): self._read_record_marker(16) if self._type == b"X231": self._read_record_marker(16) - self._length *= -(2 ** 31) + self._length *= -(2**31) previous_keyword = self._keyword self._read_keyword() if previous_keyword != self._keyword: diff --git a/src/ecl_data_io/_unformatted/write.py b/src/ecl_data_io/_unformatted/write.py index 9b5a256..ead9afc 100644 --- a/src/ecl_data_io/_unformatted/write.py +++ b/src/ecl_data_io/_unformatted/write.py @@ -15,9 +15,9 @@ def write_array_header(stream, kw_str, type_str, size): if not ecl_types.is_valid_type(type_str): raise EclWriteError(f"Not a valid ecl type: {type_str}") - if size > 2 ** 31: - write_array_header(stream, kw_str, b"X231", -(size // (2 ** 31))) - size %= 2 ** 31 + if size > 2**31: + write_array_header(stream, kw_str, b"X231", -(size // (2**31))) + size %= 2**31 stream.write((16).to_bytes(4, byteorder="big", signed=True)) stream.write(kw_str.encode("ascii")) @@ -30,7 +30,7 @@ def cast_array_to_ecl(arr): if arr.dtype in [np.int32, np.float32, np.float64]: return arr.astype(arr.dtype.newbyteorder(">")) if np.issubdtype(arr.dtype, np.bool_): - return arr.astype(">i4") + return -arr.astype(">i4") elif np.issubdtype(arr.dtype, np.integer): result_dtype = ">i4" elif np.issubdtype(arr.dtype, np.floating): diff --git a/tests/test_unformatted_write.py b/tests/test_unformatted_write.py index 96f9331..696cc72 100644 --- a/tests/test_unformatted_write.py +++ b/tests/test_unformatted_write.py @@ -55,7 +55,7 @@ def test_write_array_header_not_a_type(): (np.array([1], dtype=np.int32), np.array([1], dtype=np.int32)), (np.array([1.0], dtype=np.float32), np.array([1.0], dtype=np.float32)), (np.array([1.0], dtype=np.float64), np.array([1.0], dtype=np.float64)), - (np.array([True, False], dtype=np.bool_), np.array([1, 0], dtype=np.int32)), + (np.array([True, False], dtype=np.bool_), np.array([-1, 0], dtype=np.int32)), (np.array([10, 20], dtype=np.int8), np.array([10, 20], dtype=np.int32)), (np.array([1.0], dtype=np.float128), np.array([1.0], dtype=np.float64)), ],