Index: /branches/eam_rel8_b2/psModules/src/objects/pmObjects.c
===================================================================
--- /branches/eam_rel8_b2/psModules/src/objects/pmObjects.c	(revision 5718)
+++ /branches/eam_rel8_b2/psModules/src/objects/pmObjects.c	(revision 5719)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.2.6.1.2.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-02 21:27:57 $
+ *  @version $Revision: 1.2.6.1.2.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-12-07 18:49:38 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -1117,5 +1117,5 @@
         bool big = (sigX > (clump.X - clump.dX)) && (sigY > (clump.Y - clump.dY));
         if ((Nsatpix > 1) && big) {
-            tmpSrc->type |= PM_SOURCE_SATSTAR;
+            tmpSrc->type = PM_SOURCE_SATSTAR;
             Nsatstar ++;
             continue;
@@ -1124,5 +1124,5 @@
         // saturated object (not a star, eg bleed trails, hot pixels)
         if (Nsatpix > 1) {
-            tmpSrc->type |= PM_SOURCE_SATURATED;
+            tmpSrc->type = PM_SOURCE_SATURATED;
             Nsat ++;
             continue;
@@ -1133,5 +1133,5 @@
         // only set candidate defects if
         if ((sigX < 0.05) || (sigY < 0.05)) {
-            tmpSrc->type |= PM_SOURCE_DEFECT;
+            tmpSrc->type = PM_SOURCE_DEFECT;
             Ncr ++;
             continue;
@@ -1140,5 +1140,5 @@
         // likely unsaturated galaxy (too large to be stellar)
         if ((sigX > (clump.X + 3*clump.dX)) || (sigY > (clump.Y + 3*clump.dY))) {
-            tmpSrc->type |= PM_SOURCE_GALAXY;
+            tmpSrc->type = PM_SOURCE_GALAXY;
             Ngal ++;
             continue;
@@ -1153,5 +1153,5 @@
         psF32 radius = hypot ((sigX-clump.X)/clump.dX, (sigY-clump.Y)/clump.dY);
         if ((SN > PSF_SN_LIM) && (radius < 1.5)) {
-            tmpSrc->type |= PM_SOURCE_PSFSTAR;
+            tmpSrc->type = PM_SOURCE_PSFSTAR;
             Npsf ++;
             continue;
@@ -1159,5 +1159,5 @@
 
         // random type of star
-        tmpSrc->type |= PM_SOURCE_OTHER;
+        tmpSrc->type = PM_SOURCE_OTHER;
     }
 
@@ -1745,5 +1745,5 @@
     psFree(paramMask);
 
-    psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, rc);
+    psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, (onPic && fitStatus));
     return(onPic && fitStatus);
 }
@@ -1767,5 +1767,4 @@
     psBool fitStatus = true;
     psBool onPic     = true;
-    psBool rc        = true;
     psF32  Ro, ymodel;
 
@@ -1794,4 +1793,5 @@
         psTrace (".pmObjects.pmSourceFitModel", 4, "insufficient valid pixels\n");
         psTrace(__func__, 3, "---- %s(false) end ----\n", __func__);
+        model->status = PM_MODEL_BADARGS;
         return(false);
     }
@@ -1869,19 +1869,10 @@
     }
 
-    // XXX EAM: we need to do something (give an error?) if rc is false
-    // XXX EAM: psMinimizeLMChi2 does not check convergence
-
-    // XXX models can go insane: reject these
-    onPic &= (params->data.F32[2] >= source->pixels->col0);
-    onPic &= (params->data.F32[2] <  source->pixels->col0 + source->pixels->numCols);
-    onPic &= (params->data.F32[3] >= source->pixels->row0);
-    onPic &= (params->data.F32[3] <  source->pixels->row0 + source->pixels->numRows);
-
-    // XXX EAM: save the resulting chisq, nDOF, nIter
+    // save the resulting chisq, nDOF, nIter
     model->chisq = myMin->value;
     model->nIter = myMin->iter;
     model->nDOF  = y->n - nParams;
 
-    // XXX EAM get the Gauss-Newton distance for fixed model parameters
+    // get the Gauss-Newton distance for fixed model parameters
     if (paramMask != NULL) {
         psVector *delta = psVectorAlloc (params->n, PS_TYPE_F64);
@@ -1894,12 +1885,29 @@
     }
 
