Index: trunk/psphot/src/psphotReadout.c
===================================================================
--- trunk/psphot/src/psphotReadout.c	(revision 18834)
+++ trunk/psphot/src/psphotReadout.c	(revision 18865)
@@ -62,10 +62,10 @@
         return psphotReadoutCleanup (config, readout, recipe, detections, psf, NULL);
     }
- 
+
     // XXX test write out the footprint image
     if (0) {
-	psImage *footprintImage = pmSetFootprintArrayIDs (detections->footprints, true);
-	psphotSaveImage (NULL, footprintImage, "footprints.1.fits");
-	psFree (footprintImage);
+        psImage *footprintImage = pmSetFootprintArrayIDs (detections->footprints, true);
+        psphotSaveImage (NULL, footprintImage, "footprints.1.fits");
+        psFree (footprintImage);
     }
 
@@ -112,4 +112,31 @@
     }
 
+    // Define source fitting parameters for everything that follows PSF fits
+    // This allows different parameters to be used from the PSF fitting
+    {
+        bool status = true;             // Status of MD lookup
+        int fitIter = psMetadataLookupS32(&status, recipe, "EXT_FIT_ITER"); // Max number of fit iterations
+        if (!status || fitIter <= 0) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "EXT_FIT_ITER is not positive");
+            return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
+        }
+        float fitTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_TOL"); // Fit tolerance
+        if (!status || !isfinite(fitTol) || fitTol <= 0) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "EXT_FIT_TOL is not positive");
+            return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
+        }
+        bool poisson = psMetadataLookupBool(&status, recipe, "POISSON.ERRORS.PHOT.LMM"); // Poisson errors?
+        if (!status) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "POISSON.ERRORS.PHOT.LMM is not defined");
+            return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
+        }
+        float skySig = psMetadataLookupF32(&status, recipe, "SKY_SIG");
+        if (!status || !isfinite(skySig) || skySig <= 0) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "SKY_SIG is not positive");
+            return psphotReadoutCleanup(config, readout, recipe, detections, psf, sources);
+        }
+        pmSourceFitModelInit(fitIter, fitTol, PS_SQR(skySig), poisson);
+    }
+
     // include externally-supplied sources
     psphotLoadExtSources (config, view, sources);
@@ -124,22 +151,22 @@
     psphotFitSourcesLinear (readout, sources, recipe, psf, FALSE);
 
-    if (0) { 
-	FILE *out = fopen ("out.pass1.dat", "w");
-	
-	for (int i = 0; i < sources->n; i++) {
-	    pmSource *source = sources->data[i];
-	    if (!source) continue;
-	    pmPeak *peak = source->peak;
-	    if (!peak) continue;
-	    pmModel *model = source->modelPSF;
-	    if (!model) continue;
-	    if (!model->params) continue;
-	    
-	    fprintf (out, "%d %f %f  %f %f  %f %f\n", 
-		     i, peak->xf, peak->yf, peak->flux, peak->SN,
-		     model->params->data.F32[PM_PAR_I0],
-		     model->dparams->data.F32[PM_PAR_I0]);
-	}
-	fclose (out);
+    if (0) {
+        FILE *out = fopen ("out.pass1.dat", "w");
+
+        for (int i = 0; i < sources->n; i++) {
+            pmSource *source = sources->data[i];
+            if (!source) continue;
+            pmPeak *peak = source->peak;
+            if (!peak) continue;
+            pmModel *model = source->modelPSF;
+            if (!model) continue;
+            if (!model->params) continue;
+
+            fprintf (out, "%d %f %f  %f %f  %f %f\n",
+                     i, peak->xf, peak->yf, peak->flux, peak->SN,
+                     model->params->data.F32[PM_PAR_I0],
+                     model->dparams->data.F32[PM_PAR_I0]);
+        }
+        fclose (out);
     }
 
@@ -178,7 +205,7 @@
     // XXX test write out the footprint image
     if (0) {
-	psImage *footprintImage = pmSetFootprintArrayIDs (detections->footprints, true);
-	psphotSaveImage (NULL, footprintImage, "footprints.2.fits");
-	psFree (footprintImage);
+        psImage *footprintImage = pmSetFootprintArrayIDs (detections->footprints, true);
+        psphotSaveImage (NULL, footprintImage, "footprints.2.fits");
+        psFree (footprintImage);
     }
 
@@ -208,22 +235,22 @@
     psphotFitSourcesLinear (readout, sources, recipe, psf, TRUE);
 
-    if (0) { 
-	FILE *out = fopen ("out.pass2.dat", "w");
-	
-	for (int i = 0; i < sources->n; i++) {
-	    pmSource *source = sources->data[i];
-	    if (!source) continue;
-	    pmPeak *peak = source->peak;
-	    if (!peak) continue;
-	    pmModel *model = source->modelPSF;
-	    if (!model) continue;
-	    if (!model->params) continue;
-	    
-	    fprintf (out, "%d %f %f  %f %f  %f %f\n", 
-		     i, peak->xf, peak->yf, peak->flux, peak->SN,
-		     model->params->data.F32[PM_PAR_I0],
-		     model->dparams->data.F32[PM_PAR_I0]);
-	}
-	fclose (out);
+    if (0) {
+        FILE *out = fopen ("out.pass2.dat", "w");
+
+        for (int i = 0; i < sources->n; i++) {
+            pmSource *source = sources->data[i];
+            if (!source) continue;
+            pmPeak *peak = source->peak;
+            if (!peak) continue;
+            pmModel *model = source->modelPSF;
+            if (!model) continue;
+            if (!model->params) continue;
+
+            fprintf (out, "%d %f %f  %f %f  %f %f\n",
+                     i, peak->xf, peak->yf, peak->flux, peak->SN,
+                     model->params->data.F32[PM_PAR_I0],
+                     model->dparams->data.F32[PM_PAR_I0]);
+        }
+        fclose (out);
     }
 
@@ -234,7 +261,7 @@
 
     if (0) {
-	psphotSaveImage (NULL, readout->mask, "mask.fits");
-    }
-	
+        psphotSaveImage (NULL, readout->mask, "mask.fits");
+    }
+
     psphotExtendedSourceAnalysis (readout, sources, recipe);
 
@@ -243,6 +270,6 @@
 finish:
 
-    // plot positive sources 
-    // psphotSourcePlots (readout, sources, recipe); 
+    // plot positive sources
+    // psphotSourcePlots (readout, sources, recipe);
 
     // measure aperture photometry corrections
