Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotKronMasked.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotKronMasked.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotKronMasked.c	(revision 31328)
@@ -85,6 +85,13 @@
 
 	// generate the pixel masks
-	int Xo = source->moments->Mx;
-	int Yo = source->moments->My;
+	// int Xo = source->moments->Mx;
+	// int Yo = source->moments->My;
+	float dX = source->moments->Mx - source->peak->xf;
+	float dY = source->moments->My - source->peak->yf;
+	float dR = hypot(dX, dY);
+	
+	float Xo = (dR < 2.0) ? source->moments->Mx : source->peak->xf;
+	float Yo = (dR < 2.0) ? source->moments->My : source->peak->yf;
+	
 	int Kr = 2.5*source->moments->Mrf;
 	int Kr2 = Kr*Kr;
Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotMagnitudes.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotMagnitudes.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotMagnitudes.c	(revision 31328)
@@ -167,8 +167,26 @@
         pmSource *source = (pmSource *) sources->data[i];
 
+	bool saveTest = false;
+	psImage *testImage = NULL;
+# if (0)
+	if ((fabs(source->peak->xf-3518) < 5) && (fabs(source->peak->yf-3178) < 5)) {
+	    saveTest = true;
+	    psRegion subregion = psRegionSet (source->peak->xf - 200, source->peak->xf + 200, source->peak->yf - 200, source->peak->yf + 200);
+	    testImage = psImageSubset ((psImage *) source->pixels->parent, subregion);
+	}
+# endif
+
+	if (saveTest) {
+	    psphotSaveImage(NULL, testImage, "test.image.1.fits");
+	}
+
         // replace object in image
         if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) {
             pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
         }
+
+	if (saveTest) {
+	    psphotSaveImage(NULL, testImage, "test.image.2.fits");
+	}
 
         // clear the mask bit and set the circular mask pixels
@@ -188,4 +206,8 @@
         // re-subtract the object, leave local sky
         pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
+
+	if (saveTest) {
+	    psphotSaveImage(NULL, testImage, "test.image.3.fits");
+	}
 
         if (backModel) {
Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotModelTest.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotModelTest.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotModelTest.c	(revision 31328)
@@ -226,5 +226,5 @@
     // measure the source mags
     pmSourcePhotometryModel (&fitMag, model);
-    pmSourcePhotometryAper  (&obsMag, NULL, NULL, model, source->pixels, source->variance, source->maskObj, maskVal);
+    pmSourcePhotometryAper  (NULL, &obsMag, NULL, NULL, model, source->pixels, source->variance, source->maskObj, maskVal);
     fprintf (stderr, "ap: %f, fit: %f, apmifit: %f, nIter: %d\n", obsMag, fitMag, obsMag - fitMag, model->nIter);
 
Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotReadout.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotReadout.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotReadout.c	(revision 31328)
@@ -347,16 +347,12 @@
       psErrorStackPrint(stderr, "Unable to replace sky");
       psErrorClear();
-
-/*       psLogMsg("psphot", 3, "failed on psphotSkyReplace"); */
-/*       return(psphotReadoutCleanup(config, view, filerule)); */
-    }
+    }
+
     // drop the references to the image pixels held by each source
     if (!psphotSourceFreePixels (config, view, filerule)) { // pass 1
       psErrorStackPrint(stderr, "Unable to free source pixels");
       psErrorClear();
-
-/*       psLogMsg ("psphot", 3, "failed on psphotSourceFreePixels"); */
-/*       return(psphotReadoutCleanup(config, view, filerule)); */
-    }
+    }
+
     // create the exported-metadata and free local data
     return psphotReadoutCleanup(config, view, filerule);
Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotReplaceUnfit.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotReplaceUnfit.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotReplaceUnfit.c	(revision 31328)
@@ -67,4 +67,11 @@
     psAssert (maskVal, "missing mask value?");
 
+    // bit-mask to mark pixels not used in analysis
+    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
+    assert (markVal);
+
+    // maskVal is used to test for rejected pixels, and must include markVal
+    maskVal |= markVal;
+
     for (int i = 0; i < sources->n; i++) {
       source = sources->data[i];
Index: /branches/eam_branches/ipp-20110404/psphot/src/psphotSourceStats.c
===================================================================
--- /branches/eam_branches/ipp-20110404/psphot/src/psphotSourceStats.c	(revision 31327)
+++ /branches/eam_branches/ipp-20110404/psphot/src/psphotSourceStats.c	(revision 31328)
@@ -238,4 +238,7 @@
     psphotVisualShowMoments (sources);
 
+    // clear the mark bits
+    // psImageMaskPixels (readout->mask, "AND", PS_NOT_IMAGE_MASK(markVal));
+
     if (detections->allSources) {
         psphotMaskCosmicRayFootprintCheck(detections->allSources);
@@ -462,4 +465,10 @@
 	    continue;
 	}
+
+	// XXX test of masking neighbors when measureing moments (does this fail?)
+	// psEllipseMoments moments = {source->moments->Mxx, source->moments->Myy, source->moments->Mxy};
+	// psEllipseAxes axes = psEllipseMomentsToAxes(moments, 20.0);
+        // psImageMaskCircle (source->maskView, source->peak->x, source->peak->y, 3.0*axes.major, "OR", markVal);
+
 	Nmoments ++;
 
