Index: trunk/psphot/src/pmObjects_EAM.c
===================================================================
--- trunk/psphot/src/pmObjects_EAM.c	(revision 4950)
+++ trunk/psphot/src/pmObjects_EAM.c	(revision 4954)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-06 08:05:08 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-07 03:33:01 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -66,6 +66,5 @@
 pmModelAlloc(): Allocate the pmModel structure, along with its parameters,
 and initialize the type member.  Initialize the params to 0.0.
-XXX EAM: changing params and dparams to psVector
-XXX EAM: simplifying code with psModelParameterCount
+XXX EAM: simplifying code with pmModelParameterCount
 *****************************************************************************/
 pmModel *pmModelAlloc(pmModelType type)
@@ -76,7 +75,7 @@
     tmp->chisq = 0.0;
     tmp->nIter = 0;
-    psS32 Nparams = psModelParameterCount (type);
+    psS32 Nparams = pmModelParameterCount (type);
     if (Nparams == 0) {
-        psError(PS_ERR_UNKNOWN, true, "Undefined psModelType");
+        psError(PS_ERR_UNKNOWN, true, "Undefined pmModelType");
         return(NULL);
     }
@@ -167,5 +166,5 @@
     //
     if ((vector->data.F32[0] > vector->data.F32[1]) &&
-            (vector->data.F32[0] > threshold)) {
+	(vector->data.F32[0] > threshold)) {
         count++;
     }
