<?php
require_once "../../../../config/settings.inc.php";
define("IEM_APPID", 72);
putenv("TZ=UTC");
require_once "../../../../include/myview.php";
require_once "../../../../include/forms.php";
require_once "../../../../include/database.inc.php";

/* Needed GET variables */
$year = get_int404("year", date("Y"));
$num = get_int404("num", 1);

$t = new MyView();
$t->title = "SPC $year Convective Watch $num";

require_once "../../../../include/rview_lib.php";
$db = iemdb("postgis");
pg_query($db, "SET TIME ZONE 'UTC'");

$stname = iem_pg_prepare($db, "SELECT *, ST_xmin(geom) as x0,
      ST_xmax(geom) as x1, ST_ymin(geom) as y0, ST_ymax(geom) as y1,
      ST_askml(geom) as kml,
      ST_asGeoJSON(geom) as geojson from watches
      where extract(year from issued) = $1 and num = $2");

$ERROR = "";


$rs = pg_execute($db, $stname, array($year, $num));
if (pg_num_rows($rs) == 0) {
    $t->content = "<h3>Sorry, watch number $num for year $year was not found</h3>";
    $t->render('single.phtml');
    die();
}

$row = pg_fetch_assoc($rs);

$textlinks = "";
if (!is_null($row["product_id_saw"])) {
    $textlinks .= sprintf(
        '<tr><th>SAW Issuance</th><td><a href="/p.php?pid=%s">%s</a></td></tr>',
        $row["product_id_saw"],
        $row["product_id_saw"],
    );
}
if (!is_null($row["product_id_sel"])) {
    $textlinks .= sprintf(
        '<tr><th>SEL Issuance</th><td><a href="/p.php?pid=%s">%s</a></td></tr>',
        $row["product_id_sel"],
        $row["product_id_sel"],
    );
}
if (!is_null($row["product_id_sel"])) {
    $textlinks .= sprintf(
        '<tr><th>Watch Probs</th><td><a href="/p.php?pid=%s">%s</a></td></tr>',
        $row["product_id_wwp"],
        $row["product_id_wwp"],
    );
}

$ts = strtotime($row["issued"]);
$expires = strtotime($row["expired"]);
$d = date("d F Y H:i",  $ts);

$maptitle = htmlentities("SPC Watch number $num during $year");
$margin = 0.5;
$url = sprintf("/GIS/radmap.php?title=%s&layers[]=uscounties&layers[]=nexrad&ts=%s&height=480&width=640&layers[]=watches&layers[]=watch_by_county&sector=conus&bbox=%s,%s,%s,%s", $maptitle, gmdate('YmdHi', $ts), $row["x0"] - $margin, $row["y0"] - $margin, $row["x1"] + $margin, $row["y1"] + $margin);

/*____________________________ Plot LSRs */
if ($expires < time() && $year > 2002) {
    $lurl = sprintf("/GIS/radmap.php?layers[]=lsrs&ts=%s&ts2=%s&height=480&width=640&layers[]=watches&sector=conus&bbox=%s,%s,%s,%s", gmdate('YmdHi', $ts), gmdate('YmdHi', $expires), $row["x0"] - $margin, $row["y0"] - $margin, $row["x1"] + $margin, $row["y1"] + $margin);
}

$ourl = sprintf("/GIS/radmap.php?layers[]=nexrad&ts=%s&height=480&width=640&layers[]=watches&sector=conus", gmdate('YmdHi', $ts));

$prevlink = "";
if ($num > 1) {
    $prevlink = "<a href=\"watch.phtml?year=$year&num=" . ($num - 1) . "\">Previous Watch</a>";
}
$nextlink = "<a href=\"watch.phtml?year=$year&num=" . ($num + 1) . "\">Next Watch</a>";

$yselect = yearSelect(1997, $year);

$issue = substr($row["issued"], 0, 16);
$expire = substr($row["expired"], 0, 16);

$spclink = sprintf(
    "https://www.spc.noaa.gov/products/watch/%s/ww%04d.html",
    $year,
    $num
);

$woulink = "";
/* Include a link to download WOU outlines... */
if (date("Y", $ts) > 2005) {
    $woulink .= "<tr><th>WOU Outline Shapefiles</th><td>";
    $begints = $ts + ((60 - intval(date("i", $ts))) * 60);
    $endts = $expires - (intval(date("i", $expires)) * 60);
    while ($begints <= $endts) {
        $woulink .= sprintf(
            "<a href=\"/cgi-bin/request/gis/watch_by_county.py?year=%s&month=%s&day=%s&hour=%s&minute=00&etn=%s\">%sZ</a>, ",
            gmdate("Y", $begints),
            gmdate("m", $begints),
            gmdate("d", $begints),
            gmdate("H", $begints),
            $num,
            gmdate("H", $begints)
        );
        $begints += 3600;
    }
}

$ltext = "";
if (isset($lurl)) {
    $ltext .= "<h3>Local Storm Reports:</h3><img src=\"$lurl\">";
}
$kml = htmlentities($row["kml"]);

$t->content = <<<EOM
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="/current/severe.phtml">Severe Wx Products</a></li>
    <li class="breadcrumb-item active" aria-current="page">Storm Prediction Center Convective Watch Archive</li>
  </ol>
</nav>

<p>This application presents a browsable archive
of <a href="https://www.spc.noaa.gov">SPC</a> issued tornado and
severe thunderstorm watches. Each sequential
watch is assigned a numeric ID, which uniquely identifies a watch for each year.
Up until 2006, the polygon was the valid area of the watch.  Since then, the
valid area is issued by county and marine zone.
</p>

<p><strong>Related Links:</strong>
<a class="btn btn-primary" href="{$spclink}">View This Watch on SPC Website</a>
&nbsp;
<a class="btn btn-primary" href="/request/gis/spc_watch.phtml">Download Watches</a>
&nbsp;
<a class="btn btn-primary" href="/nws/pds_watches.php">List PDS Watches</a>
</p>


<form method="GET" action="watch.phtml">

<div class="row">
 <div class="col-md-7">
<strong>All active watches at issuance:</strong>
<img src="{$ourl}" class="img img-fluid">

</div><div class="col-md-5">

<p>
{$prevlink} &nbsp; -- &nbsp; {$nextlink}

<table class="table table-striped">
<tr><th>Type:</th><td>{$row["type"]}</td></tr>
<tr><th>Watch #:</th><td>{$yselect}
<input name="num" type="text" size="5" value="{$row["num"]}">
<input type="submit" value="GO!" class="btn btn-primary"></td></tr>

{$textlinks}
<tr><th>Issue:</th><td>{$issue} UTC</td></tr>
<tr><th>Expired/Cancelled:</th><td>{$expire} UTC</td></tr>
{$woulink}
</table>


<p><strong>Archive Status:</strong>
<ul>
  <li>Watch archive should be complete back to 1997.</li>
  <li>RADAR composite is mostly complete back to 1997.</li>
  <li>Local Storm Reports are available after 2002.</li>
</ul>

</div></div>

</form>

<h3>Initial Watch Box and Watch By County Outline</h3>
<br />{$ERROR}
<img src="{$url}">

{$ltext}

<p><strong>Watch Polygon (KML):</strong><br />
<pre>{$kml}</pre>

<p><strong>Watch Polygon (GeoJSON):</strong><br />
<pre>{$row["geojson"]}</pre>


EOM;
$t->render('single.phtml');
