"""Calendar Plot of Automated Station Summaries"""
import datetime

import numpy as np
import psycopg2.extras
import pandas as pd
from matplotlib.patches import Rectangle
from pyiem.util import (
    get_autoplot_context,
    get_sqlalchemy_conn,
    convert_value,
    get_dbconn,
)
from pyiem.reference import TRACE_VALUE
from pyiem.plot import calendar_plot
from pyiem.exceptions import NoDataFound


PDICT = {
    "max_tmpf": "High Temperature",
    "high_departure": "High Temperature Departure",
    "min_tmpf": "Low Temperature",
    "low_departure": "Low Temperature Departure",
    "avg_tmpf": "Average Temperature",
    "avg_departure": "Average Temperature Departure",
    "max_dwpf": "Highest Dew Point Temperature",
    "min_dwpf": "Lowest Dew Point Temperature",
    "avg_smph": "Average Wind Speed [mph]",
    "max_smph": "Maximum Wind Speed/Gust [mph]",
    "pday": "Precipitation",
    "max_rstage": "Maximum Water Stage [ft]",
}
STAGES = "action flood moderate major".split()
COLORS = "white #ffff72 #ffc672 #ff7272 #e28eff".split()


def get_description():
    """Return a dict describing how to call this plotter"""
    desc = {}
    desc["data"] = True
    desc[
        "description"
    ] = """This chart presents a series of daily summary data
    as a calendar.  The daily totals should be valid for the local day of the
    weather station.  The climatology is based on the nearest NCEI 1981-2010
    climate station, which in most cases is the same station.  Climatology
    values are rounded to the nearest whole degree Fahrenheit and then compared
    against the observed value to compute a departure.
    """
    today = datetime.date.today()
    m90 = today - datetime.timedelta(days=90)
    desc["arguments"] = [
        dict(
            type="sid",
            name="station",
            default="DSM",
            network="IA_ASOS",
            label="Select Station",
        ),
        dict(
            type="select",
            name="var",
            default="pday",
            label="Which Daily Variable:",
            options=PDICT,
        ),
        dict(
            type="date",
            name="sdate",
            default=m90.strftime("%Y/%m/%d"),
            label="Start Date:",