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

$network = isset($_GET['network']) ? xssafe($_GET["network"]) : 'IA_DCP';

$t = new MyView();
$t->iemss = True;
$t->iem_resource = "HADS";
define("IEM_APPID", 44);
$mesosite = iemdb('mesosite');

$t->title = "DCP/HADS Data Download";

$nselect = "<select name=\"network\">";
$rs = pg_query($mesosite, "SELECT id, name from networks " .
    "where id ~* '_DCP' or id ~* '_COOP' ORDER by name ASC");
for ($i = 0; $row = pg_fetch_assoc($rs); $i++) {
    $sel = '';
    if ($network == $row["id"]) {
        $sel = " selected='SELECTED'";
    }
    $nselect .= sprintf(
        "<option value='%s'%s>%s</option>\n",
        $row["id"],
        $sel,
        $row["name"]
    );
}
$nselect .= "</select>";

$ys1 = yearSelect(2002, date("Y"), "year1");
$ms1 = monthSelect("1", "month1");
$ds1 = daySelect("1", "day1");
$hs1 = gmtHourSelect("0", "hour1");
$ys2 = yearSelect(2002, date("Y"), "year2");
$ms2 = monthSelect(date("m"), "month2");
$ds2 = daySelect(date("d"), "day2");
$hs2 = gmtHourSelect("0", "hour2");

$t->content = <<<EOM
<ol class="breadcrumb">
 <li><a href="/DCP/">DCP/HADS Mainpage</a></li>
 <li class="active">Data Download</li>
</ol>
<h3>DCP/HADS Data Download</h3>

<p>This form allows you to download whatever data the IEM was able to collect from
the GOES/DCP/HADS/AHPS (insert other acronyms here) data collection platforms.  This
dataset is huge and the download may take a number of seconds to process.</p>

<p>A <a href="/pickup/hads/">static dump</a> of the database was made on 22 December 2022
and may be useful for those of you wanting it all!</p>

<p><a class="btn btn-secondary" href="/cgi-bin/request/hads.py?help"><i class="bi bi-file-text" aria-hidden="true"></i> Backend Documentation</a> exists for those that
wish to script against this service.</p>

<form method="GET" name="chstate">
<p>Select from State/Country: {$nselect}
<input type="submit" value="Change Group">
</form>

<form target="_blank" method="GET" action="/cgi-bin/request/hads.py" name="iemss">
<div class="form2url"></div>
<input type='hidden' name='network' value="{$network}" />

<div class="row">
<div class="col-sm-7">

<h3>1. Select Station(s):</h3>

<div id="iemss" data-network="{$network}"></div>

</div>
<div class="col-sm-5">

<h3>2. Select Start/End Time:</h3>

<p>Times are specified in UTC. Archive starts in 2002 for Iowa and on
9 Dec 2010 for all others. You can request up to one year of data when
requesting multiple stations. When requesting all stations, you can
only request 24 hours worth of data.</p>

<table class="table table-sm">
  <tr>
    <td></td>
    <th>Year</th><th>Month</th><th>Day</th>
    <th>Hour</th>
  </tr>

  <tr>
    <th>Start:</th>
    <td>{$ys1}</td>
    <td>{$ms1}</td>
    <td>{$ds1}</td>
    <td>{$hs1}
    <input type="hidden" name="minute1" value="0"></td>
    </td>
  </tr>

  <tr>
    <th>End:</th>
    <td>{$ys2}</td>
    <td>{$ms2}</td>
    <td>{$ds2}</td>
    <td>{$hs2}
    <input type="hidden" name="minute2" value="0">
    </td>
  </tr>
</table>

<h3>3. How to download/view?</h3>

<select name="what">
  <option value="txt">Download as Delimited Text File</option>
  <option value="excel">Download as Excel</option>
  <option value="html">View as HTML webpage</option>
</select>

<h3>3a. Data Delimitation:</h3>

<p>If you selected 'Delimited Text File' above, how do you want the values
separated in the downloaded file?</p>

<select name="delim">
    <option value="comma">Comma
    <option value="space">Space
    <option value="tab">Tab
</select>

<div class="card card-body">
<h3>5. <i>Optional:</i> Search for Threshold Exceedance</h3>

<p>For stations that report a stage value ("HG" SHEF variable group), you
 can optionally search for when the value exceeds this threshold.  The 
 print out will include:
 <ul>
    <li>Timestamp when threshold was exceeded.</li>
    <li>First timestamp when the value was at its peak.</li>
    <li>Timestamp when the value crossed below the threshold.</li>
 </ul>
 <br /><strong>You can only do this search for one station at a time!</strong>
 <br />Thresold: <input type="text" size="6" name="threshold" />
 <br />Type / Source Code:
<select name="thresholdvar">
    <option value="RG">GOES (RG)</option>
    <option value="RP">Phone ASCII (DARDC/LARC) (RP)</option>
    <option value="RR">Radio #1 (RR)</option>
    <option value="RS">Radio #2 (RS)</option>
    <option value="RV">Visual/manual #1 (RV)</option>
    <option value="RZ">Nonspecific (RZ)</option>
    <option value="R2">Secondard Sensor (R2)</option>
    <option value="R3">Tertiary Sensor (R3)</option>
</select>

</div>

<h3>6. Submit Form:</h3>

<input type="submit" value="Process Data Request">
<input type="reset">

</div>
</div>

<h3>Frequently Asked Questions:</h3>

<h4><span class="badge">1</span> How do I make sense of the headings in the
download file?</h4>

<p>Great question!  This dataset is based on the IEM processing of SHEF-encoded
products relayed by the NWS.  SHEF uses a series of identifiers to label various
reported values.  These labels are explicitly stored within the database without
any translation into more common terms.  Sadly, translating these labels into
more usable terms is an operation left to the end-user.</p>

<p>The magic decoder ring is the <a href="https://www.weather.gov/media/mdl/SHEF_CodeManual_5July2012.pdf">SHEF Manual</a>,
 which is 150 pages long!  Fear not, in general, you can scroll down to page 65
and review Tables 1 and on.  The variable labels used in the downloaded file
should be broken apart and used as lookups in the table.  For example, 
<code>HGIRRZ</code> should be broken up as follows.</p>

<table>
<tr><th>HG</th><th>Table 1</th><th>HG Height, river stage (FT, M)</th></tr>
<tr><th>I</th><th>Table 3</th><th>I Instantaneous (0000)</th></tr>
<tr><th>RR</th><th>Table 4</th><th>RR Radio #1</th></tr>
<tr><th>Z</th><th>Table 5</th><th>Z Null character (filler) or UTC Time</th></tr>
</table>

<p>Some codes may be longer than 6 characters, so good luck reading the manual!  In general,
you can see why it is hard to provide this data with any simplier headings.  As always,
feel free to contact us with questions. In general, the first three characters are the
most important, which are the variable type and duration of observation.</p>

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