Index: branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c
===================================================================
--- branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c	(revision 26643)
+++ branches/eam_branches/psphot.stack.20100120/src/psphotFindDetections.c	(revision 26688)
@@ -1,6 +1,30 @@
 # include "psphotInternal.h"
 
+// we store the detections on the readout->analysis for each readout this function finds new
+// peaks and new footprints.  any old peaks are saved on oldPeaks.  the resulting footprint set
+// contains all footprints (old and new)
+bool psphotFindDetections (pmConfig *config, const pmFPAview *view)
+{
+    bool status = true;
+
+    // select the appropriate recipe information
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    psAssert (recipe, "missing recipe?");
+
+    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, recipe)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to find initial detections for PSPHOT.INPUT entry %d", i);
+	    return false;
+	}
+    }
+    return true;
+}
+
 // smooth the image, search for peaks, optionally define footprints based on the peaks
-bool psphotFindDetections (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
+bool psphotFindDetectionsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
 
     bool status;
@@ -11,12 +35,8 @@
     // find the currently selected readout
     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
-    psAssert (readout, "missing file?");
+    psAssert (file, "missing file?");
 
     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     psAssert (readout, "missing readout?");
-
-    // select the appropriate recipe information
-    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
-    psAssert (recipe, "missing recipe?");
 
     // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
@@ -27,11 +47,21 @@
     const bool useFootprints = psMetadataLookupBool(NULL, recipe, "USE_FOOTPRINTS");
 
-    // on first pass, detections have not yet been allocated
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    // on the initial pass, detections have not yet been allocated or saved on readout->analysis
     if (!detections) {
+	// create the container
         detections = pmDetectionsAlloc();
+	
+	// save detections on the readout->analysis
+	if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_PTR, "psphot detectinos", detections)) {
+	    psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
+	    return false;
+	}
+
         pass = 1;
         NSIGMA_PEAK = psMetadataLookupF32 (&status, recipe, "PEAKS_NSIGMA_LIMIT"); PS_ASSERT (status, NULL);
         NMAX = psMetadataLookupS32 (&status, recipe, "PEAKS_NMAX"); PS_ASSERT (status, NULL);
     } else {
+	psMemIncrRefCounter(detections); // so we can free the detections below
         pass = 2;
         NSIGMA_PEAK = psMetadataLookupF32 (&status, recipe, "PEAKS_NSIGMA_LIMIT_2"); PS_ASSERT (status, NULL);
@@ -41,6 +71,10 @@
     float threshold = PS_SQR(NSIGMA_PEAK);
 
-    // move the old peaks array (if it exists) to oldPeaks
-    // XXX generically, we should be able to call this function an arbitrary number of times
+    // move the old peaks array (if it exists) to oldPeaks XXX generically, we should be able
+    // to call this function an arbitrary number of times the old peaks are saved so they can
+    // be freed later -- the have to be freed after psphotFindFootprints is called below, since
+    // they are also owned by the oldFootprints, which are in turn merged into the new
+    // footprints.  (what about the source->peak entry?)
+    
     assert (detections->oldPeaks == NULL);
     detections->oldPeaks = detections->peaks;
@@ -68,5 +102,5 @@
 	psFree (detections);
 	psError (PSPHOT_ERR_CONFIG, false, "failed on peak search");
-        return NULL;
+        return false;
     }
 
@@ -82,9 +116,5 @@
     psphotVisualShowFootprints (detections);
 
-    // save detections on the readout->analysis
-    if (!psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_PTR, "psphot detectinos", detections)) {
-	psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
-        return NULL;
-    }
+    psFree (detections);
 
     return true;
@@ -94,19 +124,2 @@
 // otherwise it only contains the new peaks.
 
-// for now, let's store the detections on the readout->analysis for each readout
-bool psphotFindDetections (pmConfig *config, const pmFPAview *view)
-{
-    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)) {
-            psError (PSPHOT_ERR_CONFIG, false, "failed to find initial detections for PSPHOT.INPUT entry %d", i);
-	    return false;
-	}
-    }
-    return true;
-}
