Index: /tags/ipp-20110406/ippconfig/gpc1/psastro.config
===================================================================
--- /tags/ipp-20110406/ippconfig/gpc1/psastro.config	(revision 31333)
+++ /tags/ipp-20110406/ippconfig/gpc1/psastro.config	(revision 31334)
@@ -16,6 +16,6 @@
 PSASTRO.GRID.SCALE     F32      50
 PSASTRO.GRID.NSTAR.MAX S32     800 # deprecated 
-PSASTRO.GRID.NREF.MAX  S32     800 # max stars accepted for fitting from ref catalog
-PSASTRO.GRID.NRAW.MAX  S32    1200 # max stars accepted for fitting from raw image
+PSASTRO.GRID.NREF.MAX  S32     800 # max stars accepted for grid search from ref catalog
+PSASTRO.GRID.NRAW.MAX  S32    1200 # max stars accepted for grid search from raw image
 
 PSASTRO.MAX.NRAW      S32      1000   # max stars accepted for fitting (0 for all)
@@ -28,4 +28,6 @@
 PSASTRO.MIN.INST.MAG.RAW       F32     -15.0   # min instrumental magnitude for stars accepted for fitting
 PSASTRO.MAX.INST.MAG.RAW       F32      -8.0   # max instrumental magnitude for stars accepted for fitting
+PSASTRO.GRID.MIN.INST.MAG.RAW  F32     -15.0   # min instrumental magnitude for stars accepted for grid search
+PSASTRO.GRID.MAX.INST.MAG.RAW  F32      -8.0   # max instrumental magnitude for stars accepted for grid search
 
 PSASTRO.GRID.MIN.ANGLE F32 -1.0 # start angle (degrees)
@@ -314,7 +316,16 @@
   # allow more stars per chip and boost the density to which we query
   # from the reference database.
-  PSASTRO.MAX.NRAW      S32      2000   # 
-  PSASTRO.MAX.NREF      S32      8000   # max stars accepted for fitting (0 for all)
-  DVO.GETSTAR.MAX.RHO   F32      60000.0
+  PSASTRO.MAX.NRAW               S32      2000   # 
+  PSASTRO.MAX.NREF               S32      8000   # max stars accepted for fitting (0 for all)
+  # use wider range of magnitudes for grid search
+  PSASTRO.GRID.MIN.INST.MAG.RAW  F32     -15.0   # min instrumental magnitude for stars accepted for fitting
+  PSASTRO.GRID.MAX.INST.MAG.RAW  F32     -11.0   # max instrumental magnitude for stars accepted for fitting
+
+  # reduce the magnitude range to avoid the bright stars have bad astrometry problem
+  PSASTRO.MIN.INST.MAG.RAW       F32     -12.0   # min instrumental magnitude for stars accepted for fitting
+  PSASTRO.MAX.INST.MAG.RAW       F32     -11.0   # min instrumental magnitude for stars accepted for fitting
+
+  DVO.GETSTAR.MIN.MAG.INST   F32     -15.0
+  DVO.GETSTAR.MAX.RHO        F32      60000.0
   
   # the crowding is high, so unless we restrict somewhat tightly early
@@ -324,5 +335,5 @@
 
   # single-chip radius match in pixels
-  PSASTRO.MATCH.RADIUS.N0 F32    10
+  PSASTRO.MATCH.RADIUS.N0 F32   10
   PSASTRO.MATCH.RADIUS.N1 F32    5
   PSASTRO.MATCH.RADIUS.N2 F32    5
Index: /tags/ipp-20110406/ippconfig/recipes/psastro.config
===================================================================
--- /tags/ipp-20110406/ippconfig/recipes/psastro.config	(revision 31333)
+++ /tags/ipp-20110406/ippconfig/recipes/psastro.config	(revision 31334)
@@ -83,4 +83,6 @@
 PSASTRO.MIN.INST.MAG.RAW       F32      0.0   # min instrumental magnitude for stars accepted for fitting
 PSASTRO.MAX.INST.MAG.RAW       F32      0.0   # max instrumental magnitude for stars accepted for fitting
+PSASTRO.GRID.MIN.INST.MAG.RAW       F32      0.0   # min instrumental magnitude for stars accepted for grid search
+PSASTRO.GRID.MAX.INST.MAG.RAW       F32      0.0   # max instrumental magnitude for stars accepted for grid search
 
 PSASTRO.MATCH.LUMFUNC  BOOL     FALSE
Index: /tags/ipp-20110406/psastro/src/psastroAstromGuess.c
===================================================================
--- /tags/ipp-20110406/psastro/src/psastroAstromGuess.c	(revision 31333)
+++ /tags/ipp-20110406/psastro/src/psastroAstromGuess.c	(revision 31334)
@@ -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: /tags/ipp-20110406/psastro/src/psastroChipAstrom.c
===================================================================
--- /tags/ipp-20110406/psastro/src/psastroChipAstrom.c	(revision 31333)
+++ /tags/ipp-20110406/psastro/src/psastroChipAstrom.c	(revision 31334)
@@ -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: /tags/ipp-20110406/psastro/src/psastroConvert.c
===================================================================
--- /tags/ipp-20110406/psastro/src/psastroConvert.c	(revision 31333)
+++ /tags/ipp-20110406/psastro/src/psastroConvert.c	(revision 31334)
@@ -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: /tags/ipp-20110406/psastro/src/psastroRemoveClumps.c
===================================================================
--- /tags/ipp-20110406/psastro/src/psastroRemoveClumps.c	(revision 31333)
+++ /tags/ipp-20110406/psastro/src/psastroRemoveClumps.c	(revision 31334)
@@ -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);
 	    }
 	}
