Skip to content

Commit 131430c

Browse files
committed
fix default depth key
1 parent 8ce11b5 commit 131430c

5 files changed

Lines changed: 45 additions & 15 deletions

File tree

docs/scan.qmd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ summarises multiple files and returns a
2121
[pysegy.SegyScan](reference/SegyScan.html#pysegy.SegyScan) that can lazily read data
2222
for each shot. Use ``by_receiver=True`` to group traces by receiver coordinates
2323
instead of source coordinates.
24+
25+
Depth field detection
26+
- The source and receiver depth fields are auto-detected using a fixed
27+
priority, not by picking headers with the largest value spread. This reduces
28+
surprises with unlikely headers.
29+
- Source priority: ``SourceDepth`` > ``SourceDatumElevation`` >
30+
``SourceSurfaceElevation`` > ``SourceWaterDepth``.
31+
- Receiver priority: ``RecGroupElevation`` > ``RecDatumElevation`` >
32+
``GroupWaterDepth``.

pysegy/plotting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def compare_shots(
276276
cmap: Sequence[str] | str = "gray",
277277
side_by_side: bool = False,
278278
chunksize: int = 20,
279+
new_fig: bool = False,
279280
**kw,
280281
):
281282
"""
@@ -305,7 +306,7 @@ def compare_shots(
305306
out1[:, start:start + chunksize] = arr1[:, start:start + chunksize]
306307
for start in range(chunksize, nrec, 2 * chunksize):
307308
out2[:, start:start + chunksize] = arr2[:, start:start + chunksize]
308-
plot_sdata(out1, spacing, cmap=cmap[0], **kw)
309+
plot_sdata(out1, spacing, cmap=cmap[0], new_fig=new_fig, **kw)
309310
_plot_with_units(
310311
out2,
311312
spacing,

pysegy/scan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ShotRecord:
3333
path: str
3434
coordinates: Tuple[float, float, float]
3535
fileheader: FileHeader
36-
rec_depth_key: str = "GroupWaterDepth"
36+
rec_depth_key: str = "RecGroupElevation"
3737
depth_key: str = "SourceDepth"
3838
by_receiver: bool = False
3939
segments: List[Tuple[int, int]] = field(default_factory=list)

pysegy/tests/test_extra.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_source_candidates(self, source_key):
173173
dest = self._write_block(f"depth_source_{source_key}.segy", headers)
174174
detected_source, detected_receiver = seg.detect_depth_keys(str(dest))
175175
assert detected_source == source_key
176-
assert detected_receiver == "GroupWaterDepth"
176+
assert detected_receiver == "RecGroupElevation"
177177

178178
@pytest.mark.parametrize("receiver_key", _RECEIVER_DEPTH_FIELDS)
179179
def test_receiver_candidates(self, receiver_key):
@@ -204,7 +204,7 @@ def test_defaults_when_empty(self):
204204
dest = self._write_block("depth_default.segy", [th])
205205
source_key, receiver_key = seg.detect_depth_keys(str(dest))
206206
assert source_key == "SourceDepth"
207-
assert receiver_key == "GroupWaterDepth"
207+
assert receiver_key == "RecGroupElevation"
208208

209209
def test_detect_depth_keys_with_no_traces(self):
210210
fh = FileHeader()
@@ -213,7 +213,7 @@ def test_detect_depth_keys_with_no_traces(self):
213213
seg.write.write_fileheader(f, fh)
214214
source_key, receiver_key = seg.detect_depth_keys(str(dest))
215215
assert source_key == "SourceDepth"
216-
assert receiver_key == "GroupWaterDepth"
216+
assert receiver_key == "RecGroupElevation"
217217

218218
def test_segy_scan_uses_detected_keys(self):
219219
headers = []

pysegy/utils.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@
3333
}
3434

3535
_SOURCE_DEPTH_FIELDS: Tuple[str, ...] = (
36+
# Likely-first: true source depth when present
3637
"SourceDepth",
37-
"SourceWaterDepth",
38+
# Next prefer datum elevation over water depth; surface elevation after
3839
"SourceDatumElevation",
3940
"SourceSurfaceElevation",
41+
# Least likely candidate
42+
"SourceWaterDepth",
4043
)
4144

4245
_RECEIVER_DEPTH_FIELDS: Tuple[str, ...] = (
43-
"GroupWaterDepth",
46+
# Prefer receiver group elevation first
4447
"RecGroupElevation",
4548
"RecDatumElevation",
49+
# Least likely candidate
50+
"GroupWaterDepth",
4651
)
4752

4853
_DEPTH_SENTINELS = {0, 2147483647, -2147483648, 32767, -32768}
@@ -151,6 +156,15 @@ def get_header(
151156

152157

153158
def _depth_score(values: Iterable[float]) -> Tuple[int, float]:
159+
"""
160+
Compute a simple quality score for a sequence of depth-like values.
161+
162+
The score returns a tuple of ``(count, span)`` where ``count`` is the
163+
number of non-sentinel entries and ``span`` is the range of the data. This
164+
is retained for backwards compatibility but is no longer used to choose the
165+
preferred header field. Header selection now follows a fixed priority order
166+
and simply checks for the presence of valid (non-zero, non-sentinel) data.
167+
"""
154168
cleaned: List[float] = []
155169
for val in values:
156170
fval = float(val)
@@ -227,21 +241,27 @@ def detect_depth_keys(
227241
if not headers:
228242
return source_key, receiver_key
229243

230-
best_source_score = (0, 0.0)
244+
def _has_valid(values: Iterable[float]) -> bool:
245+
"""Return True when any non-sentinel, non-zero value is present."""
246+
for v in values:
247+
fval = float(v)
248+
if fval in _DEPTH_SENTINELS:
249+
continue
250+
if fval != 0.0:
251+
return True
252+
return False
253+
231254
for candidate in _SOURCE_DEPTH_FIELDS:
232255
vals = get_header(headers, candidate)
233-
score = _depth_score(vals)
234-
if score > best_source_score:
235-
best_source_score = score
256+
if _has_valid(vals):
236257
source_key = candidate
258+
break
237259

238-
best_receiver_score = (0, 0.0)
239260
for candidate in _RECEIVER_DEPTH_FIELDS:
240261
vals = get_header(headers, candidate)
241-
score = _depth_score(vals)
242-
if score > best_receiver_score:
243-
best_receiver_score = score
262+
if _has_valid(vals):
244263
receiver_key = candidate
264+
break
245265

246266
return source_key, receiver_key
247267

0 commit comments

Comments
 (0)