Index: trunk/psphot/src/psphotReplaceUnfit.c
===================================================================
--- trunk/psphot/src/psphotReplaceUnfit.c	(revision 12625)
+++ trunk/psphot/src/psphotReplaceUnfit.c	(revision 12665)
@@ -53,2 +53,67 @@
     return true;
 }
+
+// add or sub source replace or if the source has 
+bool psphotAddWithTest (pmSource *source, bool useState) { 
+
+    // what is current state? (true : add; false : sub)
+    bool state = !(source->mode & PM_SOURCE_MODE_SUBTRACTED);
+    if (state && useState) return true;
+
+    // select appropriate model
+    pmModel *model = pmSourceGetModel (NULL, source);
+    if (model == NULL) return false;  // model must be defined
+    
+    psTrace ("psphot", 3, "replacing object at %f,%f\n", 
+	     model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
+    
+    // replace the model if 1) state says it is missing or 2) useState is false (just do it)
+    if (!state || !useState) {
+	pmModelAdd (source->pixels, source->mask, model, false, false);
+    }
+    return true;
+}
+
+// add or sub source replace or if the source has 
+bool psphotSubWithTest (pmSource *source, bool useState) { 
+
+    // what is current state? (true : sub; false : add)
+    bool state = (source->mode & PM_SOURCE_MODE_SUBTRACTED);
+    if (state && useState) return true;
+
+    // select appropriate model
+    pmModel *model = pmSourceGetModel (NULL, source);
+    if (model == NULL) return false;  // model must be defined
+    
+    psTrace ("psphot", 3, "replacing object at %f,%f\n", 
+	     model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
+    
+    // replace the model if 1) state says it is missing or 2) useState is false (just do it)
+    if (!state || !useState) {
+	pmModelSub (source->pixels, source->mask, model, false, false);
+    }
+    return true;
+}
+
+// add or sub source replace or if the source has 
+bool psphotSetState (pmSource *source, bool curState) { 
+
+    // what is desired state? (true : add; false : sub)
+    bool newState = !(source->mode & PM_SOURCE_MODE_SUBTRACTED);
+    if (curState == newState) return true;
+
+    // select appropriate model
+    pmModel *model = pmSourceGetModel (NULL, source);
+    if (model == NULL) return false;  // model must be defined
+    
+    psTrace ("psphot", 3, "replacing object at %f,%f\n", 
+	     model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
+    
+    if (curState && !newState) {
+	pmModelSub (source->pixels, source->mask, model, false, false);
+    }
+    if (newState && !curState) {
+	pmModelAdd (source->pixels, source->mask, model, false, false);
+    }
+    return true;
+}
