Skip to content

Commit e120aa3

Browse files
authored
ENH: Add RLE support for ZCORN (#1487)
1 parent feaf8be commit e120aa3

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/xtgeo/grid3d/_grdecl_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def _to_grdecl_file(self, filename, rle):
342342
if values is None:
343343
continue
344344
filestream.write(f"{kw}\n")
345-
if rle and (kw == "ACTNUM"):
345+
if rle and (kw in ["ACTNUM", "ZCORN"]):
346346
counts, unique_values = run_length_encoding(values)
347347
for i, (count, unique_value) in enumerate(
348348
zip(counts, unique_values)

tests/test_grid3d/test_grid_property.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,22 +309,41 @@ def test_grdecl_rle(tmp_path, testdata_path):
309309
testdata_path / TESTFILE13B, name="SATNUM", grid=gg
310310
)
311311

312+
def _section_tokens(tokens, keyword):
313+
try:
314+
start = tokens.index(keyword) + 1
315+
end = tokens.index("/", start)
316+
except ValueError as exc:
317+
pytest.fail(f"{keyword} section missing or unterminated: {exc}")
318+
return tokens[start:end]
319+
312320
gg.to_file(tmp_path / "grid_rle.grdecl", fformat="grdecl", rle=True)
313321
po.to_file(tmp_path / "grid_satnum_rle.grdecl", fformat="grdecl", rle=True)
314322

315-
# Ensure compression in grid (ACTNUM)
323+
# Ensure compression in grid (ZCORN and ACTNUM)
316324
with open(tmp_path / "grid_rle.grdecl") as f:
317-
fields = f.read().split()
318-
assert len(fields) == 289, "Incorrect field length, fail in compression"
319-
assert "*" in fields[-10]
320-
assert "*" in fields[-8]
321-
assert "*" in fields[-6]
325+
tokens = f.read().split()
326+
assert len(tokens) == 171, "Expected 171 fields with compression enabled"
327+
328+
zcorn_tokens = _section_tokens(tokens, "ZCORN")
329+
actnum_tokens = _section_tokens(tokens, "ACTNUM")
330+
331+
assert any("*" in token for token in zcorn_tokens), (
332+
"expected compression marker in ZCORN section"
333+
)
334+
assert any("*" in token for token in actnum_tokens), (
335+
"expected compression marker in ACTNUM section"
336+
)
322337

323338
# Ensure compression in grid property (SATNUM)
324339
with open(tmp_path / "grid_satnum_rle.grdecl") as f:
325-
content = f.read()
326-
assert "*" in content
327-
assert len(content.split()) == 3, "Incorrect field length, fail in compression"
340+
tokens = f.read().split()
341+
satnum_tokens = _section_tokens(tokens, "SATNUM")
342+
343+
assert len(tokens) == 3, "Expected 3 fields with compression enabled"
344+
assert any("*" in token for token in satnum_tokens), (
345+
"expected compression marker in SATNUM section"
346+
)
328347

329348
grid = xtgeo.grid_from_file(tmp_path / "grid_rle.grdecl")
330349
assert np.array_equal(grid.actnum_array, gg.actnum_array)

0 commit comments

Comments
 (0)