<?php 

include 'ipp.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.science.dat";
}

menu($myMenu, 'Warp Stage Exposures w/ Images', 'ipp.css', $ID['link'], $ID['proj']);

echo "<p> Warp Stage Exposures w/ Images</p>";

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

$restricted = 0;

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **
$WHERE = "WHERE warpRun.fake_id = fakeRun.fake_id AND fakeRun.cam_id = camRun.cam_id AND camRun.chip_id = chipRun.chip_id AND chipRun.exp_id = rawExp.exp_id";
$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 ('chipRun.chip_id', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('chipRun.chip_id', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('chipRun.chip_id', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('camRun.cam_id', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('camRun.cam_id', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('camRun.cam_id', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('fakeRun.fake_id', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('fakeRun.fake_id', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('fakeRun.fake_id', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('warpRun.warp_id', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('warpRun.warp_id', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('warpRun.warp_id', $WHERE, 'max', 1.0);
$WHERE = check_restrict ('warpRun.state', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('warpRun.label', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('warpRun.data_group', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('warpRun.dist_group', $WHERE, 'string', 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_restrict ('warpSummary.path_base', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('warpSummary.projection_cell', $WHERE, 'string', 1.0);
$WHERE = check_ordering ('', $WHERE);

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

// get the result table count
if (basic == "basic") {
  $sql = "SELECT count(*) FROM rawExp JOIN chipRun USING(exp_id) JOIN camRun USING (chip_id) JOIN fakeRun USING(cam_id) JOIN warpRun USING(fake_id) LEFT JOIN warpSummary ON warpSummary.warp_id = warpRun.warp_id $WHERE";
}

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading warpRun, fakeRun, camRun, chipRun, rawExp, warpSummary 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 warpRun, fakeRun, camRun, chipRun, rawExp, warpSummary 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,rawExp.exp_id,chipRun.chip_id,camRun.cam_id,fakeRun.fake_id,warpRun.warp_id,warpRun.state,warpRun.label,warpRun.data_group,warpRun.dist_group,rawExp.telescope,rawExp.camera,rawExp.dateobs,rawExp.ra,rawExp.decl,rawExp.object,rawExp.filter,rawExp.exp_time,rawExp.airmass,rawExp.bg,rawExp.bg_stdev,rawExp.comment,ifnull(warpSummary.path_base,'noimage'),ifnull(warpSummary.projection_cell,'noimage'),warpRun.tess_id FROM rawExp JOIN chipRun USING(exp_id) JOIN camRun USING (chip_id) JOIN fakeRun USING(cam_id) JOIN warpRun USING(fake_id) LEFT JOIN warpSummary ON warpSummary.warp_id = warpRun.warp_id $WHERE LIMIT $dTABLE OFFSET $rowStart";
}

/* echo "<tr><td>SQL = $sql</td></tr>\n"; */
/* menu_end(); */

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading warpRun, fakeRun, camRun, chipRun, rawExp, warpSummary 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 ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_min ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_max ('rawExp.exp_id', $buttonLink);
$buttonLink = button_restrict_string ('chipRun.chip_id', $buttonLink);
$buttonLink = button_restrict_min ('chipRun.chip_id', $buttonLink);
$buttonLink = button_restrict_max ('chipRun.chip_id', $buttonLink);
$buttonLink = button_restrict_string ('camRun.cam_id', $buttonLink);
$buttonLink = button_restrict_min ('camRun.cam_id', $buttonLink);
$buttonLink = button_restrict_max ('camRun.cam_id', $buttonLink);
$buttonLink = button_restrict_string ('fakeRun.fake_id', $buttonLink);
$buttonLink = button_restrict_min ('fakeRun.fake_id', $buttonLink);
$buttonLink = button_restrict_max ('fakeRun.fake_id', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.warp_id', $buttonLink);
$buttonLink = button_restrict_min ('warpRun.warp_id', $buttonLink);
$buttonLink = button_restrict_max ('warpRun.warp_id', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.state', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.label', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.data_group', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.dist_group', $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);
$buttonLink = button_restrict_string ('warpSummary.path_base', $buttonLink);
$buttonLink = button_restrict_string ('warpSummary.projection_cell', $buttonLink);
$buttonLink = button_restrict_string ('warpRun.tess_id', $buttonLink);
navigate_buttons ($rowStart, $rowLast, $dTABLE, 
		  $rowTotal, $buttonLink, $ID, 'warpProcessedExp_Images.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", "Exp ID");
write_header_cell ("list", "Chip ID");
write_header_cell ("list", "Cam ID");
write_header_cell ("list", "Fake ID");
write_header_cell ("list", "Warp ID");
write_header_cell ("list", "image");
write_header_cell ("list", "image");
write_header_cell ("list", "state");
write_header_cell ("list", "label");
write_header_cell ("list", "data grp");
write_header_cell ("list", "dist grp");
write_header_cell ("list", "Telescope");
write_header_cell ("list", "Camera");
write_header_cell ("list", "Date/Time");
write_header_cell ("list", "RA");
write_header_cell ("list", "DEC");
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", "Comment");
write_header_cell ("list", "TessId");
echo "</tr>\n";
echo "<tr><td></td>\n";
write_sort_cell ("list", "rawExp.exp_name", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.exp_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "chipRun.chip_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "camRun.cam_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "fakeRun.fake_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "warpRun.warp_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
echo "<td></td>\n";
echo "<td></td>\n";
write_sort_cell ("list", "warpRun.state", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "warpRun.label", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "warpRun.data_group", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "warpRun.dist_group", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.telescope", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.camera", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.dateobs", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.ra * 57.295783", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.decl * 57.295783", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.object", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.filter", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.exp_time", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.airmass", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.bg", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.bg_stdev", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "rawExp.comment", $buttonLink, $ID, 'warpProcessedExp_Images.php');
write_sort_cell ("list", "warpRun.tess_id", $buttonLink, $ID, 'warpProcessedExp_Images.php');
echo "</tr>\n";
// 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 ('rawExp.exp_id', 5, 'min');
write_query_row ('chipRun.chip_id', 7, 'min');
write_query_row ('camRun.cam_id', 7, 'min');
write_query_row ('fakeRun.fake_id', 7, 'min');
write_query_row ('warpRun.warp_id', 7, 'min');
echo "<td> &nbsp; </td>\n";
echo "<td></td>\n";
write_query_row ('warpRun.state', 7, 'string');
write_query_row ('warpRun.label', 7, 'string');
write_query_row ('warpRun.data_group', 7, 'string');
write_query_row ('warpRun.dist_group', 7, 'string');
write_query_row ('rawExp.telescope', 10, 'string');
write_query_row ('rawExp.camera', 10, 'string');
write_query_row ('rawExp.dateobs', 19, 'min');
write_query_row ('rawExp.ra', 8, 'min');
write_query_row ('rawExp.decl', 8, 'min');
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.comment', 65, 'string');
write_query_row ('warpRun.tess_id', 65, 'string');
echo "</tr><tr><td>&le;</td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.exp_id', 5, 'max');
write_query_row ('chipRun.chip_id', 7, 'max');
write_query_row ('camRun.cam_id', 7, 'max');
write_query_row ('fakeRun.fake_id', 7, 'max');
write_query_row ('warpRun.warp_id', 7, 'max');
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> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('rawExp.dateobs', 19, 'max');
write_query_row ('rawExp.ra', 8, 'max');
write_query_row ('rawExp.decl', 8, 'max');
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');
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "</tr>\n";
// echo "</tr>\n";

/* $warpIdCollection[$warpId]["00"] = "ul"; */
/* $warpIdCollection[$warpId]["01"] = "ur"; */
/* $warpIdCollection[$warpId]["10"] = "ll"; */
/* $warpIdCollection[$warpId]["11"] = "lr"; */
$warpIdCollection = array();

$noImageValue = "<a href=\"noimage.png\"><img src=\"noimage.png\"></a>";

// list the results
while ($qry->fetchInto($row)) {
  // $link = "warpProcessedExp_Images.php" . "?expID=" . $row[0] . "&" . $ID['link'];

  $class = "list";
  // ** TD CLASS **

  //Identify current warpId
  $warpId = $row[5];
  $tessId = $row[24];
  /* echo "<tr><td>Number of elements in warpIdCollection: ".count($warpIdCollection)."</td></tr>\n"; */
  /* echo "<tr><td>(warpId=$warpId)</td></tr>\n"; */

  if (!isSet($warpIdCollection[$warpId])) {
    /* echo "<tr><td>$warpId not in warpIdCollection</td></tr>"; */
    $warpIdCollection[$warpId] = array();
    $skyCellValue = preg_replace("|skycell[.]|","",$row[23]);
    $warpIdCollection[$warpId]["12"] = $skyCellValue;
    /* echo "<tr><td>Skycell is [$skyCellValue]</td></tr>"; */

    echo "<tr>\n";
    // ** TABLE DATA **
    echo "<td></td>\n";
    write_table_cell ($class, '%s', "", $row[0]);
    $link = "rawImfile.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&rawImfile.exp_id=$row[1]";
    write_table_cell ($class, '%d', $link, $row[1]);
    $link = "chipProcessedImfile.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&chipRun.chip_id=$row[2]";
    write_table_cell ($class, '%d', $link, $row[2]);
    $link = "camProcessedExp.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&camRun.cam_id=$row[3]";
    write_table_cell ($class, '%d', $link, $row[3]);
    $link = "fakeProcessedImfile.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&fakeRun.fake_id=$row[4]";
    write_table_cell ($class, '%d', $link, $row[4]);
    $link = "warpStageExp.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&warpRun.warp_id=$row[5]";
    write_table_cell ($class, '%d', $link, $row[5]);
    /* Image */
    echo "<td class=\"$class\">\n";
    echo "<table border=\"1\"><tr>\n";
    /* echo "  <td class=\"ul3x2\" id=\"$warpId.00\">$noImageValue</td>\n"; */
    echo "  <td class=\"ul3x2\" id=\"$warpId.00\"></td>\n";
    echo "  <td class=\"uc3x2\" id=\"$warpId.01\">$noImageValue</td>\n";
    echo "  <td class=\"ur3x2\" id=\"$warpId.02\">$noImageValue</td>\n";
    echo "</tr><tr>\n";
    /* echo "  <td class=\"ll3x2\" id=\"$warpId.10\">$noImageValue</td>\n"; */
    echo "  <td class=\"ll3x2\" id=\"$warpId.10\"></td>\n";
    echo "  <td class=\"lc3x2\" id=\"$warpId.11\">$noImageValue</td>\n";
    echo "  <td class=\"lr3x2\" id=\"$warpId.12\">$noImageValue</td>\n";
    echo "</tr></table>\n";
    echo "</td>\n";
    $link = "getimage.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&name=$row[22].$row[23]";
    $link = $link . "&rule=PPSKYCELL.JPEG1";
    $link = $link . "&camera=GPC1";
    $link = $link . "&class_id=NONE";

    /* Image as Text */
    echo "<td class=\"$class\">\n";
    echo "<table border=\"1\"><tr>\n";
    /* echo "  <td class=\"ul3x2\" id=\"t$warpId.00\">No image</td>\n"; */
    echo "  <td class=\"ul3x2\" id=\"t$warpId.00\"></td>\n";
    echo "  <td class=\"uc3x2\" id=\"t$warpId.01\">No image</td>\n";
    echo "  <td class=\"ur3x2\" id=\"t$warpId.02\">No image</td>\n";
    echo "</tr><tr>\n";
    /* echo "  <td class=\"ll3x2\" id=\"t$warpId.10\">No image</td>\n"; */
    echo "  <td class=\"ll3x2\" id=\"t$warpId.10\"></td>\n";
    echo "  <td class=\"lc3x2\" id=\"t$warpId.11\">No image</td>\n";
    echo "  <td class=\"lr3x2\" id=\"t$warpId.12\">No image</td>\n";
    echo "</tr></table>\n";
    echo "</td>\n";

    if ($tessId != "RINGS.V3") {
      $cellContent = "<a href=\\\"$link\\\"> <img src=\\\"getimage.php?name=$row[22].$row[23]&rule=PPSKYCELL.JPEG2&camera=$row[11]&class_id=NONE\\\"> </a>";
      echo "<script type=\"text/javascript\">changeCell(\"$warpId.12\", \"$cellContent\");</script>";
      $imageNameValue = ($row[23]=="noimage" ?
		         "No image" : $row[23]);
      echo "<script type=\"text/javascript\">changeCell(\"t$warpId.12\", \"$imageNameValue\");</script>";
    } else {
      //The cell to update is the upper right one for new tessellation
      $cellContent = "<a href=\\\"$link\\\"> <img src=\\\"getimage.php?name=$row[22].$row[23]&rule=PPSKYCELL.JPEG2&camera=$row[11]&class_id=NONE\\\"> </a>";
      echo "<script type=\"text/javascript\">changeCell(\"$warpId.02\", \"$cellContent\");</script>";
      $imageNameValue = ($row[23]=="noimage" ?
		         "No image" : $row[23]);
      echo "<script type=\"text/javascript\">changeCell(\"t$warpId.02\", \"$imageNameValue\");</script>";
    }

    //resume with "regular" columns
    write_table_cell ($class, '%s', "", $row[6]);
    write_table_cell ($class, '%s', "", $row[7]);
    write_table_cell ($class, '%s', "", $row[8]);
    write_table_cell ($class, '%s', "", $row[9]);
    write_table_cell ($class, '%s', "", $row[10]);
    write_table_cell ($class, '%s', "", $row[11]);
    write_table_cell ($class, '%T', "", $row[12]);
    write_table_cell ($class, '%C', "", $row[13] * 57.295783);
    write_table_cell ($class, '%C', "", $row[14] * 57.295783);
    write_table_cell ($class, '%s', "", $row[15]);
    write_table_cell ($class, '%s', "", $row[16]);
    write_table_cell ($class, '%.2f', "", $row[17]);
    write_table_cell ($class, '%.4f', "", $row[18]);
    write_table_cell ($class, '%.2f', "", $row[19]);
    write_table_cell ($class, '%.2f', "", $row[20]);
    write_table_cell ($class, '%s', "", $row[21]);
    write_table_cell ($class, '%s', "", $row[24]);
    echo "</tr>\n";

  } else {
    // Update table cell content
/*     echo "<tr><td>$warpId in warpIdCollection</td></tr>"; */
    $lrValue =  $warpIdCollection[$warpId]["12"];
    $skyCellValue = preg_replace("|skycell[.]|","",$row[23]);
/*     echo "<tr><td>Current skycell is [$skyCellValue] / LowerRight is [$lrValue]</td></tr>"; */

    $link = "getimage.php" . "?menu=ipp.science.dat&" . $ID['link'];
    $link = $link . "&name=$row[22].$row[23]";
    $link = $link . "&rule=PPSKYCELL.JPEG1";
    $link = $link . "&camera=GPC1";
    $link = $link . "&class_id=NONE";
    $cellContent = "<a href=\\\"$link\\\"> <img src=\\\"getimage.php?name=$row[22].$row[23]&rule=PPSKYCELL.JPEG2&camera=$row[11]&class_id=NONE\\\"> </a>";

    if ($tessId != "RINGS.V3") {
      if ($skyCellValue == $lrValue + 1) {
        $warpIdCollection[$warpId]["11"] = $skyCellValue;
      	echo "<script type=\"text/javascript\">changeCell(\"$warpId.11\", \"$cellContent\");</script>";
	echo "<script type=\"text/javascript\">changeCell(\"t$warpId.11\", \"$row[23]\");</script>";
      } else {
        if ($skyCellValue == $lrValue + 2) {
	  $warpIdCollection[$warpId]["10"] = $skyCellValue;
	  echo "<script type=\"text/javascript\">changeCell(\"$warpId.10\", \"$cellContent\");</script>";
	  echo "<script type=\"text/javascript\">changeCell(\"t$warpId.10\", \"$row[23]\");</script>";
        } else {
	  if (!isSet($warpIdCollection[$warpId]["02"])) {
	    /* echo "<tr><td>Update UpperRight</td></tr>"; */
	    $warpIdCollection[$warpId]["02"] = $skyCellValue;
	    echo "<script type=\"text/javascript\">changeCell(\"$warpId.02\", \"$cellContent\");</script>";
	    echo "<script type=\"text/javascript\">changeCell(\"t$warpId.02\", \"$row[23]\");</script>";
	  } else {
	    $urValue = $warpIdCollection[$warpId]["02"];
	    if ($urValue + 1 == $skyCellValue) {
	      echo "<script type=\"text/javascript\">changeCell(\"$warpId.01\", \"$cellContent\");</script>";
	      echo "<script type=\"text/javascript\">changeCell(\"t$warpId.01\", \"$row[23]\");</script>";
	    } else {
	      if ($urValue + 2 == $skyCellValue) {
	        echo "<script type=\"text/javascript\">changeCell(\"$warpId.00\", \"$cellContent\");</script>";
	        echo "<script type=\"text/javascript\">changeCell(\"t$warpId.00\", \"$row[23]\");</script>";
	      } else {
	        $expected = $urValue + 1;
	        $actual = $skyCellValue;
	        echo "<script type=\"text/javascript\">changeCell(\"$warpId.00\", \"Problem in UpperLeft corner: expected $expected but was $actual\");</script>";
	        echo "<tr><td>Problem in Upper part!</td></tr>";
	      }
	    } 
	  }
	}
      }
    } else { /* New tessellation */

      /* echo "<p>$lrValue -  $skyCellValue</p>"; */

      if ($skyCellValue == $lrValue + 1) {
        $warpIdCollection[$warpId]["11"] = $skyCellValue;
      	echo "<script type=\"text/javascript\">changeCell(\"$warpId.01\", \"$cellContent\");</script>";
	echo "<script type=\"text/javascript\">changeCell(\"t$warpId.01\", \"$row[23]\");</script>";
      } else {
        if ($skyCellValue == $lrValue + 2) {
	  $warpIdCollection[$warpId]["10"] = $skyCellValue;
	  echo "<script type=\"text/javascript\">changeCell(\"$warpId.00\", \"$cellContent\");</script>";
	  echo "<script type=\"text/javascript\">changeCell(\"t$warpId.00\", \"$row[23]\");</script>";
        } else {
	  if (!isSet($warpIdCollection[$warpId]["02"])) {
	    /* echo "<tr><td>Update UpperRight</td></tr>"; */
	    $warpIdCollection[$warpId]["02"] = $skyCellValue;
	    echo "<script type=\"text/javascript\">changeCell(\"$warpId.12\", \"$cellContent\");</script>";
	    echo "<script type=\"text/javascript\">changeCell(\"t$warpId.12\", \"$row[23]\");</script>";
	  } else {
	    $urValue = $warpIdCollection[$warpId]["02"];
	    if ($urValue + 1 == $skyCellValue) {
	      echo "<script type=\"text/javascript\">changeCell(\"$warpId.11\", \"$cellContent\");</script>";
	      echo "<script type=\"text/javascript\">changeCell(\"t$warpId.11\", \"$row[23]\");</script>";
	    } else {
	      if ($urValue + 2 == $skyCellValue) {
	        echo "<script type=\"text/javascript\">changeCell(\"$warpId.10\", \"$cellContent\");</script>";
	        echo "<script type=\"text/javascript\">changeCell(\"t$warpId.10\", \"$row[23]\");</script>";
	      } else {
	        $expected = $urValue + 1;
	        $actual = $skyCellValue;
	        echo "<script type=\"text/javascript\">changeCell(\"$warpId.00\", \"Problem in UpperLeft corner: expected $expected but was $actual\");</script>";
	        echo "<tr><td>Problem in Upper part!</td></tr>";
	      }
	    } 
	  }
	}

      }
    }
  }
}

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// TABLE QUERY 
// 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";

// ** TAIL CODE **

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

menu_end();

?>
