Index: branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c
===================================================================
--- branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c	(revision 30174)
+++ branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c	(revision 30175)
@@ -166,10 +166,8 @@
     for (int i = 0; i < sources->n; i++) {
         pmSource *source = sources->data[i];
-        if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
-            // keep NSTARS PSF stars, unmark the rest
+        if (source->tmpFlags & PM_SOURCE_TMPF_CANDIDATE_PSFSTAR) {
+            // keep NSTARS PSF stars
             if (stars->n < NSTARS) {
                 psArrayAdd (stars, 200, source);
-            } else {
-                source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
             }
         }
@@ -214,9 +212,5 @@
         psFree(options);
 
-        // unset the PSFSTAR flags (none are used):
-        for (int i = 0; i < sources->n; i++) {
-            pmSource *source = sources->data[i];
-            source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-        }
+        // no sources are used as PSF stars
 
         // XXX set sxx, etc from FWHM in recipe
@@ -292,9 +286,5 @@
         psLogMsg ("psphot.pspsf", PS_LOG_INFO, "Using guess PSF model");
 
-        // unset the PSFSTAR flags (none are used):
-        for (int i = 0; i < sources->n; i++) {
-            pmSource *source = sources->data[i];
-            source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-        }
+        // no sources are used as PSF stars
 
         // XXX set sxx, etc from FWHM in recipe
@@ -322,43 +312,17 @@
     pmPSFtry *try = models->data[bestN];
 
-    // unset the PSFSTAR flag for stars not used for PSF model
-    // XXX a more efficient way of achieving this would be to record a pair of arrays
-    // of the source index and the source id for the psf stars.  but that would require we do
-    // not re-sort the source list in the meanwhile
-    int nDrop = 0;
+    // set the PSFSTAR flag for stars used for the PSF model
+    int nKeep = 0;
     for (int i = 0; i < try->sources->n; i++) {
         pmSource *source = try->sources->data[i];
-        if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
-	    // need to find this source in the original list (these are copies, not pointers)
-	    for (int j = 0; j < sources->n; j++) {
-		pmSource *realSource = sources->data[j];
-		if (realSource->id != source->id) continue;
-		realSource->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-		source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
-		nDrop ++;
-		break;
-	    }
-        }
-    }
-    // fprintf (stderr, "drop %d stars as PSF stars\n", nDrop);
-
-    // XXX is this working?
-    // int N1 = 0;
-    // for (int i = 0; i < try->sources->n; i++) {
-    //     pmSource *source = try->sources->data[i];
-    // 	fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
-    // 	if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
-    // 	    N1 ++;
-    //     }
-    // }
-    // int N2 = 0;
-    // for (int i = 0; i < sources->n; i++) {
-    //     pmSource *source = sources->data[i];
-    // 	fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
-    // 	if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
-    // 	    N2 ++;
-    //     }
-    // }
-    // fprintf (stderr, "N1: %d, N2: %d\n", N1, N2);
+        if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) continue;
+
+	// this source was used: find the parent and set its PSFSTAR flag
+	pmSource *realSource = source->parent;
+	psAssert (realSource, "pmPSFtryAlloc should have set the parent pointers");
+	realSource->mode |= PM_SOURCE_MODE_PSFSTAR;
+	nKeep ++;
+    }
+    psLogMsg ("psphot.pspsf", PS_LOG_DETAIL, "used %d of %ld candidate PSF objects\n", nKeep, try->sources->n);
 
     // build a PSF residual image
@@ -386,5 +350,5 @@
     }
 
-    // XXX test dump of psf star data and psf-subtracted image
+    // test dump of psf star data and psf-subtracted image
     if (psTraceGetLevel("psphot.psfstars") > 5) {
         psphotDumpPSFStars (readout, try, options->fitRadius, maskVal, markVal);
@@ -413,7 +377,6 @@
         return false;
     }
-    psFree (psf); // XXX double-check
-
-    // move into psphotChoosePSF
+    psFree (psf);
+
     psphotVisualShowPSFModel (readout, psf);
 
