test_prestack_gather_spacing.py 7.1 KB
Newer Older
Jim King's avatar
Jim King committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
from pathlib import Path
from typing import Tuple, Union

import pytest
import openvds

from segyimport_test_config import test_data_dir, ImportExecutor, TempVDSGuard


def construct_respace_executor(output_vds: TempVDSGuard, segy_name: str, respace_setting: Union[str, None]) -> ImportExecutor:
    ex = ImportExecutor()

    if respace_setting:
        ex.add_args(["--respace-gathers", respace_setting])

    ex.add_arg("--prestack")
    ex.add_args(["--vdsfile", output_vds.filename])

    ex.add_arg(segy_name)

    return ex


@pytest.fixture
def output_vds() -> TempVDSGuard:
    return TempVDSGuard("import_mutes_test")


@pytest.fixture
def prestack_segy() -> str:
Jim King's avatar
Jim King committed
32
33
    return os.path.join(test_data_dir, "HeadwavePlatform", "PlatformIntegration", "Teleport_Trim", "3D_Prestack",
                        "ST0202R08_Gather_Time.segy")
Jim King's avatar
Jim King committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


@pytest.fixture
def default_executor(prestack_segy, output_vds) -> Tuple[ImportExecutor, TempVDSGuard]:
    """Setup an ImportExecutor with no arg for respacing"""
    ex = construct_respace_executor(output_vds, prestack_segy, None)
    return ex, output_vds


@pytest.fixture
def auto_executor(prestack_segy, output_vds) -> Tuple[ImportExecutor, TempVDSGuard]:
    """Setup an ImportExecutor with respacing set to Auto"""
    ex = construct_respace_executor(output_vds, prestack_segy, "Auto")
    return ex, output_vds


@pytest.fixture
def off_executor(prestack_segy, output_vds) -> Tuple[ImportExecutor, TempVDSGuard]:
    """Setup an ImportExecutor with respacing set to Off"""
    ex = construct_respace_executor(output_vds, prestack_segy, "Off")
    return ex, output_vds


@pytest.fixture
def on_executor(prestack_segy, output_vds) -> Tuple[ImportExecutor, TempVDSGuard]:
    """Setup an ImportExecutor with respacing set to On"""
    ex = construct_respace_executor(output_vds, prestack_segy, "On")
    return ex, output_vds


@pytest.fixture
def invalid_executor(prestack_segy, output_vds) -> Tuple[ImportExecutor, TempVDSGuard]:
    """Setup an ImportExecutor with respacing set to an invalid value"""
    ex = construct_respace_executor(output_vds, prestack_segy, "Partial")
    return ex, output_vds


Jim King's avatar
Jim King committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def get_gathers_stats(vds_filename: str) -> Tuple[int, int, int, int, int, int]:
    """
    Read all the gathers in a single inline and return stats on how many gathers have dead traces in various
    regions of the gather.

    :return tuple of (
      total gathers,
      number of gathers with dead traces only at front of gather,
      number of gathers with dead traces only within the gather,
      number of gathers with dead traces only at end of gather,
      number of gathers with dead traces in multiple places,
      number of gathers with no dead traces
      )
    """
    with openvds.open(output_vds.filename, "") as handle:
        layout = openvds.getLayout(handle)
        access_manager = openvds.getAccessManager(handle)

        trace_channel = layout.getChannelIndex("Trace")
        assert trace_channel > 0, "Trace channel not found"

        # only read one inline; make it the middle inline
        inline_index = layout.getDimensionNumSamples(3) // 2
        dim2_size = layout.getDimensionNumSamples(2)
        dim1_size = layout.getDimensionNumSamples(1)

        # stats counters
        front_only = 0
        middle_only = 0
        end_only = 0
        mixed = 0
        no_dead = 0

        for crossline_index in range(dim2_size):
            trace_flag_request = access_manager.requestVolumeSubset((0, 0, crossline_index, inline_index, 0, 0),
                                                                    (1, dim1_size, crossline_index + 1, inline_index + 1, 1,
                                                                     1),
                                                                    channel=trace_channel,
                                                                    format=openvds.VolumeDataChannelDescriptor.Format.Format_U8,
                                                                    dimensionsND=openvds.DimensionsND.Dimensions_023)

            assert dim1_size == trace_flag_request.data.shape[0]

            # analyze trace_flag_request.data to figure out which stats counter to bump

            dead_trace_ranges = []
            i = 0
            while i < dim1_size:
                if trace_flag_request.data[i] == 0:
                    start = i
                    i += 1
                    while i < dim1_size and trace_flag_request.data[i] == 0:
                        i += 1
                    end = i - 1
                    dead_trace_ranges.append((start, end))
                else:
                    i += 1

            if len(dead_trace_ranges) == 0:
                no_dead += 1
            elif len(dead_trace_ranges) == 1:
                # either front, middle, end
                start, stop = dead_trace_ranges[0]
                if start == 0:
                    front_only += 1
                elif stop == 0:
                    end_only += 1
                else:
                    middle_only += 1
            else:
                mixed += 1

        return dim2_size, front_only, middle_only, end_only, mixed, no_dead


