<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 161);
require_once "../../include/myview.php";
require_once "../../include/forms.php";
require_once "../../include/database.inc.php";
require_once "../../include/network.php";
require_once "../../include/mlib.php";

$t = new MyView();
$year = get_int404("year", 1987);
$day = get_int404("day", 1);
$level = get_str404("level", "CATEGORICAL.HIGH");
// copied from autoplot 200, sans firewx
$levels = Array(
    "ANY SEVERE.0.02" => "Any Severe 2% (Day 3+)",
    "ANY SEVERE.0.05" => "Any Severe 5% (Day 3+)",
    "ANY SEVERE.0.15" => "Any Severe 15% (Day 3+)",
    "ANY SEVERE.0.25" => "Any Severe 25% (Day 3+)",
    "ANY SEVERE.0.30" => "Any Severe 30% (Day 3+)",
    "ANY SEVERE.0.35" => "Any Severe 35% (Day 3+)",
    "ANY SEVERE.0.45" => "Any Severe 45% (Day 3+)",
    "ANY SEVERE.0.60" => "Any Severe 60% (Day 3+)",
    "ANY SEVERE.SIGN" => "Any Severe Significant (Day 3+)",
    "ANY SEVERE.CIG1" => "Any Severe CIG1 (Day 3+)",
    "ANY SEVERE.CIG2" => "Any Severe CIG2 (Day 3+)",
    "ANY SEVERE.CIG3" => "Any Severe CIG3 (Day 3+)",
    "CATEGORICAL.TSTM" => "Categorical Thunderstorm Risk (Days 1-3)",
    "CATEGORICAL.MRGL" => "Categorical Marginal Risk (2015+) (Days 1-3)",
    "CATEGORICAL.SLGT" => "Categorical Slight Risk (Days 1-3)",
    "CATEGORICAL.ENH" => "Categorical Enhanced Risk (2015+) (Days 1-3)",
    "CATEGORICAL.MDT" => "Categorical Moderate Risk (Days 1-3)",
    "CATEGORICAL.HIGH" => "Categorical High Risk (Days 1-3)",
    "HAIL.0.05" => "Hail 5% (Days 1+2)",
    "HAIL.0.15" => "Hail 15% (Days 1+2)",
    "HAIL.0.25" => "Hail 25% (Days 1+2)",
    "HAIL.0.30" => "Hail 30% (Days 1+2)",
    "HAIL.0.35" => "Hail 35% (Days 1+2)",
    "HAIL.0.45" => "Hail 45% (Days 1+2)",
    "HAIL.0.60" => "Hail 60% (Days 1+2)",
    "HAIL.SIGN" => "Hail Significant (Days 1+2)",
    "HAIL.CIG1" => "Hail CIG1 (Days 1+2)",
    "HAIL.CIG2" => "Hail CIG2 (Days 1+2)",
    "HAIL.CIG3" => "Hail CIG3 (Days 1+2)",
    "TORNADO.0.02" => "Tornado 2% (Days 1+2)",
    "TORNADO.0.05" => "Tornado 5% (Days 1+2)",
    "TORNADO.0.10" => "Tornado 10% (Days 1+2)",
    "TORNADO.0.15" => "Tornado 15% (Days 1+2)",
    "TORNADO.0.25" => "Tornado 25% (Days 1+2)",
    "TORNADO.0.30" => "Tornado 30% (Days 1+2)",
    "TORNADO.0.35" => "Tornado 35% (Days 1+2)",
    "TORNADO.0.45" => "Tornado 45% (Days 1+2)",
    "TORNADO.0.60" => "Tornado 60% (Days 1+2)",
    "TORNADO.SIGN" => "Tornado Significant (Days 1+2)",
    "TORNADO.CIG1" => "Tornado CIG1 (Days 1+2)",
    "TORNADO.CIG2" => "Tornado CIG2 (Days 1+2)",
    "TORNADO.CIG3" => "Tornado CIG3 (Days 1+2)",
    "WIND.0.05" => "Wind 5% (Days 1+2)",
    "WIND.0.15" => "Wind 15% (Days 1+2)",
    "WIND.0.25" => "Wind 25% (Days 1+2)",
    "WIND.0.30" => "Wind 30% (Days 1+2)",
    "WIND.0.35" => "Wind 35% (Days 1+2)",
    "WIND.0.45" => "Wind 45% (Days 1+2)",
    "WIND.0.60" => "Wind 60% (Days 1+2)",
    "WIND.0.75" => "Wind 75% (Days 1+2)",
    "WIND.0.90" => "Wind 90% (Days 1+2)",
    "WIND.SIGN" => "Wind Significant (Days 1+2)",
    "WIND.CIG1" => "Wind CIG1 (Days 1+2)",
    "WIND.CIG2" => "Wind CIG2 (Days 1+2)",
    "WIND.CIG3" => "Wind CIG3 (Days 1+2)",
);
if (!array_key_exists($level, $levels)) {
    die("Invalid level");
}

