<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 99);
require_once "../../include/myview.php";
require_once "../../include/database.inc.php";
require_once "../../include/iemprop.php";

$prop = get_iemprop("nwsli.syncdate");
$nwsli_sync_date = is_null($prop) ? "N/A": $prop;
$pgconn = iemdb('hads');
$rs = pg_query($pgconn, "SELECT nwsli, max(product) from unknown "
    . "WHERE length(nwsli) = 5 and "
    . "nwsli ~* '[A-Z][A-Z][A-Z][A-Z][0-9]' "
    . "GROUP by nwsli ORDER by nwsli ASC");
$table = <<<EOM
<div class="table-responsive">
<table id="table1" class="table table-striped table-bordered align-middle mb-0">
  <thead class="table-light sticky-top">
    <tr>
      <th scope="col">NWSLI</th>
      <th scope="col">WMO Source</th>
      <th scope="col">Recent Product</th>
    </tr>
  </thead>
  <tbody>
EOM;
$floor = date("YmdHi", time() - 8 * 86400);
for ($i = 0; $row = pg_fetch_assoc($rs); $i++) {
  $product = htmlspecialchars($row["max"], ENT_QUOTES);
  $nwsli = htmlspecialchars($row["nwsli"], ENT_QUOTES);
  $source = htmlspecialchars(substr($row["max"], 13, 4), ENT_QUOTES);
  $link = sprintf(
    '<a href="/p.php?pid=%s">%s</a>',
    urlencode($row["max"]),
    $product
  );
  if (substr($row["max"], 0, 12) < $floor) {
    $link = $product;
    }
    $table .= sprintf(
    "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
    $nwsli,
    $source,
        $link
    );
}
$table .= "</tbody></table></div>";

$t = new MyView();
$t->title = "Unknown NWSLIs used in SHEF Products";
$t->jsextra = <<<EOM
<script src="tomb.module.js?v=2" type="module"></script>
EOM;

$t->content = <<<EOM
<nav aria-label="breadcrumb">
  <ol class="breadcrumb bg-light px-3 py-2 mb-4 rounded">
    <li class="breadcrumb-item"><a href="/DCP/">HADS/DCP Mainpage</a></li>
    <li class="breadcrumb-item active" aria-current="page">Unknown NWSLIs</li>
  </ol>
</nav>

<div class="alert alert-info mb-4" role="alert">
  <strong>Unknown NWSLIs used in SHEF Products</strong>
</div>
<p>The IEM attempts to process all SHEF encoded products that come via a
NOAAPort data stream. The identifiers used by the stations reported in this
data feed are supposed to adhere to the <a href="https://www.weather.gov/media/mdl/SHEF_CodeManual_5July2012.pdf" target="_blank" rel="noopener">SHEF Manual</a>
specification for site identifiers. The most common set of identifiers are
found within the <a href="https://weather.gov/directives/sym/pd03012004curr.pdf" target="_blank" rel="noopener">National Weather Service Location Identifier (NWSLI)</a> database.
Unfortunately, not all site ids used by issuance centers are contained within
NWSLI. This page lists out unknown NWSLI-like identifiers.</p>

<p>The IEM gets about a quarterly dump of the NWSLI database, which was most
recently received on <strong>{$nwsli_sync_date}</strong>. So newly added sites
to NWSLI and listed here would be false positives. The IEM maintains only
a seven day archive of raw SHEF products, so thus why only some links are
active within the table.</p>

<p>This page is similar to
<a href="https://www.nohrsc.noaa.gov/shef_archive/tomb.html" target="_blank" rel="noopener">NOHRSC's Unknown Stations</a>,
but this page only contains <strong>NWSLI-like identifiers</strong>.</p>

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