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

$t = new MyView();
$t->title = "GIS-Ready Rainfall Data";

$mselect = monthSelect(date("m", time() - 86400));
$dselect = daySelect(date("d", time() - 86400));
$yselect = yearSelect(1997, date("Y", time() - 86400), "year");
$ys = yearSelect(1997, date("Y"), "syear");
$ye = yearSelect(1997, date("Y"), "eyear");

$y2select = yearSelect(2014, date("Y"), "year");
$m2select = monthSelect(date("m"));
$d2select = daySelect(date("d"));
$h2select = gmtHourSelect(0, "hour");
$ar = array(
    "1" => "1 Hour",
    "24" => "24 Hour",
    "48" => "48 Hour",
    "72" => "72 Hour"
);
$pselect = make_select("period", 1, $ar);

$archivedir = '/archive/data/' . date("Y") . '/' . date('m') . '/' . date('d') . '/GIS/mrms';

$t->content = <<<EOM
<h3>IEM Rainfall</h3>

<p>IEM rainfall is a high resolution depiction of rainfall estimates over
the state of Iowa.  These estimates are provided freely via the Internet with
no warranty or claim of accuracy.  Currently, there are a number of ways you
can view/download this dataset.</p>

<p><ul>
 <li><a href="#about">About Precipitation Estimates</a></li>
 <li><a href="#mrms">Multi-RADAR Multi-Sensor Products</a></li>
</ul>

<h3>Request Data by Point</h3>

<form method="GET" action="bypoint.phtml" name="bypoint">
<b>Instructions:</b> With this form you can request daily estimates of rainfall
for a particular year and for a particular point.  This point can be specified
in a number of ways. <b>Caveat:</b>  Geocoding is not perfect.  Your address
may not be found in the database.  If you first attempt fails, try using a
popular building address for a location near you.

<p><b>Start Year:</b>
{$ys}
 &nbsp; <b>End Year (inclusive):</b>
{$ye}
<br /><b>View Options:</b>
<select name="view">
    <option value="online">Online
    <option value="download">Download to disk
</select>

<table class="table table-bordered">
<thead>
<tr>
    <th colspan="6">Choose a method to select data point:</th>
</tr></thead>

<tbody>
<tr>
    <td colspan="2">
    <input type="radio" name="method" value="latlon" id="latlon">
    <label for="latlon">Specify Latitude &amp; Longitude</label>
    </td>
    <td colspan="2">
    <input type="radio" name="method" value="geocode" checked="checked" id="geocode">
    <label for="geocode">Geocode An Address</label>
    </td>
    <td colspan="2">
    <input type="radio" name="method" value="id" id="idid">
    <label for="idid">Specify IEM Site ID</label>
    </td>
</tr>

<tr>
<td><b>Latitude:</b></td> <td><input type="text" name="lat" size="6"></td>
<td><b>Street Address:</b></td> <td> <input type="text" name="street"></td>
<td rowspan="2"><b>Enter ID:</b></td>
<td rowspan="2"><input type="text" name="nwsli" size="6"></td>
</tr>
<tr>
<td><b>Longitude:</b></td> <td> <input type="text" name="lon" size="6"></td>
<td><b>City in Iowa:</b></td> <td> <input type="text" name="city"></td>
</tr>
</tbody>
</table>
<input type="submit" value="Show Data">
</form>


<h3>GIS Shapefiles</h3>

<form method="GET" action="dshape.php" name="shapefile">
<div>
<b>Instructions:</b>  Please select the date you would like to download a zip
file containing a shapefile of rainfall estimates. The duration is the time
period during which the estimates are valid for.  For the current month or
year, the estimates will be current for that period till yesterday.  Values
are in inches.
<table class="table table-bordered">
<thead>
<tr>
    <th>Month:</th><th>Day:</th><th>Year:</th><th>Coverage Type:</th>
    <th>Duration</th><th>Projection:</th>
</tr></thead>
<tbody>
<tr>
    <td>{$mselect}</td>
    <td>{$dselect}</td>
    <td>{$yselect}</td>
    <td><select name="geometry">
    <option value="point">Point
    <option value="polygon">Polygon
    </select></td>
    <td>
<select name="duration">
  <option value="day">One Day
  <option value="month">Calendar Month
  <option value="year">Calendar Year
</select>
    </td>
    <td><select name="epsg">
    <option value="4326">Geographic (EPSG: 4326)
    <option value="26915">NAD83 UTM Zone15N (EPSG: 26915)
    </select></td>
</tr>
</tbody>
</table>
<input type="submit" value="Generate .zip file">
</form>
<br />The point coverage is simply the centroid of the polygon coverage.
</div>

