Index: /branches/eam_branches/ipp-20111110/psphot/src/psphotMergeSources.c
===================================================================
--- /branches/eam_branches/ipp-20111110/psphot/src/psphotMergeSources.c	(revision 32685)
+++ /branches/eam_branches/ipp-20111110/psphot/src/psphotMergeSources.c	(revision 32686)
@@ -520,4 +520,113 @@
 }
 
+// create source parents children from ruleSrc for ruleOut for orphans
+bool psphotSourceParent (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
+{
+    bool status = true;
+
+    int num = psphotFileruleCount(config, ruleSrc);
+
+    // 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 (!psphotSourceParentReadout (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;
+}
+
+// create source parents from ruleSrc for ruleOut for orphaned children for this readout.  XXX currently, this is only
+// used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be called
+// repeatedly)
+
+// XXX this function will do bad things if called repeatedly.  It needs to either (1) delete
+// the existing DETECTION container on the output images, or (2) intelligently supplement the
+// existing DETECTION container with only the new detections.
+bool psphotSourceParentReadout (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 *detectionsSrc = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detectionsSrc, "missing detections?");
+
+    psArray *sourcesSrc = detectionsSrc->allSources;
+    psAssert (sourcesSrc, "missing sources?");
+
+    // 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?");
+
+    // generate a new detection structure for the output filerule
+    pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detectionsOut, "missing PSPHOT.DETECTIONS?");
+
+    // loop over the sources, redefine their pixels to point at the new filerule image,
+    // copy the source data, and add a reference back to the original source
+    
+    // copy the sources from sourceSrcs to the new detection structure
+    for (int i = 0; i < sourcesSrc->n; i++) {
+      pmSource *sourceSrc = sourcesSrc->data[i];
+      if (sourceSrc->parent) continue; // I am not an orphan
+
+      pmSource *sourceOut = pmSourceCopy(sourceSrc);
+      sourceOut->parent = sourceSrc;
+      
+      // keep the original source flags
+      sourceOut->seq      = sourceSrc->seq;
+      sourceOut->type     = sourceSrc->type;
+      sourceOut->mode     = sourceSrc->mode;
+      sourceOut->mode2    = sourceSrc->mode2;
+      sourceOut->tmpFlags = sourceSrc->tmpFlags;
+
+      // does this copy all model data? (NO)
+      sourceOut->modelPSF = pmModelCopy(sourceSrc->modelPSF);
+      sourceOut->modelEXT = pmModelCopy(sourceSrc->modelEXT);
+
+      if (sourceSrc->modelFits) {
+	  sourceOut->modelFits = psArrayAlloc(sourceSrc->modelFits->n);
+	  for (int j = 0; j < sourceSrc->modelFits->n; j++) {
+	      sourceOut->modelFits->data[j] = pmModelCopy(sourceSrc->modelFits->data[j]);
+	  }
+      }
+
+      // drop the references to the original image pixels:
+      pmSourceFreePixels (sourceOut);
+
+      // XXX do we need to skip the Chisq image sources?
+
+      // allocate image, weight, mask for the new image for each peak
+      if (sourceOut->modelPSF) {
+	pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
+      }
+
+      // child sources have not been subtracted in this image, but this flag may be raised if
+      // they were subtracted in the parent's image
+      sourceOut->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
+
+      psArrayAdd (detectionsOut->allSources, 100, sourceOut);
+      psFree (sourceOut);
+    }
+    psLogMsg ("psphot", 3, "%ld known sources supplied", detectionsOut->allSources->n);
+
+
+    return true;
+}
+
 // create source children from ruleSrc for ruleOut
 bool psphotSourceChildren (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
@@ -545,4 +654,8 @@
 // used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be called
 // repeatedly)
+
+// XXX this function will do bad things if called repeatedly.  It needs to either (1) delete
+// the existing DETECTION container on the output images, or (2) intelligently supplement the
+// existing DETECTION container with only the new detections.
 bool psphotSourceChildrenReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
 
@@ -593,4 +706,5 @@
       
       // keep the original source flags
+      sourceOut->seq      = sourceSrc->seq;
       sourceOut->type     = sourceSrc->type;
       sourceOut->mode     = sourceSrc->mode;
@@ -612,4 +726,6 @@
       pmSourceFreePixels (sourceOut);
 
+      // XXX do we need to skip the Chisq image sources?
+
       // allocate image, weight, mask for the new image for each peak
       if (sourceOut->modelPSF) {
@@ -630,7 +746,7 @@
 }
 
-// create source children associated with 'filerule' from the objectsSrc.  XXX currently, this
-// is only used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be
-// called repeatedly)
+// create source children associated with 'filerule' from the objectsSrc.  returns a new object
+// array containing the child sources.  XXX currently, this is only used by psphotStackReadout
+// (sources go on allSources so that psphotChoosePSF can be called repeatedly)
 psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc) {
 
@@ -652,4 +768,5 @@
 	psAssert (readout, "missing readout?");
 
+	// create DETECTIONS containers for each image, in case one lacks it
 	pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
 	if (!detections) {
@@ -665,4 +782,6 @@
 	    psAssert (detections, "missing detections?");
 	}
+
+	// we need to save the new sources on the detection arrays of the appropriate image
 	detArrays->data[i] = psMemIncrRefCounter(detections);
 	readouts->data[i] = psMemIncrRefCounter(readout);
@@ -695,7 +814,8 @@
 
 	    pmSource *sourceOut = pmSourceCopy(sourceSrc);
+	    sourceOut->parent = sourceSrc;
+
+	    // save on the output object array at the same location
 	    objectOut->sources->data[i] = sourceOut;
-
-	    sourceOut->parent = sourceSrc;
 
 	    // keep the original source flags and sequence ID (if set)
@@ -720,5 +840,5 @@
 	    pmSourceFreePixels (sourceOut);
 
-	    // set the output readotu
+	    // set the output readout
 	    int index = sourceOut->imageID;
 	    if (index >= readouts->n) continue; // skip the sources generated by the chisq image
Index: /branches/eam_branches/ipp-20111110/psphot/src/psphotStackReadout.c
===================================================================
--- /branches/eam_branches/ipp-20111110/psphot/src/psphotStackReadout.c	(revision 32685)
+++ /branches/eam_branches/ipp-20111110/psphot/src/psphotStackReadout.c	(revision 32686)
@@ -115,5 +115,7 @@
     }
 
