Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmGrowthCurveGenerate.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 29230)
@@ -197,5 +197,5 @@
         // mask the given aperture and measure the apMag
         psImageKeepCircle (mask, xc, yc, radius, "OR", markVal);
-        if (!pmSourcePhotometryAper (&apMag, model, pixels, mask, maskVal)) {
+        if (!pmSourcePhotometryAper (&apMag, NULL, NULL, model, pixels, NULL, mask, maskVal)) {
 	    psFree (growth);
 	    psFree (view);
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmPSFtryFitPSF.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmPSFtryFitPSF.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmPSFtryFitPSF.c	(revision 29230)
@@ -41,4 +41,6 @@
 #include "pmSourcePhotometry.h"
 #include "pmSourceVisual.h"
+
+# define DEBUG 1
 
 // stage 3: Refit with fixed shape parameters.  This function uses the LMM fitting, but could
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.c	(revision 29230)
@@ -132,18 +132,22 @@
 
     // default values are NAN
-    source->psfMag     = NAN;
-    source->psfFlux    = NAN;
-    source->psfFluxErr = NAN;
-    source->extMag = NAN;
-    source->errMag = NAN;
-    source->apMag  = NAN;
-    source->sky    = NAN;
-    source->skyErr = NAN;
-    source->pixWeightNotBad = NAN;
+    source->psfMag     	     = NAN;
+    source->psfFlux    	     = NAN;
+    source->psfFluxErr 	     = NAN;
+    source->extMag 	     = NAN;    
+    source->errMag 	     = NAN;
+    source->apMag  	     = NAN;
+    source->apMagRaw  	     = NAN;
+    source->apRadius  	     = NAN;
+    source->apFlux    	     = NAN;
+    source->apFluxErr 	     = NAN; 
+    source->sky    	     = NAN;
+    source->skyErr 	     = NAN;    
+    source->pixWeightNotBad  = NAN;
     source->pixWeightNotPoor = NAN;
 
-    source->psfChisq = NAN;
-    source->crNsigma = NAN;
-    source->extNsigma = NAN;
+    source->psfChisq         = NAN;
+    source->crNsigma         = NAN;
+    source->extNsigma        = NAN;
 
     psTrace("psModules.objects", 10, "---- end ----\n");
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.h
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.h	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSource.h	(revision 29230)
@@ -76,4 +76,6 @@
     float apMagRaw;                     ///< raw mag in given aperture
     float apRadius;			///< radius for aperture magnitude
+    float apFlux;                       ///< apFlux corresponding to psfMag or extMag (depending on type)
+    float apFluxErr;                    ///< apFluxErr corresponding to psfMag or extMag (depending on type)
 
     float pixWeightNotBad;              ///< PSF-weighted coverage of unmasked (not BAD) pixels
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_DV2.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_DV2.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourceIO_CMF_PS1_DV2.c	(revision 29230)
@@ -185,8 +185,11 @@
         psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_FLUX",    PS_DATA_F32, "PSF fit instrumental magnitude",             source->psfFlux);
         psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_FLUX_SIG",PS_DATA_F32, "Sigma of PSF instrumental magnitude",        source->psfFluxErr);
+
         psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG",           PS_DATA_F32, "magnitude in standard aperture",             source->apMag);
-        // XXX replace with AP_FLUX (& ERR)? psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG",           PS_DATA_F32, "magnitude in standard aperture",             source->apMag);
-
+        psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RAW",       PS_DATA_F32, "magnitude in real aperture",                 source->apMagRaw);
         psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RADIUS",    PS_DATA_F32, "radius used for aperture mags",              apRadius);
+        psMetadataAdd (row, PS_LIST_TAIL, "AP_FLUX",          PS_DATA_F32, "instrumental flux in standard aperture",     source->apFlux);
+        psMetadataAdd (row, PS_LIST_TAIL, "AP_FLUX_SIG",      PS_DATA_F32, "aperture flux error",                        source->apFluxErr);
+
         psMetadataAdd (row, PS_LIST_TAIL, "PEAK_FLUX_AS_MAG", PS_DATA_F32, "Peak flux expressed as magnitude",           peakMag);
         psMetadataAdd (row, PS_LIST_TAIL, "CAL_PSF_MAG",      PS_DATA_F32, "PSF Magnitude using supplied calibration",   calMag);
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.c	(revision 29230)
@@ -62,15 +62,15 @@
 
 /**
-    this function is used to calculate the three defined source magnitudes:
-    - apMag  : only if S/N > AP_MIN_SN
-             : is optionally corrected for curve-of-growth if:
-        - the source is a STAR (PSF)
-        - 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 (mode & PM_SOURCE_PHOT_APCORR)
-
-    - extMag : all sources with non-NULL modelEXT
+   this function is used to calculate the three defined source magnitudes:
+   - apMag  : only if S/N > AP_MIN_SN
+   : is optionally corrected for curve-of-growth if:
+   - the source is a STAR (PSF)
+   - 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 (mode & PM_SOURCE_PHOT_APCORR)
+
+   - extMag : all sources with non-NULL modelEXT
 **/
 
@@ -88,8 +88,12 @@
     pmModel *model;
 
-    source->psfMag = NAN;
-    source->extMag = NAN;
-    source->errMag = NAN;
-    source->apMag  = NAN;
+    source->psfMag    = NAN;
+    source->extMag    = NAN;
+    source->errMag    = NAN;
+    source->apMag     = NAN;
+    source->apMagRaw  = NAN;
+    source->apRadius  = NAN;
+    source->apFlux    = NAN;
+    source->apFluxErr = NAN;
 
     // we must have a valid model
@@ -182,5 +186,7 @@
     // 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)
-    psImage *flux = NULL, *mask = NULL; // Star flux and mask images, to photometer
+    psImage *flux = NULL;
+    psImage *variance = NULL;
+    psImage *mask = NULL; // Star flux and mask images, to photometer
     if (mode & PM_SOURCE_PHOT_INTERP) {
         float dx = 0.5 - x + (int)x;
@@ -199,9 +205,10 @@
     } else {
         flux = source->pixels;
+        variance = source->variance;
         mask = source->maskObj;
     }
 
     // measure object aperture photometry
-    status = pmSourcePhotometryAper  (&source->apMagRaw, model, flux, mask, maskVal);
+    status = pmSourcePhotometryAperSource (source, model, flux, variance, mask, maskVal);
     if (!status) {
         psTrace ("psModules.objects", 3, "fail mag : bad Ap Mag");
@@ -215,4 +222,5 @@
         if (psf->growth && (mode & PM_SOURCE_PHOT_GROWTH)) {
             source->apMag = source->apMagRaw + pmGrowthCurveCorrect (psf->growth, source->apRadius);
+	    // XXX correct the apFlux?
         }
         if (mode & PM_SOURCE_PHOT_APCORR) {
@@ -234,7 +242,7 @@
 
 /*
-aprMag' - fitMag = flux*skySat + r^2*rflux*skyBias + ApTrend(x,y)
-(aprMag - flux*skySat - r^2*rflux*skyBias) - fitMAg = ApTrend(x,y)
-(aprMag - flux*skySat - r^2*rflux*skyBias) = fitMAg + ApTrend(x,y)
+  aprMag' - fitMag = flux*skySat + r^2*rflux*skyBias + ApTrend(x,y)
+  (aprMag - flux*skySat - r^2*rflux*skyBias) - fitMAg = ApTrend(x,y)
+  (aprMag - flux*skySat - r^2*rflux*skyBias) = fitMAg + ApTrend(x,y)
 
 */
@@ -268,5 +276,22 @@
 
 // return source aperture magnitude
-bool pmSourcePhotometryAper (float *apMag, pmModel *model, psImage *image, psImage *mask, psImageMaskType maskVal)
+bool pmSourcePhotometryAperSource (pmSource *source, pmModel *model, psImage *image, psImage *variance, psImage *mask, psImageMaskType maskVal)
+{
+    PS_ASSERT_PTR_NON_NULL(source, false);
+    PS_ASSERT_PTR_NON_NULL(image, false);
+    PS_ASSERT_PTR_NON_NULL(mask, false);
+
+    if (DO_SKY) {
+	PS_ASSERT_PTR_NON_NULL(model, false);
+    }
+
+    bool status;
+    status = pmSourcePhotometryAper(&source->apMagRaw, &source->apFlux, &source->apFluxErr, model, image, variance, mask, maskVal);
+
+    return status;
+}
+
+// return source aperture magnitude
+bool pmSourcePhotometryAper (float *apMag, float *apFluxOut, float *apFluxErr, pmModel *model, psImage *image, psImage *variance, psImage *mask, psImageMaskType maskVal)
 {
     PS_ASSERT_PTR_NON_NULL(apMag, false);
@@ -278,37 +303,34 @@
     }
 
-    float apSum = 0;
     float sky = 0;
-    *apMag = NAN;
+    float apFlux = 0;
+    float apFluxVar = 0;
 
     if (DO_SKY) {
         sky = model->params->data.F32[PM_PAR_SKY];
-    } else {
-        sky = 0;
     }
 
     psF32 **imData = image->data.F32;
     psImageMaskType **mkData = mask->data.PS_TYPE_IMAGE_MASK_DATA;
-    int nAperPix = 0;
-
-    // measure apMag
-    // XXX save the apFlux and apFluxErr
+    psF32 **varData = (variance) ? variance->data.F32 : image->data.F32; // if variance is not supplied, assume gain of 1.0, no read noise
+
+    // measure apFlux and apFluxVar, save apMag if not NAN
     // XXX note that these fluxes/mags are uncorrected for masked pixels
     // XXX raise a bit if the aperture has a masked pixel (not marked)?
     for (int iy = 0; iy < image->numRows; iy++) {
 	for (int ix = 0; ix < image->numCols; ix++) {
-            if (mkData[iy][ix] & maskVal)
-                continue;
-            apSum += imData[iy][ix] - sky;
-	    nAperPix ++;
-	    // fprintf (stderr, "aper: %d %d  %f  %f  %f\n", ix, iy, sky, imData[iy][ix], apSum);
-        }
-    }
-    if (apSum <= 0) {
+            if (mkData[iy][ix] & maskVal) continue;
+            apFlux += imData[iy][ix] - sky;
+            apFluxVar += varData[iy][ix];
+        }
+    }
+    if (apFluxOut) *apFluxOut = apFlux;
+    if (apFluxErr) *apFluxErr = sqrt(apFluxVar);
+
+    if (apFlux <= 0) {
         *apMag = NAN;
-        return true;
-    }
-
-    *apMag = -2.5*log10(apSum);
+    } else {
+	*apMag = -2.5*log10(apFlux);
+    }
     return true;
 }
@@ -617,14 +639,14 @@
 
             switch (term) {
-            case 0:
+	      case 0:
                 factor = 1;
                 break;
-            case 1:
+	      case 1:
                 factor = xi + Pi->col0;
                 break;
-            case 2:
+	      case 2:
                 factor = yi + Pi->row0;
                 break;
-            default:
+	      default:
                 psAbort("invalid term for pmSourceWeight");
             }
@@ -695,14 +717,14 @@
 
             switch (term) {
-            case 0:
+	      case 0:
                 factor = 1;
                 break;
-            case 1:
+	      case 1:
                 factor = xi + Pi->col0;
                 break;
-            case 2:
+	      case 2:
                 factor = yi + Pi->row0;
                 break;
-            default:
+	      default:
                 psAbort("invalid term for pmSourceWeight");
             }
Index: /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.h
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.h	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psModules/src/objects/pmSourcePhotometry.h	(revision 29230)
@@ -44,7 +44,19 @@
 
 bool pmSourcePhotometryAper(
-    float   *apMag,                     ///< aperture flux magnitude
+    float *apMag,
+    float *apFluxOut,
+    float *apFluxErr,
     pmModel *model,                     ///< model used for photometry
     psImage *image,                     ///< image pixels to be used
+    psImage *variance,                  ///< variance pixels to be used
+    psImage *mask,                      ///< mask of pixels to ignore
+    psImageMaskType maskVal             ///< Value to mask
+);
+
+bool pmSourcePhotometryAperSource(
+    pmSource *source,			///< aperture flux magnitude
+    pmModel *model,                     ///< model used for photometry
+    psImage *image,                     ///< image pixels to be used
+    psImage *variance,                  ///< variance pixels to be used
     psImage *mask,                      ///< mask of pixels to ignore
     psImageMaskType maskVal             ///< Value to mask
Index: /branches/eam_branches/ipp-20100823/psphot/src/psphotModelTest.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psphot/src/psphotModelTest.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psphot/src/psphotModelTest.c	(revision 29230)
@@ -226,5 +226,5 @@
     // measure the source mags
     pmSourcePhotometryModel (&fitMag, model);
-    pmSourcePhotometryAper  (&obsMag, model, source->pixels, source->maskObj, maskVal);
+    pmSourcePhotometryAper  (&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-20100823/psphot/src/psphotSourceStats.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psphot/src/psphotSourceStats.c	(revision 29229)
+++ /branches/eam_branches/ipp-20100823/psphot/src/psphotSourceStats.c	(revision 29230)
@@ -418,5 +418,5 @@
 	    
 	    float apMag = NAN;
-	    pmSourcePhotometryAper (&apMag, NULL, source->pixels, source->maskObj, maskVal);
+	    pmSourcePhotometryAper (&apMag, NULL, NULL, NULL, source->pixels, source->variance, source->maskObj, maskVal);
 	    fprintf (stderr, "apMag: %f, kronMag: %f\n", apMag, -2.5*log10(source->moments->KronFlux));
 
