Index: /branches/eam_02_branch/psModules/src/objects/pmModel.c
===================================================================
--- /branches/eam_02_branch/psModules/src/objects/pmModel.c	(revision 12950)
+++ /branches/eam_02_branch/psModules/src/objects/pmModel.c	(revision 12951)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.9.6.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-04-21 00:02:35 $
+ *  @version $Revision: 1.9.6.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-04-21 21:30:21 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -135,6 +135,5 @@
                    psImage *mask,
                    pmModel *model,
-                   bool center,
-                   bool sky,
+                   pmModelAddMode mode,
                    bool add
                       )
@@ -151,10 +150,29 @@
     psS32 imageCol;
     psS32 imageRow;
-    psF32 skyValue = params->data.F32[0];
     psF32 pixelValue;
     
-    float xCenter = model->params->data.F32[PM_PAR_XPOS];
-    float yCenter = model->params->data.F32[PM_PAR_YPOS];
-    float Io = model->params->data.F32[PM_PAR_I0];
+    // save original values; restore before returning
+    // use the true source position for the residual model
+    // the PSF model has presumably already been set for this coordinate
+    float xPos    = params->data.F32[PM_PAR_XPOS];
+    float yPos    = params->data.F32[PM_PAR_YPOS];
+    float IoSave  = params->data.F32[PM_PAR_I0];
+    float skySave = params->data.F32[PM_PAR_SKY];
+
+    if (mode & PM_MODEL_ADD_NORM) {
+	params->data.F32[PM_PAR_I0] = 1.0;
+    }
+    if (!(mode & PM_MODEL_ADD_SKY)) {
+	params->data.F32[PM_PAR_SKY] = 0.0;
+    }
+    if (mode & PM_MODEL_ADD_CENTER) {
+	params->data.F32[PM_PAR_XPOS] = image->col0 + 0.5*image->numCols;
+	params->data.F32[PM_PAR_YPOS] = image->row0 + 0.5*image->numRows;
+    }
+
+    // use these values for this realization
+    float xCenter  = params->data.F32[PM_PAR_XPOS];
+    float yCenter  = params->data.F32[PM_PAR_YPOS];
+    float Io       = params->data.F32[PM_PAR_I0];
 
     int xBin = 1;
@@ -166,5 +184,5 @@
     psImageInterpolateOptions *Rx = NULL;
     psImageInterpolateOptions *Ry = NULL;
