Index: trunk/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- trunk/psModules/src/objects/pmSourcePhotometry.c	(revision 9882)
+++ trunk/psModules/src/objects/pmSourcePhotometry.c	(revision 9883)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-11-06 02:11:04 $
+ *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-11-07 09:11:12 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -51,14 +51,14 @@
              : is optionally corrected for curve-of-growth if:
         - the source is a STAR (PSF)
-        - the option is selected (how??)
+        - the option is selected (mode & PM_SOURCE_PHOT_GROWTH)
     - psfMag : all sources with non-NULL modelPSF
              : is optionally corrected for aperture residual if:
         - the source is a STAR (PSF)
-        - the option is selected (how??)
+        - the option is selected (mode & PM_SOURCE_PHOT_APCORR)
  
     - extMag : all sources with non-NULL modelEXT
 **/
 
-// XXX EAM : masked region should be (optionally) elliptical
+// XXX masked region should be (optionally) elliptical
 bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode)
 {
@@ -71,9 +71,11 @@
     pmModel *model;
 
-    source->psfMag = 99.0;
-    source->extMag = 99.0;
-    source->errMag = 99.0;
-    source->apMag  = 99.0;
-
+    source->psfMag = NAN;
+    source->extMag = NAN;
+    source->errMag = NAN;
+    source->apMag  = NAN;
+
+    // XXX these tests prevent aperture photometry without valid model.  is
+    // this reasonable?  probably not...
     switch (source->type) {
     case PM_SOURCE_TYPE_STAR:
@@ -99,6 +101,6 @@
         source->errMag = 1.0 / SN;
     } else {
-        SN = 0.0;
-        source->errMag = 0.0;
+        SN = NAN;
+        source->errMag = NAN;
     }
     x = model->params->data.F32[PM_PAR_XPOS];
@@ -114,15 +116,31 @@
     }
 
-    if (SN < AP_MIN_SN)
+    if (!isfinite(SN) || (SN < AP_MIN_SN))
         return false;
 
     // replace source flux
-    // XXX test to see if source has been subtracted?
-    pmModelAdd (source->pixels, source->mask, model, false, false);
+    // XXX need to be certain which model and size of mask for prior subtraction
+    if (source->mode & PM_SOURCE_MODE_SUBTRACTED) {
+        pmModelAdd (source->pixels, source->mask, model, false, false);
+    }
+
+    // if we are measuring aperture photometry and applying the growth correction,
+    // we need to shift the flux in the selected pixels (but not the mask)
+    // psImageShift ();
+    psImage *flux;
+    if (mode & PM_SOURCE_PHOT_INTERP) {
+        float dx = 0.5 - x + (int)x;
+        float dy = 0.5 - y + (int)y;
+        flux = psImageShift (NULL, source->pixels, dx, dy, 0.0, PS_INTERPOLATE_BICUBE);
+    } else {
+        flux = source->pixels;
+    }
 
     // set aperture mask circle to model radius
+    // XXX use a different radius for apertures and fits...
     psImageKeepCircle (source->mask, x, y, model->radiusFit, "OR", PM_MASK_MARK);
 
     // measure the weight of included pixels
+    // XXX is this supposed to use the weight or the flux?
     if (mode & PM_SOURCE_PHOT_WEIGHT) {
         pmSourcePixelWeight (&source->pixWeight, model, source->pixels, source->mask);
@@ -130,5 +148,5 @@
 
     // measure object aperture photometry
-    status = pmSourcePhotometryAper  (&source->apMag, model, source->pixels, source->mask);
+    status = pmSourcePhotometryAper  (&source->apMag, model, flux, source->mask);
 
     // for PSFs, correct both apMag and psfMag to same system, consistent with infinite flux star in aperture RADIUS
@@ -142,10 +160,15 @@
         }
     }
+    if (mode & PM_SOURCE_PHOT_INTERP) {
+        psFree (flux);
+    }
 
     // unmask aperture
     psImageKeepCircle (source->mask, x, y, model->radiusFit, "AND", PS_NOT_U8(PM_MASK_MARK));
 
-    // subtract object, leave local sky
-    pmModelSub (source->pixels, source->mask, model, false, false);
+    // if source was originally subtracted, re-subtract object, leave local sky
+    if (source->mode & PM_SOURCE_MODE_SUBTRACTED) {
+        pmModelSub (source->pixels, source->mask, model, false, false);
+    }
 
     return status;
@@ -164,5 +187,5 @@
 
     float fitSum = 0;
-    *fitMag = 99.0;
+    *fitMag = NAN;
 
     if (model == NULL) {
@@ -183,10 +206,9 @@
 
 // return source aperture magnitude
-// interpolate the image pixels to place the center at xi+0.5,yi+0.5
 bool pmSourcePhotometryAper (float *apMag, pmModel *model, psImage *image, psImage *mask)
 {
     float apSum = 0;
     float sky = 0;
-    *apMag = 99.0;
+    *apMag = NAN;
 
     if (model == NULL) {
@@ -200,16 +222,4 @@
     }
 
-    // shift image to have centroid at xi+0.5, yi+0.5
-    # if (0)
-        // this is necessary to make the measurement consistent with the growth model.
-        // only do this if we are applying the model?
-        float dx = 0.5 - model->params->data.F32[PM_PAR_XPOS] + (int)model->params->data.F32[PM_PAR_XPOS];
-    float dy = 0.5 - model->params->data.F32[PM_PAR_YPOS] + (int)model->params->data.F32[PM_PAR_YPOS];
-    psImage *tmpImage = psImageShift (NULL, image, dx, dy, 0.0, PS_INTERPOLATE_BICUBE);
-    # else
-
-        psImage *tmpImage = psImageCopy (NULL, image, PS_TYPE_F32);
-    # endif
-
     // measure apMag
     for (int ix = 0; ix < image->numCols; ix++) {
@@ -217,8 +227,7 @@
             if (mask->data.U8[iy][ix])
                 continue;
-            apSum += tmpImage->data.F32[iy][ix] - sky;
-        }
-    }
-    psFree (tmpImage);
+            apSum += image->data.F32[iy][ix] - sky;
+        }
+    }
     if (apSum <= 0)
         return false;
