# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Repeat the first two assignments from a subset of Hanawalt (1986), Fig. 2."""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parents[1] / "reference"))
from hanawalt import search_match

observed = [
    [3.061, 100], [3.198, 78], [6.397, 66], [1.874, 31], [2.540, 23],
    [1.598, 22], [2.767, 19], [2.650, 17], [2.259, 14],
]
references = {
    "Y2O3": [[3.060, 100], [1.874, 46], [1.599, 31], [2.652, 30], [2.261, 8]],
    "As2O3": [[3.195, 100], [6.394, 63], [2.541, 38], [2.768, 28], [2.262, 12]],
}

# Use the work form's residual annotations after the yttria assignment.
# The analyst supplied these residuals. The search itself only ranks candidates.
residual_intensity = {3.061: 30, 2.259: 8, 1.874: 0, 1.598: 0, 2.650: 0}


def after_yttria():
    remaining = [[d, residual_intensity.get(d, intensity)] for d, intensity in observed]
    return [[d, intensity] for d, intensity in remaining if intensity > 0]


if __name__ == "__main__":
    for peaks in [observed, after_yttria()]:
        best = search_match(peaks, references, 0.01)[0]
        print(f"{best['phase']}: {best['matched']} of {len(references[best['phase']])} selected reference lines")