Jim King's avatar
Jim King committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def test_gather_spacing_invalid_arg(invalid_executor):
    ex, output_vds = invalid_executor

    result = ex.run()

    assert result != 0, ex.output()
    # TODO what text to look for?
    assert "some message" in ex.output().lower()


def test_gather_spacing_default(default_executor):
    ex, output_vds = default_executor

    result = ex.run()

    assert result == 0, ex.output()
    assert Path(output_vds.filename).exists()

Jim King's avatar
Jim King committed
164
    total, leading_only, middle_only, trailing_only, mixed, no_dead = get_gathers_stats(output_vds.filename)
Jim King's avatar
Jim King committed
165

Jim King's avatar
Jim King committed
166
167
168
169
170
171
    assert total == 60
    assert middle_only > 0
    assert mixed > 0
    assert trailing_only == 0
    assert leading_only > 0
    assert no_dead == 0
Jim King's avatar
Jim King committed
172
173
174
175
176
177
178
179
180
181


def test_gather_spacing_auto(auto_executor):
    ex, output_vds = auto_executor

    result = ex.run()

    assert result == 0, ex.output()
    assert Path(output_vds.filename).exists()

Jim King's avatar
Jim King committed
182
    total, leading_only, middle_only, trailing_only, mixed, no_dead = get_gathers_stats(output_vds.filename)
Jim King's avatar
Jim King committed
183

Jim King's avatar
Jim King committed
184
185
186
187
188
189
    assert total == 60
    assert middle_only > 0
    assert mixed > 0
    assert trailing_only == 0
    assert leading_only > 0
    assert no_dead == 0
Jim King's avatar
Jim King committed
190
191
192
193
194
195
196
197
198
199


def test_gather_spacing_on(on_executor):
    ex, output_vds = on_executor

    result = ex.run()

    assert result == 0, ex.output()
    assert Path(output_vds.filename).exists()

Jim King's avatar
Jim King committed
200
    total, leading_only, middle_only, trailing_only, mixed, no_dead = get_gathers_stats(output_vds.filename)
Jim King's avatar
Jim King committed
201

Jim King's avatar
Jim King committed
202
203
204
205
206
207
    assert total == 60
    assert middle_only > 0
    assert mixed > 0
    assert trailing_only == 0
    assert leading_only > 0
    assert no_dead == 0
Jim King's avatar
Jim King committed
208
209
210
211
212
213
214
215
216
217


def test_gather_spacing_off(off_executor):
    ex, output_vds = off_executor

    result = ex.run()

    assert result == 0, ex.output()
    assert Path(output_vds.filename).exists()

Jim King's avatar
Jim King committed
218
    total, leading_only, middle_only, trailing_only, mixed, no_dead = get_gathers_stats(output_vds.filename)
Jim King's avatar
Jim King committed
219

Jim King's avatar
Jim King committed
220
221
222
223
224
225
    assert total == 60
    assert trailing_only == total
    assert middle_only == 0
    assert mixed == 0
    assert leading_only == 0
    assert no_dead == 0