"""Depth-to-time conversion of logs onto the seismic time axis, with anti-alias filtering. The thesis point-interpolates 0.15 m log samples directly to 4 ms (prepare_data.py:203–204), which causes aliasing (plan §1.1 #8). Here the log is first mapped onto a fine time grid, low-pass filtered with a zero-phase Butterworth filter (sosfiltfilt) at `cutoff_frac`·Nyquist of the target grid, and only then resampled. """ from __future__ import annotations import numpy as np from scipy import signal from ml_porosity.data.tdr import TDR def log_to_time( md: np.ndarray, values: np.ndarray, tdr: TDR, t_out_ms: np.ndarray, fine_dt_ms: float = 0.25, cutoff_frac: float = 0.8, order: int = 6, ) -> np.ndarray: """Log values at `t_out_ms` (regular grid). NaN where the log has no coverage. Internal gaps in the log are linearly interpolated before filtering, and target samples falling inside a gap are reset to NaN afterwards.""" md = np.asarray(md, dtype=float) v = np.asarray(values, dtype=float) ok = np.isfinite(v) t_log = tdr.time_at(md[ok]) v = v[ok] if v.size < 2: return np.full(t_out_ms.shape, np.nan) dt_out = float(t_out_ms[1] - t_out_ms[0]) t_fine = np.arange(t_log[0], t_log[-1] + fine_dt_ms / 2, fine_dt_ms) v_fine = np.interp(t_fine, t_log, v) if t_fine.size > 3 * (order + 1): nyq_out = 0.5 / dt_out nyq_fine = 0.5 / fine_dt_ms sos = signal.butter(order, cutoff_frac * nyq_out / nyq_fine, output="sos") v_fine = signal.sosfiltfilt(sos, v_fine) out = np.interp(t_out_ms, t_fine, v_fine, left=np.nan, right=np.nan) # Set to NaN the target samples lying in log gaps longer than one target sample. gaps = np.flatnonzero(np.diff(t_log) > dt_out) for g in gaps: out[(t_out_ms > t_log[g]) & (t_out_ms < t_log[g + 1])] = np.nan return out