<h3>OGC Web Services</h3>

<div>
If your GIS system supports accessing Web Map Service (WMS),
you can directly pull in some of these rainfall estimates
from the IEM server.  Our WMS URI is:
<pre>
{$EXTERNAL_BASEURL}/cgi-bin/wms/iowa/rainfall.cgi?
</pre>

<p>Pulling data into GIS applications via these services has not been widely
tested.  If you run into any problems, please let us know!

</div>

<h3><a name="about" href="#about"><i class="bi bi-bookmark" aria-hidden="true"></i></a> About Precipitation Estimates</h3>


These rainfall estimates are directly based on
the <a href="https://nomads.ncep.noaa.gov/">Stage 4</a>
precipitation analysis produced by the
<a href="https://www.ncep.noaa.gov">National Center for Environmental Prediction</a>.
The IEM processes this data source in the following ways:
<ul>
    <li>Extract data valid for Iowa from the national analyses.</li>
    <li>Convert the data from Grib format to a relational database.</li>
    <li>Produce summary totals for the local day instead of GMT day.</li>
    <li>Provide instant access to this data from this page and others.</li>
</ul>

<p>Rainfall estimates are produced each day at 3:30 AM for the previous day.
These estimates are later revised as more information is made available to
better refine the estimates.  If you downloaded data for yesterday at the time,
you may wish to redownload that dataset again later, since the estimates may
be refined then.  Sounds confusing.  Please
<a href="/info/contacts.php">contact us</a> if you have questions.</p>


<h3><a name="mrms" href="#mrms"><i class="bi bi-bookmark" aria-hidden="true"></i></a> MRMS Estimates</h3>

<p>NOAA has begun working on an advanced replacement of the system that produces
rainfall estimates.  This project is called
<a href="https://www.nssl.noaa.gov/projects/mrms/">Multi-RADAR Multi-Sensor System</a>.  The IEM
processes some of the data found in this feed and produces products from it.

<h4>GIS RASTER PNG Images of Accumulation Estimates</h4>

<p>The technique used provides 8-bit PNG images whereby a function can be used
to convert the 8-bit value into a rainfall.  This equation is different for
the various products produced by the IEM in order to accomodate the data ranges
necessary to capture.  These images can be found in a
<a href="/data/gis/images/4326/mrms/">current directory</a>
and <a href="{$archivedir}">date based archive</a>. The units for the data is
millimeters.</p>

<p><strong>1,24,48,72 Hour Precipitation</strong>
<br ><table class="table">
<thead>
<tr><th>8-bit value/index</th><th>Precip [mm]</th><th>Approx Precip [in]</th></tr>
</thead>
<tbody>
<tr><td>0</td><td>0</td><td>0</td></tr>
<tr><td>1-100 (0.25mm)</td><td>0.25 to 25</td><td>~0.01 to 1 inch</td></tr>
<tr><td>101-180 (1.25mm)</td><td>26.25 to 125</td><td>~1.05 to 5 inch</td></tr>
<tr><td>181-254 (5mm)</td><td>130 to 500</td><td>~5 to 20 inch</td></tr>
<tr><td>255</td><td>Missing</td><td>Missing</td></tr>
</tbody>
</table>

<h4>GIS RASTER in ERDAS Imagine (.IMG) Format</h4>

<p>This tool provides you an ERDAS Imagine file for the time and
accumulation period of your choice.  The IMG file is an unsigned 16 bit
image, with a scale unit of 0.1mm.  The 65535 value is set to missing and
your GIS system hopefully will automatically understand that. The time you
choose is the end time of the given accumulation period. <strong>This time
is in UTC / GMT time!</strong> You could script
against this service by using a URI like so:<br /><br />
<code>
{$EXTERNAL_BASEURL}/rainfall/mrms2img.py?year=2014&amp;month=9&amp;day=4&amp;hour=0&amp;period=24
</code>
</p>

<p>Please be patient with this download!  It may take 10-20 seconds to generate,
the script converts the 8bit PNG mentioned above to uint16 IMG file on the fly!</p>

<form method="GET" action="mrms2img.py" name="mrmsdl">
<br ><table class="table">
<thead>
<tr><th>Accumulation Period</th><th>UTC Date</th><th> UTC Hour</th></tr>
</thead>
<tbody>
<tr><td>{$pselect}</td>
    <td>{$y2select} {$m2select} {$d2select}</td>
    <td> {$h2select} </td>
</tr>
</tbody>
</table>

<input type="submit" value="Request ERDAS Imagine File" />
</form>

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