"""Excluded-mode ledger (Gamma acoustic, imaginary) per salt and sampling, with the same enumeration as export_modes_pairs.py.

    python export_excluded_modes.py RELEASE OUTDIR [SALT ...]
"""
import json
import sys
from pathlib import Path

import numpy as np

import export_modes_pairs as e  # parses RELEASE/OUTDIR from sys.argv; defines the enumeration

for salt in e.SALTS:
    import phonopy
    base = e.RELEASE / "inputs" / salt
    files = json.loads((base / "inputs.json").read_text())["files"]
    ph = phonopy.load(str(base / "phonopy_params.yaml"))
    man = json.loads((base / files["displacement_manifest_dense"]["file"]).read_text())
    out = {}
    for label, grid in (("gamma_G111", [1, 1, 1]), ("dense", e.tr.dense_grid(ph, e.MIN_WIDTH).tolist())):
        _modes, excluded, _ = e.enumerate_modes(ph, man, np.array(grid))
        out[label] = dict(q_grid=grid, rule="Gamma: the 3 smallest |omega^2| are acoustic; every mode with omega <= 0 (imaginary, reported as negative cm-1) "
                                            "is excluded; modes above 500 cm-1 are outside the main coupling set (not listed)",
                          n_standing_waves_excluded_imaginary=sum(x["n_standing_waves"] for x in excluded if x["reason"] == "imaginary_or_zero"),
                          excluded=excluded)
    (e.OUT / "modes" / salt / "excluded_modes.json").write_text(json.dumps(out, indent=1))
    print(salt, {k: (v["q_grid"], v["n_standing_waves_excluded_imaginary"], len(v["excluded"])) for k, v in out.items()}, flush=True)
