<?php
require_once "../../../config/settings.inc.php";
define("IEM_APPID", 17);
require_once "../../../include/forms.php";
require_once "../../../include/myview.php";
require_once "../../../include/network.php";
$nt = new NetworkTable("WFO");

$t = new MyView();
$t->iemss = true;
$t->iem_resource = "LSR";
$t->title = "Archived Local Storm Reports";

$ys = yearSelect(2003, date("Y"), "year1");
$ms = monthSelect(0, "month1");
$ds = daySelect(0, "day1");
$hs = gmtHourSelect(0, "hour1");
$ns = minuteSelect(0, "minute1");
$ys2 = yearSelect(2003, date("Y"), "year2");
$ms2 = monthSelect(date("m"), "month2");
$ds2 = daySelect(date("d"), "day2");
$hs2 = gmtHourSelect(23, "hour2");
$ns2 = minuteSelect(59, "minute2");
$stateselect = stateSelect("_ALL", '', "state", 5, true, true);
$stateselect = str_replace('<select', '<select class="form-select" id="state"', $stateselect);
$ss = "";
foreach ($nt->table as $key => $value) {
    $ss .= "<option value='$key'>[$key] " . $value["name"] . "</option>\n";
}
$types = Array(
    "ALL" => "All Types / No Limit",
    "AVALANCHE" => "Avalanche",
    "BLIZZARD" => "Blizzard",
    "BLOWING DUST" => "Blowing Dust",
    "COASTAL FLOOD" => "Coastal Flood",
    "DEBRIS FLOW" => "Debris Flow",
    "DENSE FOG" => "Dense Fog",
    "DOWNBURST" => "Downburst",
    "DUST STORM" => "Dust Storm",
    "EXCESSIVE HEAT" => "Excessive Heat",
    "EXTREME COLD" => "Extreme Cold",
    "EXTREME HEAT" => "Extreme Heat",
    "EXTR WIND CHILL" => "Extreme Wind Chill",
    "FLASH FLOOD" => "Flash Flood",
    "FLOOD" => "Flood",
    "FOG" => "Fog",
    "FREEZE" => "Freeze",
    "FREEZING RAIN" => "Freezing Rain",
    "FUNNEL CLOUD" => "Funnel Cloud",
    "HAIL" => "Hail",
    "HEAVY RAIN" => "Heavy Rain",
    "HEAVY SLEET" => "Heavy Sleet",
    "HEAVY SNOW" => "Heavy Snow",
    "HIGH ASTR TIDES" => "High Astronomical Tides",
    "HIGH SURF" => "High Surf",
    "HIGH SUST WINDS" => "High Sustained Winds",
    "ICE JAM" => "Ice Jam",
    "ICE STORM" => "Ice Storm",
    "LANDSLIDE" => "Landslide",
    "LANDSPOUT" => "Landspout",
    "LIGHTNING" => "Lightning",
    "LOW ASTR TIDES" => "Low Astronomical Tides",
    "MARINE TSTM WIND" => "Marine Thunderstorm Wind",
    "MISC MRN/SRF HZD" => "Misc Marine/Surf Hazard",
    "NON-TSTM WND DMG" => "Non-Thunderstorm Wind Damage",
    "NON-TSTM WND GST" => "Non-Thunderstorm Wind Gust",
    "RAIN" => "Rain",
    "RIP CURRENTS" => "Rip Currents",
    "SEICHE" => "Seiche",
    "SLEET" => "Sleet",
    "SNOW" => "Snow",
    "SNOW/ICE DMG" => "Snow/Ice Damage",
    "SNOW SQUALL" => "Snow Squall",
    "STORM SURGE" => "Storm Surge",
    "TORNADO" => "Tornado",
    "TROPICAL CYCLONE" => "Tropical Cyclone",
    "TROPICAL STORM" => "Tropical Storm",
    "TSTM WND DMG" => "Thunderstorm Wind Damage",
    "TSTM WND GST" => "Thunderstorm Wind Gust",
    "WATER SPOUT" => "Waterspout",
    "WATERSPOUT" => "Waterspout",
    "WILDFIRE" => "Wildfire",
    "WIND CHILL" => "Wind Chill",
);
$typeselect = make_select("type", "ALL", $types, "", "", TRUE, TRUE, FALSE);
$typeselect = str_replace('<select', '<select class="form-select" id="type"', $typeselect);

$t->content = <<<EOM
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="/GIS/">GIS Mainpage</a></li>
<li class="breadcrumb-item active" aria-current="page">Archived Local Storm Reports Shapefiles</li>
</ol>
</nav>

<p>This page allows you to generate a GIS shapefile of NWS issued Local Storm
Reports for a time period of your choice.  Please be sure to read the
archive status and dbf schema below. The IEM's archive is certainly not complete or official.

<p>If you are looking for a real-time source of lsrs, you can find with the following
files generated every 5 minutes:  <a href="/data/gis/shape/4326/us/lsr_24hour.zip">ESRI Shapefile</a>,
<a href="/data/gis/shape/4326/us/lsr_24hour.csv">Comma Delimited</a>,
<a href="/data/gis/shape/4326/us/lsr_24hour.geojson">GeoJSON</a>.</p>


