Index: /branches/eam_branch_20080324/psphot/src/psphotExtendedSourceFits.c
===================================================================
--- /branches/eam_branch_20080324/psphot/src/psphotExtendedSourceFits.c	(revision 17348)
+++ /branches/eam_branch_20080324/psphot/src/psphotExtendedSourceFits.c	(revision 17349)
@@ -8,5 +8,5 @@
     int Nconvolve = 0;
     int Nplain = 0;
-    bool savePics = true;
+    bool savePics = false;
 
     // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
@@ -14,5 +14,5 @@
     assert (maskVal);
 
-    // perform full non-linear fits / extended source analysis?
+    // perform full extended source non-linear fits?
     if (!psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS")) {
 	psLogMsg ("psphot", PS_LOG_INFO, "skipping extended source measurements\n");
@@ -107,6 +107,17 @@
         Next ++;
 
+	// save the modelFlux here in case we need to subtract it (for failure)
+	psImage *modelFluxStart = psMemIncrRefCounter (source->modelFlux);
+
 	if (savePics) {
 	  psphotSaveImage (NULL, readout->image, "image.xp.fits");
+	}
+
+	// array to store the pointers to the model flux images while the models are being fitted
+	psArray *modelFluxes = psArrayAllocEmpty (4);
+    
+	// allocate the array to store the model fits
+	if (source->modelFits == NULL) {
+	    source->modelFits = psArrayAllocEmpty (4);
 	}
 
@@ -136,5 +147,6 @@
 	  assert (status);
 
-	  psTraceSetLevel ("psLib.math.psMinimizeLMChi2", 6);
+	  // XXX psTraceSetLevel ("psLib.math.psMinimizeLMChi2", 6);
+	  // XXX psTraceSetLevel ("psphot.psphotModelWithPSF_LMM", 6);
 
 	  // fit the model as convolved or not
@@ -145,5 +157,8 @@
 	    Nconvolve ++;
 	  } else {
+	      psFree (source->modelFlux);
+	      source->modelFlux = NULL;
 	    modelFit = psphotFitEXT (readout, source, modelType, maskVal);
+	    pmSourceCacheModel (source, maskVal);
 	    psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f\n", source->moments->x, source->moments->y, pmModelClassGetName (modelFit->type), modelFit->chisq);
 	    Nplain ++;
@@ -154,29 +169,10 @@
 	  } 
 
-	  if (savePics) {
-	    pmModel *saveModel = source->modelEXT;
-	    source->modelEXT = modelFit;
-	    pmSourceCacheModel (source, maskVal); // XXX put this in the source model function?
-	    pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
-
-	    psphotSaveImage (NULL, readout->image, "image.xf.fits");
-
-	    pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
-	    source->modelEXT = saveModel;
-
-	    char key[10];
-	    fprintf (stdout, "continue? ");
-	    fgets (key, 8, stdin);
-	    if (key[0] == 'n') {
-	      savePics = false;
-	    }
-	  }
-
-	  if (source->modelFits == NULL) {
-	      source->modelFits = psArrayAllocEmpty (5);
-	  }
-	  
+	  // save each of the model flux images and store the best
+	  psArrayAdd (modelFluxes, 4, source->modelFlux);
+
 	  // test for fit quality / result
-	  psArrayAdd (source->modelFits, 5, modelFit);
+	  psArrayAdd (source->modelFits, 4, modelFit);
+
 	  psFree (modelFit);
 	}
@@ -204,6 +200,13 @@
 	  // re-subtract the object, leave local sky
 	  psTrace ("psphot", 5, "failed to fit extended source model to object at %f, %f", source->moments->x, source->moments->y);
+
+	  // replace original model, subtract it
+	  psFree (source->modelFlux);
+	  source->modelFlux = modelFluxStart;
+
 	  pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
 	  source->mode |= PM_SOURCE_MODE_SUBTRACTED;
+
+	  psFree (modelFluxes);
 
 	  if (savePics) {
@@ -216,21 +219,25 @@
 	    }
 	  }
-
 	  continue;
 	}	
 
-	// XXX probably need to free the current value
+	// save the best extended model in modelEXT
 	psFree (source->modelEXT);
 	source->modelEXT = psMemIncrRefCounter (source->modelFits->data[minModel]);
 
-	psTrace ("psphot", 4, "best ext model for %f, %f : %s chisq = %f\n", source->moments->x, source->moments->y, pmModelClassGetName (source->modelEXT->type), source->modelEXT->chisq);
-
-	// cache only the chosen model
-	pmSourceCacheModel (source, maskVal); // XXX put this in the source model function?
-	psTrace ("psphot", 5, "psf-convolved model for source at %7.1f, %7.1f", source->moments->x, source->moments->y);
-
-        // re-subtract the object, leave local sky
+	// save the modelFlux for the best model
+	psFree (source->modelFlux);
+	source->modelFlux = psMemIncrRefCounter (modelFluxes->data[minModel]);
+
+        // subtract the best fit from the object, leave local sky
         pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
         source->mode |= PM_SOURCE_MODE_SUBTRACTED;
+
+	// the initial model flux is no longer needed
+	psFree (modelFluxStart);
+	psFree (modelFluxes);
+
+	psTrace ("psphot", 4, "best ext model for %f, %f : %s chisq = %f\n", source->moments->x, source->moments->y, pmModelClassGetName (source->modelEXT->type), source->modelEXT->chisq);
+	psTrace ("psphot", 5, "extended source model for source at %7.1f, %7.1f", source->moments->x, source->moments->y);
 
 	if (savePics) {
