|
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
8 | 8 | import dask |
| 9 | +import numpy as np |
9 | 10 | import pytest |
10 | 11 | import xarray.testing as xrt |
11 | 12 | from tests.integration.conftest import get_segy_mock_obn_spec |
@@ -158,3 +159,104 @@ def test_import_obn_without_calculate_shot_index_raises( |
158 | 159 | error_message = str(exc_info.value) |
159 | 160 | assert "shot_index" in error_message |
160 | 161 | assert "ObnReceiverGathers3D" in error_message |
| 162 | + |
| 163 | + |
| 164 | +class TestImportObnMultilineTypeA: |
| 165 | + """Test OBN SEG-Y import with multiple shot lines and Type A geometry. |
| 166 | +
|
| 167 | + This test class verifies the fix for a bug where analyze_lines_for_guns() |
| 168 | + would return early upon detecting Type A geometry, leaving the |
| 169 | + unique_guns_per_line dictionary incomplete. This caused KeyError when |
| 170 | + CalculateShotIndex.transform() tried to access shot lines that weren't |
| 171 | + in the dictionary. |
| 172 | +
|
| 173 | + Regression test for: KeyError when ingesting OBN data with multiple shot |
| 174 | + lines where Type A geometry is detected on an earlier line. |
| 175 | + """ |
| 176 | + |
| 177 | + def test_import_obn_multiline_type_a_all_lines_processed( |
| 178 | + self, |
| 179 | + segy_mock_obn_multiline_type_a: Path, |
| 180 | + zarr_tmp: Path, |
| 181 | + ) -> None: |
| 182 | + """Test that all shot lines are processed with Type A geometry. |
| 183 | +
|
| 184 | + This test verifies that: |
| 185 | + 1. CalculateShotIndex works with Type A geometry (non-interleaved shots) |
| 186 | + 2. All shot lines are included in the output, not just the first one |
| 187 | + 3. shot_index is correctly calculated for Type A (0-based from unique values) |
| 188 | + """ |
| 189 | + segy_spec = get_segy_mock_obn_spec(include_component=True) |
| 190 | + grid_override = {"CalculateShotIndex": True} |
| 191 | + |
| 192 | + segy_to_mdio( |
| 193 | + segy_spec=segy_spec, |
| 194 | + mdio_template=TemplateRegistry().get("ObnReceiverGathers3D"), |
| 195 | + input_path=segy_mock_obn_multiline_type_a, |
| 196 | + output_path=zarr_tmp, |
| 197 | + overwrite=True, |
| 198 | + grid_overrides=grid_override, |
| 199 | + ) |
| 200 | + |
| 201 | + ds = open_mdio(zarr_tmp) |
| 202 | + |
| 203 | + # Verify ALL shot lines are present (the bug would cause lines to be missing) |
| 204 | + expected_shot_lines = [1, 2, 3] |
| 205 | + xrt.assert_duckarray_equal(ds["shot_line"], expected_shot_lines) |
| 206 | + |
| 207 | + # Verify guns are present |
| 208 | + expected_guns = [1, 2] |
| 209 | + xrt.assert_duckarray_equal(ds["gun"], expected_guns) |
| 210 | + |
| 211 | + # Verify shot_index is calculated correctly for Type A geometry |
| 212 | + # Type A: shot points [1, 2, 3] are already unique per gun |
| 213 | + # shot_index should be 0-based indices: [0, 1, 2] |
| 214 | + expected_shot_index = [0, 1, 2] |
| 215 | + xrt.assert_duckarray_equal(ds["shot_index"], expected_shot_index) |
| 216 | + |
| 217 | + # Verify other dimensions |
| 218 | + expected_receivers = [101, 102] |
| 219 | + xrt.assert_duckarray_equal(ds["receiver"], expected_receivers) |
| 220 | + |
| 221 | + expected_components = [1] |
| 222 | + xrt.assert_duckarray_equal(ds["component"], expected_components) |
| 223 | + |
| 224 | + # Verify shot_point is preserved as a coordinate |
| 225 | + assert "shot_point" in ds.coords |
| 226 | + assert ds["shot_point"].dims == ("shot_line", "gun", "shot_index") |
| 227 | + |
| 228 | + def test_import_obn_multiline_type_a_sparse_shot_points( |
| 229 | + self, |
| 230 | + segy_mock_obn_multiline_type_a_sparse: Path, |
| 231 | + zarr_tmp: Path, |
| 232 | + ) -> None: |
| 233 | + """Test Type A shot_index mapping with sparse, non-contiguous shot points. |
| 234 | +
|
| 235 | + Guards the vectorized Type A path (np.searchsorted over np.unique) by |
| 236 | + using shot points that are not 0/1-based or contiguous. shot_index must |
| 237 | + be a dense 0-based sequence over the sorted unique shot points, and the |
| 238 | + original shot_point values must be preserved as a coordinate. |
| 239 | + """ |
| 240 | + segy_spec = get_segy_mock_obn_spec(include_component=True) |
| 241 | + grid_override = {"CalculateShotIndex": True} |
| 242 | + |
| 243 | + segy_to_mdio( |
| 244 | + segy_spec=segy_spec, |
| 245 | + mdio_template=TemplateRegistry().get("ObnReceiverGathers3D"), |
| 246 | + input_path=segy_mock_obn_multiline_type_a_sparse, |
| 247 | + output_path=zarr_tmp, |
| 248 | + overwrite=True, |
| 249 | + grid_overrides=grid_override, |
| 250 | + ) |
| 251 | + |
| 252 | + ds = open_mdio(zarr_tmp) |
| 253 | + |
| 254 | + # Sparse shot points [10, 50, 100] map to dense 0-based indices [0, 1, 2] |
| 255 | + expected_shot_index = [0, 1, 2] |
| 256 | + xrt.assert_duckarray_equal(ds["shot_index"], expected_shot_index) |
| 257 | + |
| 258 | + # Original shot_point values preserved as coordinate, not remapped |
| 259 | + assert "shot_point" in ds.coords |
| 260 | + assert ds["shot_point"].dims == ("shot_line", "gun", "shot_index") |
| 261 | + unique_shot_points = np.unique(ds["shot_point"].values) |
| 262 | + xrt.assert_duckarray_equal(unique_shot_points, [10, 50, 100]) |
0 commit comments