Index: branches/simmosaic_branches/psphot/src/psphotDeblendSatstars.c
===================================================================
--- branches/simmosaic_branches/psphot/src/psphotDeblendSatstars.c	(revision 24860)
+++ branches/simmosaic_branches/psphot/src/psphotDeblendSatstars.c	(revision 27839)
@@ -1,14 +1,58 @@
 # include "psphotInternal.h"
 
-bool psphotDeblendSatstars (psArray *sources, psMetadata *recipe) {
+// for now, let's store the detections on the readout->analysis for each readout
+bool psphotDeblendSatstars (pmConfig *config, const pmFPAview *view)
+{
+    bool status = true;
+
+    int num = psMetadataLookupS32 (&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 (!psphotDeblendSatstarsReadout (config, view, "PSPHOT.INPUT", i)) {
+            psError (PSPHOT_ERR_CONFIG, false, "failed on saturated star deblend analysis for PSPHOT.INPUT entry %d", i);
+	    return false;
+	}
+    }
+    return true;
+}
+
+bool psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int fileIndex) {
 
     int N;
     pmSource *source;
+    bool status;
 
     psTimerStart ("psphot.deblend.sat");
 
     int Nblend = 0;
-    float SAT_TEST_LEVEL = 50000;
     float SAT_MIN_RADIUS = 5.0;
+
+    // find the currently selected readout
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, fileIndex); // File of interest
+    psAssert (file, "missing file?");
+
+    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+    psAssert (readout, "missing readout?");
+
+    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
+    psAssert (detections, "missing detections?");
+
+    psArray *sources = detections->newSources;
+    psAssert (sources, "missing sources?");
+
+    if (!sources->n) {
+	psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping satstar blend");
+	return true;
+    }
+
+    // select the appropriate recipe information
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
+    psAssert (recipe, "missing recipe?");
+
+    pmCell *cell = readout->parent;
+    float SATURATION = 0.75*psMetadataLookupF32 (&status, cell->concepts, "CELL.SATURATION");
+    float SAT_TEST_LEVEL = 0.5*SATURATION;
 
     // we need sources spatially-sorted to find overlaps
@@ -32,5 +76,5 @@
         // XXX filter? if (source->mode & PM_SOURCE_MODE_SATSTAR) continue;
         if (source->mode & PM_SOURCE_MODE_BLEND) continue;
-        if (source->peak->flux < SAT_TEST_LEVEL) continue;
+        if (source->peak->flux < SATURATION) continue;
 
 	// save these for reference below
@@ -45,6 +89,8 @@
 	psVector *xVec = contour->data[0];
 	psVector *yVec = contour->data[1];
-
-	// XXX should we filter based on the number of pixels in the contour?
+	if (xVec->n < 5) {
+	    psFree(contour);
+	    continue;
+	}
 
 	// find the center of the contour (let's just use mid[x,y])
@@ -62,4 +108,9 @@
 	int yCenter = 0.5*(yMin + yMax);
 	psFree (contour);
+
+	psAssert (xCenter >= source->pixels->col0, "invalid shift in object center");
+	psAssert (xCenter <  source->pixels->col0 + source->pixels->numCols, "invalid shift in object center");
+	psAssert (yCenter >= source->pixels->row0, "invalid shift in object center");
+	psAssert (yCenter <  source->pixels->row0 + source->pixels->numRows, "invalid shift in object center");
 
 	// reset the peak for this source to the value of the center pixel
