Index: trunk/psastro/src/psastroConvert.c
===================================================================
--- trunk/psastro/src/psastroConvert.c	(revision 41367)
+++ trunk/psastro/src/psastroConvert.c	(revision 41434)
@@ -14,4 +14,10 @@
 // leak free 2006.04.27
 
+// To select good stars, I want them to have a PSFmodel fit (PM_SOURCE_MODE_PSFMODEL & PM_SOURCE_MODE_FITTED) and exlcude things that are CR, EXT, FAIL, POOR 
+// Stars are allowed to be saturated, as long as the PSF model fits
+# define PHOT_SOURCE_MASK (PM_SOURCE_MODE_FAIL | PM_SOURCE_MODE_BLEND | PM_SOURCE_MODE_BADPSF | \
+                           PM_SOURCE_MODE_DEFECT | PM_SOURCE_MODE_CR_LIMIT | PM_SOURCE_MODE_EXT_LIMIT | \
+                           PM_SOURCE_MODE_POOR) // Mask to apply to sources for rejection
+
 static psArray *chooseStars(psArray *inStars, char *listName, psArray *sources, psVector *index, int nMax, float iMagMin, float iMagMax, pmSourceMode skip);
 
@@ -142,4 +148,28 @@
         }
     }
+
+    //also select a sample of stars which are allowed to be saturated and bright, as long as they are well fit by a PSF model
+    int j = 0;
+    psArray *calStars = psArrayAlloc(inStars->n);
+
+    for (int i = 0; (i < inStars->n) && (j < rawStars->n); i++) {
+        int n = index->data.S32[i];
+        pmSource *source = sources->data[n];
+
+        // we only want to use stars which are not bad. Use the source->mode to check for the rejection mask while keeping fitted PSFmodel
+        if (source->mode & PHOT_SOURCE_MASK || !isfinite(source->psfMag)) {
+            continue;
+        }
+
+        //Ensure the source has a fitted PSF model
+        if (!(source->mode & PM_SOURCE_MODE_PSFMODEL) || !(source->mode & PM_SOURCE_MODE_FITTED) ) {
+            continue;
+        }
+
+        calStars->data[j] = psMemIncrRefCounter (inStars->data[n]);
+        j++;
+    }
+    calStars->n = j;
+    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.CALSTARS", PS_DATA_ARRAY, "astrometry objects for masking", calStars);
 
     psFree (index);
