Index: /branches/eam_branches/psphot.20100506/src/psphot.h
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27919)
@@ -349,5 +349,5 @@
 bool psphotStackImageLoop (pmConfig *config);
 bool psphotStackReadout (pmConfig *config, const pmFPAview *view);
-bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *ruleDet, const char *ruleCnv);
 bool psphotStackChisqImageAddReadout(const pmConfig *config, // Configuration
 				     const pmFPAview *view,
@@ -455,3 +455,6 @@
 pmFPAfile *psphotStackGetConvolveSource (pmConfig *config, psphotStackOptions *options, int index);
 
+bool psphotCopySources (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
+bool psphotCopySourcesReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
+
 #endif
Index: /branches/eam_branches/psphot.20100506/src/psphotMergeSources.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotMergeSources.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotMergeSources.c	(revision 27919)
@@ -399,2 +399,58 @@
     return true;
 }
+
+// copy the detections from one pmFPAfile to another
+bool psphotCopySources (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
+{
+    bool status = true;
+
+    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
+    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
+
+    // skip the chisq image because it is a duplicate of the detection version
+    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
+    if (!status) chisqNum = -1;
+
+    // loop over the available readouts
+    for (int i = 0; i < num; i++) {
+	if (i == chisqNum) continue; // skip chisq image
+        if (!psphotCopySourcesReadout (config, view, ruleOut, ruleSrc, i)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to copy sources from %s to %s entry %d", ruleSrc, ruleOut, i);
+            return false;
+        }
+    }
+    return true;
+}
+
+// add newly selected sources to the existing list of sources
+bool psphotCopySourcesReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
+
+    bool status;
+
+    // find the currently selected readout
+    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
+    psAssert (fileSrc, "missing file?");
+
+    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
+    psAssert (readoutSrc, "missing readout?");
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
+    // find the currently selected readout
+    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
+    psAssert (fileOut, "missing file?");
+
+    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
+    psAssert (readoutOut, "missing readout?");
+
+    // save detections on the readout->analysis
+    // XXX this replaced any existing entry; allow this operation to merge?
+    if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detections)) {
+	psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
+	return false;
+    }
+
+    return true;
+}
+
Index: /branches/eam_branches/psphot.20100506/src/psphotPetrosianRadialBins.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotPetrosianRadialBins.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotPetrosianRadialBins.c	(revision 27919)
@@ -182,5 +182,5 @@
 	psFree(values);
 	psFree(stats);
-	return false;
+	return true;
     }
 
Index: /branches/eam_branches/psphot.20100506/src/psphotPetrosianStats.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotPetrosianStats.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotPetrosianStats.c	(revision 27919)
@@ -15,4 +15,9 @@
 
     pmSourceRadialProfile *profile = source->extpars->petProfile;
+
+    if (!profile->binSB) {
+	psLogMsg ("psphot", PS_LOG_DETAIL, "no petrosian profile, skipping source %f, %f", source->peak->xf, source->peak->yf);
+	return true;
+    }
 
     psVector *binSB      = profile->binSB;
Index: /branches/eam_branches/psphot.20100506/src/psphotRadialBins.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotRadialBins.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotRadialBins.c	(revision 27919)
@@ -44,6 +44,12 @@
     psVector *radMin = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.LOWER");
     psVector *radMax = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.UPPER");
-    if (!radMin || !radMin->n) return false;
-    if (!radMax || !radMax->n) return false;
+    if (!radMin || !radMin->n) {
+	psError (PSPHOT_ERR_CONFIG, true, "error in definition of annular bins (radMin missing or empty)");
+	return false;
+    }
+    if (!radMax || !radMax->n) {
+	psError (PSPHOT_ERR_CONFIG, true, "error in definition of annular bins (radMax missing or empty)");
+	return false;
+    }
 
     psVector *binSB      = psVectorAllocEmpty(radMin->n, PS_TYPE_F32); // surface brightness of radial bin
@@ -155,5 +161,5 @@
 	psFree(values);
 	psFree(stats);
-	return false;
+	return true;
     }
 
