Index: branches/eam_branches/ipp-20100621/psphot/src/Makefile.am
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/Makefile.am	(revision 28692)
+++ branches/eam_branches/ipp-20100621/psphot/src/Makefile.am	(revision 28702)
@@ -163,5 +163,4 @@
 	psphotOutput.c		       \
 	psphotFakeSources.c	       \
-	psphotModelWithPSF.c           \
 	psphotExtendedSourceAnalysis.c \
 	psphotExtendedSourceAnalysisByObject.c \
Index: branches/eam_branches/ipp-20100621/psphot/src/psphot.h
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphot.h	(revision 28692)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphot.h	(revision 28702)
@@ -426,5 +426,5 @@
 bool psphotStackObjectsUnifyPosition (psArray *objects);
 
-bool psphotFitSersicIndexPCM (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal);
+bool psphotFitSersicIndexPCM (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
 pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
 
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotFitSourcesLinear.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotFitSourcesLinear.c	(revision 28692)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotFitSourcesLinear.c	(revision 28702)
@@ -102,11 +102,11 @@
     }
     
-    float MIN_VALID_FLUX = psMetadataLookupBool(&status, recipe, "PSF_FIT_MIN_VALID_FLUX");
+    float MIN_VALID_FLUX = psMetadataLookupF32(&status, recipe, "PSF_FIT_MIN_VALID_FLUX");
     if (!status) {
         MIN_VALID_FLUX = 1e-8;
     }
-    float MAX_VALID_FLUX = psMetadataLookupBool(&status, recipe, "PSF_FIT_MAX_VALID_FLUX");
-    if (!status) {
-        MAX_VALID_FLUX = 1e-8;
+    float MAX_VALID_FLUX = psMetadataLookupF32(&status, recipe, "PSF_FIT_MAX_VALID_FLUX");
+    if (!status) {
+        MAX_VALID_FLUX = 1e+8;
     }
 
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c	(revision 28692)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c	(revision 28702)
@@ -479,4 +479,12 @@
 pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
 
+    // allocate the model
+    pmModel *model = pmModelAlloc(modelType);
+    if (!model) {
+	return NULL;
+    }
+
+    pmSourceFitOptions options = *fitOptions;
+
     if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
         psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
@@ -489,7 +497,9 @@
     // at this stage, skip Gaussian windowing, and do not clip pixels by S/N
     // this uses the footprint to judge both radius and aperture?
-    if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) return false;
-
-    pmSourceFitOptions options = *fitOptions;
+    if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) {
+	// XXX set some mask bit/
+        model->flags |= PM_MODEL_STATUS_BADARGS;
+	return model;
+    }
 
     NfitPCM ++;
@@ -500,10 +510,10 @@
     // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
 
-    pmPCMdata *pcm = pmPCMinit (source, &options, modelType, maskVal, psfSize);
+    pmPCMdata *pcm = pmPCMinit (source, &options, model, maskVal, psfSize);
     if (!pcm) {
 	psTrace ("psphot", 5, "failed to generate a model for source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
-	return NULL;
-    }
-    // XXX check for nDOF too small
+        model->flags |= PM_MODEL_STATUS_BADARGS; // XXX this is probably already set in pmPCMinit
+	return model;
+    }
 
     // use the source moments, etc to guess basic model parameters
@@ -512,5 +522,5 @@
     // for sersic models, use a grid search to choose an index, then float the params there
     if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
-    	psphotFitSersicIndexPCM (pcm, source, fitOptions, maskVal, markVal);
+    	psphotFitSersicIndexPCM (pcm, source, fitOptions, maskVal, markVal, psfSize);
     }
 
@@ -520,19 +530,19 @@
 	options.mode = PM_SOURCE_FIT_EXT;
     }
-    pmSourceFitPCM (source, PCM, &options, maskVal);
+    pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
 
     // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
-    return (PCM);
+    psFree (pcm);
+
+    return model;
 }
 
 // note that these should be 1/2n of the standard sersic index
 float indexGuess[] = {0.5, 0.33, 0.25, 0.167, 0.125, 0.083};
-define N_INDEX_GUESS 6
+# define N_INDEX_GUESS 6
 
 // A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
 // for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
