Index: /branches/eam_branches/psphot.20100506/src/Makefile.am
===================================================================
--- /branches/eam_branches/psphot.20100506/src/Makefile.am	(revision 27949)
+++ /branches/eam_branches/psphot.20100506/src/Makefile.am	(revision 27950)
@@ -99,4 +99,5 @@
 	psphotStackMatchPSFsPrepare.c \
 	psphotStackOptions.c          \
+	psphotStackObjects.c          \
 	psphotStackPSF.c	      \
 	psphotCleanup.c
Index: /branches/eam_branches/psphot.20100506/src/psphot.h
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27949)
+++ /branches/eam_branches/psphot.20100506/src/psphot.h	(revision 27950)
@@ -465,3 +465,5 @@
 bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule);
 
+bool psphotStackObjectsUnifyPosition (psArray *objects);
+
 #endif
Index: /branches/eam_branches/psphot.20100506/src/psphotExtendedSourceAnalysisByObject.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotExtendedSourceAnalysisByObject.c	(revision 27949)
+++ /branches/eam_branches/psphot.20100506/src/psphotExtendedSourceAnalysisByObject.c	(revision 27950)
@@ -1,9 +1,6 @@
 # include "psphotInternal.h"
 
-// ?? these cannot happen here --> we would need to do this in psphotExtendedSourceAnalysis
-// XXX option to choose a consistent position
 // XXX option to choose a consistent elliptical contour
 // XXX SDSS uses the r-band petrosian radius to measure petrosian fluxes in all bands
-// XXX consistent choice of extendedness...
 
 // aperture-like measurements for extended sources
@@ -64,5 +61,5 @@
 
     // source analysis is done in S/N order (brightest first)
-    // XXX add this in (need to put S/N in pmPhotObj) : objects = psArraySort (objects, pmPhotObjSortBySN);
+    objects = psArraySort (objects, pmPhotObjSortBySN);
 
     // process the objects in order.  
@@ -72,6 +69,10 @@
 	if (!object->sources) continue;
 
+	// we need to decide for an object if we are going to measure all sources or not
+	// simple rule : if *any* of the sources would be measured, measure the object
+
 	// choose the sources of interest
-	for (int j = 0; j < object->sources->n; j++) {
+	bool measureSource = false;
+	for (int j = 0; !measureSource && (j < object->sources->n); j++) {
 
 	    pmSource *source = object->sources->data[j];
@@ -88,4 +89,14 @@
 	    assert (source->peak); // how can a source not have a peak?
 	    if (source->peak->SN < SN_LIM) continue;
+	    measureSource = true;
+	}
+	if (!measureSource) continue;
+
+	// choose the sources of interest
+	for (int j = 0; j < object->sources->n; j++) {
+
+	    pmSource *source = object->sources->data[j];
+	    psAssert (source, "programming error"); // all entries in object->sources must exist, right?
+	    psAssert (source->peak, "programming error"); // how can a source not have a peak?
 
 	    // replace object in image
@@ -146,19 +157,2 @@
     return true;
 }
-
-// sort by X (ascending)
-# if (0)
-int pmPhotObjSortBySN (const void **a, const void **b)
-{
-    pmPhotObj *objA = *(pmPhotObj **)a;
-    pmPhotObj *objB = *(pmPhotObj **)b;
-
-    psF32 fA = objA->x;
-    psF32 fB = objB->x;
-
-    psF32 diff = fA - fB;
-    if (diff > FLT_EPSILON) return (+1);
-    if (diff < FLT_EPSILON) return (-1);
-    return (0);
-}
-# endif
Index: /branches/eam_branches/psphot.20100506/src/psphotRadialAperturesByObject.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotRadialAperturesByObject.c	(revision 27949)
+++ /branches/eam_branches/psphot.20100506/src/psphotRadialAperturesByObject.c	(revision 27950)
@@ -41,6 +41,5 @@
 
     // source analysis is done in S/N order (brightest first)
-    // XXX are we getting the objects out of order? does it matter?
-    // XXX objects = psArraySort (objects, pmPhotObjSortBySN);
+    objects = psArraySort (objects, pmPhotObjSortBySN);
 
     // generate look-up arrays for readouts
Index: /branches/eam_branches/psphot.20100506/src/psphotStackObjects.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackObjects.c	(revision 27950)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackObjects.c	(revision 27950)
@@ -0,0 +1,48 @@
+# include "psphotInternal.h"
+
+// set a consistent position
+bool psphotStackObjectsUnifyPosition (psArray *objects) {
+
+    // consistent position: AVERAGE or CHISQ?
+    for (int i = 0; i < objects->n; i++) {
+        pmPhotObj *object = objects->data[i];
+	if (!object) continue;
+	if (!object->sources) continue;
+
+	int npts = 0;
+	float x = 0.0;
+	float y = 0.0;
+	// measure the average position (weighted?)
+	for (int j = 0; j < object->sources->n; j++) {
+
+	    pmSource *source = object->sources->data[j];
+	    if (!source) continue;
+	    if (!source->peak) continue;
+
+	    x += source->peak->xf;
+	    y += source->peak->yf;
+	    npts ++;
+	}
+	if (npts == 0) continue;
+
+	x /= (float) npts;
+	y /= (float) npts;
+
+	// set the positions
+	for (int j = 0; j < object->sources->n; j++) {
+
+	    pmSource *source = object->sources->data[j];
+	    if (!source) continue;
+	    if (!source->peak) continue;
+
+	    source->peak->xf = x;
+	    source->peak->yf = y;
+	    npts ++;
+	}
+	object->x = x;
+	object->y = y;
+    }
+
+    psLogMsg ("psphot", PS_LOG_INFO, "updated positions\n");
+    return true;
+}
Index: /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c
===================================================================
--- /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c	(revision 27949)
+++ /branches/eam_branches/psphot.20100506/src/psphotStackReadout.c	(revision 27950)
@@ -73,8 +73,4 @@
     }
 
-    // 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)) {
@@ -90,5 +86,5 @@
     }
 
-    // *** generate the objects (which unify the sources from the different images)
+    // generate the objects (object unify the sources from the different images)
     psArray *objects = psphotMatchSources (config, view, STACK_OUT);
 
@@ -137,8 +133,4 @@
     }
 
-    // include externally-supplied sources
-    // XXX fix this in the new multi-input context
-    // psphotLoadExtSources (config, view, "PSPHOT.INPUT"); // pass 1
-
     // construct an initial model for each object, set the radius to fitRadius, set circular fit mask
     psphotGuessModels (config, view, STACK_OUT);
@@ -161,5 +153,7 @@
     }
 
+    psphotStackObjectsUnifyPosition (objects);
     psphotRadialAperturesByObject (config, objects, view, STACK_OUT); 
+
     psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_OUT); // pass 1 (detections->allSources)
     psphotExtendedSourceFits (config, view, STACK_OUT); // pass 1 (detections->allSources)
@@ -182,5 +176,4 @@
     psphotSourceFreePixels (config, view, STACK_OUT);
 
-    // XXX do this now or above?
     // remove chisq image from config->file:PSPHOT.INPUT (why?)
     psphotStackRemoveChisqFromInputs(config, STACK_RAW);
