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

$t = new MyView();
$sortcol = get_str404("sortcol", "peak");
$metar = get_str404("metar", "no");
$sorder = get_str404("sorder", "desc");
$state = get_str404("state", "IA", 2);

$t->refresh = 60;
$t->title = "$state Current Conditions";

$arr = array("state" => $state);
$jobj = iemws_json("currents.json", $arr);

$vals = array(
    "tmpf" => "Air Temperature [°F]", "dwpf" => "Dew Point Temp [°F]",
    "sknt" => "Wind Speed [knots]", "drct" => "Wind Direction [deg]",
    "alti" => "Altimeter [mb]", "peak" => "Today's Wind Gust [knots]",
    "peak_ts" => "Time of Peak Gust", "relh" => "Relative Humidity",
    "feel" => "Feels Like [°F]", "vsby" => "Visibility [miles]",
    "ts" => "Observation Time", "phour" => "Last Hour Rainfall [inch]",
    "min_tmpf" => "Today's Low Temperature",
    "max_tmpf" => "Today's High Temperature",
    "pday" => "Today Rainfall [inch]"
);
if (! array_key_exists($sortcol, $vals)) {
    die405();
}

$t->current_network = 'By State';

$mno = ($metar == "no") ? " CHECKED" : '';
$myes = ($metar == "yes") ? " CHECKED" : '';
$ar = array(
    "asc" => "Ascending",
    "desc" => "Descending"
);
$aselect = make_select("sorder", $sorder, $ar);

$uri = "all.phtml?state=$state&metar=$metar&sorder=$sorder&sortcol=";

$mydata = array();
foreach ($jobj["data"] as $bogus => $iemob) {
    $key = $iemob["station"];
    $mydata[$key] = $iemob;
    $mydata[$key]["sped"] = $mydata[$key]["sknt"] * 1.15078;
    $mydata[$key]["relh"] = relh(f2c($mydata[$key]["tmpf"]), f2c($mydata[$key]["dwpf"]));
    if ($mydata[$key]["relh"] < 5) {
        $mydata[$key]["relh"] = "M";
        $mydata[$key]["dewpf"] = "M";
        if ($sortcol == "feel" || $sortcol == "dwpf" || $sortcol == "relh") {
            $mydata[$key]["feel"] = null;
            continue;
        }
    }
    if ($mydata[$key]["tmpf"] < -60) {
        $mydata[$key]["tmpf"] = "M";
        if ($sortcol == "tmpf" || $sortcol == "feel" || $sortcol == "dwpf" || $sortcol == "relh") {
            $mydata[$key]["feel"] = null;
            continue;
        }
    }
    if ($mydata[$key]["alti"] < -60) {
        $mydata[$key]["alti"] = "M";
        if ($sortcol == "alti") {
            $mydata[$key]["alti"] = null;
            continue;
        }
    }
    if ($mydata[$key]["vsby"] < 0) {
        $mydata[$key]["vsby"] = "M";
        if ($sortcol == "vsby") {
            $mydata[$key]["vsby"] = null;
            continue;
        }
    }

    if ($mydata[$key]["max_gust"] > $mydata[$key]["max_sknt"]) {
        $mydata[$key]["peak"] = $mydata[$key]["max_gust"];
        if (! is_null($mydata[$key]["local_max_gust_ts"])) {
            $mydata[$key]["peak_ts"] = strtotime($mydata[$key]["local_max_gust_ts"]);
        }
    } else {
        $mydata[$key]["peak"] = $mydata[$key]["max_sknt"];
        $mydata[$key]["peak_ts"] = 0;
        if (! is_null($mydata[$key]["local_max_sknt_ts"])) {
            $mydata[$key]["peak_ts"] = strtotime($mydata[$key]["local_max_sknt_ts"]);
        }
    }
}