$tokens = explode(".", $level, 2);
$category = $tokens[0];
$threshold = $tokens[1];

$url = "/json/spc_bysize.py";
$wsargs = [
    "day" => $day,
    "threshold" => $threshold,
    "category" => $category,
    "syear" => $year,
    "sort" => "desc",
];

$public = sprintf("%s?%s", $url, http_build_query($wsargs));

$table = <<<EOM

<p>JSON Webservice for the data below:<br />
<code>{$public}</code></p>
<table class="table table-striped">
<thead>
<tr><th>Rank</th>
<th>Canonical Outlook Date</th>
<th>Cycle Hour UTC</th><th>Size (sq km)</th><th>SPC Link/Image</th></tr></thead>
<tbody>

EOM;

$stable = str_replace("sort=desc", "sort=asc", $table);

$json = require_json_response($url, $wsargs);
if (sizeof($json["outlooks"]) == 0) {
    $table .= "<tr><td colspan=\"5\">No Data Found</td></tr>";
} else {
    $rank = 1;
    foreach ($json["outlooks"] as $key => $val) {
        $table .= sprintf(
            "<tr><th>%s</th><td>%s</td><td>%s</td><td>%s</td>".
            "<td><img src=\"%s\" class=\"img-fluid\">".
            "</div></tr>",
            $rank,
            $val["date"],
            $val["cycle"],
            $val["area_sqkm"],
            $val["imgsrc"],
        );
        $rank += 1;
    }
}
$table .= "</tbody></table>\n";

$wsargs["sort"] = "asc";
$json = require_json_response($url, $wsargs);
if (sizeof($json["outlooks"]) == 0) {
    $stable .= "<tr><td colspan=\"5\">No Data Found</td></tr>";
} else {
    $rank = 1;
    foreach ($json["outlooks"] as $key => $val) {
        $stable .= sprintf(
            "<tr><th>%s</th><td>%s</td><td>%s</td><td>%s</td>".
            "<td><img src=\"%s\" class=\"img-fluid\">".
            "</div></tr>",
            $rank,
            $val["date"],
            $val["cycle"],
            $val["area_sqkm"],
            $val["imgsrc"],
        );
        $rank += 1;
    }
}
$stable .= "</tbody></table>\n";


$days = Array(
    "1" => "Day 1",
    "2" => "Day 2",
    "3" => "Day 3",
    "4" => "Day 4",
    "5" => "Day 5",
    "6" => "Day 6",
    "7" => "Day 7",
    "8" => "Day 8",
);
$dayselect = make_select("day", $day, $days);
$lselect = make_select("level", $level, $levels);
$yrselect = yearSelect(1987, $year);

$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 SPC Outlooks by size</li>
 </ol>
</nav>

<h3>Top 10 SPC Outlooks by Size</h3>

<div class="alert alert-info">
<strong>New:</strong> Scroll down to see the top 10 smallest outlooks by size!
</div>

<p>The IEM attempts a robust archive of SPC outlooks.  These outlooks have
changed methodologies and threshold meanings over the years. You are strongly
encouraged to set a start year to limit the data to a given period. The listing
only inludes one outlook for a given canonical date.</p>

<p><strong>This app can be slow to generate!</strong> For common outlook types,
the backend database query can take a number of seconds to complete. The computed
area is done within the <a href="https://spatialreference.org/ref/epsg/9311/">US National Atlas Equal Area</a> (EPSG:9311).</p>

<p><strong>Related</strong>:
<a href="mcd_top10.phtml" class="btn btn-primary">Top 10 MCDs by Size</a>
<a href="/request/gis/outlooks.phtml" class="btn btn-primary">SPC Outlook Shapefile Download</a>
</p>

<p><form method="GET" name="u">
<table class="table table-bordered">
<tr>
<td>Inclusive Start Year: $yrselect</td>
<td>Select Day: $dayselect</td>
<td>Select Threshold: $lselect</td>
</table>
<input type="submit" value="Update Table" class="btn btn-primary">
</form></p>

<h3>Top 10 Largest Day: {$day} Category: {$category} Threshold: {$threshold}</h3>

<p>Generated at: <code>{$json["generated_at"]}</code> in {$json["query_time[s]"]}s</p>

$table

<h3>Top 10 Smallest Day: {$day} Category: {$category} Threshold: {$threshold}</h3>

<p>Generated at: <code>{$json["generated_at"]}</code> in {$json["query_time[s]"]}s</p>

$stable

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