<?php 

$debug = 0;

include 'ipp.php';

$ID = checkID ();

$filesToRemove = array();

// 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, 'Histogram', 'ipp.css', $ID['link'], $ID['proj']);

echo "<p> Histogram </p>";

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

$restricted = 0;

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **
$WHERE = "WHERE camRun.state = 'full' AND camRun.chip_id = chipRun.chip_id AND chipRun.exp_id = rawExp.exp_id AND camProcessedExp.cam_id  = camRun.cam_id AND camProcessedExp.fault = 0";
$WHERE = check_restrict ('rawExp.exp_name', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('chipRun.label', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('chipRun.data_group', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('chipRun.dist_group', $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.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 ('camProcessedExp.fwhm_major', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_uq', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_uq', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_uq', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_lq', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_lq', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_major_lq', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_uq', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_uq', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_uq', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_lq', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_lq', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.fwhm_minor_lq', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_major', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_major', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_major', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_minor', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_minor', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_fwhm_minor', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m2', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m2', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m2', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m3', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m3', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m3', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m4', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m4', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camProcessedExp.iq_m4', $WHERE, 'max', 1.0);
$WHERE = check_ordering ('', $WHERE);

if ($restricted == 0) {
  if ("none" != "none") {
    $WHERE = "$WHERE none";
  }
}

// get the result table count
if (basic == "basic") {
  $sql = "SELECT count(*) FROM camRun, chipRun, rawExp, camProcessedExp $WHERE";
}
if (basic == "summary") {
  $sql = "SELECT count(*) FROM (SELECT rawExp.exp_name,chipRun.label,chipRun.data_group,chipRun.dist_group,rawExp.dateobs,rawExp.filter,rawExp.exp_time,rawExp.airmass,camProcessedExp.fwhm_major,camProcessedExp.fwhm_minor,camProcessedExp.fwhm_major_uq,camProcessedExp.fwhm_major_lq,camProcessedExp.fwhm_minor_uq,camProcessedExp.fwhm_minor_lq,camProcessedExp.iq_fwhm_major,camProcessedExp.iq_fwhm_minor,camProcessedExp.iq_m2,camProcessedExp.iq_m3,camProcessedExp.iq_m4 FROM camRun, chipRun, rawExp, camProcessedExp $WHERE) as TEMP";
}

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading camRun, chipRun, rawExp, camProcessedExp 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 camRun, chipRun, rawExp, camProcessedExp table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
// set up the row counter variables
if ($ID['from']) {
  $rowStart = $ID['from'];
} else {
  $rowStart = 0;
}
$rowLast = $rowStart + $dTABLE;
$rowTotal = $row[0];
if ($rowLast > $rowTotal) { $rowLast = $rowTotal; }
echo "<b> $rowStart to $rowLast of $rowTotal items</b><br>\n";

// query the database
if (basic == "basic") {
  $sql = "SELECT rawExp.exp_name,chipRun.label,chipRun.data_group,chipRun.dist_group,rawExp.dateobs,rawExp.filter,rawExp.exp_time,rawExp.airmass,camProcessedExp.fwhm_major,camProcessedExp.fwhm_minor,camProcessedExp.fwhm_major_uq,camProcessedExp.fwhm_major_lq,camProcessedExp.fwhm_minor_uq,camProcessedExp.fwhm_minor_lq,camProcessedExp.iq_fwhm_major,camProcessedExp.iq_fwhm_minor,camProcessedExp.iq_m2,camProcessedExp.iq_m3,camProcessedExp.iq_m4 FROM camRun, chipRun, rawExp, camProcessedExp $WHERE";
}
if (basic == "summary") {
  $sql = "SELECT rawExp.exp_name,chipRun.label,chipRun.data_group,chipRun.dist_group,rawExp.dateobs,rawExp.filter,rawExp.exp_time,rawExp.airmass,camProcessedExp.fwhm_major,camProcessedExp.fwhm_minor,camProcessedExp.fwhm_major_uq,camProcessedExp.fwhm_major_lq,camProcessedExp.fwhm_minor_uq,camProcessedExp.fwhm_minor_lq,camProcessedExp.iq_fwhm_major,camProcessedExp.iq_fwhm_minor,camProcessedExp.iq_m2,camProcessedExp.iq_m3,camProcessedExp.iq_m4 FROM camRun, chipRun, rawExp, camProcessedExp $WHERE";
}

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

// ** HEAD CODE **

// ** BUTTON RESTRICTIONS **
$buttonLink = button_restrict_string ('rawExp.exp_name', $buttonLink);
$buttonLink = button_restrict_string ('chipRun.label', $buttonLink);
$buttonLink = button_restrict_string ('chipRun.data_group', $buttonLink);
$buttonLink = button_restrict_string ('chipRun.dist_group', $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.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 ('camProcessedExp.fwhm_major', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_major', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_major', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.fwhm_minor', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_minor', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_minor', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.fwhm_major_uq', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_major_uq', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_major_uq', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.fwhm_major_lq', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_major_lq', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_major_lq', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.fwhm_minor_uq', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_minor_uq', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_minor_uq', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.fwhm_minor_lq', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.fwhm_minor_lq', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.fwhm_minor_lq', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.iq_fwhm_major', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.iq_fwhm_major', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.iq_fwhm_major', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.iq_fwhm_minor', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.iq_fwhm_minor', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.iq_fwhm_minor', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.iq_m2', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.iq_m2', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.iq_m2', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.iq_m3', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.iq_m3', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.iq_m3', $buttonLink);
$buttonLink = button_restrict_string ('camProcessedExp.iq_m4', $buttonLink);
$buttonLink = button_restrict_min ('camProcessedExp.iq_m4', $buttonLink);
$buttonLink = button_restrict_max ('camProcessedExp.iq_m4', $buttonLink);

$buttonLink = button_restrict_checkbox ('hist.exp_time', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.airmass', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_major_psf', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_minor_psf', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_uq', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_lq', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_major_moments', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_minor_moments', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_m2_moment', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_m3_moment', $buttonLink);
$buttonLink = button_restrict_checkbox ('hist.fwhm_m4_moment', $buttonLink);

$buttonLink = button_restrict_string ('hist_delta.exp_time', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.airmass', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_major_psf', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_minor_psf', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_uq', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_lq', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_major_moments', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_minor_moments', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_m2_moment', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_m3_moment', $buttonLink);
$buttonLink = button_restrict_string ('hist_delta.fwhm_m4_moment', $buttonLink);

navigate_buttons ($rowStart, $rowLast, $dTABLE, $rowTotal, $buttonLink, $ID, 'histogram.php');

echo "&nbsp; : &nbsp; or enter start row: <input type=\"text\" name=\"from\" size=\"5\" value=\"$rowStart\">\n";

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

// echo "<tr><td></td>\n"; // first field is a label set below for the query rows only
// ** TABLE HEADER **
echo "<tr><td></td>\n";
write_header_cell ("list", "Exp Name");
write_header_cell ("list", "Label");
write_header_cell ("list", "data grp");
write_header_cell ("list", "dist grp");
write_header_cell ("list", "Date/Time");
write_header_cell ("list", "FILTER");
write_header_cell ("list", "exp_time    ");
write_header_cell ("list", "airmass     ");
write_header_cell ("list", "fwhm major psf");
write_header_cell ("list", "fwhm minor psf");
write_header_cell ("list", "fwhm UQ");
write_header_cell ("list", "fwhm LQ");
write_header_cell ("list", "fwhm major moments");
write_header_cell ("list", "fwhm minor moments");
write_header_cell ("list", "m2 moment");
write_header_cell ("list", "m3 moment");
write_header_cell ("list", "m4 moment");
echo "</tr>\n";

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// ** TABLE QUERY **
echo "<tr><td>&ge;</td>\n";
write_query_row ('rawExp.exp_name', 5, 'string');
write_query_row ('chipRun.label', 7, 'string');
write_query_row ('chipRun.data_group', 7, 'string');
write_query_row ('chipRun.dist_group', 7, 'string');
write_query_row ('rawExp.dateobs', 19, 'min');
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 ('camProcessedExp.fwhm_major', 5, 'min');
write_query_row ('camProcessedExp.fwhm_minor', 5, 'min');
write_query_row ('camProcessedExp.fwhm_major_uq', 5, 'min');
write_query_row ('camProcessedExp.fwhm_major_lq', 5, 'min');
write_query_row ('camProcessedExp.iq_fwhm_major', 5, 'min');
write_query_row ('camProcessedExp.iq_fwhm_minor', 5, 'min');
write_query_row ('camProcessedExp.iq_m2', 5, 'min');
write_query_row ('camProcessedExp.iq_m3', 5, 'min');
write_query_row ('camProcessedExp.iq_m4', 5, 'min');
echo "</tr><tr><td>&le;</td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.dateobs', 19, 'max');
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.exp_time', 5, 'max');
write_query_row ('rawExp.airmass', 5, 'max');
write_query_row ('camProcessedExp.fwhm_major', 5, 'max');
write_query_row ('camProcessedExp.fwhm_minor', 5, 'max');
write_query_row ('camProcessedExp.fwhm_major_uq', 5, 'max');
write_query_row ('camProcessedExp.fwhm_major_lq', 5, 'max');
write_query_row ('camProcessedExp.iq_fwhm_major', 5, 'max');
write_query_row ('camProcessedExp.iq_fwhm_minor', 5, 'max');
write_query_row ('camProcessedExp.iq_m2', 5, 'max');
write_query_row ('camProcessedExp.iq_m3', 5, 'max');
write_query_row ('camProcessedExp.iq_m4', 5, 'max');
echo "</tr>\n";
// echo "</tr>\n";

//checkboxes
echo "<tr><td></td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";//dateobs
echo "<td> &nbsp; </td>\n";
write_query_checkbox ('hist.exp_time', 'Histogram?');
write_query_checkbox ('hist.airmass', 'Histogram?');
write_query_checkbox ('hist.fwhm_major_psf', 'Histogram?');
write_query_checkbox ('hist.fwhm_minor_psf', 'Histogram?');
write_query_checkbox ('hist.fwhm_uq', 'Histogram?');
write_query_checkbox ('hist.fwhm_lq', 'Histogram?');
write_query_checkbox ('hist.fwhm_major_moments', 'Histogram?');
write_query_checkbox ('hist.fwhm_minor_moments', 'Histogram?');
write_query_checkbox ('hist.fwhm_m2_moment', 'Histogram?');
write_query_checkbox ('hist.fwhm_m3_moment', 'Histogram?');
write_query_checkbox ('hist.fwhm_m4_moment', 'Histogram?');
echo "</tr>\n";

echo "<tr><td></td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> Bin size (delta)</td>\n";
write_query_row ('hist.exp_time_delta', 5, 'string');
write_query_row ('hist.airmass_delta', 5, 'string');
write_query_row ('hist.fwhm_major_psf_delta', 5, 'string');
write_query_row ('hist.fwhm_minor_psf_delta', 5, 'string');
write_query_row ('hist.fwhm_uq_delta', 5, 'string');
write_query_row ('hist.fwhm_lq_delta', 5, 'string');
write_query_row ('hist.fwhm_major_moments_delta', 5, 'string');
write_query_row ('hist.fwhm_minor_moments_delta', 5, 'string');
write_query_row ('hist.fwhm_m2_moment_delta', 5, 'string');
write_query_row ('hist.fwhm_m3_moment_delta', 5, 'string');
write_query_row ('hist.fwhm_m4_moment_delta', 5, 'string');
echo "</tr>\n";

echo "<tr><td></td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> Bins number (default: 100)</td>\n";
write_query_row ('hist.exp_time_count', 5, 'string');
write_query_row ('hist.airmass_count', 5, 'string');
write_query_row ('hist.fwhm_major_psf_count', 5, 'string');
write_query_row ('hist.fwhm_minor_psf_count', 5, 'string');
write_query_row ('hist.fwhm_uq_count', 5, 'string');
write_query_row ('hist.fwhm_lq_count', 5, 'string');
write_query_row ('hist.fwhm_major_moments_count', 5, 'string');
write_query_row ('hist.fwhm_minor_moments_count', 5, 'string');
write_query_row ('hist.fwhm_m2_moment_count', 5, 'string');
write_query_row ('hist.fwhm_m3_moment_count', 5, 'string');
write_query_row ('hist.fwhm_m4_moment_count', 5, 'string');
echo "</tr>\n";

// close the table and form
echo "</table>\n";
echo "<a href=\"http://en.wikipedia.org/wiki/Histogram#Number_of_bins_and_width\">About the \"good\" number of bins<br><br></a>\n";
echo "<input type=\"submit\" name=\"constraints\" value=\"refine search/show histograms\">\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";

$keysToRowIndex = array( 
      'hist.exp_time' => 6,
      'hist.airmass' => 7,
      'hist.fwhm_major_psf' => 8,
      'hist.fwhm_minor_psf' => 9,
      'hist.fwhm_uq'=> 10,
      'hist.fwhm_lq' => 11,
      'hist.fwhm_major_moments' => 12,
      'hist.fwhm_minor_moments' => 13,
      'hist.fwhm_m2_moment' => 14,
      'hist.fwhm_m3_moment' => 15,
      'hist.fwhm_m4_moment' =>16);
$handles = array();
$filenames = array();
foreach ($keysToRowIndex as $i => $value) {
    if (get_value_from_key($i, TRUE) == "on") {
       $filenames[$i] = tempnam("/tmp/serge", "histodat.");
       if ($debug) {
          echo "<b>Using $filenames[$i] for $i</b>\n";
       }
       $handles[$i] = fopen($filenames[$i], "w");
    }
}
// ** TAIL CODE **
while ($qry->fetchInto($row)) {
  foreach ($keysToRowIndex as $i => $value) {
    if (get_value_from_key($i, TRUE) == "on") {
      fwrite($handles[$i],
	sprintf("%.3f\n", $row[$value]));
    }
  }
}
foreach ($keysToRowIndex as $i => $value) {
  if ($handles[$i] != NULL) {
    fclose($handles[$i]);
    chmod($filenames[$i], 0777);
  }
}

if ($debug) {
   $show = get_value_from_key('hist.exp_time', TRUE);
   $show .= " && ".get_value_from_key('hist.airmass', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_major_psf', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_minor_psf', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_uq', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_lq', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_major_moments', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_minor_moments', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_m2_moment', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_m3_moment', TRUE);
   $show .= " && ".get_value_from_key('hist.fwhm_m4_moment', TRUE);
   echo "==&gt; ($show)<br>\n";
}

//Get environment
$PATH = getenv("PATH");
putenv("PATH=$BINDIR:$PATH");
$LD_LIBRARY_PATH = getenv("LD_LIBRARY_PATH");
putenv("LD_LIBRARY_PATH=$LIBDIR:$LD_LIBRARY_PATH");

echo "<br>\n";
foreach ($filenames as $i => $value) {
  $outfile = $value.".png";
  $deltaValue = get_value_from_key($i."_delta", TRUE);
  $binValue = get_value_from_key($i."_count", TRUE);
  if ($binValue == "") {
     $binValue = "-1";
     if ($deltaValue == "") {
       $binValue = "100";
       $deltaValue = "-1";
     }    
  } else {
    if ($deltaValue != "") {
      echo "<font color=red>Warning! Both 'Bins number' and 'delta value' are specified =&gt; 'delta value' will be ignored<br></font>\n";
      $deltaValue = "-1";
    } else {
      $deltaValue = "-1";
    }
  }
  $title = preg_replace ('|hist\.|', '', $i);
  $title = preg_replace ('|_|', '-', $title);

  echo "Showing histogram for [$title] /  delta = $deltaValue / bins = $binValue<br>\n";
  exec("build_histogram.dvo $value $outfile $deltaValue $binValue $title",
  	$output, $status);
  if ($debug) {
    echo "&nbsp;&nbsp;&nbsp;&nbsp;Status = $status<br>\n";
    for ($i = 0; $i < count($output); $i++) {
      echo "&nbsp;&nbsp;&nbsp;&nbsp;output $i: $output[$i]<br>";
    }
  }
  $blah=count($output);
  if ($debug) {
    echo "&nbsp;&nbsp;&nbsp;&nbsp;end: $output / $blah<br>\n";
  }
  echo "<img src=\"show_and_delete_image.php?file=$outfile\"><br><br>\n";
  unset($output);
  $filesToRemove[$value] = '';
}

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

if ($debug) {
  echo "Cleaning<br>\n";
}
foreach ($filesToRemove as $index => $value) {
  if ($debug) {
    echo "Deleting $index<br>\n";
  }
  unlink($index);
}
if ($debug) {
  echo "Cleaning end<br>\n";
}

menu_end();

?>