-    // if DET and SRC are different images, copy the detections from DET to SRC 
+    // If DET and SRC are different images, copy the detections from DET to SRC.  This 'copy'
+    // is just a copy of the container pointer; the sources on both DET and SRC are the same
+    // memory objects
     if (strcmp(STACK_SRC, STACK_DET)) {
 	if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
@@ -197,5 +199,5 @@
     // linear fit to include all sources (subtract again)
     // NOTE : apply to ALL sources (extended + psf)
-    // NOTE 2 : this function subtracts the models from the given filerule (SRC)
+    // NOTE 2 : this function subtracts the models from the given filerule (SRC), not DET
     psphotFitSourcesLinear (config, view, STACK_SRC, true); // pass 2 (detections->allSources)
 
@@ -204,6 +206,13 @@
     // NOTE: this block performs the 2nd pass low-significance PSF detection stage
     { 
+	// XXX the steps below to subtract the sources from DET should be skipped if SRC == DET!
+
+	// generate children sources for all sources in the SRC image
+	// XXX (DROP existing detections list) 
+	psphotSourceChildren (config, view, STACK_DET, STACK_SRC);
+
 	//  subtract all sources from DET (this will subtract using the psf model for SRC, which
 	//  will somewhat oversubtract the sources -- this is OK
+	// *** this fails because the source->pixels are still pointing at SRC, not DET
 	psphotRemoveAllSources (config, view, STACK_DET, true); // ignore subtraction state for sources
 
@@ -222,5 +231,12 @@
 	// (this operation just ensures the metadata container has a view on SRC as well
 	if (strcmp(STACK_SRC, STACK_DET)) {
-	    if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
+	    // XXX this operation now needs to create source parents for the new detections
+	    // XXX if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
+	    // XXX 	psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
+	    // XXX 	return psphotReadoutCleanup (config, view, STACK_SRC);
+	    // XXX }
+	    
+	    // the old detection here are children of SRC; generate parents for the new detections
+	    if (!psphotSourceParents (config, view, STACK_SRC, STACK_DET)) {
 		psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
 		return psphotReadoutCleanup (config, view, STACK_SRC);
