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

$t = new MyView();
$t->title = "Quality Control, MADIS Raw QC";
$t->headextra = <<<EOM
<link rel="stylesheet" type="text/css" href="/QC/madis/network.css">
EOM;
$dbconn = iemdb("iem");
$stname = iem_pg_prepare(
    $dbconn,
    "SELECT c.*, t.id as station, t.name, " .
        "c.valid at time zone 'UTC' as uv from current_qc c JOIN stations t on " .
        "(c.iemid = t.iemid) WHERE t.network = $1"
);
$sortcol = isset($_GET["sortcol"]) ? xssafe($_GET["sortcol"]) : "station";
$network = isset($_GET["network"]) ? substr(xssafe($_GET["network"]), 0, 10) : "IA_ASOS";
if ($network == "AWOS") {
    $network = "IA_ASOS";
}
$baa = "network.phtml?network={$network}";

$rs = pg_execute($dbconn, $stname, array($network));

$data = array();
while ($row = pg_fetch_assoc($rs)) {
    $data[$row["station"]] = $row;
}

$finalA = array();
$finalA = aSortBySecondIndex($data, $sortcol);

$rs = pg_query(
    iemdb("mesosite"),
    "SELECT id, name from networks"
);
$networks = array();
while ($row = pg_fetch_assoc($rs)) {
    $networks[$row["id"]] = $row["name"];
}

$rs = pg_query(
    $dbconn,
    "select distinct network from current_qc q JOIN stations t " .
        "ON (q.iemid = t.iemid) ORDER by network ASC"
);
$ar = array();
while ($row = pg_fetch_assoc($rs)) {
    $ar[$row["network"]] = $networks[$row["network"]];
}
$sselect = make_select("network", $network, $ar);

$i = false;

function vp($v, $r)
{
    if (is_null($v)) return "<td>M</td>";
    $v = floatval($v);
    if ($v > 0) return '<td><font id="c">' . myround($v, $r) . '</font></td>';
    else if ($v < 0) return '<td><font id="w">' . myround($v, $r) . '</font></td>';
    return '<td>' . myround($v, $r) . '</td>';
}
function pp($v, $r)
{
    if (is_null($v)) return "M";
    $v = floatval($v);
    if (intval($v) == -17966) return 'M';
    return myround($v, $r);
}
$table = "";
$now = new DateTime("now");
$thisyear = $now->format("Y");
foreach ($finalA as $key => $val) {
    $ts = new DateTime($val["uv"]);
    $fmt = ($ts->format("Y") == $thisyear) ? "d G:i" : "Y M d G:i";
    $table .= '<tr ';
    if ($i) $table .= 'class="even"';
    $table .= '><td>' . $val["station"] . '</td><td>' . $val["name"] . '</td>
  <td>' . $ts->format($fmt) . 'Z</td>
  <td id="divider">' . pp($val["tmpf"], 0) . '</td>' .
        vp($val["tmpf_qc_av"], 1) .
        vp($val["tmpf_qc_sc"], 1) .
        '<td id="divider">' . pp($val["dwpf"], 0) . '</td>' .
        vp($val["dwpf_qc_av"], 1) .
        vp($val["dwpf_qc_sc"], 1) .
        '<td id="divider">' . pp($val["alti"], 2) . '</td>' .
        vp($val["alti_qc_av"], 2) .
        vp($val["alti_qc_sc"], 2) .
        '</tr>';

    $i = !$i;
}

$t->content = <<<EOM
<h1 class="h4 mb-3">MADIS QC Values</h1>
<p>Here are the MADIS quality control values for selected networks in the IEM.
<span class="text-danger fw-semibold">Red numbers</span> indicate the observed value is above the expected range.
<span class="text-primary fw-semibold">Blue numbers</span> indicate the observed value is below the expected range.
The <strong>QC</strong> value is an average of all MADIS QC routines. The <strong>Spatial</strong> column is the result of the <em>Spatial Consistency Check</em>.</p>

<form method="GET" name="switchNetwork" class="row gy-2 align-items-end" role="search" aria-label="Select network to view">
    <div class="col-auto">
        <label for="network" class="form-label fw-semibold">Network</label>
        {$sselect}
    </div>
    <div class="col-auto">
        <button type="submit" class="btn btn-primary">Switch Network</button>
    </div>
</form>

<div class="mt-3 mb-2 small text-muted" id="madis-qc-legend" aria-live="polite">Color legend: <span class="text-danger fw-semibold">High bias</span>, <span class="text-primary fw-semibold">Low bias</span>.</div>

<table id="madis-qc-table" class="table table-sm table-striped table-bordered align-middle" aria-describedby="madis-qc-legend">
<caption class="text-start">MADIS QC table listing recent observation valid time (UTC) with observed (OB), overall quality control (QC) average, and spatial consistency (Spatial) checks for Air Temperature, Dewpoint, and Altimeter. Column sort links reload the page.</caption>
<thead class="sticky">
<tr>
    <th scope="col" rowspan="2"><a href="{$baa}&sortcol=station">Site ID</a></th>
    <th scope="col" rowspan="2"><a href="{$baa}&sortcol=sname">Station Name</a></th>
    <th scope="col" rowspan="2">Ob Valid (UTC)</th>
    <th scope="col" colspan="3" id="atgroup">Air Temperature [F]</th>
    <th scope="col" colspan="3" id="dwgroup">Dewpoint [F]</th>
    <th scope="col" colspan="3" id="altigroup">Altimeter [inch]</th>
</tr>
<tr>
    <th scope="col"><a href="{$baa}&sortcol=tmpf">OB</a></th>
    <th scope="col"><a href="{$baa}&sortcol=tmpf_qc_av">QC</a></th>
    <th scope="col"><a href="{$baa}&sortcol=tmpf_qc_sc">Spatial</a></th>
    <th scope="col"><a href="{$baa}&sortcol=dwpf">OB</a></th>
    <th scope="col"><a href="{$baa}&sortcol=dwpf_qc_av">QC</a></th>
    <th scope="col"><a href="{$baa}&sortcol=dwpf_qc_sc">Spatial</a></th>
    <th scope="col"><a href="{$baa}&sortcol=alti">OB</a></th>
    <th scope="col"><a href="{$baa}&sortcol=alti_qc_av">QC</a></th>
    <th scope="col"><a href="{$baa}&sortcol=alti_qc_sc">Spatial</a></th>
</tr>
</thead>
<tbody>
{$table}
</tbody>
</table>
EOM;
$t->render('single.phtml');
