"""Fetch me the NARR

Appears that data for the previous month is available by the 9th of current mon

called from RUN_2AM.sh
"""

import glob
import os
import subprocess
from datetime import date, datetime, timedelta
from pathlib import Path

import click
import pygrib
import requests
from pyiem.util import logger

LOG = logger()
TMP = "/mesonet/tmp"


def process(tarfn):
    """Process this tarfn"""
    os.chdir(TMP)
    subprocess.call(["tar", "-xf", tarfn])
    for grbfn in glob.glob("merged_AWIP32*sfc"):
        grbs = pygrib.open(grbfn)
        # NB: this sfc file has two DSWRF fields, one is a 3 hour average and
        # the other is a instantaneous forecast, we want the average!
        radgrb = grbs.select(parameterName="204", stepType="avg")[0]
        pcpgrb = grbs.select(parameterName="61")[0]

        dt = radgrb["dataDate"]
        hr = int(radgrb["dataTime"]) / 100.0
        ts = datetime.strptime(f"{dt} {hr:.0f}", "%Y%m%d %H")
        for prefix, grb in zip(["rad", "apcp"], [radgrb, pcpgrb], strict=True):
            fn = f"{prefix}_{ts:%Y%m%d%H%M}.grib"
            Path(fn).write_bytes(grb.tostring())
            pqstr = (
                f"data a {ts:%Y%m%d%H%M} bogus "
                f"model/NARR/{prefix}_{ts:%Y%m%d%H%M}.grib grib"
            )
            subprocess.call(["pqinsert", "-p", pqstr, fn])
            LOG.info("grbfn: %s fn: %s", grbfn, fn)
            os.remove(fn)
        os.remove(grbfn)


def fetch_gdex(year: int, month: int):
    """Get data please from GDEX"""
    days = ["0109", "1019"]
    lastday = (date(year, month, 1) + timedelta(days=35)).replace(
        day=1
    ) - timedelta(days=1)
    days.append(f"20{lastday.day}")
    for day in days:
        uri = (
            "https://osdf-director.osg-htc.org/ncar/gdex/d608000/3HRLY/"
            f"{year}/NARRsfc_{year}{month:02.0f}_{day}.tar"
        )
        tmpfn = f"{TMP}/narr.tar"
        with (
            requests.get(uri, stream=True, timeout=30) as resp,
            open(tmpfn, "wb") as fh,
        ):
            if resp.status_code != 200:
                LOG.warning(
                    "Fetching %s returned %s, aborting", uri, resp.status_code
                )
                return
            for chunk in resp.iter_content(chunk_size=1024 * 1024):
                if chunk:
                    fh.write(chunk)
        process(tmpfn)
        os.unlink(tmpfn)

    # Now call coop script
    subprocess.call(
        [
            "python",
            Path(__file__).parent.parent / "climodat" / "narr_solarrad.py",
            f"--year={year}",
            f"--month={month}",
        ]
    )
    subprocess.call(
        [
            "python",
            Path(__file__).parent.parent / "iemre" / "merge_narr.py",
            f"--year={year}",
            f"--month={month}",
        ]
    )


@click.command()
@click.option("--year", type=int, required=True)
@click.option("--month", type=int, required=True)
def main(year: int, month: int):
    """Go Main Go"""
    fetch_gdex(year, month)


if __name__ == "__main__":
    main()