Index: /branches/eam_branches/psphot.20100506/src/psphotSourceStats.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotSourceStats.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotSourceStats.c	(revision 27919)
@@ -16,6 +16,11 @@
     psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
 
+    // skip the chisq image (optionally?)
+    // int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
+    // if (!status) chisqNum = -1;
+
     // loop over the available readouts
     for (int i = 0; i < num; i++) {
+	// if (i == chisqNum) continue; // skip chisq image
         if (!psphotSourceStatsReadout (config, view, filerule, i, recipe, setWindow)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed to find initial detections for %s entry %d", filerule, i);
Index: /branches/eam_branches/psphot.20100506/src/psphotStackChisqImage.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackChisqImage.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackChisqImage.c	(revision 27919)
@@ -6,5 +6,5 @@
 
 // XXX supply filename or keep PSPHOT.INPUT fixed?
-bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *filerule)
+bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *ruleDet, const char *ruleCnv)
 {
     bool status = false;
@@ -21,7 +21,8 @@
 
     // loop over the available readouts
+    // generate the chisq image from the 'detection' images
     for (int i = 0; i < num; i++) {
-        if (!psphotStackChisqImageAddReadout(config, view, filerule, &chiReadout, i)) {
-            psError (PSPHOT_ERR_CONFIG, false, "failed to model background for %s entry %d", filerule, i);
+        if (!psphotStackChisqImageAddReadout(config, view, ruleDet, &chiReadout, i)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed to model background for %s entry %d", ruleDet, i);
             return false;
         }
@@ -35,6 +36,12 @@
     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "", num);
 
-    // save the resulting image
-    if (!psMetadataAddPtr(config->files, PS_LIST_TAIL, filerule, PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "", chisqFile)) {
+    // save the resulting image in the 'detection' set
+    if (!psMetadataAddPtr(config->files, PS_LIST_TAIL, ruleDet, PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "", chisqFile)) {
+        psError(PM_ERR_CONFIG, false, "could not add chisqFPA to config files");
+        return false;
+    }
+
+    // save the resulting image in the 'convolved' set
+    if (!psMetadataAddPtr(config->files, PS_LIST_TAIL, ruleCnv, PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "", chisqFile)) {
         psError(PM_ERR_CONFIG, false, "could not add chisqFPA to config files");
         return false;
Index: /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c	(revision 27918)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c	(revision 27919)
@@ -39,4 +39,5 @@
     // generate a background model (median, smoothed image)
     // XXX I think this is not defined correctly for an array of images.
+    // XXX probably need to subtract the model (same model?) for both RAW and OUT
     if (!psphotModelBackground (config, view, STACK_RAW)) {
 	return psphotReadoutCleanup (config, view, STACK_OUT);
@@ -56,5 +57,5 @@
     }
 
-    if (!psphotStackChisqImage(config, view, STACK_RAW)) {
+    if (!psphotStackChisqImage(config, view, STACK_RAW, STACK_OUT)) {
         psError (PSPHOT_ERR_UNKNOWN, false, "failure to generate chisq image");
         return psphotReadoutCleanup (config, view, STACK_OUT);
@@ -68,4 +69,14 @@
     if (!psphotFindDetections (config, view, STACK_RAW, true)) { // pass 1
         // this only happens if we had an error in psphotFindDetections
+        psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
+        return psphotReadoutCleanup (config, view, STACK_OUT);
+    }
+
+    // XXX do this now or below?
+    // remove chisq image from config->file:PSPHOT.INPUT (why?)
+    // psphotStackRemoveChisqFromInputs(config, STACK_RAW);
+
+    // copy the detections from RAW to OUT
+    if (!psphotCopySources (config, view, STACK_OUT, STACK_RAW)) {
         psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
         return psphotReadoutCleanup (config, view, STACK_OUT);
@@ -170,4 +181,5 @@
     psphotSourceFreePixels (config, view, STACK_OUT);
 
+    // XXX do this now or above?
     // remove chisq image from config->file:PSPHOT.INPUT (why?)
     psphotStackRemoveChisqFromInputs(config, STACK_RAW);
