<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 152);
require_once "../../include/myview.php";
require_once "../../include/forms.php";

$year = get_int404("year", date("Y", time() - 3600));
$month = get_int404("month", date("m", time() - 3600));
$day = get_int404("day", date("d", time() - 3600));
$sortby = isset($_GET["sortby"]) ? xssafe($_GET["sortby"]) : "size";
$typ = isset($_GET["typ"]) ? xssafe($_GET["typ"]) : "W";

$d = array("size" => "S", "wfo" => "W", "time" => "T");

$ts = mktime(0, 0, 0, $month, $day, $year);

$archivebegin = mktime(0, 0, 0, 1, 1, 2005);
if ($ts < $archivebegin) {
    $ts = $archivebegin;
}

$imap = sprintf(
    "%s/plotting/auto/plot/203/typ:%s::sort:%s::date:%s.txt",
    $INTERNAL_BASEURL,
    substr($typ, 0, 1),
    $d[$sortby],
    date("Y-m-d", $ts)
);
$imgurl = sprintf(
    "/plotting/auto/plot/203/typ:%s::sort:%s::date:%s.png",
    substr($typ, 0, 1),
    $d[$sortby],
    date("Y-m-d", $ts)
);

$gis_url = sprintf(
    "/cgi-bin/request/gis/watchwarn.py?year1=%s&amp;year2=%s&amp;" .
        "month1=%s&amp;day1=%s&amp;hour1=0&amp;minute1=0&amp;month2=%s&amp;" .
        "day2=%s&amp;hour2=0&amp;minute2=0&amp;limit1=yes",
    date("Y", $ts),
    date("Y", $ts),
    date("m", $ts),
    date("d", $ts),
    date("m", $ts + 86400),
    date("d", $ts + 86400)
);

$t = new MyView();
$t->title = "Storm Based Warning Polygon Visual Summary";

$opts = array(
    "size" => "Polygon Size",
    "wfo" => "NWS WFO Office",
    "time" => "Issuance Time"
);
$sselect = make_select("sortby", $sortby, $opts);
$opts = array(
    "W" => "Tornado + Severe Thunderstorm Warning",
    "F" => "Flash Flood Warning",
    "M" => "Marine Warning"
);
$wselect = make_select("typ", $typ, $opts);


$today = date("d M Y", time() - 3600);

$ys = yearSelect(2002, date("Y", $ts), "year");
$ds = daySelect(date("d", $ts), "day");
$ms = monthSelect(date("m", $ts), "month");
$content = <<<EOM

<div class="page-header">
<h3>Daily Storm Based Warning Summaries</h3>
</div>

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

<form method="GET">

<table class="table table-bordered"><caption>Select a date:</caption>
<thead><tr><th>Year:</th><th>Month:</th><th>Day:</th><td></td></tr></thead>
<tbody>
<tr>
<td>{$ys}</td>
<td>{$ms}</td>
<td>{$ds}</td>
<td rowspan="2"><input type="submit" value="GO!"></td>
</tr>
<tr><td colspan="3">
Sort by: {$sselect}
<br />Warning Types: {$wselect}

</td></tr>
</tbody>
</table>
</form>
</div><!-- end of col-md-5 -->
<div class="col-md-7">

<p>This application displays an image summary of NWS
Storm Based Warnings for a
given UTC date (0z to 0z).  This summary is based 
on an archive the IEM maintains and so is unofficial.  Also, prior to
1 October 2007, these warnings were not official and their shapes may or 
may not have been considered by the issuing forecaster. This page works for
dates between 1 Jan 2002 and {$today}.
Areas are computed and polygons shown using a US National Atlas Equal Area 
projection (EPSG:9311).</p>

<p><strong>Related Data Links:</strong>
<br /> You can download a GIS dataset with these polygons via the
<a href="/request/gis/watchwarn.phtml">GIS WWA Download</a>. For convenience,
here is a <a href="{$gis_url}">direct shapefile link</a> with the polygons
you see below. The <a href="sbwstats.phtml">summary size statistics page</a>
provides an overview of the size of these polygons.  The actual image below
was generated by <a href="/plotting/auto/?q=203">Autoplot #203</a>.</p>

</div><!-- ./ col-md-7 -->
</div><!-- ./ row -->

EOM;
$content .= file_get_contents($imap);
$content .= <<<EOM
<div class='alert alert-info'>Note: You can click on the polygon to get the 
warning text, radar, and more!</div>

<!-- note: can't make this responsive as then the imagemap gets fouled up -->
<p><img src="{$imgurl}" usemap="#mymap"></p>

<p><b>Image Legend:</b><br />
<div style="float: left; margin-right: 5px;"><img src="example.png"></div>
The storm based warning is drawn with red meaning Tornado and yellow 
meaning Severe Thunderstorm. "422 km^2" is the size of the polygon, "73%" 
is the reduction in size of the warning versus the county based warning.
"BMX.TO.84.2252" indicates the Forecast Office, warning type, event ID 
number for the year, and issuance time for the date the image is valid (22:52 UTC).
<br /><span style="color: #f00;">Red text indicates less than 25% reduction in size</span>
<br /><span style="color: #0f0;">Green text indicates greater than 75% reduction in size</span>
EOM;
$t->content = $content;
$t->render('single.phtml');
