Index: /branches/eam_branches/20090715/psModules/src/objects/pmPSFtryFitPSF.c
===================================================================
--- /branches/eam_branches/20090715/psModules/src/objects/pmPSFtryFitPSF.c	(revision 25595)
+++ /branches/eam_branches/20090715/psModules/src/objects/pmPSFtryFitPSF.c	(revision 25596)
@@ -1,5 +1,4 @@
-/** @file  pmPSFtry.c
- *
- *  XXX: need description of file purpose
+/** @file  pmPSFtryFitPSF.c
+ *  @brief Fit the PSF model to the candidate PSF stars (part of PSF model generation)
  *
  *  @author EAM, IfA
@@ -8,5 +7,4 @@
  *  @date $Date: 2009-01-27 06:39:38 $
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
- *
  */
 
@@ -48,4 +46,12 @@
     maskVal |= markVal;
 
+    // DEBUG code: save the PSF model fit data in detail
+# ifdef DEBUG
+    char filename[64];
+    snprintf (filename, 64, "psffit.%dx%d.dat", psfTry->psf->trendNx, psfTry->psf->trendNy);
+    FILE *f = fopen (filename, "w");
+    psAssert (f, "failed open");
+# endif
+
     int Npsf = 0;
     for (int i = 0; i < psfTry->sources->n; i++) {
@@ -80,5 +86,5 @@
         // skip poor fits
         if (!status) {
-            psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->fitRadius, "AND", PS_NOT_IMAGE_MASK(markVal));
+            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
             psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_PSF_FAIL;
             psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : failed PSF fit\n", i, source->peak->x, source->peak->y);
@@ -87,18 +93,13 @@
 
         // set object mask to define valid pixels for APERTURE magnitude
-	if (options->fitRadius < options->apRadius) {
-	    // this is not really a recommended situation...
-            psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->fitRadius, "AND", PS_NOT_IMAGE_MASK(markVal));
-	    psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "OR", markVal);
-	}
-	if (options->fitRadius > options->apRadius) {
-	    // ensure the aperture is defined
+	if (options->fitRadius != options->apRadius) {
+            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
 	    psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "OR", markVal);
 	}
 
 	// This function calculates the psf and aperture magnitudes
-        status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal);
+        status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal); // raw PSF mag, AP mag
         if (!status || isnan(source->apMag)) {
-            psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "AND", PS_NOT_IMAGE_MASK(markVal));
+            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
             psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_PHOT;
             psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : poor photometry\n", i, source->peak->x, source->peak->y);
@@ -107,10 +108,22 @@
 
         // clear object mask to define valid pixels
-        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "AND", PS_NOT_IMAGE_MASK(markVal));
+	psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
 
         psfTry->fitMag->data.F32[i] = source->psfMag;
-        // psfTry->metric->data.F32[i] = -2.5*log10(source->moments->Sum) - source->psfMag;
         psfTry->metric->data.F32[i] = source->apMag - source->psfMag;
         psfTry->metricErr->data.F32[i] = source->errMag;
+
+	// XXX this did not work: modifies shape of psf too much
+        // psfTry->metric->data.F32[i] = -2.5*log10(source->moments->Sum) - source->psfMag;
+
+# ifdef DEBUG
+	fprintf (f, "%6.1f %6.1f : %6.1f %6.1f : %8.3f %8.3f %8.3f : %f : %f %f %f : %f\n",
+		 source->peak->xf, source->peak->yf, 
+		 source->modelPSF->params->data.F32[PM_PAR_XPOS], source->modelPSF->params->data.F32[PM_PAR_YPOS], 
+		 source->psfMag, source->apMag, source->errMag,
+		 source->modelPSF->params->data.F32[PM_PAR_I0], 
+		 source->modelPSF->params->data.F32[PM_PAR_SXX], source->modelPSF->params->data.F32[PM_PAR_SXY], 
+		 source->modelPSF->params->data.F32[PM_PAR_SYY], source->modelPSF->params->data.F32[PM_PAR_7]);
+# endif
 
         psTrace ("psModules.object", 6, "keeping source %d (%d) of %ld\n", i, Npsf, psfTry->sources->n);
@@ -119,5 +132,8 @@
     psfTry->psf->nPSFstars = Npsf;
 
-    // XXX test code:
+# ifdef DEBUG
+    fclose (f);
+# endif
+
     pmSourceVisualShowModelFits (psfTry->psf, psfTry->sources, maskVal);
 
