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

$day = get_int404("day", date("d"));
$month = get_int404("month", date("m"));
$hour = get_int404("hour", date("H"));
$minute = get_int404("minute", 0);
$sz = isset($_GET["sz"]) ? floatval($_GET["sz"]) : 2.5;

/* Do areal extents */
$map_x = get_int404("map_x", 0);
$map_y = get_int404("map_y", 0);
$ul_x = -126.0;
$ul_y = 50.0;
$lr_y = 24.0;
$lr_x = -66.0;
if ($map_x != 0 && $map_y != 0)
{
  $dx = ($ul_x - $lr_x) / 300.0;
  $dy = ($ul_y - $lr_y) / 130.0;
  $clickx = ($map_x * (0 - $dx) ) + $ul_x;
  $clicky = ($map_y * (0 - $dy) ) + $ul_y;
  $sz_x = floatval($sz);
  $sz_y = floatval($sz) * 7.0 / 8.0 ;

  $extents = Array($clickx - $sz_x, $clicky - $sz_y, $clickx + $sz_x, $clicky + $sz_y);

} else if (isset($_GET["osz"]) && $sz != $_GET["osz"]){
  $oextents = explode(",", $_GET["oextents"]);
  $clickx = $oextents[0] + (($oextents[2] - $oextents[0]) / 2.0);
  $clicky = $oextents[1] + (($oextents[3] - $oextents[1]) / 2.0);
  $sz_x = floatval($sz) ;
  $sz_y = floatval($sz) * 7.0 / 8.0 ;
  $extents = Array($clickx - $sz_x, $clicky - $sz_y, $clickx + $sz_x, $clicky + $sz_y);

} else if (isset($_GET["oextents"])){
  $extents = explode(",", $_GET["oextents"]);
} else {
  $extents = Array(-98,39,-90,46);
}


$ourl = sprintf("rby-overview.php?BBOX=%s,%s,%s,%s",  $extents[0], $extents[1], $extents[2], $extents[3]);
$image_url = sprintf("rad-by-year.php?month=%s&day=%s&hour=%s&minute=%s&BBOX=%s,%s,%s,%s", $month, $day, $hour, $minute, $extents[0], $extents[1], $extents[2], $extents[3]);

$t = new MyView();
$t->title = "IEM NEXRAD by Year";
$er = implode(",", $extents);

$ar = Array("0.5" => "100 km",
    "2.5" => "500 km",
    "5" => "1000 km",
    "20" => "4000 km");
$szselect = make_select("sz", $sz, $ar);

$monselect = monthSelect($month, "month");
$dayselect = daySelect($day, "day");
$hrselect =  hourSelect($hour, "hour");
$minselect = minuteSelect($minute, "minute", 5);

$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">IEM NEXRAD Mosaic by Year</li>
  </ol>
</nav>

<h3>IEM NEXRAD Mosiac of Base Reflectivity by Year</h3>

<p>This application generates a summary image of IEM generated NEXRAD
mosaic of base reflectivity for a given time over the archive's duration. 
You can click on the map to move the desired location.</p>

<form method="GET" action="rad-by-year-fe.phtml" name="Selector">
<input type="hidden" name="oextents" value="{$er}">
<input type="hidden" name="osz" value="{$sz}">
<table cellpadding="2" border="0" cellspacing="0">
<tr><th>Select Area</th><th colspan="2">Select Date</th></tr>
<tr>
<td rowspan="5">
  <input type="image" src="{$ourl}" name="map">
<br /><strong>Select Rough Box Size</strong>
{$szselect}
</td>
<td>Month:</td><td>{$monselect}</td></tr>
<tr><td>Day:</td><td>{$dayselect}</td></tr>
<tr><td>Hour:</td><td>{$hrselect}</td></tr>
<tr><td>Minute:</td><td>{$minselect}</td></tr>
<tr><td colspan='2'><input type='submit' value='Generate Image' class='btn btn-primary' aria-label='Generate image'></td></tr>
</table>
</form>
<p><i>The image below may take a few moments to appear...</i><br />
<img src='$image_url' class='img-fluid'>
EOM;
$t->render('single.phtml');
