|
| 1 | +"""Round-trip tests for SEG-Y files that declare IBM float (``ibm32``) trace-header fields. |
| 2 | +
|
| 3 | +These guard the IBM-real header data path end-to-end through real file I/O: |
| 4 | +
|
| 5 | +* Ingestion (Defect A): mdio used to persist an ``ibm32`` header in its raw ``uint32`` slot, |
| 6 | + casting the decoded float down to an integer. That truncated decimals (``118.625`` -> |
| 7 | + ``118``) and wrapped the sign of negatives (``-50.25`` -> a huge unsigned int). The fix |
| 8 | + promotes ``ibm32`` header fields to ``float32`` so the decoded value is stored faithfully. |
| 9 | +* Export (Defect B): SegyFactory IBM-encodes ``ibm32`` *header* fields from ``segy`` 0.6.0 |
| 10 | + onward (the minimum mdio requires), so the full round-trip exercises that encode/decode. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +import fsspec |
| 18 | +import numpy as np |
| 19 | +from segy.factory import SegyFactory |
| 20 | +from segy.schema import HeaderField |
| 21 | +from segy.standards import get_segy_standard |
| 22 | + |
| 23 | +from mdio import mdio_to_segy |
| 24 | +from mdio.api.io import open_mdio |
| 25 | +from mdio.builder.template_registry import TemplateRegistry |
| 26 | +from mdio.converters.segy import segy_to_mdio |
| 27 | +from mdio.segy.file import SegyFileWrapper |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + from pathlib import Path |
| 31 | + |
| 32 | + import pytest |
| 33 | + from segy.schema import SegySpec |
| 34 | + |
| 35 | +# Dyadic rationals chosen so they are exactly representable in IBM hex float, letting us assert |
| 36 | +# exact equality. They cover the original failure modes: decimal truncation and sign wrapping. |
| 37 | +IBM_HEADER_VALUES = np.array([118.625, -50.25, 0.5, -1.5], dtype="float32") |
| 38 | + |
| 39 | +GRID_INLINES = 3 |
| 40 | +GRID_CROSSLINES = 4 |
| 41 | +NUM_SAMPLES = 8 |
| 42 | +IBM_FIELD_NAME = "ibm_attr" |
| 43 | + |
| 44 | + |
| 45 | +def _ibm32_segy_spec() -> SegySpec: |
| 46 | + """Build a rev1 SegySpec with an ``ibm32`` trace-header field alongside index/coord fields.""" |
| 47 | + fields = [ |
| 48 | + HeaderField(name="inline", byte=189, format="int32"), |
| 49 | + HeaderField(name="crossline", byte=193, format="int32"), |
| 50 | + HeaderField(name="coordinate_scalar", byte=71, format="int16"), |
| 51 | + HeaderField(name="cdp_x", byte=181, format="int32"), |
| 52 | + HeaderField(name="cdp_y", byte=185, format="int32"), |
| 53 | + HeaderField(name="samples_per_trace", byte=115, format="int16"), |
| 54 | + HeaderField(name="sample_interval", byte=117, format="int16"), |
| 55 | + # Unassigned rev1 bytes 233-240: safe spot for a custom IBM-float attribute. |
| 56 | + HeaderField(name=IBM_FIELD_NAME, byte=233, format="ibm32"), |
| 57 | + ] |
| 58 | + spec = get_segy_standard(1).customize(trace_header_fields=fields) |
| 59 | + spec.segy_standard = 1 |
| 60 | + return spec |
| 61 | + |
| 62 | + |
| 63 | +def _ibm_values_per_trace(num_traces: int) -> np.ndarray: |
| 64 | + """Tile the sample IBM values to cover every trace.""" |
| 65 | + reps = int(np.ceil(num_traces / IBM_HEADER_VALUES.size)) |
| 66 | + return np.tile(IBM_HEADER_VALUES, reps)[:num_traces].astype("float32") |
| 67 | + |
| 68 | + |
| 69 | +def _write_ibm32_segy(path: Path, spec: SegySpec) -> np.ndarray: |
| 70 | + """Write a small 3D SEG-Y whose ``ibm32`` header holds real IBM-float values. |
| 71 | +
|
| 72 | + ``segy`` >= 0.6.0 exposes ``ibm32`` header fields as ``float32`` in the factory template and |
| 73 | + IBM-encodes them on write, so real float values are assigned directly. |
| 74 | +
|
| 75 | + Args: |
| 76 | + path: Destination path for the generated SEG-Y file. |
| 77 | + spec: SegySpec describing the trace header layout (must declare the ibm32 field). |
| 78 | +
|
| 79 | + Returns: |
| 80 | + The IBM header value assigned to each trace, in trace order. |
| 81 | + """ |
| 82 | + num_traces = GRID_INLINES * GRID_CROSSLINES |
| 83 | + factory = SegyFactory(spec=spec, samples_per_trace=NUM_SAMPLES) |
| 84 | + samples = factory.create_trace_sample_template(num_traces) |
| 85 | + headers = factory.create_trace_header_template(num_traces) |
| 86 | + |
| 87 | + inlines, crosslines = np.mgrid[0:GRID_INLINES, 0:GRID_CROSSLINES] |
| 88 | + headers["inline"] = (10 + inlines).ravel() |
| 89 | + headers["crossline"] = (100 + crosslines * 2).ravel() |
| 90 | + headers["coordinate_scalar"] = -100 |
| 91 | + headers["cdp_x"] = (700_000 + inlines * 100).ravel() |
| 92 | + headers["cdp_y"] = (4_000_000 + crosslines * 100).ravel() |
| 93 | + headers["samples_per_trace"] = NUM_SAMPLES |
| 94 | + headers["sample_interval"] = 4000 |
| 95 | + |
| 96 | + ibm_values = _ibm_values_per_trace(num_traces) |
| 97 | + headers[IBM_FIELD_NAME] = ibm_values |
| 98 | + |
| 99 | + samples[:] = (np.arange(num_traces) + 1)[:, None] |
| 100 | + |
| 101 | + with fsspec.open(path.as_posix(), mode="wb") as fp: |
| 102 | + fp.write(factory.create_textual_header()) |
| 103 | + fp.write(factory.create_binary_header()) |
| 104 | + fp.write(factory.create_traces(headers, samples)) |
| 105 | + |
| 106 | + return ibm_values |
| 107 | + |
| 108 | + |
| 109 | +def test_ingested_ibm32_header_preserves_value(tmp_path: Path) -> None: |
| 110 | + """SEG-Y -> MDIO must store ibm32 headers as float32 without truncating or wrapping (Defect A).""" |
| 111 | + spec = _ibm32_segy_spec() |
| 112 | + segy_path = tmp_path / "ibm32.sgy" |
| 113 | + mdio_path = tmp_path / "ibm32.mdio" |
| 114 | + |
| 115 | + expected = _write_ibm32_segy(segy_path, spec) |
| 116 | + |
| 117 | + segy_to_mdio( |
| 118 | + segy_spec=spec, |
| 119 | + mdio_template=TemplateRegistry().get("PostStack3DTime"), |
| 120 | + input_path=segy_path, |
| 121 | + output_path=mdio_path, |
| 122 | + overwrite=True, |
| 123 | + ) |
| 124 | + |
| 125 | + headers = open_mdio(mdio_path)["headers"].values |
| 126 | + stored = headers[IBM_FIELD_NAME].ravel() |
| 127 | + |
| 128 | + assert stored.dtype == np.float32 |
| 129 | + np.testing.assert_array_equal(stored, expected) |
| 130 | + |
| 131 | + |
| 132 | +def test_ibm32_header_full_roundtrip(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 133 | + """SEG-Y -> MDIO -> SEG-Y must preserve ibm32 header values end-to-end.""" |
| 134 | + # The SEG-Y file headers must be persisted at ingest so they can be re-emitted on export. |
| 135 | + monkeypatch.setenv("MDIO__IMPORT__SAVE_SEGY_FILE_HEADER", "true") |
| 136 | + |
| 137 | + spec = _ibm32_segy_spec() |
| 138 | + segy_path = tmp_path / "ibm32.sgy" |
| 139 | + mdio_path = tmp_path / "ibm32.mdio" |
| 140 | + segy_rt_path = tmp_path / "ibm32_rt.sgy" |
| 141 | + |
| 142 | + expected = _write_ibm32_segy(segy_path, spec) |
| 143 | + |
| 144 | + segy_to_mdio( |
| 145 | + segy_spec=spec, |
| 146 | + mdio_template=TemplateRegistry().get("PostStack3DTime"), |
| 147 | + input_path=segy_path, |
| 148 | + output_path=mdio_path, |
| 149 | + overwrite=True, |
| 150 | + ) |
| 151 | + mdio_to_segy(segy_spec=spec, input_path=mdio_path, output_path=segy_rt_path) |
| 152 | + |
| 153 | + roundtripped = SegyFileWrapper(segy_rt_path.as_posix(), spec=spec).trace[:].header[IBM_FIELD_NAME] |
| 154 | + np.testing.assert_array_equal(roundtripped, expected) |
0 commit comments