"""Plot the saved curved-coupler result beside the CC BY paper reference.

Keep finest-transmission.json and reference-v2.json beside this script.
Requires photonhub and matplotlib. No FDTD or mode solver is run.
"""
import json
from pathlib import Path

import matplotlib.pyplot as plt
import photonhub as ph


def plot_transmission(result, reference, output):
    axes = {}
    for arm in ("bar", "cross"):
        paper = reference["series"][f"{arm}_simulated"]
        ax = ph.viz.plot_comparison(
            result["wavelength_nm"], result["normalized"][arm],
            reference=[dict(x=paper["x"], y=paper["normalized_power"],
                            label="Paper: simulated")],
            label=f"PhotonHub: {result['mesh_nm']:g} nm",
            ylabel=f"Normalized {arm} power", ylim=(0, 1),
        )
        ax.figure.savefig(output / f"finest-{arm}.png", dpi=180, bbox_inches="tight")
        axes[arm] = ax
        plt.close(ax.figure)
    return axes


if __name__ == "__main__":
    here = Path(__file__).resolve().parent
    result = json.loads((here / "finest-transmission.json").read_text())
    reference = json.loads((here / "reference-v2.json").read_text())
    plot_transmission(result, reference, here)
