Index: /branches/eam_branches/20091113/psastro/src/psastroMosaicAstrom.c
===================================================================
--- /branches/eam_branches/20091113/psastro/src/psastroMosaicAstrom.c	(revision 26195)
+++ /branches/eam_branches/20091113/psastro/src/psastroMosaicAstrom.c	(revision 26196)
@@ -20,5 +20,4 @@
 
     bool status;
-    char filename[256];
 
     // select the current recipe
@@ -54,21 +53,27 @@
     if (!status || !outroot) psAbort ("Can't find outroot on config->arguments");
 
-    if (!psastroMosaicFit (fpa, recipe, outroot, 0)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 1)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 2)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 3)) return false;
+    int nIter = psMetadataLookupS32 (&status, recipe, "PSASTRO.MOSAIC.CHIP.NITER");
+    if (!status) psAbort ("missing config value");
+
+    // this should be in a loop with nIter = 
+    for (int iter = 0; iter < nIter; iter++) {
+	if (!psastroMosaicFit (fpa, recipe, outroot, iter)) return false;
+    }
 
     // now fit the chips under the common distortion with higher-order terms
     // first, re-perform the match with a slightly tighter circle
-    if (!psastroMosaicSetMatch (fpa, recipe, 4)) {
+    if (!psastroMosaicSetMatch (fpa, recipe, nIter)) {
         psError(PSASTRO_ERR_UNKNOWN, false, "failed to match raw and ref stars for mosaic (4th pass)");
         return false;
     }
-    if (!psastroMosaicChipAstrom (fpa, recipe, 4)) {
+    if (!psastroMosaicChipAstrom (fpa, recipe, nIter)) {
         psError(PSASTRO_ERR_UNKNOWN, false, "failed to measure chip astrometry in mosaic mode (4th pass)");
         return false;
     }
+
     if (psTraceGetLevel("psastro.dump") > 0) {
-        snprintf (filename, 256, "%s.10.dat", outroot);
+	// the last filename (see filenames in psastroMosaicFit)
+	char filename[256];
+	snprintf (filename, 256, "%s.%d.dat", outroot, 2*nIter + 2);
         psastroDumpMatches (fpa, filename);
     }
