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

$var = isset($_GET["var"]) ? xssafe($_GET["var"]) : "gdd50";
$year = get_int404("year", date("Y"));
$smonth = get_int404("smonth", 5);
$emonth = get_int404("emonth", 9);
$sday = get_int404("sday", 1);
$eday = get_int404("eday", 30);

$OL = "10.5.0";

// Make sure our edate is not too late
$today = time() - 86400;
$edate = mktime(0, 0, 0, $emonth, $eday, $year);
if ($edate > $today) {
    $emonth = date("m", $today);
    $eday = date("d", $today);
}

$varDef = array(
    "gdd32" => "Growing Degree Days (base=32)",
    "gdd41" => "Growing Degree Days (base=41)",
    "gdd46" => "Growing Degree Days (base=46)",
    "gdd48" => "Growing Degree Days (base=48)",
    "gdd50" => "Growing Degree Days (base=50)",
    "gdd51" => "Growing Degree Days (base=51)",
    "gdd52" => "Growing Degree Days (base=52)",
    "et" => "Potential Evapotranspiration",
    "precip" => "Precipitation",
    "srad" => "Solar Radiation (langleys)",
    "sgdd50" => "Soil Growing Degree Days (base=50)",
    "sgdd52" => "Soil Growing Degree Days (base=52)"
);
if (!array_key_exists($var, $varDef)) {
    die("variable undefined.");
}
$gddbase = 50;
$datavar = $var;
if (substr($var, 0, 3) == "gdd") {
    $gddbase = intval(str_replace("gdd", "", $var));
    $datavar = "gdd";
}
if (substr($var, 0, 4) == "sgdd") {
    $gddbase = intval(str_replace("sgdd", "", $var));
    $datavar = "sgdd";
}

$wsuri = sprintf(
    "/api/1/isusm/daily.geojson?sdate=%s-%02.0f-%02.0f&edate=%s-%02.0f-%02.0f&" .
        "gddbase=%s&gddceil=%s",
    $year,
    $smonth,
    $sday,
    $year,
    $emonth,
    $eday,
    $gddbase,
    86
);

$t = new MyView();
$t->title = "ISUAG Growing Season Data";
$t->headextra = <<<EOM
<link rel="stylesheet" href="/vendor/openlayers/{$OL}/ol.css" type="text/css">
<link rel="stylesheet" href="/vendor/openlayers/{$OL}/ol-layerswitcher.css" type="text/css">
<link rel="stylesheet" href="gsplot.css" type="text/css">
EOM;

$vselect = make_select("var", $var, $varDef);

$h3 = "";
if ($datavar == "gdd" ||  $var == "precip") {
    $h3 = "Departure from climatology is shown below the accumulated value.";
}
$yselect = yearSelect(1986, $year, "year");
$smselect = monthSelect($smonth, 'smonth');
$sdselect = daySelect($sday, 'sday');
$emselect = monthSelect($emonth, 'emonth');
$edselect = daySelect($eday, 'eday');

$t->content = <<<EOM
<h3>Growing Season Maps</h3>

<p>This application will plot an accumulated variable for a time period of your 
selection.  Data is based on the automated sensors from the ISU AG Climate
network.  A <a href="/GIS/apps/coop/gsplot.phtml">similar application</a> 
exists based on the COOP network data, which
is of higher quality for temperature and precipitation.

<form method="GET" action="gsplot.phtml">
<table class="table"> 
<thead>
<tr>
   <th>Plot Parameter</th>
   <th>Year</th>
   <th>Month</th>
   <th>Day</th>
   <td></td>
  </tr>
</thead>
 <tr>
  <td rowspan=2>{$vselect}</td>
  <td rowspan=2>{$yselect}</td>
  <td>{$smselect}</td>
  <td>{$sdselect}</td>
  <td rowspan=2><input type="submit" value="Update Map"></td>
  </tr>
 <tr>
  <td>{$emselect}</td>
  <td>{$edselect}</td>
  </tr>
</table>
</form>

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

<h3>Interactive Map</h3>
{$h3}
<p>Click on any station marker to view detailed information. Use mouse wheel to zoom, drag to pan.</p>
<div id="map" 
     data-wsuri="{$wsuri}" 
     data-var="{$var}" 
     data-datavar="{$datavar}" 
     data-vardisplay="{$varDef[$var]}" 
     data-period="{$smonth}/{$sday} - {$emonth}/{$eday}"
     data-year="{$year}"
     role="application"
     aria-label="Interactive weather station map showing {$varDef[$var]} data"></div>

<h3>Dynamic Historical Data:</h3>
<a href="/plotting/auto/?q=199">Daily Data Plots</a>
<br /><b>Growing Season Plots</b>


<p><b>*Note:</b> Precipitation recorded from this network is not reliable.
 You should use precipitation obs from the <a href="/COOP/">COOP</a> network.

</div></div>

<div class="row">
<div class="col-sm-12">
<h3>Station Data Table</h3>
<p>This table contains the raw data for all weather stations shown on the map above.</p>
<p>
<button type="button" role="button" id="save" class="btn btn-primary" 
        aria-describedby="download-help">Download CSV Text</button>
<span id="download-help" class="sr-only">Downloads the station data as a CSV file for use in spreadsheet applications</span>
<br />
<pre id="datatable" aria-live="polite" aria-label="Weather station data table">Loading data...</pre>

<p>You should be able to 'copy & paste' this raw data listing into a simple
text editor and save the data file on your machine.  Most GIS systems can 
then import this dataset as a layer.  You may have to omit the commented lines
(#) if your GIS has trouble importing the data.
</div>
</div>
EOM;
$t->jsextra = <<<EOM
<script src="/vendor/openlayers/{$OL}/ol.js"></script>
<script src="/vendor/openlayers/{$OL}/ol-layerswitcher.js"></script>
<script src="gsplot.js?v=7"></script>
EOM;
$t->render('single.phtml');