-    if (model->residuals) {
+    if (model->residuals && (mode & (PM_MODEL_ADD_RES0 | PM_MODEL_ADD_RES1))) {
 	Ro = psImageInterpolateOptionsAlloc(
 	    PS_INTERPOLATE_BILINEAR,
@@ -189,42 +207,41 @@
 
             // Convert i/j to image coord space:
-            // 'center' option changes meaning of i,j:
-            if (center) {
-                imageCol = ix - 0.5*image->numCols + xCenter;
-                imageRow = iy - 0.5*image->numRows + yCenter;
-            } else {
-                imageCol = ix + image->col0;
-                imageRow = iy + image->row0;
-            }
+	    // XXX should we use using 0.5 pixel offset?
+	    imageCol = ix + image->col0;
+	    imageRow = iy + image->row0;
 
             x->data.F32[0] = (float) imageCol;
             x->data.F32[1] = (float) imageRow;
 
-            // set the appropriate pixel value for this coordinate
-            if (sky) {
-                pixelValue = modelFunc (NULL, params, x);
-            } else {
-                pixelValue = modelFunc (NULL, params, x) - skyValue;
-            }
+	    pixelValue = 0.0;
+
+            // add in the desired components for this coordinate
+            if (mode & PM_MODEL_ADD_FUNC) {
+                pixelValue += modelFunc (NULL, params, x);
+            } 
 
 	    // get the contribution from the residual model
-	    // XXX for a test, do this for all sources and all pixels
 	    if (Ro) {
 		// fractional image position
-		// this is wrong for the 'center' case
-		float ox = xBin*(ix + 0.5 + image->col0 - xCenter) + xResidCenter;
-		float oy = yBin*(iy + 0.5 + image->row0 - yCenter) + yResidCenter;
-
-		psU8 mflux = 0;
-		double Fo = 0.0;
-		double Fx = 0.0;
-		double Fy = 0.0;
-		psImageInterpolate (&Fo, NULL, &mflux, ox, oy, Ro);
-		psImageInterpolate (&Fx, NULL, &mflux, ox, oy, Rx);
-		psImageInterpolate (&Fy, NULL, &mflux, ox, oy, Ry);
-
-		if (!mflux && isfinite(Fo) && isfinite(Fx) && isfinite(Fy)) {
-		    double flux = Fo + xCenter*Fx + yCenter*Fy;
-		    pixelValue += Io*flux;
+		float ox = xBin*(imageCol + 0.5 - xCenter) + xResidCenter;
+		float oy = yBin*(imageRow + 0.5 - yCenter) + yResidCenter;
+
+		if (mode & PM_MODEL_ADD_RES0) {
+		    psU8 mflux = 0;
+		    double Fo = 0.0;
+		    psImageInterpolate (&Fo, NULL, &mflux, ox, oy, Ro);
+		    if (!mflux && isfinite(Fo)) {
+			pixelValue += Io*Fo;
+		    }
+		}
+		if (mode & PM_MODEL_ADD_RES1) {
+		    psU8 mflux = 0;
+		    double Fx = 0.0;
+		    double Fy = 0.0;
+		    psImageInterpolate (&Fx, NULL, &mflux, ox, oy, Rx);
+		    psImageInterpolate (&Fy, NULL, &mflux, ox, oy, Ry);
+		    if (!mflux && isfinite(Fx) && isfinite(Fy)) {
+			pixelValue += Io*(xPos*Fx + yPos*Fy);
+		    }
 		}
 	    }
@@ -238,4 +255,11 @@
         }
     }
+
+    // restore original values
+    params->data.F32[PM_PAR_I0]   = IoSave;
+    params->data.F32[PM_PAR_SKY]  = skySave;
+    params->data.F32[PM_PAR_XPOS] = xPos;
+    params->data.F32[PM_PAR_YPOS] = yPos;
+
     psFree(x);
     psFree(Ro);
@@ -251,9 +275,8 @@
                 psImage *mask,
                 pmModel *model,
-                bool center,
-                bool sky)
-{
-    psTrace("psModules.objects", 3, "---- %s() begin ----\n", __func__);
-    psBool rc = AddOrSubModel(image, mask, model, center, sky, true);
+                pmModelAddMode mode)
+{
+    psTrace("psModules.objects", 3, "---- %s() begin ----\n", __func__);
+    psBool rc = AddOrSubModel(image, mask, model, mode, true);
     psTrace("psModules.objects", 3, "---- %s(%d) end ----\n", __func__, rc);
     return(rc);
@@ -265,9 +288,8 @@
                 psImage *mask,
                 pmModel *model,
-                bool center,
-                bool sky)
-{
-    psTrace("psModules.objects", 3, "---- %s() begin ----\n", __func__);
-    psBool rc = AddOrSubModel(image, mask, model, center, sky, false);
+                pmModelAddMode mode)
+{
+    psTrace("psModules.objects", 3, "---- %s() begin ----\n", __func__);
+    psBool rc = AddOrSubModel(image, mask, model, mode, false);
     psTrace("psModules.objects", 3, "---- %s(%d) end ----\n", __func__, rc);
     return(rc);
Index: /branches/eam_02_branch/psModules/src/objects/pmModel.h
===================================================================
--- /branches/eam_02_branch/psModules/src/objects/pmModel.h	(revision 12950)
+++ /branches/eam_02_branch/psModules/src/objects/pmModel.h	(revision 12951)
@@ -5,6 +5,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.6.6.1 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-04-21 00:03:11 $
+ * @version $Revision: 1.6.6.2 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-04-21 21:30:21 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -26,4 +26,15 @@
     PM_MODEL_BADARGS   ///< model fit called with invalid args
 } pmModelStatus;
+
+typedef enum {
+    PM_MODEL_ADD_NONE 	= 0x00,
+    PM_MODEL_ADD_FUNC 	= 0x01,
+    PM_MODEL_ADD_RES0 	= 0x02,
+    PM_MODEL_ADD_RES1 	= 0x04,
+    PM_MODEL_ADD_FULL   = 0x07,
+    PM_MODEL_ADD_SKY  	= 0x08,
+    PM_MODEL_ADD_CENTER = 0x10,
+    PM_MODEL_ADD_NORM  	= 0x20,
+} pmModelAddMode;
 
 /** pmModel data structure
@@ -91,11 +102,9 @@
  */
 bool pmModelAdd(
-    psImage *image,   ///< The output image (float)
-    psImage *mask,   ///< The image pixel mask (valid == 0)
-    pmModel *model,   ///< The input pmModel
-    bool center,   ///< A boolean flag that determines whether pixels are centered
-    bool sky    ///< A boolean flag that determines if the sky is subtracted
+    psImage *image,			///< The output image (float)
+    psImage *mask,			///< The image pixel mask (valid == 0)
+    pmModel *model,			///< The input pmModel
+    pmModelAddMode mode		     ///< mode to control how the model is added into the image
 );
-
 
 /** pmModelSub()
@@ -110,9 +119,8 @@
  */
 bool pmModelSub(
-    psImage *image,   ///< The output image (float)
-    psImage *mask,   ///< The image pixel mask (valid == 0)
-    pmModel *model,   ///< The input pmModel
-    bool center,   ///< A boolean flag that determines whether pixels are centered
-    bool sky    ///< A boolean flag that determines if the sky is subtracted
+    psImage *image,			///< The output image (float)
+    psImage *mask,			///< The image pixel mask (valid == 0)
+    pmModel *model,			///< The input pmModel
+    pmModelAddMode mode		     ///< mode to control how the model is added into the image
 );
 
Index: /branches/eam_02_branch/psModules/src/objects/pmPSF.c
===================================================================
--- /branches/eam_02_branch/psModules/src/objects/pmPSF.c	(revision 12950)
+++ /branches/eam_02_branch/psModules/src/objects/pmPSF.c	(revision 12951)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.19.2.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-04-21 00:03:31 $
+ *  @version $Revision: 1.19.2.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-04-21 21:30:21 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -424,5 +424,6 @@
     // place the reference object in the image center
     // no need to mask the source here
-    pmModelAdd (image, NULL, model, false, false);
+    // XXX should we measure this for the analytical model only or the full model?
+    pmModelAdd (image, NULL, model, PM_MODEL_ADD_FULL);
 
     // loop over a range of source fluxes
Index: /branches/eam_02_branch/psModules/src/objects/pmSource.h
===================================================================
--- /branches/eam_02_branch/psModules/src/objects/pmSource.h	(revision 12950)
+++ /branches/eam_02_branch/psModules/src/objects/pmSource.h	(revision 12951)
@@ -3,6 +3,6 @@
  * @author EAM, IfA; GLG, MHPCC
  *
- * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-01-24 02:54:15 $
+ * @version $Revision: 1.10.6.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-04-21 21:30:21 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -13,21 +13,4 @@
 /// @addtogroup Objects Object Detection / Analysis Functions
 /// @{
-
-/**
- * In the object analysis process, we will use specific mask values to mark the
- * image pixels. The following structure defines the relevant mask values.
- *
- * XXX: This is probably a bad solution: we will want to set mask values
- * outside of the PSPHOT code.  Perhaps we can set up a registered set of mask
- * values with specific meanings that other functions can add to or define?
- 
- * XXX We will only use the PM_MASK_xxx mask values
-typedef enum {
-    PM_SOURCE_MASK_CLEAR     = 0x00,
-    PM_SOURCE_MASK_INVALID   = 0x01,
-    PM_SOURCE_MASK_SATURATED = 0x02,
-    PM_SOURCE_MASK_MARKED    = 0x08,
-} psphotMaskValues;
-*/
 
 /** pmSourceType enumeration
Index: /branches/eam_02_branch/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- /branches/eam_02_branch/psModules/src/objects/pmSourcePhotometry.c	(revision 12950)
+++ /branches/eam_02_branch/psModules/src/objects/pmSourcePhotometry.c	(revision 12951)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.22.4.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-04-21 00:03:31 $
+ *  @version $Revision: 1.22.4.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-04-21 21:30:21 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -106,6 +106,8 @@
     // replace source flux
     // XXX need to be certain which model and size of mask for prior subtraction
+    // XXX full model or just analytical?
+    // XXX use pmSourceAdd instead?
     if (source->mode & PM_SOURCE_MODE_SUBTRACTED) {
-        pmModelAdd (source->pixels, source->mask, model, false, false);
+        pmModelAdd (source->pixels, source->mask, model, PM_MODEL_ADD_FULL);
     }
 
@@ -210,5 +212,5 @@
     // 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);
+        pmModelSub (source->pixels, source->mask, model, PM_MODEL_ADD_FULL);
     }
 
Index: /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel.c
===================================================================
--- /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel.c	(revision 12950)
+++ /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel.c	(revision 12951)
@@ -136,5 +136,5 @@
 
     // create an image with the model, and add noise: gain is 1, subtracted sky is 100, readnoise is 5
-    pmModelAdd (source->pixels, source->mask, source->modelEXT, false, false);
+    pmModelAdd (source->pixels, source->mask, source->modelEXT, PM_MODEL_ADD_FULL);
     int npix = 0;
     for (int j = 0; j < source->pixels->numRows; j++) {
Index: /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel_Delta.c
===================================================================
--- /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel_Delta.c	(revision 12950)
+++ /branches/eam_02_branch/psModules/test/objects/tap_pmSourceFitModel_Delta.c	(revision 12951)
@@ -54,5 +54,5 @@
 
     // create an image with the model, and add noise: gain is 1, subtracted sky is 100, readnoise is 5
-    pmModelAdd (source->pixels, source->mask, source->modelEXT, false, false);
+    pmModelAdd (source->pixels, source->mask, source->modelEXT, PM_MODEL_ADD_FULL);
     int npix = 0;
     for (int j = 0; j < source->pixels->numRows; j++) {
