Skip to content

Commit 1cf03ee

Browse files
authored
BUG: Fix SEGY cube export producing non-UTF8 byte in binary header (#1543)
1 parent 0c7e047 commit 1cf03ee

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/xtgeo/cube/_cube_export.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def export_segy(cube: Cube, sfile: str) -> None:
8888
f.bin[segyio.BinField.SortingCode] = 4 # trace sorting from C: needed?
8989
# TODO: Make this read from cube._measurement (or something)
9090
f.bin[segyio.BinField.MeasurementSystem] = 1
91+
text_header = bytearray(f.text[0])
92+
text_header[-1] = 0x20 # ASCII space
93+
f.text[0] = bytes(text_header)
9194

9295

9396
def export_rmsreg(self, sfile):

tests/test_cube/test_cube.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,24 @@ def test_segy_export_to_bytesio_raises():
242242
cube.to_file(stream, fformat="segy")
243243

244244

245+
def test_segy_export_sanitizes_text_header_last_byte(
246+
tmp_path: pathlib.Path,
247+
) -> None:
248+
"""Text header last byte is a safe EBCDIC space after export."""
249+
cube = xtgeo.Cube(ncol=3, nrow=2, nlay=5, xinc=10, yinc=10, zinc=1)
250+
cube.values = list(range(30))
251+
outfile = tmp_path / "cube_text_header_sanitized.segy"
252+
253+
cube.to_file(outfile, fformat="segy")
254+
raw = outfile.read_bytes()
255+
assert raw[3199] == 0x40 # EBCDIC space
256+
assert raw[3199] != 0x80
257+
258+
with segyio.open(outfile, "r") as seg:
259+
assert seg.bin[segyio.BinField.Interval] == 1000
260+
assert seg.bin[segyio.BinField.Samples] == 5
261+
262+
245263
def test_cube_resampling(loadsfile1):
246264
"""Import a cube, then make a smaller and resample, then export the new"""
247265

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)