@@ -176,6 +175,6 @@
     for (psU32 i = 1; i < n-1 ; i++) {
         if ((vector->data.F32[i] > vector->data.F32[i-1]) &&
-                (vector->data.F32[i] > vector->data.F32[i+1]) &&
-                (vector->data.F32[i] > threshold)) {
+	    (vector->data.F32[i] > vector->data.F32[i+1]) &&
+	    (vector->data.F32[i] > threshold)) {
             count++;
         }
@@ -186,5 +185,5 @@
     //
     if ((vector->data.F32[n-1] > vector->data.F32[n-2]) &&
-            (vector->data.F32[n-1] > threshold)) {
+	(vector->data.F32[n-1] > threshold)) {
         count++;
     }
@@ -201,5 +200,5 @@
     //
     if ((vector->data.F32[0] > vector->data.F32[1]) &&
-            (vector->data.F32[0] > threshold)) {
+	(vector->data.F32[0] > threshold)) {
         tmpVector->data.U32[count++] = 0;
     }
@@ -210,6 +209,6 @@
     for (psU32 i = 1; i < (n-1) ; i++) {
         if ((vector->data.F32[i] > vector->data.F32[i-1]) &&
-                (vector->data.F32[i] > vector->data.F32[i+1]) &&
-                (vector->data.F32[i] > threshold)) {
+	    (vector->data.F32[i] > vector->data.F32[i+1]) &&
+	    (vector->data.F32[i] > threshold)) {
             tmpVector->data.U32[count++] = i;
         }
@@ -220,5 +219,5 @@
     //
     if ((vector->data.F32[n-1] > vector->data.F32[n-2]) &&
-            (vector->data.F32[n-1] > threshold)) {
+	(vector->data.F32[n-1] > threshold)) {
         tmpVector->data.U32[count++] = n-1;
     }
@@ -319,6 +318,6 @@
         if (col == 0) {
             if ( (image->data.F32[row][col] >  image->data.F32[row][col+1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row+1][col]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
+		 (image->data.F32[row][col] >  image->data.F32[row+1][col]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
 
                 if (image->data.F32[row][col] > threshold) {
@@ -328,8 +327,8 @@
         } else if (col < (image->numCols - 1)) {
             if ( (image->data.F32[row][col] >= image->data.F32[row][col-1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row][col+1]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row+1][col]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
+		 (image->data.F32[row][col] >  image->data.F32[row][col+1]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
+		 (image->data.F32[row][col] >  image->data.F32[row+1][col]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
                 if (image->data.F32[row][col] > threshold) {
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], PM_PEAK_EDGE);
@@ -339,6 +338,6 @@
         } else if (col == (image->numCols - 1)) {
             if ( (image->data.F32[row][col] >= image->data.F32[row][col-1]) &&
-                    (image->data.F32[row][col] > image->data.F32[row+1][col]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row+1][col-1])) {
+		 (image->data.F32[row][col] > image->data.F32[row+1][col]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row+1][col-1])) {
                 if (image->data.F32[row][col] > threshold) {
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], PM_PEAK_EDGE);
@@ -375,8 +374,8 @@
                 // If col==0, then we can not read col-1 pixels
                 if ((image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
+		    (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
                     myType = PM_PEAK_EDGE;
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], myType);
@@ -385,31 +384,31 @@
                 // This is an interior pixel
                 if ((image->data.F32[row][col] >= image->data.F32[row-1][col-1]) &&
-                        (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
-                        (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
+		    (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
+		    (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col+1])) {
                     if (image->data.F32[row][col] > threshold) {
                         if ((image->data.F32[row][col] > image->data.F32[row-1][col-1]) &&
-                                (image->data.F32[row][col] > image->data.F32[row-1][col]) &&
-                                (image->data.F32[row][col] > image->data.F32[row-1][col+1]) &&
-                                (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
-                                (image->data.F32[row][col] > image->data.F32[row][col+1]) &&
-                                (image->data.F32[row][col] > image->data.F32[row+1][col-1]) &&
-                                (image->data.F32[row][col] > image->data.F32[row+1][col]) &&
-                                (image->data.F32[row][col] > image->data.F32[row+1][col+1])) {
+			    (image->data.F32[row][col] > image->data.F32[row-1][col]) &&
+			    (image->data.F32[row][col] > image->data.F32[row-1][col+1]) &&
+			    (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
+			    (image->data.F32[row][col] > image->data.F32[row][col+1]) &&
+			    (image->data.F32[row][col] > image->data.F32[row+1][col-1]) &&
+			    (image->data.F32[row][col] > image->data.F32[row+1][col]) &&
+			    (image->data.F32[row][col] > image->data.F32[row+1][col+1])) {
                             myType = PM_PEAK_LONE;
                         }
 
                         if ((image->data.F32[row][col] == image->data.F32[row-1][col-1]) ||
-                                (image->data.F32[row][col] == image->data.F32[row-1][col]) ||
-                                (image->data.F32[row][col] == image->data.F32[row-1][col+1]) ||
-                                (image->data.F32[row][col] == image->data.F32[row][col-1]) ||
-                                (image->data.F32[row][col] == image->data.F32[row][col+1]) ||
-                                (image->data.F32[row][col] == image->data.F32[row+1][col-1]) ||
-                                (image->data.F32[row][col] == image->data.F32[row+1][col]) ||
-                                (image->data.F32[row][col] == image->data.F32[row+1][col+1])) {
+			    (image->data.F32[row][col] == image->data.F32[row-1][col]) ||
+			    (image->data.F32[row][col] == image->data.F32[row-1][col+1]) ||
+			    (image->data.F32[row][col] == image->data.F32[row][col-1]) ||
+			    (image->data.F32[row][col] == image->data.F32[row][col+1]) ||
+			    (image->data.F32[row][col] == image->data.F32[row+1][col-1]) ||
+			    (image->data.F32[row][col] == image->data.F32[row+1][col]) ||
+			    (image->data.F32[row][col] == image->data.F32[row+1][col+1])) {
                             myType = PM_PEAK_FLAT;
                         }
@@ -421,9 +420,9 @@
                 // If col==numCols - 1, then we can not read col+1 pixels
                 if ((image->data.F32[row][col] >= image->data.F32[row-1][col-1]) &&
-                        (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                        (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
-                        (image->data.F32[row][col] >= image->data.F32[row+1][col])) {
+		    (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
+		    (image->data.F32[row][col] > image->data.F32[row][col-1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row][col+1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col-1]) &&
+		    (image->data.F32[row][col] >= image->data.F32[row+1][col])) {
                     myType = PM_PEAK_EDGE;
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], myType);
@@ -448,6 +447,6 @@
         if (col == 0) {
             if ( (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row][col+1])) {
+		 (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
+		 (image->data.F32[row][col] >  image->data.F32[row][col+1])) {
                 if (image->data.F32[row][col] > threshold) {
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], PM_PEAK_EDGE);
@@ -456,8 +455,8 @@
         } else if (col < (image->numCols - 1)) {
             if ( (image->data.F32[row][col] >= image->data.F32[row-1][col-1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row][col-1]) &&
-                    (image->data.F32[row][col] >= image->data.F32[row][col+1])) {
+		 (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row-1][col+1]) &&
+		 (image->data.F32[row][col] >  image->data.F32[row][col-1]) &&
+		 (image->data.F32[row][col] >= image->data.F32[row][col+1])) {
                 if (image->data.F32[row][col] > threshold) {
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], PM_PEAK_EDGE);
@@ -467,6 +466,6 @@
         } else if (col == (image->numCols - 1)) {
             if ( (image->data.F32[row][col] >= image->data.F32[row-1][col-1]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
-                    (image->data.F32[row][col] >  image->data.F32[row][col-1])) {
+		 (image->data.F32[row][col] >  image->data.F32[row-1][col]) &&
+		 (image->data.F32[row][col] >  image->data.F32[row][col-1])) {
                 if (image->data.F32[row][col] > threshold) {
                     list = myListAddPeak(list, row, col, image->data.F32[row][col], PM_PEAK_EDGE);
@@ -495,5 +494,5 @@
 	(y >= valid->y0) &&
 	(y <= valid->y1)) {
-      return(true);
+	return(true);
     }
 
@@ -511,4 +510,8 @@
  
 XXX: changed API to create a NEW output psArray (should change name as well)
+ 
+XXX: Do we free the psList elements of those culled peaks?
+
+XXX EAM : do we still need pmCullPeaks, or only pmPeaksSubset?
 *****************************************************************************/
 psList *pmCullPeaks(psList *peaks,
@@ -517,5 +520,4 @@
 {
     PS_ASSERT_PTR_NON_NULL(peaks, NULL);
-    //    PS_ASSERT_PTR_NON_NULL(valid, NULL);
 
     psListElem *tmpListElem = (psListElem *) peaks->head;
@@ -526,6 +528,6 @@
         pmPeak *tmpPeak = (pmPeak *) tmpListElem->data;
         if ((tmpPeak->counts > maxValue) ||
-                ((valid != NULL) &&
-                 (true == isItInThisRegion(valid, tmpPeak->x, tmpPeak->y)))) {
+	    ((valid != NULL) &&
+	     (true == isItInThisRegion(valid, tmpPeak->x, tmpPeak->y)))) {
             psListRemoveData(peaks, (psPtr) tmpPeak);
         }
@@ -625,59 +627,19 @@
     // XXX EAM : I added this code to stay on the image. So did George
     // XXX EAM : EndRow is *exclusive* of pixel region (ie, last pixel + 1)
+    // XXX EAM : dropped the Annulus width (not needed)
+    // XXX EAM : dropped off-boundary tests (not needed)
     psS32 SubImageStartRow  = PS_MAX (0, SubImageCenterRow - outerRadiusS32);
     psS32 SubImageEndRow    = PS_MIN (image->numRows, SubImageCenterRow + outerRadiusS32 + 1);
     psS32 SubImageStartCol  = PS_MAX (0, SubImageCenterCol - outerRadiusS32);
     psS32 SubImageEndCol    = PS_MIN (image->numCols, SubImageCenterCol + outerRadiusS32 + 1);
-    // AnulusWidth == number of pixels width in the annulus.  We add one since
-    // the pixels at the inner AND outher radius are included.
-    // XXX EAM : not used : psS32 AnulusWidth = 1 + (outerRadiusS32 - innerRadiusS32);
-    // Example: assume an outer/inner radius of 20/10.  Then the subimage
-    // should have width/length of 40.  An 18-by-18 interior region will
-    // be masked.
-    //    printf("pmSourceLocalSky(): innerRadiusS32 is %d\n", innerRadiusS32);
-    //    printf("pmSourceLocalSky(): outerRadiusS32 is %d\n", outerRadiusS32);
-    //    printf("pmSourceLocalSky(): AnulusWidth is %d\n", AnulusWidth);
-
-    // XXX EAM : these tests should not be needed: we can never hit this error because of above
-    # if (1)
-
-        if (SubImageStartRow < 0) {
-            psError(PS_ERR_UNKNOWN, true, "Sub image startRow is outside image boundaries (%d).\n",
-                    SubImageStartRow);
-            return(NULL);
-        }
-    if (SubImageEndRow > image->numRows) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image endRow is outside image boundaries (%d).\n",
-                SubImageEndRow);
-        return(NULL);
-    }
-    if (SubImageStartCol < 0) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image startCol is outside image boundaries (%d).\n",
-                SubImageStartCol);
-        return(NULL);
-    }
-    if (SubImageEndCol > image->numCols) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image endCol is outside image boundaries (%d).\n",
-                SubImageEndCol);
-        return(NULL);
-    }
-    # endif
-
     //
     // Grab a subimage of the original image of size (2 * outerRadius).
     //
-    // XXX: Must fix for new psImageSubset
-    //    psImage *subImage = psImageSubset((psImage *) image,
-    //                                      SubImageStartCol,
-    //                                      SubImageStartRow,
-    //                                      SubImageEndCol,
-    //                                      SubImageEndRow);
-    //    printf("pmSourceLocalSky: subimage width/length is (%d, %d)\n", subImage->numCols, subImage->numRows);
-    psRegion tmpRegion = psRegionSet(SubImageStartCol,
-                                     SubImageEndCol,
-                                     SubImageStartRow,
-                                     SubImageEndRow);
-    psImage *subImage = psImageSubset((psImage *) image, tmpRegion);
-    // XXX EAM : can merge these: psImageSubset (image, psRegionSet ());
+    // XXX EAM : merged psImageSubset & psRegionSet
+    // XXX EAM : cast for image is needed because of const (above)
+    psImage *subImage = psImageSubset((psImage *) image, psRegionSet(SubImageStartCol,
+							 SubImageEndCol,
+							 SubImageStartRow,
+							 SubImageEndRow));
 
     psImage *subImageMask = psImageAlloc(subImage->numCols,
@@ -685,14 +647,16 @@
                                          PS_TYPE_U8);
     // XXX EAM : for consistency, mask needs col0,row0 set to match image
-    subImageMask->col0 = subImage->col0;
-    subImageMask->row0 = subImage->row0;
-
-    //
-    // Loop through the subimage mask, initialize mask to 1 (invalid pixel)
+    // XXX EAM : CONFLICT between psLib and code requirement. FIX PSLIB!!!
+    // subImageMask->col0 = subImage->col0;
+    // subImageMask->row0 = subImage->row0;
+    psAbort ("pmObjects", "must fix this error before psphot will work!!!\n");
+
+    //
+    // Loop through the subimage mask, initialize mask to 0x01 (invalid pixel)
     // XXX EAM : use PSPHOT_MASK_INVALID?
     //
     for (psS32 row = 0 ; row < subImageMask->numRows; row++) {
         for (psS32 col = 0 ; col < subImageMask->numCols; col++) {
-            subImageMask->data.U8[row][col] = 1;
+            subImageMask->data.U8[row][col] = 0x01;
         }
     }
@@ -702,4 +666,6 @@
     // XXX this uses a static mask value of 0
     // XXX EAM : this was wrong: it masked the wrong pixels at the edge of the image
+    // XXX EAM : this masks the pixels in the center region
+    // XXX EAM : should we be using psImageMaskRegion ???
     psS32 StartRow  = PS_MAX (0, SubImageCenterRow - subImageMask->row0 - innerRadiusS32);
     psS32 EndRow    = PS_MIN (subImageMask->numRows, SubImageCenterRow - subImageMask->row0 + innerRadiusS32 + 1);
@@ -712,5 +678,5 @@
     }
 
-
+    // XXX EAM : make this trace information??
     //    for (psS32 row = 0 ; row < subImage->numRows; row++) {
     //        for (psS32 col = 0 ; col < subImage->numCols; col++) {
@@ -800,5 +766,5 @@
 XXX: mask values?
 *****************************************************************************/
-bool pmSourceMoments(psSource *source,
+bool pmSourceMoments(pmSource *source,
 		     psF32 radius)
 {
@@ -849,5 +815,5 @@
 	    psS32 imgColCoord = col + source->pixels->col0;
 	    psS32 imgRowCoord = row + source->pixels->row0;
-	    if (CheckRadius(source->peak,
+	    if (checkRadius(source->peak,
 			    radius,
 			    imgColCoord,
@@ -874,6 +840,6 @@
     // XXX EAM - the limit is a bit arbitrary.  make it user defined?
     if ((numPixels < 3) || (Sum <= 0)) {
-      psTrace (".psModules.pmSourceMoments", 5, "no valid pixels for source\n");
-      return (false);
+	psTrace (".psModules.pmSourceMoments", 5, "no valid pixels for source\n");
+	return (false);
     }
 
@@ -890,8 +856,8 @@
     y = Y1/Sum;
     if ((fabs(x) > radius) || (fabs(y) > radius)) {
-      psTrace (".psModules.pmSourceMoments", 5, 
-	       "large centroid swing; invalid peak %d, %d\n", 
-	       source->peak->x, source->peak->y);
-      return (false);
+	psTrace (".psModules.pmSourceMoments", 5, 
+		 "large centroid swing; invalid peak %d, %d\n", 
+		 source->peak->x, source->peak->y);
+	return (false);
     }
 
@@ -914,39 +880,4 @@
 
     return(true);
-
-    // XXX EAM : the following code should be the same as above, but it is not very stable: ignore it
-    # if (0)
-        //
-        // second loop: get the difference sums
-        //
-        X2 = Y2 = 0;
-    for (psS32 row = 0; row < source->pixels->numRows ; row++) {
-        for (psS32 col = 0; col < source->pixels->numCols ; col++) {
-            if ((source->mask != NULL) && (source->mask->data.U8[row][col] != 0)) {
-                psS32 imgColCoord = col + source->pixels->col0;
-                psS32 imgRowCoord = row + source->pixels->row0;
-                if (checkRadius(source->peak,
-                                radius,
-                                imgColCoord,
-                                imgRowCoord)) {
-                    psF32 xDiff = (psF32) (imgColCoord - source->peak->x);
-                    psF32 yDiff = (psF32) (imgRowCoord - source->peak->y);
-                    psF32 pDiff = source->pixels->data.F32[row][col] - sky;
-
-                    Sum+= pDiff;
-                    X2+= PS_SQR(xDiff - x) * pDiff;
-                    Y2+= PS_SQR(yDiff - y) * pDiff;
-                }
-            }
-        }
-    }
-
-    //
-    // second moment X = sqrt (X2/Sum)
-    //
-    source->moments->Sx = (X2/Sum);
-    source->moments->Sy = (Y2/Sum);
-    return(true);
-    # endif
 }
 
@@ -990,6 +921,6 @@
 {
 
-    # define NPIX 10
-    # define SCALE 0.1
+# define NPIX 10
+# define SCALE 0.1
 
     psArray *peaks  = NULL;
@@ -997,6 +928,6 @@
     pmPSFClump psfClump = emptyClump;
 
-    //    PS_PTR_CHECK_NULL(sources, emptyClump);
-    //    PS_PTR_CHECK_NULL(metadata, emptyClump);
+    PS_ASSERT_PTR_NON_NULL(sources, emptyClump);
+    PS_ASSERT_PTR_NON_NULL(metadata, emptyClump);
 
     // find the sigmaX, sigmaY clump
@@ -1010,5 +941,5 @@
         splane = psImageAlloc (NPIX/SCALE, NPIX/SCALE, PS_TYPE_F32);
 	for (int i = 0; i < splane->numRows; i++) {
-	  memset (splane->data.F32[i], 0, splane->numCols*sizeof(PS_TYPE_F32));
+	    memset (splane->data.F32[i], 0, splane->numCols*sizeof(PS_TYPE_F32));
 	}
 
@@ -1139,4 +1070,6 @@
 
 XXX: How can this function ever return FALSE?
+
+XXX EAM : add the saturated mask value to metadata 
 *****************************************************************************/
 
@@ -1178,10 +1111,11 @@
         psF32 A  = 4 * M_PI * sigX * sigY;
         psF32 B  = tmpSrc->moments->Sky;
-        psF32 RT = PS_SQRT_F32(S + (A * B) + (A * PS_SQR(RDNOISE) / PS_SQRT_F32(GAIN)));
-        psF32 SN = (S * PS_SQRT_F32(GAIN) / RT);
+        psF32 RT = sqrt(S + (A * B) + (A * PS_SQR(RDNOISE) / sqrt(GAIN)));
+        psF32 SN = (S * sqrt(GAIN) / RT);
 	tmpSrc->moments->SN = SN;
 
-	// XXX EAM : can we use the value of SATIRATE if mask is NULL?
-	int Nsatpix = pmCountSatPixels (tmpSrc->mask);
+	// XXX EAM : can we use the value of SATURATE if mask is NULL?
+# define MASK_SATURATED 0x02
+	int Nsatpix = psImageCountPixelMask (tmpSrc->mask, MASK_SATURATED);
 
         // saturated star (size consistent with PSF or larger)
@@ -1253,18 +1187,4 @@
 }
 
-int pmCountSatPixels (psImage *image) 
-{
-    int Nsatpix = 0;
-  
-    for (int i = 0; i < image->numRows; i++) {
-	for (int j = 0; j < image->numCols; j++) {
-	    if (image->data.U8[i][j] & 0x02) {
-		Nsatpix ++;
-	    }
-	}
-    }
-    return (Nsatpix);
-}
-
 /******************************************************************************
 pmSourceSetPixelsCircle(source, image, radius)
@@ -1286,5 +1206,5 @@
     PS_ASSERT_PTR_NON_NULL(source, false);
     PS_ASSERT_PTR_NON_NULL(source->moments, false);
-    // PS_ASSERT_PTR_NON_NULL(source->peak, false);
+    PS_ASSERT_PTR_NON_NULL(source->peak, false);
     PS_FLOAT_COMPARE(0.0, radius, false);
 
@@ -1305,29 +1225,4 @@
     psS32 SubImageEndCol    = PS_MIN (image->numCols, SubImageCenterCol + radiusS32 + 1);
 
-    // XXX EAM : this should not be needed: we can never hit this error
-    # if (1)
-
-        if (SubImageStartRow < 0) {
-            psError(PS_ERR_UNKNOWN, true, "Sub image startRow is outside image boundaries (%d).\n",
-                    SubImageStartRow);
-            return(false);
-        }
-    if (SubImageEndRow > image->numRows) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image endRow is outside image boundaries (%d).\n",
-                SubImageEndRow);
-        return(false);
-    }
-    if (SubImageStartCol < 0) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image startCol is outside image boundaries (%d).\n",
-                SubImageStartCol);
-        return(false);
-    }
-    if (SubImageEndCol > image->numCols) {
-        psError(PS_ERR_UNKNOWN, true, "Sub image endCol is outside image boundaries (%d).\n",
-                SubImageEndCol);
-        return(false);
-    }
-    # endif
-
     // XXX: Must recycle image.
     // XXX EAM: this message reflects a programming error we know about.
@@ -1338,10 +1233,4 @@
         psFree(source->pixels);
     }
-    // XXX: Must fix this.  psImageSubset() has different parameters in latest CVS.
-    //    source->pixels = psImageSubset((psImage *) image,
-    //                                   SubImageStartCol,
-    //                                   SubImageStartRow,
-    //                                   SubImageEndCol,
-    //                                   SubImageEndRow);
     source->pixels = psImageSubset((psImage *) image, psRegionSet(SubImageStartCol,
 								  SubImageStartRow,
@@ -1390,14 +1279,13 @@
 functions will be in image, not subImage coords.  Remember this.
 *****************************************************************************/
-psModel *pmSourceModelGuess(psSource *source,
-			    psModelType modelType)
-{
-    PS_PTR_CHECK_NULL(source, false);
-    PS_PTR_CHECK_NULL(source->moments, false);
-    PS_PTR_CHECK_NULL(source->peak, false);
-
-    psModel *model = psModelAlloc(modelType);
-
-    psModelGuessFunc modelGuessFunc = psModelGuessFunc_GetFunction (modelType);
+pmModel *pmSourceModelGuess(pmSource *source,
+			    pmModelType modelType)
+{
+    PS_ASSERT_PTR_NON_NULL(source->moments, false);
+    PS_ASSERT_PTR_NON_NULL(source->peak, false);
+
+    pmModel *model = pmModelAlloc(modelType);
+
+    pmModelGuessFunc modelGuessFunc = pmModelGuessFunc_GetFunction (modelType);
     modelGuessFunc (model, source);
     return(model);
@@ -1425,21 +1313,13 @@
 testing.  Try to reproduce that and debug.
 *****************************************************************************/
-static psF32 evalModel(pmSource *src,
-                       psU32 row,
-                       psU32 col)
-{
-    PS_ASSERT_PTR_NON_NULL(src, false);
-    PS_ASSERT_PTR_NON_NULL(src->modelPSF, false);
-    PS_ASSERT_PTR_NON_NULL(src->modelPSF->params, false);
-
-XXX EAM : I've made this a public function
-XXX EAM : this now uses a psModel as the input
-XXX EAM : it was using src->type to find the model, not model->type
-*****************************************************************************/
-psF32 psModelEval(psModel *model, psImage *image, psS32 col, psS32 row)
-{
-    PS_PTR_CHECK_NULL(image, false);
-    PS_PTR_CHECK_NULL(model, false);
-    PS_PTR_CHECK_NULL(model->params, false);
+
+// XXX EAM : I have made this a public function
+// XXX EAM : this now uses a pmModel as the input
+// XXX EAM : it was using src->type to find the model, not model->type
+psF32 pmModelEval(pmModel *model, psImage *image, psS32 col, psS32 row)
+{
+    PS_ASSERT_PTR_NON_NULL(image, false);
+    PS_ASSERT_PTR_NON_NULL(model, false);
+    PS_ASSERT_PTR_NON_NULL(model->params, false);
 
     // Allocate the x coordinate structure and convert row/col to image space.
@@ -1449,7 +1329,7 @@
     x->data.F32[1] = (psF32) (row + image->row0);
     psF32 tmpF;
-    psModelFunc modelFunc;
-
-    modelFunc = psModelFunc_GetFunction (model->type);
+    pmModelFunc modelFunc;
+
+    modelFunc = pmModelFunc_GetFunction (model->type);
     tmpF = modelFunc (NULL, model->params, x);
     psFree(x);
@@ -1491,7 +1371,7 @@
     }
 
-    // XXX EAM : i changed this to match psModelEval above, but see
+    // XXX EAM : i changed this to match pmModelEval above, but see
     // XXX EAM   the note below in pmSourceContour
-    psF32 oldValue = psModelEval(source->modelFLT, source->pixels, subCol, subRow);
+    psF32 oldValue = pmModelEval(source->modelFLT, source->pixels, subCol, subRow);
     if (oldValue == level) {
         return(((psF32) (subCol + source->pixels->col0)));
@@ -1514,5 +1394,5 @@
 
     while (subCol != lastColumn) {
-        psF32 newValue = psModelEval(source->modelFLT, source->pixels, subCol, subRow);
+        psF32 newValue = pmModelEval(source->modelFLT, source->pixels, subCol, subRow);
         if (oldValue == level) {
             return((psF32) (subCol + source->pixels->col0));
@@ -1674,5 +1554,5 @@
     //           tests below could be conditions (!NULL)
 
-    psModelFunc modelFunc = psModelFunc_GetFunction (model->type);
+    pmModelFunc modelFunc = pmModelFunc_GetFunction (model->type);
 
     psVector *params = model->params;
@@ -1694,6 +1574,6 @@
     }
     if (count <  nParams + 1) {
-      psTrace (".pmObjects.pmSourceFitModel", 4, "insufficient valid pixels\n");
-      return(false);
+	psTrace (".pmObjects.pmSourceFitModel", 4, "insufficient valid pixels\n");
+	return(false);
     }
 
@@ -1722,15 +1602,15 @@
 
     psMinimization *myMin = psMinimizationAlloc(PM_SOURCE_FIT_MODEL_NUM_ITERATIONS,
-                            PM_SOURCE_FIT_MODEL_TOLERANCE);
+						PM_SOURCE_FIT_MODEL_TOLERANCE);
 
     // PSF model only fits first 4 parameters, FLT model fits all
     if (PSF) {
-      paramMask = psVectorAlloc (params->n, PS_TYPE_U8);
-      for (int i = 0; i < 4; i++) {
-	paramMask->data.U8[i] = 0;
-      }
-      for (int i = 4; i < paramMask->n; i++) {
-	paramMask->data.U8[i] = 1;
-      }
+	paramMask = psVectorAlloc (params->n, PS_TYPE_U8);
+	for (int i = 0; i < 4; i++) {
+	    paramMask->data.U8[i] = 0;
+	}
+	for (int i = 4; i < paramMask->n; i++) {
+	    paramMask->data.U8[i] = 1;
+	}
     }       
 
@@ -1782,17 +1662,16 @@
 bool p_pmSourceAddOrSubModel(psImage *image,
 			     psImage *mask,
-                             psModel *model,
+                             pmModel *model,
                              bool center,
                              psS32 flag)
 {
   
-  // XXX EAM : convert to ASSERTS
-  // PS_PTR_CHECK_NULL(model, false);
-  // PS_IMAGE_CHECK_NULL(image, false);
-  // PS_IMAGE_CHECK_TYPE(image, PS_TYPE_F32, false);
+    PS_ASSERT_PTR_NON_NULL(model, false);
+    PS_ASSERT_IMAGE_NON_NULL(image, false);
+    PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, false);
 
     psVector *x = psVectorAlloc(2, PS_TYPE_F32);
     psVector *params = model->params;
-    psModelFunc modelFunc = psModelFunc_GetFunction (model->type);
+    pmModelFunc modelFunc = pmModelFunc_GetFunction (model->type);
     psS32 imageCol;
     psS32 imageRow;
@@ -1842,5 +1721,5 @@
 bool pmSourceAddModel(psImage *image,
 		      psImage *mask,
-                      psModel *model,
+                      pmModel *model,
                       bool center)
 {
@@ -1852,5 +1731,5 @@
 bool pmSourceSubModel(psImage *image,
 		      psImage *mask,
-                      psModel *model,
+                      pmModel *model,
                       bool center)
 {
