Skip to content

Commit

Permalink
Fix representation of bools
Browse files Browse the repository at this point in the history
Changed "True" from 1 to -1 to match Eclipse binary file format.
  • Loading branch information
mariuswinger authored and oyvindeide committed Apr 6, 2022
1 parent 3949ce2 commit 3386bd6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ecl_data_io/_unformatted/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/ecl_data_io/_unformatted/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_unformatted_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
],
Expand Down

0 comments on commit 3386bd6

Please sign in to comment.