-bool psphotFitSersicIndex (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
-
-    pmModel *model = pcm->modelConv;
+bool psphotFitSersicIndex (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal) {
 
     assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
@@ -544,12 +554,14 @@
     options.nIter = 3;
 
-    float xMin, chiSquare[N_INDEX_GUESS];
-    int iMin;
+    int iMin = -1;
+    float xMin = NAN;
+    float chiSquare[N_INDEX_GUESS];
 
     for (int i = 0; i < N_INDEX_GUESS; i++) {
 	model->params->data.F32[PM_PAR_7] = indexGuess[i];
-	pmSourceModelGuessPCM (pcm, source, maskVal, markVal);
-
-	pmSourceFitPCM (pcm, source, &options, maskVal);
+
+	model->modelGuess(model, source);
+	pmSourceFitModel (source, model, &options, maskVal);
+
 	chiSquare[i] = model->chisq;
 	if (i == 0) {
@@ -573,5 +585,7 @@
 // A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
 // for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
-bool psphotFitSersicIndexPCM (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal) {
+bool psphotFitSersicIndexPCM (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
+
+    pmModel *model = pcm->modelConv;
 
     assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
@@ -583,13 +597,17 @@
     options.nIter = 3;
 
-    float xMin, chiSquare[N_INDEX_GUESS];
-    int iMin;
-
-    // XXX we probably cannot be calling model->modelGuess() : this does not include the psf sigma
+    int iMin = -1;
+    float xMin = NAN;
+    float chiSquare[N_INDEX_GUESS];
 
     for (int i = 0; i < N_INDEX_GUESS; i++) {
 	model->params->data.F32[PM_PAR_7] = indexGuess[i];
+	
 	model->modelGuess(model, source);
-	pmSourceFitPCM (source, model, &options, maskVal);
+	pmSourceFitModel (source, model, &options, maskVal);
+
+	// pmSourceModelGuessPCM(pcm, source, maskVal, markVal);
+	// pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
+
 	chiSquare[i] = model->chisq;
 	if (i == 0) {
@@ -604,8 +622,11 @@
     }
 
+    assert (iMin >= 0);
+    
     model->flags = PM_MODEL_STATUS_NONE; // do not attempt to handle failures here, let the next iteration deal with it
     model->params->data.F32[PM_PAR_7] = indexGuess[iMin];
-    model->modelGuess(model, source);
-
-    return true;
-}
+
+    pmSourceModelGuessPCM(pcm, source, maskVal, markVal);
+
+    return true;
+}
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c	(revision 28692)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c	(revision 28702)
@@ -170,4 +170,5 @@
     psVector *ApErr = psVectorAllocEmpty (100, PS_TYPE_F32);
 
+    psImageMaskType markVal = options->markVal;
     psImageMaskType maskVal = options->maskVal | options->markVal;
 
@@ -295,4 +296,5 @@
     pmSourcePhotometryMode photMode = PM_SOURCE_PHOT_WEIGHT;
 
+    psImageMaskType markVal = options->markVal;
     psImageMaskType maskVal = options->maskVal | options->markVal;
 
@@ -364,7 +366,7 @@
         float nSigmaMYY = (Myy - psfClump->Y) / hypot(psfClump->dY, psfClump->Y*psfClump->Y*source->errMag);
 
-	fprintf (stderr, "%f %f : Mxx: %f, Myy: %f, dx: %f, dy: %f, psfMag: %f, apMag: %f, dMag: %f, errMag: %f, nSigmaMag: %f\n", 
+	fprintf (stderr, "%f %f : Mxx: %f, Myy: %f, dx: %f, dy: %f, psfMag: %f, apMag: %f, dMag: %f, errMag: %f, nSigmaMag: %f, nSigmaMxx: %f, nSigmaMyy: %f\n", 
 		 source->peak->xf, source->peak->yf, Mxx, Myy, source->peak->xf - source->moments->Mx, source->peak->yf - source->moments->My, 
-		 source->psfMag, apMag, dMag, source->errMag, nSigmaMAG);
+		 source->psfMag, apMag, dMag, source->errMag, nSigmaMAG, nSigmaMXX, nSigmaMYY);
 
         // partially-masked sources are more likely to be mis-measured PSFs