<p><strong>Looking to automate the downloading of LSRs?</strong>
<br />You can use the following URI form to get the most recent number of seconds
worth of LSRs.  Note that these times are based on the valid time of the LSR and
not the issuance of the text product itself that contained the LSR.
<br /><code>{$EXTERNAL_BASEURL}/cgi-bin/request/gis/lsr.py?wfo=ALL&amp;recent=86400&amp;fmt=csv</code>
<br /><code>{$EXTERNAL_BASEURL}/cgi-bin/request/gis/lsr.py?wfo=DMX,DVN&amp;recent=86400&amp;fmt=csv</code>
<br /><code>{$EXTERNAL_BASEURL}/cgi-bin/request/gis/lsr.py?state=IA&amp;recent=86400&amp;fmt=csv</code>
<br />Omit the <code>fmt=csv</code> portion to return the zipped shapefile.</p>

<p>Example to fetch a month's worth of data
<br /><code>{$EXTERNAL_BASEURL}/cgi-bin/request/gis/lsr.py?wfo=JAN&amp;sts=2020-08-01T00:00Z&amp;ets=2020-09-01T00:00Z&amp;fmt=csv</code>
</p>

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

<form method="GET" action="/cgi-bin/request/gis/lsr.py">
<div class="form2url"></div>
<h4>Filter Options</h4>
<div class="row">
  <div class="col-md-3">
    <div class="mb-3">
      <label for="wfo" class="form-label">Limit Weather Forecast Office (WFO)</label>
      <select name="wfo" id="wfo" class="form-select" multiple size="5">
        <option value="ALL" selected>All Available</option>
        {$ss}
      </select>
    </div>
  </div>
  <div class="col-md-3">
    <div class="mb-3">
      <label for="state" class="form-label">Limit by State</label>
      {$stateselect}
    </div>
  </div>
  <div class="col-md-3">
    <div class="mb-3">
      <label for="type" class="form-label">Limit by Report Type</label>
      {$typeselect}
    </div>
  </div>
  <div class="col-md-3">
    <div class="mb-3">
      <label for="magge" class="form-label">Minimum Magnitude</label>
      <input type="number" step="any" class="form-control" id="magge"
       name="magge" placeholder="e.g. 0.5">
      <div class="form-text">Only include reports with magnitude &ge; this
       value. Leave blank for no filter. This is a blunt tool and generally
       does not work for things without a magnitude, like tornado LSRs.</div>
    </div>
  </div>
</div><!-- ./row -->

<h4>Select time interval</h4>
<i>(Times are in UTC.  Local Storm Reports with an event time <b>valid</b>
between these times selected below will be included in the output GIS file...)</i>
<table class="table table-sm table-bordered table-striped">
  <thead>
    <tr>
      <th scope="col"></th>
      <th scope="col">Year</th>
      <th scope="col">Month</th>
      <th scope="col">Day</th>
      <th scope="col">Hour</th>
      <th scope="col">Minute</th>
    </tr>
  </thead>
  <tbody>
  <tr>
    <th>Start:</th>
    <td>{$ys}</td>
    <td>{$ms}</td>
    <td>{$ds}</td>
    <td>{$hs}</td>
    <td>{$ns}</td>
  </tr>

  <tr>
    <th>End:</th>
    <td>{$ys2}</td>
    <td>{$ms2}</td>
    <td>{$ds2}</td>
    <td>{$hs2}</td>
    <td>{$ns2}</td>
   </tr>
    </tbody>
</table>

<div class="mb-3">
<label for="fmt" class="form-label"><strong>Download Format:</strong></label>
<select name="fmt" id="fmt" class="form-select" style="width: auto; display: inline-block;">
  <option value="shp">ESRI Shapefile</option>
  <option value="csv">Comma Delimited (.csv)</option>
  <option value="kml">KML (.kml)</option>
  <option value="excel">Microsoft Excel (.xlsx)</option>
</select>
</div>

<div class="mb-3">
<button type="submit" class="btn btn-primary">Generate File</button>
</div>
</form>

<h4>Shapefile DBF schema:</h4>
<pre>
VALID    Timestamp of LSR in GMT/UTC time
MAG      Magnitude value of the LSR
WFO      Weather Forecast Office originating the LSR
TYPECODE 1 character identifier of the report type, IEM specific
TYPETEXT Textual value of LSR type used in report
CITY     Location used for the LSR
COUNTY   County/Parish of the LSR city
STATE    Two character state abbreviation
SOURCE   Who reported the LSR
REMARK   Text summary with the LSR
LAT      Latitude
LON      Longitude
UGC      IEM computed NWS UGC code (6 character), sometimes null
UGCNAME  IEM computed NWS UGC name (128 character), sometimes null
QUALIFY  1 char describing if the MAG is Estimated, Measured, or Unknown
</pre>

<h4>Archive notes:</h4>
<ul>
 <li>Before 1 Dec 2006, there was no differentiation between Snow and Sleet reports.</li>
  <li><strong>18 July 2022:</strong> Added output fields that attempt to cross
  reference NWS UGC County/Zones database to the referenced county/state found
  in the LSR text.  This all is sub-optimal, but works in 99% of cases.</li>
</ul>
EOM;
$t->render('single.phtml');
