Index: branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c
===================================================================
--- branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c	(revision 26642)
+++ branches/eam_branches/psphot.stack.20100120/src/psphotSourceStats.c	(revision 26643)
@@ -3,5 +3,5 @@
 bool psphotSetMomentsWindow (psMetadata *recipe, psMetadata *analysis, psArray *sources);
 
-psArray *psphotSourceStats (pmConfig *config, pmReadout *readout, pmDetections *detections, bool setWindow) {
+bool psphotSourceStatsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, bool setWindow) {
 
     bool status = false;
@@ -10,7 +10,17 @@
     psTimerStart ("psphot.stats");
 
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
+    psAssert (readout, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
     // select the appropriate recipe information
     psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
-    assert (recipe);
+    psAssert (recipe, "missing recipe?");
 
     // determine the number of allowed threads
@@ -21,16 +31,17 @@
 
     // determine properties (sky, moments) of initial sources
-    float OUTER    = psMetadataLookupF32 (&status, recipe, "SKY_OUTER_RADIUS");
-    if (!status) return NULL;
-
+    float OUTER = psMetadataLookupF32 (&status, recipe, "SKY_OUTER_RADIUS");
+    psAssert (status, "missing SKY_OUTER_RADIUS in recipe?");
+
+    // XXX this seems like an arbitrary number...
     OUTER = PS_MAX(OUTER, 20.0); // XXX Guarantee that we can encompass the max moments radius
 
     char *breakPt  = psMetadataLookupStr (&status, recipe, "BREAK_POINT");
-    if (!status) return NULL;
+    if (!status) return false;
 
     psArray *peaks = detections->peaks;
     if (!peaks) {
         psError(PS_ERR_UNEXPECTED_NULL, false, "No peaks found!");
-        return NULL;
+        return false;
     }
 
@@ -64,5 +75,12 @@
         psLogMsg ("psphot", PS_LOG_INFO, "break point PEAKS, skipping MOMENTS\n");
         psphotVisualShowMoments (sources);
-        return sources;
+	// save sources on the readout->analysis
+	if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, "psphot sources", sources)) {
+	    psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
+	    psFree(sources);
+	    return false;
+	}
+	psFree(sources);
+        return true;
     }
 
@@ -70,5 +88,6 @@
         if (!psphotSetMomentsWindow(recipe, readout->analysis, sources)) {
             psError(PS_ERR_UNEXPECTED_NULL, false, "Failed to determine Moments Window!");
-            return NULL;
+	    psFree(sources);
+            return false;
         }
     }
@@ -103,5 +122,6 @@
                 psError(PS_ERR_UNKNOWN, false, "Unable to launch thread job PSPHOT_SOURCE_STATS");
                 psFree (job);
-                return NULL;
+		psFree(sources);
+                return false;
             }
             psFree(job);
@@ -111,5 +131,6 @@
         if (!psThreadPoolWait (false)) {
             psError(PS_ERR_UNKNOWN, false, "Failure in thread job PSPHOT_SOURCE_STATS");
-            return NULL;
+	    psFree(sources);
+            return false;
         }
 
@@ -138,7 +159,15 @@
     psphotVisualShowMoments (sources);
 
-    return (sources);
+    // save sources on the readout->analysis
+    if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, "psphot sources", sources)) {
+	psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
+	psFree(sources);
+        return false;
+    }
+    psFree(sources);
+    return true;
 }
 
+// XXX fix the return status of this function...
 bool psphotSourceStatsUpdate (psArray *sources, pmConfig *config, pmReadout *readout) {
 
@@ -451,2 +480,24 @@
     return true;
 }
+
+
+// if we use the footprints, the output peaks list contains both old and new peaks,
+// otherwise it only contains the new peaks.
+
+// for now, let's store the detections on the readout->analysis for each readout
+bool psphotSourceStats (pmConfig *config, const pmFPAview *view, bool setWindow)
+{
+    bool status = true;
+
+    int num = psMetadataAddS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
+
+    // loop over the available readouts
+    for (int i = 0; i < num; i++) {
+	if (!psphotFindDetectionsReadout (config, view, "PSPHOT.INPUT", i, setWindow)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to find initial detections for PSPHOT.INPUT entry %d", i);
+	    return false;
+	}
+    }
+    return true;
+}
