Index: /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c	(revision 26611)
+++ /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c	(revision 26612)
@@ -21,4 +21,6 @@
 
 #include "ppSub.h"
+
+psArray *ppSubSelectPSFSources(psArray *sources);
 
 bool ppSubMakePSF(ppSubData *data)
@@ -79,5 +81,9 @@
     // Here, we assume the image is background-subtracted
     psArray *sources = psMetadataLookupPtr(&mdok, minuend->analysis, "PSPHOT.SOURCES");
-    if (!psphotReadoutFindPSF(config, view, sources)) {
+    
+    // XXX filter sources?  limit the total number and return only brighter objects?
+    // use flags to toss totally bogus entries?
+    psArray *goodSources = ppSubSelectPSFSources (sources);
+    if (!psphotReadoutFindPSF(config, view, goodSources)) {
         // This is likely a data quality issue
         // XXX Split into multiple cases using error codes?
@@ -86,4 +92,5 @@
         ppSubDataQuality(data, PSPHOT_ERR_PSF, PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
         psFree(view);
+	psFree(goodSources);
         return true;
     }
@@ -100,4 +107,5 @@
     psMetadataRemoveKey(photRO->analysis, "PSPHOT.HEADER");
 
+    psFree(goodSources);
     psFree(view);
 
@@ -156,2 +164,30 @@
     return true;
 }
+
+
+// XXX hardwired MAX for now
+# define MAX_NPSF 500
+
+psArray *ppSubSelectPSFSources(psArray *sources){ 
+
+    sources = psArraySort (sources, pmSourceSortBySN);
+
+    psArray *subset = psArrayAllocEmpty(MAX_NPSF);
+
+    int nPSF = 0;
+    for (int i = 0; (nPSF < MAX_NPSF) && (i < sources->n); i++) {
+
+	pmSource *source = sources->data[i];
+	if (!source) continue;
+
+	// skip non-astronomical objects (very likely defects)
+	if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
+	if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
+	if (source->mode & PM_SOURCE_MODE_SATSTAR) continue;
+	
+	psArrayAdd (subset, 100, source);
+	nPSF++;
+    }
+    
+    return subset;
+}
