"""Horizon F3 dạng ASCII (X, Y, TWT ms) trong Rawdata/Surface_data/.""" from __future__ import annotations from pathlib import Path import numpy as np from ml_porosity.data.geometry import SurveyGeometry from ml_porosity.paths import f3_root # Thứ tự từ nông đến sâu tại khu vực giếng. F3_HORIZONS = ("Shallow", "FS8", "FS7", "Truncation", "Top-Foresets", "FS6", "MFS4") def horizon_path(name: str) -> Path: return f3_root() / "Rawdata" / "Surface_data" / f"F3-Horizon-{name}.txt" def load_horizon_bins(name: str, geom: SurveyGeometry) -> dict[tuple[int, int], float]: """Ánh xạ (IL, XL) → TWT (ms). Mỗi dòng XY được gán về bin gần nhất.""" a = np.loadtxt(horizon_path(name)) il, xl = geom.xy_to_bin(a[:, 0], a[:, 1]) il, xl = np.rint(il).astype(int), np.rint(xl).astype(int) return {(i, j): t for i, j, t in zip(il, xl, a[:, 2])} def horizon_at(name: str, geom: SurveyGeometry, bins: list[tuple[int, int]], radius: int = 2) -> np.ndarray: """TWT của horizon tại từng bin; nếu bin không có pick thì lấy trung bình các pick trong ô (2r+1)², NaN nếu ô cũng trống.""" h = load_horizon_bins(name, geom) out = [] for il, xl in bins: if (il, xl) in h: out.append(h[(il, xl)]) continue vals = [h[(il + a, xl + b)] for a in range(-radius, radius + 1) for b in range(-radius, radius + 1) if (il + a, xl + b) in h] out.append(float(np.mean(vals)) if vals else np.nan) return np.asarray(out)