<?php 

include 'ipp.php';
include 'site.php';

$ID = checkID ();

// require an explicit project
if (! $ID['proj']) { projectform ($ID); }

$db = dbconnect($ID['proj']);

if ($ID['menu']) {
  $myMenu = $ID['menu'];
} else {
  $myMenu = "ipp.imfiles.dat";
}

menu($myMenu, 'test simple plot', 'ipp.css', $ID['link'], $ID['proj']);

echo "<p> test sky plot - raw </p>";

// set up the form
echo "<form action=\"simplePlotraw.php\" method=\"POST\">\n";

$restricted = 0;

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **
$WHERE = check_restrict ('rawExp.exp_name', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.exp_id', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.exp_id', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.exp_id', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.telescope', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.camera', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.dateobs', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.dateobs', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.dateobs', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.ra', $WHERE, 'min', 0.017453);
$WHERE = check_restrict ('rawExp.ra', $WHERE, 'max', 0.017453);
$WHERE = check_restrict ('rawExp.decl', $WHERE, 'min', 0.017453);
$WHERE = check_restrict ('rawExp.decl', $WHERE, 'max', 0.017453);
$WHERE = check_restrict ('rawExp.object', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.filter', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.exp_time', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.exp_time', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.exp_time', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.airmass', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.airmass', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.airmass', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.bg', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.bg', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.bg', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.bg_stdev', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('rawExp.bg_stdev', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('rawExp.bg_stdev', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('rawExp.comment', $WHERE, 'string', 1.0);
$WHERE = check_ordering ('', $WHERE);

if ($restricted == 0) {
  if ($WHERE == "") {
    $WHERE = "WHERE (0 > 1)";
  } else {
    $WHERE = "WHERE (0 > 1)";
  }
}

// get the result table count
$sql = "SELECT count(*) FROM rawExp $WHERE";

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading rawExp table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
if (!$qry->fetchInto($row)) {
  echo "<b>error reading rawExp table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
$Npoints = $row[0];

echo "<b>$Npoints exposures match selection</b><br>\n";

// ** HEAD CODE **

// ** BUTTON RESTRICTIONS **
$buttonLink = button_restrict_string ('rawExp.exp_name', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.telescope', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.camera', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.dateobs', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.dateobs', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.dateobs', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.ra', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.ra', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.ra', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.decl', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.decl', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.decl', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.object', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.filter', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.exp_time', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.exp_time', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.exp_time', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.airmass', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.airmass', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.airmass', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.bg', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.bg', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.bg', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.bg_stdev', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.bg_stdev', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.bg_stdev', $buttonLink);
$buttonLink = button_restrict_string ('rawExp.comment', $buttonLink);

// set up the table
echo "<table class=list>\n";

// ** TABLE HEADER **
echo "<tr><td></td>\n";
write_header_cell ("list", "Exp Name");
write_header_cell ("list", "Exp ID");
write_header_cell ("list", "Telescope");
write_header_cell ("list", "Camera");
write_header_cell ("list", "RA");
write_header_cell ("list", "DEC");
write_header_cell ("list", "Comment");
echo "</tr>\n";

// query restriction form
// ** TABLE QUERY **
echo "<tr><td>&ge;</td>\n";
write_query_row ('rawExp.exp_name', 5, 'string');
write_query_row ('rawExp.exp_id', 5, 'min');
write_query_row ('rawExp.telescope', 10, 'string');
write_query_row ('rawExp.camera', 10, 'string');
write_query_row ('rawExp.ra', 8, 'min');
write_query_row ('rawExp.decl', 8, 'min');
write_query_row ('rawExp.comment', 65, 'string');
echo "</tr><tr><td>&le;</td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.exp_id', 5, 'max');
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.ra', 8, 'max');
write_query_row ('rawExp.decl', 8, 'max');
echo "<td> &nbsp; </td>\n";
echo "</tr>\n";

// ** TABLE HEADER **
echo "<tr><td></td>\n";
write_header_cell ("list", "Object");
write_header_cell ("list", "FILTER");
write_header_cell ("list", "exp_time    ");
write_header_cell ("list", "airmass     ");
write_header_cell ("list", "backgnd");
write_header_cell ("list", "stdev    ");
write_header_cell ("list", "Date/Time");
echo "</tr>\n";

// query restriction form
// ** TABLE QUERY **
echo "<tr><td>&ge;</td>\n";
write_query_row ('rawExp.object', 8, 'string');
write_query_row ('rawExp.filter', 10, 'string');
write_query_row ('rawExp.exp_time', 5, 'min');
write_query_row ('rawExp.airmass', 5, 'min');
write_query_row ('rawExp.bg', 5, 'min');
write_query_row ('rawExp.bg_stdev', 5, 'min');
write_query_row ('rawExp.dateobs', 19, 'min');
echo "</tr><tr><td>&le;</td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.exp_time', 5, 'max');
write_query_row ('rawExp.airmass', 5, 'max');
write_query_row ('rawExp.bg', 5, 'max');
write_query_row ('rawExp.bg_stdev', 5, 'max');
write_query_row ('rawExp.dateobs', 19, 'max');
echo "</tr>\n";

// close the table and form
echo "</table>\n";

echo "<input type=\"submit\" name=\"constraints\" value=\"refine search\">\n";
$pass = $ID['pass'];
$proj = $ID['proj'];
$menu = $ID['menu'];
echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\">\n";
echo "<input type=\"hidden\" name=\"proj\" value=\"$proj\">\n";
echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
echo "</form>\n";

// if there are no results, don't make an empty figure
if ($row[0] == 0) {
  menu_end();
}

// we write the ra & dec to tmp file 1 and have the plotter generate tmp file 2:

$datfilename = tempnam("/tmp", "dvodat.");
$pngfilename = tempnam("/tmp", "dvopng.");

$datfile = fopen($datfilename, "w");

// query the database
$sql = "SELECT rawExp.ra,rawExp.decl,rawExp.filter FROM rawExp $WHERE";

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading rawExp table</b><br>\n";
  echo "<br><small><b> table query : $sql </b></small><br>\n";
  menu_end();
}

// list the results
while ($qry->fetchInto($row)) {
  switch ($row[2]) {
   case 'g.00000':
    $filter = 1;
    break;
   case 'r.00000':
    $filter = 2;
    break;
   case 'i.00000':
    $filter = 3;
    break;
   case 'z.00000':
    $filter = 4;
    break;
   case 'y.00000':
    $filter = 5;
    break;
   case 'w.00000':
    $filter = 6;
    break;
   default:
    $filter = 0;
    break;
  }

  $line = "$row[0] $row[1] $filter\n";
  fwrite ($datfile, $line);
}
fclose($datfile);

echo "<img src=\"skyplot.php?input=$datfilename&output=$pngfilename\"><br>\n";

// ** TAIL CODE **

echo "<small> WHERE: $WHERE<br><br></small>\n";
echo "<small> SQL: $sql<br><br></small>\n";

menu_end();

unlink ($datfilename);
unlink ($pngfilename);

?>
