<?php
require_once "../../../../config/settings.inc.php";
define("IEM_APPID", 68);
require_once "../../../../include/myview.php";
require_once "../../../../include/rview_lib.php";
require_once "../../../../include/network.php";
require_once "../../../../include/forms.php";
require_once "../../../../include/vendor/mapscript.php";

$t = new MyView();

$county = isset($_GET['county']) ? xssafe($_GET["county"]) : "";
$cntyext = isset($_GET['cntyext']) ? xssafe($_GET["cntyext"]) : "";
$ul_x = isset($_GET["ul_x"]) ? xssafe($_GET["ul_x"]) : "";
$ul_y = isset($_GET["ul_y"]) ? xssafe($_GET["ul_y"]) : "";
$lr_x = isset($_GET["lr_x"]) ? xssafe($_GET["lr_x"]) : "";
$lr_y = isset($_GET["lr_y"]) ? xssafe($_GET["lr_y"]) : "";
$map_x = isset($_GET["map_x"]) ? xssafe($_GET["map_x"]) : "";
$map_y = isset($_GET["map_y"]) ? xssafe($_GET["map_y"]) : "";
$map_height = isset($_GET["map_height"]) ? xssafe($_GET["map_height"]) : "";
$map_width = isset($_GET["map_width"]) ? xssafe($_GET["map_width"]) : "";

if ($ul_x != "") {
    $dx = ($ul_x - $lr_x) / $map_width;
    $dy = ($ul_y - $lr_y) / $map_height;
    $clickx = ($map_x * (0 - $dx)) + $ul_x;
    $clicky = ($map_y * (0 - $dy)) + $ul_y;
    $extents = array($clickx - 0.3, $clicky - 0.3, $clickx + 0.3, $clicky + 0.3);
} else if ($cntyext != "") {
    $extents = explode(" ", $cntyext);
} else {
    $extents = array(-96.639706, 39.1497, -90.140061, 44.726);
}

$t->title = "RADAR Comparison";
$nt = new NetworkTable("NEXRAD");

$latlon = new projectionObj("init=epsg:4326");
$p26915 = new projectionObj("init=epsg:26915");

$cntyMdpoint = new pointObj();
$cntyMdpoint->setXY(($extents[0] + $extents[2]) / 2,
    ($extents[1] + $extents[3]) / 2
);
$cntyMdpoint->project($latlon, $p26915);

$n = array("DMX", "DVN", "ARX", "MPX", "FSD", "OAX", "EAX");
$distances = array();
foreach ($n as $key => $site) {
    $r = new pointObj();
    $r->setXY($nt->table[$site]['lon'], $nt->table[$site]['lat']);
    $r->project($latlon, $p26915);
    $distances[$site] = round($r->distanceToPoint($cntyMdpoint) / 1000, 0);
}

$site = "DMX";
$dmx_url = drawCountyNEXRAD($site, $extents);
$site = "DVN";
$dvn_url = drawCountyNEXRAD($site, $extents);
$site = "ARX";
$arx_url = drawCountyNEXRAD($site, $extents);
$site = "FSD";
$fsd_url = drawCountyNEXRAD($site, $extents);
$site = "OAX";
$oax_url = drawCountyNEXRAD($site, $extents);
$site = "MPX";
$mpx_url = drawCountyNEXRAD($site, $extents);
$site = "EAX";
$eax_url = drawCountyNEXRAD($site, $extents);

$ftime = filemtime('/mesonet/ldmdata/gis/images/4326/ridge/DMX/N0B_0.png');
$radTS = date("m/d h:i a", $ftime);
$s = drawStateNEXRAD($extents);

$t->content = <<<EOM
<ol class="breadcrumb">
<li><a href="/current/radar.phtml">Current RADAR Products</a></li>
<li class="active">RADAR comparison</li>
</ol>

<h3>RADAR Base Reflectivity Comparison:</h3><p>
<table>
<tr>
 <td><b>DMX - Des Moines:</b>
   <br><i>Distance:</i> {$distances['DMX']} km
   <br><img src="{$dmx_url}"></td>
 <td></td>
 <td rowspan=4 valign="TOP">
  <b>Directions:</b> Click on the State NEXRAD view to change your comparison 
  area (shown in white)<br>
  {$s}
 
    <a href="compare.phtml">Reset app</a> <i>(view state)</i>
      <br>RADAR composite valid: $radTS 
<p><b>Notes:</b>
<li>This app is purely for educational purposes.</li>
<li>Distances are measured from the RADAR site to the midpoint of the area 
 you selected.
<li>Only radars in precip mode are shown.</li>

</td>
</tr>
<tr>
 <td><b>ARX - LaCrosse:</b>
   <br><i>Distance:</i> {$distances['ARX']} km
   <br><img src="{$arx_url}"></td>
 <td><b>MPX - Minneapolis:</b>
   <br><i>Distance:</i> {$distances['MPX']} km
   <br><img src="{$mpx_url}"></td>
</tr>
<tr>
 <td><b>FSD - Sioux Falls:</b>
   <br><i>Distance:</i> {$distances['FSD']} km
   <br><img src="{$fsd_url}"></td>
 <td><b>OAX - Omaha:</b>
   <br><i>Distance:</i> {$distances['OAX']} km
   <br><img src="{$oax_url}"></td>
</tr>
<tr>
 <td><b>DVN - Davenport:</b>
   <br><i>Distance:</i> {$distances['DVN']} km
   <br><img src="{$dvn_url}"></td>
 <td><b>EAX - Pleasant Hill:</b>
   <br><i>Distance:</i> {$distances['EAX']} km
   <br><img src="{$eax_url}"></td>
</tr>

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