Index: trunk/psastro/src/psastroAstromGuess.c
===================================================================
--- trunk/psastro/src/psastroAstromGuess.c	(revision 31161)
+++ trunk/psastro/src/psastroAstromGuess.c	(revision 31333)
@@ -161,4 +161,27 @@
                     psastroPlotRawstars (rawstars, fpa, chip, recipe);
                 }
+
+                // Next if we are using a different set of stars for grid search fill out their pmAstromObjs
+                psArray *grid_rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.GRID.RAWSTARS");
+                if (grid_rawstars == NULL || grid_rawstars == rawstars) { continue; }
+
+                for (int i = 0; i < grid_rawstars->n; i++) {
+                    pmAstromObj *raw = grid_rawstars->data[i];
+
+                    psPlaneTransformApply (raw->FP, chip->toFPA, raw->chip);
+                    psPlaneTransformApply (raw->TP, fpa->toTPA, raw->FP);
+                    psDeproject (raw->sky, raw->TP, fpa->toSky);
+
+                    // rationalize ra to sky range centered on boresite
+                    while (raw->sky->r < RAminSky) raw->sky->r += 2.0*M_PI;
+                    while (raw->sky->r > RAmaxSky) raw->sky->r -= 2.0*M_PI;
+
+                    RAmin = PS_MIN (raw->sky->r, RAmin);
+                    RAmax = PS_MAX (raw->sky->r, RAmax);
+
+                    DECmin = PS_MIN (raw->sky->d, DECmin);
+                    DECmax = PS_MAX (raw->sky->d, DECmax);
+                }
+                // XXX: should we plot grid_rawstars?
             }
         }
Index: trunk/psastro/src/psastroChipAstrom.c
===================================================================
--- trunk/psastro/src/psastroChipAstrom.c	(revision 31161)
+++ trunk/psastro/src/psastroChipAstrom.c	(revision 31333)
@@ -60,4 +60,16 @@
 
                 // select the raw objects for this readout
+                psArray *gridrawstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.GRID.RAWSTARS.SUBSET");
+                if (gridrawstars == NULL) {
+                    gridrawstars = rawstars;
+                } else {
+                    // the absolute minimum number of stars is 4 (for order = 1)
+                    if (gridrawstars->n < 4) {
+                        readout->data_exists = false;
+                        psLogMsg ("psastro", 3, "insufficient gird rawstars (%ld)", gridrawstars->n);
+                        continue;
+                    }
+                }
+
                 psArray *refstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.REFSTARS.SUBSET");
                 if (refstars == NULL) { continue; }
@@ -80,5 +92,5 @@
 
                 // XXX update the header with info to reflect the failure
-                if (!psastroOneChipGrid (fpa, chip, refstars, rawstars, recipe, updates)) {
+                if (!psastroOneChipGrid (fpa, chip, refstars, gridrawstars, recipe, updates)) {
                     readout->data_exists = false;
                     psLogMsg ("psastro", 3, "failed to find a solution\n");
Index: trunk/psastro/src/psastroConvert.c
===================================================================
--- trunk/psastro/src/psastroConvert.c	(revision 31161)
+++ trunk/psastro/src/psastroConvert.c	(revision 31333)
@@ -14,4 +14,6 @@
 // leak free 2006.04.27
 
+static psArray * chooseStars(psArray *inStars, char *listName, psArray *sources, psVector *index, int nMax, float iMagMin, float iMagMax, pmSourceMode skip);
+
 bool psastroConvertFPA (pmFPA *fpa, psMetadata *recipe) {
 
@@ -87,7 +89,36 @@
     }
 
+    psArray *rawStars = chooseStars(inStars, "", sources, index, PS_MIN(nMax, inStars->n), iMagMin, iMagMax, skip);
+    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.RAWSTARS", PS_DATA_ARRAY, "astrometry objects", rawStars);
+
+    bool gridSearch = psMetadataLookupBool (&status, recipe, "PSASTRO.GRID.SEARCH");
+    if (gridSearch) {
+        // See if different magnitude limits have been specified for grid search. If so, create a separate list of stars to use.
+        float iGridMagMax = psMetadataLookupF32 (&status, recipe, "PSASTRO.GRID.MAX.INST.MAG.RAW");
+        float iGridMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.GRID.MIN.INST.MAG.RAW");
+        int   nMaxGrid = psMetadataLookupS32 (&status, recipe, "PSASTRO.GRID.NRAW.MAX");
+    
+        // XXX Should we check PSASTRO.GRID.NRAW.MAX != PSASTRO.MAX.NRAW as well? It usually is smaller so that would cause
+        // us to always create a separate list. So I won't check.
+
+        if ((iGridMagMax != iMagMax) || (iGridMagMin != iMagMin)) {
+            psArray *gridStars = chooseStars(inStars, "grid search ", sources, index, PS_MIN(nMaxGrid, inStars->n), iGridMagMin, iGridMagMax, skip);
+            psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.GRID.RAWSTARS", PS_DATA_ARRAY, "astrometry objects for grid search", gridStars);
+            psFree(gridStars);
+        }
+    }
+
+    psFree (index);
+    psFree (inStars);
+    psFree (rawStars);
+
+    return true;
+}
+
+
+psArray * chooseStars(psArray *inStars, char *listName, psArray *sources, psVector *index, int nMax, float iMagMin, float iMagMax, pmSourceMode skip) {
     // choose the first nMax sources
     int j = 0;
-    psArray *rawStars = psArrayAlloc (PS_MIN (nMax, inStars->n));
+    psArray *rawStars = psArrayAlloc(nMax);
 
     float mMin = +100.0;
@@ -128,14 +159,8 @@
     rawStars->n = j;
 
-    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.RAWSTARS", PS_DATA_ARRAY, "astrometry objects", rawStars);
-
-    psLogMsg ("psastro", 4, "loaded %ld sources, using %ld of %ld good sources (inst mag: %f to %f)\n", sources->n, rawStars->n, inStars->n, mMin, mMax);
+    psLogMsg ("psastro", 4, "loaded %ld %ssources, using %ld of %ld good sources (inst mag: %f to %f)\n", sources->n, listName, rawStars->n, inStars->n, mMin, mMax);
     psLogMsg ("psastro", 4, "skip reasons: mode: %d, faint: %d, bright: %d, inf: %d\n", nModeSkip, nFaintSkip, nBrightSkip, nInfSkip);
 
-    psFree (index);
-    psFree (inStars);
-    psFree (rawStars);
-
-    return true;
+    return rawStars;
 }
 
Index: trunk/psastro/src/psastroRemoveClumps.c
===================================================================
--- trunk/psastro/src/psastroRemoveClumps.c	(revision 31161)
+++ trunk/psastro/src/psastroRemoveClumps.c	(revision 31333)
@@ -61,4 +61,11 @@
 		psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.RAWSTARS.SUBSET", PS_DATA_ARRAY, "astrometry objects", subset);
 		psFree (subset);
+
+                psArray *gridstars = psMetadataLookupPtr(&status, readout->analysis, "PSASTRO.GRID.RAWSTARS");
+                if ((gridstars == rawstars) || (gridstars == NULL)) { continue; }
+
+		psArray *gridstars_subset = psastroRemoveClumpsIterate(gridstars, 150, 3);
+		psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.GRID.RAWSTARS.SUBSET", PS_DATA_ARRAY, "astrometry objects", gridstars_subset);
+		psFree (gridstars_subset);
 	    }
 	}
