Index: trunk/psastro/src/psastroOneChipGrid.c
===================================================================
--- trunk/psastro/src/psastroOneChipGrid.c	(revision 26259)
+++ trunk/psastro/src/psastroOneChipGrid.c	(revision 31161)
@@ -28,19 +28,33 @@
 
     // do we need to get a rough initial match?
-    REQUIRED_RECIPE_VALUE (int maxNstar, "PSASTRO.GRID.NSTAR.MAX", S32);
-
-    // generate the bright subset of maxNstar entries (note: rawstars is sorted by S/N)
-    psArray *gridStars = psArrayAlloc (PS_MIN (maxNstar, rawstars->n));
-    for (int i = 0; (i < maxNstar) && (i < rawstars->n); i++) {
-	gridStars->data[i] = psMemIncrRefCounter (rawstars->data[i]);
+    int nMaxRaw = psMetadataLookupS32(&status, recipe, "PSASTRO.GRID.NRAW.MAX");
+    if (!status) {
+	nMaxRaw = psMetadataLookupS32(&status, recipe, "PSASTRO.GRID.NSTAR.MAX");
+    }
+    int nMaxRef = psMetadataLookupS32(&status, recipe, "PSASTRO.GRID.NREF.MAX");
+    if (!status) {
+	nMaxRef = refstars->n;
     }
 
-    psLogMsg ("psastro", 3, "grid search using %ld raw vs %ld ref stars\n", gridStars->n, refstars->n);
+    // generate the bright subset of nMaxRaw entries of rawstars (already sorted by S/N)
+    psArray *rawGridStars = psArrayAlloc (PS_MIN (nMaxRaw, rawstars->n));
+    for (int i = 0; (i < nMaxRaw) && (i < rawstars->n); i++) {
+	rawGridStars->data[i] = psMemIncrRefCounter (rawstars->data[i]);
+    }
+
+    // generate the bright subset of nMaxRef entries of refstars (already sorted by S/N)
+    psArray *refGridStars = psArrayAlloc (PS_MIN (nMaxRef, refstars->n));
+    for (int i = 0; (i < nMaxRef) && (i < refstars->n); i++) {
+	refGridStars->data[i] = psMemIncrRefCounter (refstars->data[i]);
+    }
+
+    psLogMsg ("psastro", 3, "grid search using %ld raw vs %ld ref stars\n", rawGridStars->n, refGridStars->n);
 
     // find initial offset / rotation / scale
-    pmAstromStats *gridStats = pmAstromGridMatch (gridStars, refstars, recipe);
+    pmAstromStats *gridStats = pmAstromGridMatch (rawGridStars, refGridStars, recipe);
     if (gridStats == NULL) {
 	psLogMsg ("psastro", 3, "failed to find a grid match solution\n");
-	psFree (gridStars);
+	psFree (rawGridStars);
+	psFree (refGridStars);
 	return false;
     }
@@ -48,9 +62,10 @@
 
     // tweak the position by finding peak of matches stars
-    stats = pmAstromGridTweak (gridStars, refstars, recipe, gridStats);
+    stats = pmAstromGridTweak (rawGridStars, refGridStars, recipe, gridStats);
     if (stats == NULL) {
 	psLogMsg ("psastro", 3, "failed to measure tweaked grid solution\n");
 	psFree (gridStats);
-	psFree (gridStars);
+	psFree (rawGridStars);
+	psFree (refGridStars);
 	return false;
     }
@@ -61,5 +76,6 @@
     psastroUpdateChipToFPA (fpa, chip); // updates PSASTRO.RAWSTARS and PSASTRO.REFSTARS
     psFree (gridStats);
-    psFree (gridStars);
+    psFree (rawGridStars);
+    psFree (refGridStars);
     psFree (stats);
 
