"""Plot the first time slice; gray marks the missing synthetic cell.

Run: python plot_netcdf.py example.nc
"""
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from netCDF4 import Dataset


def plot_example(path="example.nc", output="netcdf-temperature.png"):
    with Dataset(path) as ds:
        latitude = ds.variables["lat"][:]
        longitude = ds.variables["lon"][:]
        temperature = ds.variables["temperature"][0, :, :]
        units = ds.variables["temperature"].units
    plt.rcParams.update({"font.size": 12, "axes.spines.top": False,
                         "axes.spines.right": False})
    fig, ax = plt.subplots(figsize=(6.4, 4.5), layout="constrained")
    ax.set_facecolor("#d9d9d9")
    mesh = ax.pcolormesh(longitude, latitude, temperature,
                         shading="nearest", cmap="viridis")
    ax.set_xlabel("Longitude (degrees east)")
    ax.set_ylabel("Latitude (degrees north)")
    fig.colorbar(mesh, ax=ax, label=f"Synthetic temperature ({units})")
    fig.savefig(output, dpi=200)
    plt.close(fig)
    print(f"Saved {output}")
    return Path(output)


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("path", nargs="?", default="example.nc")
    parser.add_argument("--output", default="netcdf-temperature.png")
    args = parser.parse_args()
    plot_example(args.path, args.output)
