Index: trunk/psvideophot/src/psvideoLoop.c
===================================================================
--- trunk/psvideophot/src/psvideoLoop.c	(revision 31715)
+++ trunk/psvideophot/src/psvideoLoop.c	(revision 31813)
@@ -5,7 +5,7 @@
 #include "psvideophot.h"
 
-// XXX: get these from a recipe
-#define DX 20
-#define DY 10
+// XXX: derive these from a recipe perhaps basing them on seeing
+#define DX 10
+#define DY 5
 #define SKIP 0
 #define EDGE 3
@@ -22,4 +22,5 @@
 static double SubSum (psImage *image, double xy[2], double median);
 static void Centering(psImage *image, long edge, double *pSum, double xy[], double median);
+static psArray *readVideoTable(pmFPAfile *input, psString video_extension_name);
 
 bool psvideophotLoop(pmConfig *config, psvideophotOptions *options)
@@ -90,5 +91,16 @@
 
             psString cellName = psMetadataLookupStr(&status, cell->concepts, "CELL.NAME");
-	    psWarning("Found video cell %s %d\n", cellName, view->cell);
+	    psLogMsg("psvideophotLoop", 1, "Found video cell %s %d\n", cellName, view->cell);
+
+            // get the position of this cell on the chip 
+            int cellX0 = psMetadataLookupS32(&status, cell->concepts, "CELL.X0");
+            int cellY0 = psMetadataLookupS32(&status, cell->concepts, "CELL.Y0");
+            int xParity = psMetadataLookupS32(&status, cell->concepts, "CELL.XPARITY");
+            int yParity = psMetadataLookupS32(&status, cell->concepts, "CELL.YPARITY");
+            int xBin = psMetadataLookupS32(&status, cell->concepts, "CELL.XBIN");
+            int yBin = psMetadataLookupS32(&status, cell->concepts, "CELL.YBIN");
+
+            // avoid errors about unused variables
+            (void) xParity; (void) yParity; (void) cellX0; (void)cellY0;
 
             psStats *stats = psStatsAlloc(
@@ -101,6 +113,21 @@
                 );
 
+            // go find the video extension data
+            psString video_extension_name = NULL;
+            psStringAppend(&video_extension_name, "video_table_%s", cellName);
+            psArray *video_table = readVideoTable(input, video_extension_name);
+            if (!video_table) {
+                psError(PS_ERR_UNKNOWN, false, "failed to find video extension:%s", video_extension_name);
+                ESCAPE("invalid image file");
+            }
+            psMetadataAddArray(cell->analysis, PS_LIST_TAIL, "VIDEO_DATA", 0, "", video_table);
+
+            if (cell->readouts->n != video_table->n) {
+                psError(PS_ERR_UNKNOWN, true, "number of readouts %ld does not match length of video extension: %ld", 
+                    cell->readouts->n, video_table->n);
+                ESCAPE("invalid data");
+            }
+
             // process each of the readouts
-            int frame = 0;
             pmReadout *readout;         // Readout from cell
             while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
@@ -108,5 +135,6 @@
                     ESCAPE("load failure for Readout");
 	        }
-                if (++frame < SKIP) {
+                int frame = view->readout;
+                if (frame < SKIP - 1) {
                     continue;
                 }
@@ -114,4 +142,56 @@
                     continue;
                 }
