<!-- Daily Stats -->
<table>
  <tr>
    <td class="head" colspan="6" >Daily Statistics</td>
</tr>
  <tbody>
    <tr>
      <td class="firstcol"><b>Day</b>
<?php
	$day_limit = 5;
	if (isset($_GET["day"])) {
		$day_limit = $_GET["day"];
	}

	if ($day_limit == 10) {
		echo "<a href=\"" . $this_document . "?day=5$options\"> [-] </a>";
	} else {
		echo "<a href=\"" . $this_document . "?day=10$options\"> [+] </a>";
	}
?>
	  </td>
      <td class="nextcol"><b>T<sup>min</sup> <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>T<sup>max</sup> <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>Mean <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>&sigma;</b></td>
      <td ><b>n</b></td>
    </tr>
<?php
$query = "SELECT id, DATE_FORMAT(time,'%Y') AS year, DATE_FORMAT(time,'%m') AS month, DATE_FORMAT(time, '%D') AS day, DATE_FORMAT(time, '%W %M %D') AS dbtime, ROUND(max($t_corr),1) AS max, ROUND(min($t_corr),1) AS min, ROUND(SUM($t_corr)/COUNT(*),1) AS mean, COUNT(*) as count, ROUND(STDDEV($t_corr) * 100, 0) AS err FROM $table GROUP BY YEAR(time), MONTH(time), DAYOFMONTH(time) ORDER BY -time LIMIT $day_limit";

$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);
mysql_select_db($DBNAME, $DB);
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
        echo "\t<tr class=\"row\"><td>";
	$plot_file = sprintf("$plotdir/temp_%d%02d%02d.png", $row["year"], $row["month"], $row["day"]);	
	echo "<a href=\"javascript:void(window.open('$plot_file','graph_window','resizable,scrollbars=no,width=768,height=288,left=100,top=100'))\">";
        echo $row["dbtime"] . "</a></td>";
        echo " <td> " . $row["min"] . "</td>";
        echo " <td> " . $row["max"] . "</td>";
        echo " <td> " . $row["mean"] . "</td>";
        echo " <td> " . $row["err"] . "% </td>";
        echo " <td> " . $row["count"] . "</td></tr>\n";
}
print "</table>\n";
?>

<p> </p>



<!-- Weekly Stats -->
<table>
  <tr>
    <td class="head" colspan="6">Weekly Statistics</td>
  </tr>
  <tbody>
    <tr>
      <td class="firstcol"><b>Week</b>
<?php
	$week_limit = 3;
	if (isset($_GET["week"])) {
		$week_limit = $_GET["week"];
	}

	if ($week_limit == 5) {
		echo "<a href=\"" . $this_document . "?week=3$options\"> [-] </a>";
	} else {
		echo "<a href=\"" . $this_document . "?week=5$options\"> [+] </a>";
	}
?>
	  </td>
      <td class="nextcol"><b>T<sup>min</sup> <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>T<sup>max</sup> <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>Mean <?php print $deg_unit;?></b></td>
      <td class="nextcol"><b>&sigma;</b></td>
      <td><b>n</b></td>
    </tr>
<?php
$query = "SELECT id, DATE_FORMAT(time,'%M %D') AS dbtime, WEEK(time,3) AS wk, ROUND(max($t_corr),1) AS max, ROUND(min($t_corr),1) AS min, ROUND(SUM($t_corr)/COUNT(*),1) AS mean, COUNT(*) as count, ROUND(STDDEV($t_corr) * 100, 0) AS err, YEARWEEK(time, 3) as yearweek FROM $table GROUP BY YEARWEEK(time, 3) ORDER BY -time LIMIT $week_limit";

$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);
mysql_select_db($DBNAME, $DB);
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
  echo "\t<tr class=\"row\"><td>";
  $plot_file = "$plotdir/temp_" . $row["yearweek"] . "w.png";
  echo "<a href=\"javascript:void(window.open('$plot_file','graph_window','resizable,scrollbars=no,width=768,height=288,left=100,top=100'))\">";
        echo " Week " . $row["wk"] . " (" .$row["dbtime"] . ")</a></td>";
        echo " <td> " . $row["min"] . "</td>";
        echo " <td> " . $row["max"] . "</td>";
        echo " <td> " . $row["mean"] . "</td>";
        echo " <td> " . $row["err"] . "% </td>";
        echo " <td> " . $row["count"] . "</td></tr>\n";
}
print "</table>\n";
?>

<p> </p>

<TABLE>
 <TBODY>
  
<!-- Daily plot -->
  <TR><TD class="update">Updated every <?php echo $update_sample ?>.</TD></TR>
  <TR><TD><IMG alt="Temperature today" src="<?php print $day_plot ?>"></TD></TR>
  <TR><TD>&nbsp;</TD></TR>

<!-- Weekly plot -->
  <TR><TD><IMG src="<?php print $week_plot ?>" alt="Temperature this month"></TD></TR>

</TBODY>
</TABLE>