<?php
require_once "../../config/settings.inc.php";
require_once "../../include/myview.php";
require_once "../../include/forms.php";
require_once "../../include/database.inc.php";
require_once "../../include/network.php";
require_once "../../include/reference.php";
require_once "../../include/mlib.php";

$t = new MyView();

$nt = new NetworkTable("WFO");
$nt->table["ALL"] = array("name" => " --- All ---- ", "id" => "ALL", "archive_begin" => null);
$vtec_phenomena = $reference["vtec_phenomena"];
$vtec_significance = $reference["vtec_significance"];
$postgis = iemdb('postgis');

$wfo = get_str404("wfo", 'ALL', 4);
$limit = isset($_GET["limit"]) ? xssafe($_GET["limit"]) : 'TOSV';
$tlimit = isset($_GET["tlimit"]) ? xssafe($_GET["tlimit"]) : '2007';
$lstring = "'SV','TO'";
if ($limit == "TO") {
    $lstring = "'TO'";
} else if ($limit == 'SV') {
    $lstring = "'SV'";
} else if ($limit == 'FF') {
    $lstring = "'FF'";
} else if ($limit == 'MA') {
    $lstring = "'MA'";
} else if ($limit == 'SQ') {
    $lstring = "'SQ'";
}

$tstring = '1980-01-01';
if ($tlimit == '2007') {
    $tstring = '2007-10-01';
} else if ($tlimit == '2011') {
    $tstring = '2011-01-01';
}
$wlimit = "and wfo = $1";
if ($wfo == 'ALL') {
    $wlimit = "and 'ALL' = $1";
}

pg_query($postgis, "SET TIME ZONE 'UTC'");
$stname_asc = iem_pg_prepare(
    $postgis,
    "select round((ST_AREA(ST_Transform(geom,9311)) / 1000000.0)::numeric,2) as area,
    eventid, date(issue), phenomena, significance, wfo, vtec_year from sbw
WHERE status = 'NEW' and phenomena in ($lstring) $wlimit and
significance = 'W' and issue > '$tstring' and ST_Area(geom) > 0 ORDER by area ASC LIMIT 10");
$stname_desc = iem_pg_prepare($postgis, "select round((ST_AREA(ST_Transform(geom,9311)) / 1000000.0)::numeric,2) as area,
eventid, date(issue), phenomena, significance, wfo, vtec_year from sbw
WHERE status = 'NEW' and phenomena in ($lstring) $wlimit and
significance = 'W' and issue > '$tstring'  ORDER by area DESC LIMIT 10");

$t->title = "NWS SVR/TOR Storm Based Warnings by Size";

$extra = array("ALL" => array("name" => "--- ALL Forecast Offices ---", "id" => "ALL", "archive_begin" => null));
$sselect = networkSelect("WFO", $wfo, $extra, "wfo");

$ar = array(
    "TOSV" => "Tornado + Severe Thunderstorm",
    "TO" => "Tornado Only",
    "SV" => "Severe Thunderstorm Only",
    "FF" => "Flash Flood Only",
    "MA" => "Marine Only",
    "SQ" => "Snow Squall",
);

$lselect = make_select("limit", $limit, $ar);

$ar = array(
    "all" => "All Available",
    "2007" => "After 1 Oct 2007",
    "2011" => "After 1 Jan 2011"
);
$tselect = make_select("tlimit", $tlimit, $ar);

$ltable = "";
$rs = pg_execute($postgis, $stname_desc, array(unrectify_wfo($wfo)));
while ($row = pg_fetch_assoc($rs)) {

    $url = sprintf(
        "/vtec/?year=%s&wfo=%s&phenomena=%s&significance=%s&eventid=%s",
        $row["vtec_year"],
        rectify_wfo($row['wfo']),
        $row["phenomena"],
        $row['significance'],
        $row["eventid"]
    );
    $ltable .= sprintf(
        "<tr><td>%s</td><td>%.0f</td><td>%s</td><td><a href='%s'>%s %s %s</a></td></tr>\n",
        $row['date'],
        $row['area'],
        $row['wfo'],
        $url,
        $vtec_phenomena[$row['phenomena']],
        $vtec_significance[$row["significance"]],
        $row['eventid']
    );
}

$btable = "";
$rs = pg_execute($postgis, $stname_asc, array(unrectify_wfo($wfo)));
while ($row = pg_fetch_assoc($rs)) {
    $url = sprintf(
        "/vtec/?year=%s&wfo=%s&phenomena=%s&significance=%s&eventid=%s",
        $row["vtec_year"],
        rectify_wfo($row['wfo']),
        $row["phenomena"],
        $row['significance'],
        $row["eventid"]
    );
    $btable .= sprintf(
        "<tr><td>%s</td><td>%.2f</td><td>%s</td><td><a href='%s'>%s %s %s</a></td></tr>\n",
        $row['date'],
        $row['area'],
        $row["wfo"],
        $url,
        $vtec_phenomena[$row['phenomena']],
        $vtec_significance[$row["significance"]],
        $row['eventid']
    );
}

$t->content = <<<EOM

<nav aria-label="Breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/current/severe.phtml">Severe Weather Mainpage</a></li>
        <li class="breadcrumb-item active" aria-current="page">Top 10 Warnings by size</li>
    </ol>
</nav>

<h3>Top 10 NWS WFO Storm Based Warnings by Size</h3>

<p>This application prints out a listing of the top 10 largest and smallest
storm based warnings by Weather Forecast Office (WFO). You can click the
Event ID to get more information on that warning. This page defaults to show the
largest and smallest warnings from any WFO.  Storm Based Warnings did not become
operational until 1 Oct 2007, so warning polygons prior to that date were only
experimental at best.  The areas shown are computed using
<a href="https://spatialreference.org/ref/epsg/9311/">EPSG:9311</a> (US National Atlas Equal Area).</p>

<div class="alert alert-info" role="status" aria-live="polite">Please be patient for page to load...</div>

<form name="selector" method="GET" aria-label="Filter warnings by WFO, type, and time period">
    <div class="row g-2 align-items-end mb-3">
        <div class="col-md-4">
            <label for="wfo" class="form-label">Select WFO:</label>
            {$sselect}
        </div>
        <div class="col-md-4">
            <label for="limit" class="form-label">Limit Option:</label>
            {$lselect}
        </div>
        <div class="col-md-4">
            <label for="tlimit" class="form-label">Time Period:</label>
            {$tselect}
        </div>
        <div class="col-auto">
            <button type="submit" class="btn btn-primary" aria-label="Apply filters">Giveme Giveme!</button>
        </div>
    </div>
</form>


<h3 id="largest-table">Top 10 Largest for WFO: {$wfo}</h3>
<table class="table table-sm table-striped" aria-labelledby="largest-table">
    <thead>
        <tr>
            <th scope="col">Date Issued</th>
            <th scope="col">Size (sq km)</th>
            <th scope="col">WFO</th>
            <th scope="col">Phenomena</th>
        </tr>
    </thead>
    <tbody>
        {$ltable}
    </tbody>
</table>


<h3 id="smallest-table">Top 10 Smallest for WFO: {$wfo}</h3>
<table class="table table-sm table-striped" aria-labelledby="smallest-table">
    <thead>
        <tr>
            <th scope="col">Date Issued</th>
            <th scope="col">Size (sq km)</th>
            <th scope="col">WFO</th>
            <th scope="col">Phenomena</th>
        </tr>
    </thead>
    <tbody>
        {$btable}
    </tbody>
</table>
EOM;
$t->render('single.phtml');