$finalA = array();
$finalA = aSortBySecondIndex($mydata, $sortcol, $sorder);
$now = time();
$i = 0;
$table = "";
foreach ($finalA as $key => $val) {
    $i++;
    if ($i > 30) break;
    $parts = $finalA[$key];

    $table .= "<tr";
    if ($i % 2 == 0)  $table .= " bgcolor='#eeeeee'";
    $table .= "><td><input type=\"checkbox\" name=\"st[]\" value=\"{$key}\"></td>";

    $tdiff = $now - strtotime($parts["local_valid"]);
    $table .= "<td>" . $parts["name"] . " (" . $key . ")</td>";
    $table .= "<td ";
    $fmt = "h:i A";
    if ($tdiff > 10000) {
        $fmt = "d M h:i A";
        $table .= 'bgcolor="red"';
    } else if ($tdiff > 7200) {
        $table .= 'bgcolor="orange"';
    } else if ($tdiff > 3600) {
        $table .= 'bgcolor="green"';
    }
    $table .= ">" . date($fmt, strtotime($parts["local_valid"])) . "</td>
     <td align='center'>" . myround($parts["tmpf"], 0) . "(<font color=\"#ff0000\">" . myround($parts["max_tmpf"], 0) . "</font>/<font color=\"#0000ff\">" . myround($parts["min_tmpf"], 0) . "</font>)</td>
     <td>" . myround($parts["dwpf"], 0) . "</td>
     <td>" . myround($parts["feel"], 0) . "</td>
        <td>" . $parts["relh"] . "</td>
        <td>" . $parts["alti"] . "</td>
        <td>" . $parts["vsby"] . "</td>
             <td>" . myround($parts["sknt"], 0);
    if (strlen($parts["gust"] != 0)) {
        $table .= "G" . myround($parts["gust"], 0);
    }
    $table .= "</td>";
    $table .= "<td>" . $parts["drct"] . "</td><td>";
    if (array_key_exists("peak", $parts) && array_key_exists("peak_ts", $parts)) {
        $table .= myround($parts["peak"], 0) . " @ " . date("h:i A", $parts["peak_ts"]);
    }
    $table .= "<td>{$parts['phour']}</td><td>{$parts['pday']}</td></tr>\n";
    if ($metar == "yes") {
        $table .= "<tr";
        if ($i % 2 == 0)  $table .= " bgcolor='#eeeeee'";
        $table .= ">";
        $table .= "<td colspan=14>
             <font color=\"brown\">" . $parts["raw"] . "</font></td>
             </tr>\n";
    }
}
$sselect = stateSelect($state);
$t->content = <<<EOM
<p>
<form method="GET" action="all.phtml" name="work">
<input type="hidden" value="{$sortcol}" name="sortcol">
<table border=1 cellspacing=0 cellpadding=1>
<tr>
  <th>View Options:</th>
  <td>Include METARS:
  <input value="no" type="radio" name="metar"{$mno}> No
  <input value="yes" type="radio" name="metar"{$myes}> Yes
</td>
<td>Sort Order:{$aselect}</td>
<td>State:{$sselect}</td>
<td><input type="submit" value="Go!"></form></td>
</tr></table>
<p>Top 30 values of <b>{$vals[$sortcol]}</b> displayed from the combination
of RWIS, ASOS, & AWOS sites in the state.

<form method="GET" action="/my/current.phtml">

<table style="width: 100%; font-size: 10pt;" cellspacing=0 cellpadding=1 border=1>
<thead class="sticky">
<tr>
  <th rowspan="2">ADD:</th>
  <th rowspan="2">Station:</th>
  <th rowspan="2"><a href="{$uri}ts">Ob Time</a></th>
  <th colspan="3">Temps &deg;F</th>
  <th colspan="3">&nbsp;</th>
  <th colspan="3">Wind [knots]</th>
  <th colspan="2">Precip</font></th>
<tr>
  <th>
 <a href="{$uri}tmpf">Air</a>
 (<a href="{$uri}max_tmpf">Hi</a> /
 <a href="{$uri}min_tmpf">Lo</a>)
</th>
  <th><a href="{$uri}dwpf">Dewp</a></th>
  <th><a href="{$uri}feel">Feels Like</a></th>
  <th><a href="{$uri}relh">RH %</a></th>
  <th><a href="{$uri}alti">Alti</a></th>
  <th><a href="{$uri}vsby">Vsby</a></th>
  <th><a href="{$uri}sknt">Speed</a></th>
  <th><a href="{$uri}drct">Direction</a></th>
  <th><a href="{$uri}peak">Gust</a>
    @ <a href="{$uri}peak_ts">Time</a></th>
  <th><a href="{$uri}phour">Last Hour</a></th>
  <th><a href="{$uri}pday">Today</a></th>
</tr></thead>
<tbody>
{$table}
</tbody>
</table>

<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form></div>
EOM;
$t->render('sortables.phtml');
