|
| 1 | +"""Unit tests for ZGY to MDIO conversion.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pytest |
| 11 | +import xarray as xr |
| 12 | + |
| 13 | +from mdio.api.io import open_mdio |
| 14 | +from mdio.converters.zgy import zgy_to_mdio |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from pathlib import Path |
| 18 | + |
| 19 | + |
| 20 | +def _create_mock_zgy_dataset( |
| 21 | + n_ilines: int = 5, |
| 22 | + n_xlines: int = 10, |
| 23 | + n_samples: int = 50, |
| 24 | + vertical_dim: str = "time", |
| 25 | +) -> xr.Dataset: |
| 26 | + """Create a mock xarray Dataset simulating pyzgy output. |
| 27 | +
|
| 28 | + Args: |
| 29 | + n_ilines: Number of inlines. |
| 30 | + n_xlines: Number of crosslines. |
| 31 | + n_samples: Number of samples. |
| 32 | + vertical_dim: Name of vertical dimension ('time' or 'depth'). |
| 33 | +
|
| 34 | + Returns: |
| 35 | + Mock xarray Dataset. |
| 36 | + """ |
| 37 | + rng = np.random.default_rng(seed=42) |
| 38 | + |
| 39 | + data = rng.standard_normal((n_ilines, n_xlines, n_samples)).astype(np.float32) |
| 40 | + |
| 41 | + return xr.Dataset( |
| 42 | + {"data": (["iline", "xline", vertical_dim], data)}, |
| 43 | + coords={ |
| 44 | + "iline": np.arange(1, n_ilines + 1), |
| 45 | + "xline": np.arange(1, n_xlines + 1), |
| 46 | + vertical_dim: np.arange(0, n_samples * 4, 4), |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +class TestZgyToMdio: |
| 52 | + """Tests for zgy_to_mdio function.""" |
| 53 | + |
| 54 | + def test_output_exists_no_overwrite(self, tmp_path: Path) -> None: |
| 55 | + """Test that FileExistsError is raised when output exists and overwrite=False.""" |
| 56 | + output_path = tmp_path / "output.mdio" |
| 57 | + output_path.mkdir() |
| 58 | + |
| 59 | + mock_pyzgy = MagicMock() |
| 60 | + with ( |
| 61 | + patch.dict("sys.modules", {"pyzgy": mock_pyzgy}), |
| 62 | + pytest.raises(FileExistsError, match="exists"), |
| 63 | + ): |
| 64 | + zgy_to_mdio("input.zgy", output_path, overwrite=False) |
| 65 | + |
| 66 | + def test_pyzgy_not_installed(self, tmp_path: Path) -> None: |
| 67 | + """Test that ImportError is raised when pyzgy is not installed.""" |
| 68 | + output_path = tmp_path / "output.mdio" |
| 69 | + |
| 70 | + with ( |
| 71 | + patch.dict("sys.modules", {"pyzgy": None}), |
| 72 | + pytest.raises(ImportError, match="pyzgy"), |
| 73 | + ): |
| 74 | + zgy_to_mdio("input.zgy", output_path) |
| 75 | + |
| 76 | + def test_full_conversion_time_domain(self, tmp_path: Path) -> None: |
| 77 | + """Test full conversion workflow for time-domain data.""" |
| 78 | + mock_ds = _create_mock_zgy_dataset(n_ilines=5, n_xlines=10, n_samples=50, vertical_dim="time") |
| 79 | + output_path = tmp_path / "output.mdio" |
| 80 | + |
| 81 | + mock_pyzgy = MagicMock() |
| 82 | + with ( |
| 83 | + patch.dict("sys.modules", {"pyzgy": mock_pyzgy}), |
| 84 | + patch("xarray.open_dataset", return_value=mock_ds), |
| 85 | + ): |
| 86 | + zgy_to_mdio("test.zgy", output_path, overwrite=True) |
| 87 | + |
| 88 | + # Verify output |
| 89 | + assert output_path.exists() |
| 90 | + |
| 91 | + ds = open_mdio(output_path) |
| 92 | + |
| 93 | + # Check dimensions were renamed |
| 94 | + assert "inline" in ds.dims |
| 95 | + assert "crossline" in ds.dims |
| 96 | + assert "time" in ds.dims |
| 97 | + assert ds.sizes["inline"] == 5 |
| 98 | + assert ds.sizes["crossline"] == 10 |
| 99 | + assert ds.sizes["time"] == 50 |
| 100 | + |
| 101 | + # Check data variable renamed |
| 102 | + assert "amplitude" in ds.data_vars |
| 103 | + |
| 104 | + # Check trace mask created |
| 105 | + assert "trace_mask" in ds.data_vars |
| 106 | + assert ds["trace_mask"].shape == (5, 10) |
| 107 | + assert ds["trace_mask"].values.all() |
| 108 | + |
| 109 | + # Check metadata |
| 110 | + assert ds.attrs["name"] == "PostStack3DTime" |
| 111 | + |
| 112 | + def test_full_conversion_depth_domain(self, tmp_path: Path) -> None: |
| 113 | + """Test full conversion workflow for depth-domain data.""" |
| 114 | + mock_ds = _create_mock_zgy_dataset(n_ilines=3, n_xlines=4, n_samples=25, vertical_dim="depth") |
| 115 | + output_path = tmp_path / "output_depth.mdio" |
| 116 | + |
| 117 | + mock_pyzgy = MagicMock() |
| 118 | + with ( |
| 119 | + patch.dict("sys.modules", {"pyzgy": mock_pyzgy}), |
| 120 | + patch("xarray.open_dataset", return_value=mock_ds), |
| 121 | + ): |
| 122 | + zgy_to_mdio("test.zgy", output_path, overwrite=True) |
| 123 | + |
| 124 | + ds = open_mdio(output_path) |
| 125 | + |
| 126 | + assert "depth" in ds.dims |
| 127 | + assert "time" not in ds.dims |
| 128 | + assert ds.attrs["name"] == "PostStack3DDepth" |
| 129 | + |
| 130 | + def test_overwrite_existing(self, tmp_path: Path) -> None: |
| 131 | + """Test that overwrite=True allows overwriting existing files.""" |
| 132 | + mock_ds = _create_mock_zgy_dataset() |
| 133 | + output_path = tmp_path / "output.mdio" |
| 134 | + |
| 135 | + mock_pyzgy = MagicMock() |
| 136 | + |
| 137 | + # First write |
| 138 | + with ( |
| 139 | + patch.dict("sys.modules", {"pyzgy": mock_pyzgy}), |
| 140 | + patch("xarray.open_dataset", return_value=mock_ds), |
| 141 | + ): |
| 142 | + zgy_to_mdio("test.zgy", output_path, overwrite=True) |
| 143 | + |
| 144 | + assert output_path.exists() |
| 145 | + |
| 146 | + # Second write with overwrite |
| 147 | + with ( |
| 148 | + patch.dict("sys.modules", {"pyzgy": mock_pyzgy}), |
| 149 | + patch("xarray.open_dataset", return_value=mock_ds), |
| 150 | + ): |
| 151 | + zgy_to_mdio("test.zgy", output_path, overwrite=True) |
| 152 | + |
| 153 | + assert output_path.exists() |
0 commit comments