Index: branches/eam_branches/ipp-20101205/psphot/src/psphotReplaceUnfit.c
===================================================================
--- branches/eam_branches/ipp-20101205/psphot/src/psphotReplaceUnfit.c	(revision 30173)
+++ branches/eam_branches/ipp-20101205/psphot/src/psphotReplaceUnfit.c	(revision 30174)
@@ -159,9 +159,8 @@
       float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
       float radius = source->modelPSF->fitRadius;
-      if (radius < 1) {
-	  fprintf (stderr, "!");
-      }
-
-      pmSourceRedefinePixels (source, readout, Xo, Yo, radius, true);
+
+      // force a redefine to this image
+      pmSourceFreePixels(source);
+      pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
     }
     return true;
@@ -220,24 +219,52 @@
       source = sources->data[i];
 
-      // sources have not yet been subtracted in this image (but this flag may be raised)
-      source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
-
-      // the guess central intensity comes from the peak:
-      float Io = source->peak->flux;
-      float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
-      float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
-      float radius = source->modelPSF->fitRadius;
-
-      // generate a model for this object with Io = 1.0
-      pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
-      if (modelPSF == NULL) {
-	  psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
-	  continue;
+      // *** we need to cache the 'best' model, and we have 3 cases:
+      // 1) model is the psf model --> generate from the new psf
+      // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
+      // 3) model is a convolved extended model --> re-generate
+
+      // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
+      bool isPSF = false;
+      pmModel *model = pmSourceGetModel(&isPSF, source);
+      float radius = model->fitRadius; // save for future use below
+
+      // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
+      if (isPSF || model->isPCM) {
+	  // the guess central intensity comes from the peak:
+	  float Io = source->peak->flux;
+	  float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
+	  float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
+
+	  // generate a model for this object with Io = 1.0
+	  pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
+	  if (modelPSF == NULL) {
+	      psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
+	      continue;
+	  }
+
+	  // set the source PSF model
+	  psFree (source->modelPSF);
+	  source->modelPSF = modelPSF;
+	  source->modelPSF->fitRadius = radius;
       }
 
-      // set the source PSF model
-      psFree (source->modelPSF);
-      source->modelPSF = modelPSF;
-      source->modelPSF->fitRadius = radius;
+      if (model->isPCM) {
+	  psAssert(false, "this section is not complete");
+
+	  pmSourceCachePSF (source, maskVal);
+
+# if (0)
+	  psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
+	  if (!psfKernel) { 
+	      psWarning ("no psf kernel");
+	  }
+
+	  psImage *modelFlux = getmodelflux(model);
+	  psImageConvolveFFT (source->modelFlux, modelFlux, NULL, 0, psfKernel);
+	  
+	  psFree (psfKernel);
+	  psFree (modelFlux);
+# endif
+      }
 
       pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