-    psFree(paramMask);
+    // set the model success or failure status
+    if (!fitStatus) {
+        model->status = PM_MODEL_NONCONVERGE;
+    } else {
+        model->status = PM_MODEL_SUCCESS;
+    }
+
+    // models can go insane: reject these, and save this status
+    onPic &= (params->data.F32[2] >= source->pixels->col0);
+    onPic &= (params->data.F32[2] <  source->pixels->col0 + source->pixels->numCols);
+    onPic &= (params->data.F32[3] >= source->pixels->row0);
+    onPic &= (params->data.F32[3] <  source->pixels->row0 + source->pixels->numRows);
+    if (!onPic) {
+        model->status = PM_MODEL_OFFIMAGE;
+    }
+
     psFree(x);
     psFree(y);
+    psFree(yErr);
     psFree(myMin);
-
-    rc = (onPic && fitStatus);
-    psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, rc);
-    return(rc);
+    psFree(covar);
+    psFree(paramMask);
+
+    psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, (onPic && fitStatus));
+    return(onPic && fitStatus);
 }
 
@@ -1908,5 +1916,7 @@
                              pmModel *model,
                              bool center,
-                             psS32 flag)
+                             bool sky,
+                             bool add
+                                )
 {
     psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
@@ -1921,4 +1931,6 @@
     psS32 imageCol;
     psS32 imageRow;
+    psF32 skyValue = params->data.F32[0];
+    psF32 pixelValue;
 
     for (psS32 i = 0; i < image->numRows; i++) {
@@ -1926,12 +1938,6 @@
             if ((mask != NULL) && mask->data.U8[i][j])
                 continue;
-            psF32 pixelValue;
-            // XXX: Should you be adding the pixels for the entire subImage,
-            // or a radius of pixels around it?
-
-            // Convert i/j to imace coord space:
-            // XXX: Make sure you have col/row order correct.
-            // XXX EAM : 'center' option changes this
-            // XXX EAM : i == numCols/2 -> x = model->params->data.F32[2]
+
+            // Convert i/j to image coord space (depending on 'center'):
             if (center) {
                 imageCol = j - 0.5*image->numCols + model->params->data.F32[2];
@@ -1942,17 +1948,23 @@
             }
 
+            // build the model coordinate
             x->data.F32[0] = (float) imageCol;
             x->data.F32[1] = (float) imageRow;
-            pixelValue = modelFunc (NULL, params, x);
-            // fprintf (stderr, "%f %f  %d %d  %f\n", x->data.F32[0], x->data.F32[1], i, j, pixelValue);
-
-            if (flag == 1) {
-                pixelValue = -pixelValue;
-            }
-
-            // XXX: Must figure out how to calculate the image coordinates and
-            // how to use the boolean "center" flag.
-
-            image->data.F32[i][j]+= pixelValue;
+
+            // set the appropriate pixel value for this coordinate
+            if (sky) {
+                pixelValue = modelFunc (NULL, params, x);
+            } else {
+                pixelValue = modelFunc (NULL, params, x) - skyValue;
+            }
+
+            // add or subtract the value
+            if (add
+               ) {
+                image->data.F32[i][j] += pixelValue;
+            }
+            else {
+                image->data.F32[i][j] -= pixelValue;
+            }
         }
     }
@@ -1969,8 +1981,9 @@
                       psImage *mask,
                       pmModel *model,
-                      bool center)
-{
-    psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
-    psBool rc = p_pmSourceAddOrSubModel(image, mask, model, center, 0);
+                      bool center,
+                      bool sky)
+{
+    psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
+    psBool rc = p_pmSourceAddOrSubModel(image, mask, model, center, sky, true);
     psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, rc);
     return(rc);
@@ -1982,8 +1995,9 @@
                       psImage *mask,
                       pmModel *model,
-                      bool center)
-{
-    psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
-    psBool rc = p_pmSourceAddOrSubModel(image, mask, model, center, 1);
+                      bool center,
+                      bool sky)
+{
+    psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
+    psBool rc = p_pmSourceAddOrSubModel(image, mask, model, center, sky, false);
     psTrace(__func__, 3, "---- %s(%d) end ----\n", __func__, rc);
     return(rc);
