"""
Smooth inversion and depth of investigation
===========================================

Invert a sounding without choosing the number of layers, and find the depth
below which the model no longer reflects the data.
"""

# %%
# Data over a weathered crystalline basement
# ------------------------------------------
# Topsoil, a clay-rich weathered layer and fresh basement: a common setting
# for groundwater in Nigeria's basement complex.

import numpy as np

import ertkit
from ertkit import plot, ves

truth = ves.LayeredModel(resistivity=[200.0, 30.0, 2000.0], thickness=[1.5, 12.0])
survey = ves.Survey.schlumberger(ab2=np.geomspace(2.5, 150.0, 24), mn2=0.5)
data = ves.Sounding.synthetic(truth, survey, ertkit.ErrorModel(relative=0.03), rng=4)

# %%
# Occam inversion
# ---------------
# Thirty layers whose thicknesses grow with depth. The regularization weight
# is the largest that still fits the data to their errors.

smooth = ves.invert_smooth(data)
print(f"chi2 = {smooth.chi2:.1f} for {smooth.n_data} data ({smooth.stop_reason})")
print(f"depth of investigation: {smooth.doi:.1f} m")

# %%
# The shaded region lies below the depth of investigation: two further
# inversions, pulled towards very different reference models, disagree there,
# so the data do not determine the resistivity at those depths. The depth axis
# is logarithmic, which suits layers that thicken with depth.

ax = plot.layered_model(
    truth, smooth, labels=["true model", "smooth model"], log_depth=True
)

# %%
# The depth of investigation index itself, layer by layer. It is near 0 where
# the data control the model and near 1 where the reference model does.

import matplotlib.pyplot as plt

tops = np.concatenate([[0.0], smooth.model.depths])
bottoms = np.concatenate([smooth.model.depths, [2.0 * smooth.model.depths[-1]]])
fig, ax = plt.subplots(figsize=(4.5, 4.5), layout="constrained")
# One vertical segment per layer, spanning its depth range.
ax.plot(
    np.repeat(smooth.doi_index, 2),
    np.ravel(np.column_stack([np.maximum(tops, 0.5 * tops[1]), bottoms])),
)
ax.axvline(ves.DOI_THRESHOLD, color="gray", linestyle="--", label="threshold")
ax.axhline(smooth.doi, color="black", linewidth=0.8, label="depth of investigation")
ax.set_yscale("log")
ax.set_ylim(bottoms[-1], 0.5 * tops[1])
ax.set_xlabel("depth-of-investigation index")
ax.set_ylabel("depth (m)")
ax.legend()

# %%
# The depth of investigation is about a quarter of the largest AB/2. For a
# homogeneous earth, half of the Schlumberger signal comes from above about
# 0.38 AB/2 (the median depth of investigation; Edwards 1977); here the
# conductive weathered layer channels the current and the basement below it is
# seen less deeply.
#
# A longer spread looks deeper:

for ab2_max in (50.0, 150.0, 500.0):
    longer = ves.Survey.schlumberger(ab2=np.geomspace(2.5, ab2_max, 24), mn2=0.5)
    sounding = ves.Sounding.synthetic(
        truth, longer, ertkit.ErrorModel(relative=0.03), rng=4
    )
    print(
        f"AB/2 up to {ab2_max:5.0f} m: depth of investigation {ves.invert_smooth(sounding).doi:6.1f} m"
    )