+
+                psMetadata *row = video_table->data[frame];
+                if (!row) {
+                    psError(PS_ERR_UNKNOWN, true, "video_table row for frame %d is null", frame);
+                    // XXX: rather than fail, should we just skip this frame
+                    ESCAPE(" ");
+                }
+
+                // extract the camera's measurements from the video table
+                // We should probably get the column names from a configuration file so that we can
+                // handle any changes more gracefully
+                // Time at which frame was completely received from controller
+                double frame_start = psMetadataLookupF64(&status, row, "frame_start_time");
+                // Time at which frame was completely received from controller
+                double frame_complete = psMetadataLookupF64(&status, row, "frame_complete_time");
+                // star centriod, x axis
+                double centroid_x = psMetadataLookupF32(&status, row, "centroid_x");
+                // star centriod, y axis
+                double centroid_y = psMetadataLookupF64(&status, row, "centroid_y");
+                // star full width at half maximum
+                double fwhm = psMetadataLookupF64(&status, row, "fwhm");
+                // star full width at half maximum, x axis
+                double fwhm_x = psMetadataLookupF64(&status, row, "fwhm_x");
+                // star full width at half maximum, y axis
+                double fwhm_y = psMetadataLookupF64(&status, row, "fwhm_y");
+                // calculated sky level
+                double sky = psMetadataLookupF64(&status, row, "sky");
+                // flux from star
+                double flux = psMetadataLookupF64(&status, row, "flux");
+                // signal to noise ration of video frame
+                double snr = psMetadataLookupF64(&status, row, "snr");
+                // number of parallel row shits before integration (cleans)
+                double rowpre = psMetadataLookupS32(&status, row, "rowpre");
+                // number of cols skipped before video readout
+                double cnpix1 = psMetadataLookupS32(&status, row, "cnpix1");
+                // number of rows skipped before video readout
+                double cnpix2 = psMetadataLookupS32(&status, row, "cnpix2");
+
+                (void) frame_start; (void) frame_complete;
+                (void) rowpre; (void) cnpix1; (void) cnpix2; (void) fwhm_x; (void) fwhm_y; (void) fwhm;
+
+                double star_chip_x = cellX0 + (centroid_x * xParity);
+                double star_chip_y = cellY0 + (centroid_y * yParity);
+                (void) star_chip_x; (void) star_chip_y;
+
+                // measured position of star in readout coordinates
+                double star_readout_x = (centroid_x - cnpix1) / xBin;
+                double star_readout_y = (centroid_y - cnpix2) / yBin;
+
+                printf("%4d %9.2f %9.2f %9.2f %.1f %.1f %9.6f %6.3f %6.3f %6.3f\n",
+                        frame, flux, snr, sky, star_readout_x, star_readout_y, frame_complete - frame_start,
+                            fwhm, fwhm_x, fwhm_y);
 
                 psStatsInit(stats);
@@ -120,4 +200,5 @@
                     psError(PS_ERR_UNKNOWN, false, "unable to generate image stats for cell %d readout %d\n",
                         view->cell, view->readout);
+                    // XXX: rather than fail, should we just skip this frame
                     ESCAPE("failed to generate stats");
                 }
@@ -131,9 +212,9 @@
                     double xy[2];
                     Centering(readout->image, EDGE, &sum, xy, median);
-                    double sum2;
-                    sum2 = SubSum(readout->image, xy, median);
-
-                    printf("%4d %9.2f %9.2f %8.2f %.1f %.1f %9.2f\n",
-                            frame, sum, stddev, median, xy[0], xy[1], sum2);
+                    double ourFlux;
+                    ourFlux = SubSum(readout->image, xy, median);
+
+                    printf("%4d %9.2f %9.2f %9.2f %.1f %.1f %9.2f\n",
+                             frame, ourFlux, ourFlux/stddev, median, xy[0], xy[1], stddev);
                 } else {
                     printf("%4d %9.2f %9.2f %8.2f\n",
@@ -141,4 +222,5 @@
                 }
             }
+            psLogMsg ("psvideophotLoop", 5, "Done processing video cell");
         }
 
@@ -205,6 +287,8 @@
         }
     }
-    xy[0] = edge-1+sxf/sum;
+    xy[0] = edge-2+sxf/sum;
     xy[1] = edge-2+syf/sum;
+    xy[0] = sxf/sum;
+    xy[1] = syf/sum;
     *pSum = sum;
     //printf("%f %f\n", xc, yc);
@@ -238,2 +322,11 @@
     return(sum);
 }
+static psArray *readVideoTable(pmFPAfile *input, psString video_extension_name)
+{
+    if (!psFitsMoveExtName(input->fits, video_extension_name)) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to move to video extension: %s", video_extension_name);
+        return NULL;
+    }
+
+    return psFitsReadTable(input->fits);
+}
