Index: branches/eam_branches/ipp-20100621/psphot/src/Makefile.am
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/Makefile.am	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/Makefile.am	(revision 28692)
@@ -168,5 +168,4 @@
 	psphotExtendedSourceFits.c     \
 	psphotKernelFromPSF.c	       \
-	psphotPSFConvModel.c	       \
 	psphotFitSet.c		       \
 	psphotSourceFreePixels.c       \
Index: branches/eam_branches/ipp-20100621/psphot/src/psphot.h
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphot.h	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphot.h	(revision 28692)
@@ -12,12 +12,4 @@
 
 #define PSPHOT_RECIPE_PSF_FAKE_ALLOW "PSF.FAKE.ALLOW" // Name for recipe component permitting fake PSFs
-
-// pmPCMData : PSF Convolved Model data storage structure
-typedef struct {
-    psImage *model;
-    psArray *dmodels;
-    psImage *modelConv;
-    psArray *dmodelsConv;
-} pmPCMData;
 
 // top-level psphot functions
@@ -293,33 +285,4 @@
 bool psphotRadialBins (psMetadata *recipe, pmSource *source, float radiusMax, float skynoise);
 
-// structures & functions to support psf-convolved model fitting
-
-// psf-convolved model fitting
-bool psphotModelWithPSF_LMM (
-    psMinimization *min,
-    psImage *covar,
-    psVector *params,
-    psMinConstraint *constraint,
-    pmSource *source,
-    const psKernel *psf,
-    psMinimizeLMChi2Func func);
-
-psF32 psphotModelWithPSF_SetABX(
-    psImage  *alpha,
-    psVector *beta,
-    const psVector *params,
-    const psVector *paramMask,
-    pmPCMData *pcm,
-    const pmSource *source,
-    const psKernel *psf,
-    psMinimizeLMChi2Func func);
-
-pmPCMData *pmPCMDataAlloc (
-    const psVector *params,
-    const psVector *paramMask,
-    pmSource *source);
-
-psImage *pmPCMDataSaveImage (pmPCMData *pcm);
-
 int psphotKapaOpen (void);
 bool psphotKapaClose (void);
@@ -463,3 +426,6 @@
 bool psphotStackObjectsUnifyPosition (psArray *objects);
 
+bool psphotFitSersicIndexPCM (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal);
+pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
+
 #endif
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotApResid.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotApResid.c	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotApResid.c	(revision 28692)
@@ -459,5 +459,5 @@
         psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
 
-        bool status = pmSourceMagnitudes (source, psf, photMode, maskVal);
+        bool status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
 
         // clear the mask bit
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotMagnitudes.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotMagnitudes.c	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotMagnitudes.c	(revision 28692)
@@ -186,5 +186,5 @@
         psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
 
-        status = pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
+        status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
         if (status && isfinite(source->apMag)) Nap ++;
 
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c	(revision 28692)
@@ -8,4 +8,5 @@
 static int NfitDBL = 0;
 static int NfitEXT = 0;
+static int NfitPCM = 0;
 
 bool psphotFitInit (int nThreads) {
@@ -440,4 +441,8 @@
 pmModel *psphotFitEXT (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
 
+    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);
+    }
+
     pmSourceFitOptions options = *fitOptions;
 
@@ -456,8 +461,4 @@
     }
 
-    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);
-    }
-
     // for sersic models, use a grid search to choose an index, then float the params there
     if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
@@ -476,5 +477,17 @@
 }
 
-pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
+pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
+
+    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);
+    }
+
+    float radius = psphotSetRadiusEXT (readout, source, markVal);
+
+    // XXX note that this changes the source moments that are published...
+    // recalculate the source moments using the larger extended-source moments radius
+    // 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;
@@ -487,18 +500,17 @@
     // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
 
-    // use the source moments, etc to guess basic model parameters
-    pmModel *EXT = pmSourceModelGuess (source, modelType);
-    if (!EXT) {
+    pmPCMdata *pcm = pmPCMinit (source, &options, modelType, 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;
     }
-
-    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);
-    }
+    // XXX check for nDOF too small
+
+    // use the source moments, etc to guess basic model parameters
+    pmSourceModelGuessPCM (pcm, source, maskVal, markVal);
 
     // for sersic models, use a grid search to choose an index, then float the params there
     if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
-    	psphotFitSersicIndexPCM (source, EXT, fitOptions, maskVal);
+    	psphotFitSersicIndexPCM (pcm, source, fitOptions, maskVal, markVal);
     }
 
@@ -508,8 +520,8 @@
 	options.mode = PM_SOURCE_FIT_EXT;
     }
-    pmSourceFitPCM (source, EXT, &options, maskVal);
+    pmSourceFitPCM (source, PCM, &options, maskVal);
 
     // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
-    return (EXT);
+    return (PCM);
 }
 
@@ -520,5 +532,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 psphotFitSersicIndex (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal) {
+bool psphotFitSersicIndex (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
+
+    pmModel *model = pcm->modelConv;
 
     assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
@@ -535,6 +549,7 @@
     for (int i = 0; i < N_INDEX_GUESS; i++) {
 	model->params->data.F32[PM_PAR_7] = indexGuess[i];
-	model->modelGuess(model, source);
-	pmSourceFitModel (source, model, &options, maskVal);
+	pmSourceModelGuessPCM (pcm, source, maskVal, markVal);
+
+	pmSourceFitPCM (pcm, source, &options, maskVal);
 	chiSquare[i] = model->chisq;
 	if (i == 0) {
@@ -570,4 +585,6 @@
     float xMin, chiSquare[N_INDEX_GUESS];
     int iMin;
+
+    // XXX we probably cannot be calling model->modelGuess() : this does not include the psf sigma
 
     for (int i = 0; i < N_INDEX_GUESS; i++) {
Index: branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c
===================================================================
--- branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c	(revision 28687)
+++ branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c	(revision 28692)
@@ -191,5 +191,5 @@
 
         // XXX can we test if psfMag is set and calculate only if needed?
-        pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
+        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
 
         // clear the mask bit
@@ -342,5 +342,5 @@
 
         // XXX can we test if psfMag is set and calculate only if needed?
-        pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
+        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
 
         // clear the mask bit
