P0 - ml_porosity/: survey geometry, LAS/TDR/SEG-Y readers (4 ms delay fix, invalid time scalar, header LUT for incomplete cubes), anti-aliased log-to-time resampling, CNN-GRU v2 with the thesis loss, 41+ unit tests. - Full F3 Demo 2023 under data/ (text well/fault files tracked, bulk data ignored); the filtered copy and its cleanup notes are removed. - SHA-256 manifests for F3, Volve, NLOG and the thesis outputs. P1 - configs/protocol.yaml v2, locked by SHA-256 at 2026-10-26T04:28:09Z, before the first LOWO run (04:30:50Z); 05_lowo.py refuses to run on a hash mismatch. This commit postdates the runs. - Wavelet/synthetic tie QC, residual misregistration, LOWO 5 seeds for B0a, B0b, B1, B2, B3a, B3b (scripts 04-06); gate G-A: no pivot. - Volve D3: horizons, well picks, checkshots and the ST10010 cube read from data/Volve (scripts/07). - D-1 label check against measured NLOG density (scripts/08): PHIE_DT stays the primary label. - docs/plan.md, Volve and NLOG inventories, report.html. Full-text reference PDFs in refs/ stay local (ignored). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
6.3 KiB
Python
143 lines
6.3 KiB
Python
"""Quan hệ thời gian–độ sâu (TDR): MD (m, từ KB) ↔ TWT (ms, từ datum seismic).
|
||
|
||
Ba cách dựng TDR:
|
||
* `legacy_sonic_tdr` – tái hiện cách của luận văn (prepare_data.py:110): tích phân DT
|
||
với t = 0 tại mẫu LAS đầu tiên, không hiệu chỉnh drift. Chỉ dùng để audit.
|
||
* `sonic_tdr` – tích phân DT bắt đầu từ một điểm neo (md0, t0).
|
||
* `calibrate_to_anchors` – hiệu chỉnh drift: co giãn thời gian sonic trong từng khoảng
|
||
giữa hai anchor để đi qua đúng mọi anchor. Vì hệ số co giãn
|
||
của mỗi khoảng dương, TDR kết quả luôn đơn điệu tăng.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
|
||
import numpy as np
|
||
from scipy import integrate
|
||
|
||
US_PER_M_TO_S_PER_M = 1e-6
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class TDR:
|
||
md: np.ndarray # (N,) tăng nghiêm ngặt
|
||
twt_ms: np.ndarray # (N,) tăng nghiêm ngặt
|
||
name: str = ""
|
||
|
||
def __post_init__(self):
|
||
md = np.asarray(self.md, dtype=float)
|
||
t = np.asarray(self.twt_ms, dtype=float)
|
||
if md.shape != t.shape or md.ndim != 1 or md.size < 2:
|
||
raise ValueError("md và twt_ms phải là mảng 1D cùng độ dài ≥ 2")
|
||
if np.any(np.diff(md) <= 0):
|
||
raise ValueError(f"TDR {self.name!r}: MD không tăng nghiêm ngặt")
|
||
if np.any(np.diff(t) <= 0):
|
||
raise ValueError(f"TDR {self.name!r}: TWT không tăng nghiêm ngặt")
|
||
object.__setattr__(self, "md", md)
|
||
object.__setattr__(self, "twt_ms", t)
|
||
|
||
@classmethod
|
||
def from_pairs(cls, pairs: np.ndarray, name: str = "", drop_flat: bool = False) -> TDR:
|
||
"""`drop_flat=True` bỏ các điểm không tăng thời gian so với điểm giữ lại trước đó
|
||
(ví dụ F02-1.wlt có hai cặp MD khác nhau cùng TWT 700 và 800 ms)."""
|
||
pairs = np.asarray(pairs, dtype=float)
|
||
pairs = pairs[np.argsort(pairs[:, 0], kind="stable")]
|
||
if drop_flat:
|
||
keep = [0]
|
||
for i in range(1, len(pairs)):
|
||
if pairs[i, 0] > pairs[keep[-1], 0] and pairs[i, 1] > pairs[keep[-1], 1]:
|
||
keep.append(i)
|
||
pairs = pairs[keep]
|
||
return cls(pairs[:, 0], pairs[:, 1], name)
|
||
|
||
def time_at(self, md) -> np.ndarray:
|
||
"""TWT (ms) tại MD; ngoài phạm vi thì ngoại suy tuyến tính theo khoảng đầu/cuối."""
|
||
return _interp_extrap(md, self.md, self.twt_ms)
|
||
|
||
def depth_at(self, twt_ms) -> np.ndarray:
|
||
return _interp_extrap(twt_ms, self.twt_ms, self.md)
|
||
|
||
def interval_velocity(self) -> np.ndarray:
|
||
"""Vận tốc khoảng (m/s) giữa các nút liên tiếp."""
|
||
return 2000.0 * np.diff(self.md) / np.diff(self.twt_ms)
|
||
|
||
|
||
def _interp_extrap(x, xp, fp) -> np.ndarray:
|
||
x = np.asarray(x, dtype=float)
|
||
y = np.interp(x, xp, fp)
|
||
lo, hi = x < xp[0], x > xp[-1]
|
||
if np.any(lo):
|
||
y[lo] = fp[0] + (x[lo] - xp[0]) * (fp[1] - fp[0]) / (xp[1] - xp[0])
|
||
if np.any(hi):
|
||
y[hi] = fp[-1] + (x[hi] - xp[-1]) * (fp[-1] - fp[-2]) / (xp[-1] - xp[-2])
|
||
return y
|
||
|
||
|
||
def fill_nan(a: np.ndarray) -> np.ndarray:
|
||
"""Nội suy tuyến tính chỗ NaN theo chỉ số, giữ giá trị biên ở hai đầu (như luận văn)."""
|
||
a = np.asarray(a, dtype=float).copy()
|
||
ok = np.isfinite(a)
|
||
if not ok.any():
|
||
raise ValueError("toàn bộ giá trị là NaN")
|
||
a[~ok] = np.interp(np.flatnonzero(~ok), np.flatnonzero(ok), a[ok])
|
||
return a
|
||
|
||
|
||
def legacy_sonic_tdr(md: np.ndarray, dt_us_per_m: np.ndarray) -> TDR:
|
||
"""TWT = 2·∫DT dz với t = 0 tại mẫu LAS đầu tiên (prepare_data.py:110)."""
|
||
dt = fill_nan(np.where(np.asarray(dt_us_per_m) < 0, np.nan, dt_us_per_m))
|
||
owt_s = integrate.cumulative_trapezoid(dt * US_PER_M_TO_S_PER_M, md, initial=0.0)
|
||
return TDR(md, 2000.0 * owt_s, "legacy_sonic")
|
||
|
||
|
||
def sonic_tdr(md: np.ndarray, dt_us_per_m: np.ndarray, md0: float, t0_ms: float) -> TDR:
|
||
"""Tích phân DT đi qua điểm neo (md0, t0_ms). Chỉ dùng mẫu DT hợp lệ, nên khoảng thiếu
|
||
được tích phân như DT tuyến tính giữa hai đầu; nếu md0 nằm trên mẫu DT đầu tiên thì
|
||
đoạn md0 → mẫu đầu dùng DT của mẫu đầu."""
|
||
md = np.asarray(md, dtype=float)
|
||
dt = np.asarray(dt_us_per_m, dtype=float)
|
||
ok = np.isfinite(dt) & (dt > 0)
|
||
md_v, dt_v = md[ok], dt[ok]
|
||
t = 2000.0 * integrate.cumulative_trapezoid(dt_v * US_PER_M_TO_S_PER_M, md_v, initial=0.0)
|
||
if md0 < md_v[0]:
|
||
t_md0 = -2000.0 * (md_v[0] - md0) * dt_v[0] * US_PER_M_TO_S_PER_M
|
||
else:
|
||
t_md0 = float(_interp_extrap(md0, md_v, t))
|
||
return TDR(md_v, t - t_md0 + t0_ms, "sonic")
|
||
|
||
|
||
def calibrate_to_anchors(base: TDR, anchor_md, anchor_twt_ms, name: str = "calibrated") -> TDR:
|
||
"""Hiệu chỉnh drift theo khoảng: giữa hai anchor liên tiếp (z_i, t_i), (z_{i+1}, t_{i+1})
|
||
|
||
t(z) = t_i + (t_b(z) − t_b(z_i)) · (t_{i+1} − t_i) / (t_b(z_{i+1}) − t_b(z_i))
|
||
|
||
với t_b là TDR gốc (sonic). Ngoài phạm vi anchor: dịch đều (bulk) theo anchor gần nhất.
|
||
Kết quả đi qua đúng mọi anchor và đơn điệu nếu anchor tăng nghiêm ngặt theo cả z và t.
|
||
"""
|
||
za = np.asarray(anchor_md, dtype=float)
|
||
ta = np.asarray(anchor_twt_ms, dtype=float)
|
||
order = np.argsort(za)
|
||
za, ta = za[order], ta[order]
|
||
if np.any(np.diff(za) <= 0) or np.any(np.diff(ta) <= 0):
|
||
raise ValueError("anchor phải tăng nghiêm ngặt theo MD và TWT")
|
||
|
||
z = np.union1d(base.md, za)
|
||
tb = base.time_at(z)
|
||
tba = base.time_at(za)
|
||
t = np.empty_like(z)
|
||
|
||
k = np.clip(np.searchsorted(za, z, side="right") - 1, 0, len(za) - 2)
|
||
inside = (z >= za[0]) & (z <= za[-1])
|
||
scale = (ta[k + 1] - ta[k]) / (tba[k + 1] - tba[k])
|
||
t[inside] = ta[k[inside]] + (tb[inside] - tba[k[inside]]) * scale[inside]
|
||
above, below = z < za[0], z > za[-1]
|
||
t[above] = tb[above] + (ta[0] - tba[0])
|
||
t[below] = tb[below] + (ta[-1] - tba[-1])
|
||
return TDR(z, t, name)
|
||
|
||
|
||
def misfit_at_anchors(tdr: TDR, anchor_md, anchor_twt_ms) -> np.ndarray:
|
||
"""TWT theo TDR trừ TWT của anchor (ms). Âm nghĩa là TDR sớm hơn anchor."""
|
||
return tdr.time_at(anchor_md) - np.asarray(anchor_twt_ms, dtype=float)
|