Index: /branches/rel10_ifa/psModules/src/astrom/Makefile.am
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/Makefile.am	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/astrom/Makefile.am	(revision 6899)
@@ -7,5 +7,4 @@
 	pmFPAConstruct.c \
 	pmFPACopy.c \
-	pmFPAMaskWeight.c \
 	pmFPARead.c \
 	pmFPAUtils.c \
@@ -22,4 +21,5 @@
 	pmFPAfile.c
 
+#	pmFPAMaskWeight.c \
 #	pmFPAAstrometry.c
 #	pmAstrometryObjects.c
@@ -31,5 +31,4 @@
 	pmFPAConstruct.h \
 	pmFPACopy.h \
-	pmFPAMaskWeight.h \
 	pmFPARead.h \
 	pmFPAUtils.h \
@@ -46,4 +45,5 @@
 	pmFPAfile.h
 
+#	pmFPAMaskWeight.h \
 #	pmFPAAstrometry.h
 #	pmAstrometryObjects.h
Index: /branches/rel10_ifa/psModules/src/astrom/pmReadout.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmReadout.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/astrom/pmReadout.c	(revision 6899)
@@ -2,6 +2,6 @@
 #include "pslib.h"
 #include "pmFPA.h"
+#include "pmMaskBadPixels.h"
 #include "pmHDUUtils.h"
-#include "pmFPAMaskWeight.h"
 
 // Get the bias images for a readout, using the CELL.BIASSEC
Index: /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.c
===================================================================
--- /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.c	(revision 6899)
@@ -24,6 +24,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.3.12.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 03:10:27 $
+ *  @version $Revision: 1.3.12.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -148,8 +148,8 @@
             /* Pixels which satisfy growVal and within the grow radius shall be masked */                    \
             if(mask->data.PS_TYPE_MASK_DATA[j][i] & growVal) {                                               \
-                rowMin = MAX(j-grow, 0);                                                                     \
-                rowMax = MIN(j+grow+1, inImage->numRows);                                                    \
-                colMin = MAX(i-grow, 0);                                                                     \
-                colMax = MIN(i+grow+1, inImage->numCols);                                                    \
+                rowMin = PS_MAX(j-grow, 0);                                                                     \
+                rowMax = PS_MIN(j+grow+1, inImage->numRows);                                                    \
+                colMin = PS_MAX(i-grow, 0);                                                                     \
+                colMax = PS_MIN(i+grow+1, inImage->numCols);                                                    \
                 for(jj=rowMin; jj<rowMax; jj++) {                                                            \
                     for(ii=colMin; ii<colMax; ii++) {                                                        \
Index: /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.h
===================================================================
--- /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.h	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/detrend/pmMaskBadPixels.h	(revision 6899)
@@ -24,6 +24,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.2.12.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-02-17 17:13:41 $
+ *  @version $Revision: 1.2.12.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -33,19 +33,27 @@
 #include "pmFPA.h"
 
+// these defines are necessary to yield 8-bit results (use instead of ~)
+# define NOT_U8(A)(UCHAR_MAX-(A))
+# define NOT_U16(A)(USHORT_MAX-(A))
+
 /** Mask values */
 typedef enum {
-    PM_MASK_TRAP    = 0x0001,   ///< The pixel is a charge trap.
-    PM_MASK_BADCOL  = 0x0002,   ///< The pixel is a bad column.
-    PM_MASK_SAT     = 0x0004,   ///< The pixel is saturated.
-    PM_MASK_BAD     = 0x0008,   ///< The pixel is low
-    PM_MASK_FLAT    = 0x0010    ///< The pixel is non-positive in the flat-field.
+    PM_MASK_CLEAR   = 0x00,   ///< The pixel is a charge trap.
+    PM_MASK_TRAP    = 0x01,   ///< The pixel is a charge trap.
+    PM_MASK_BADCOL  = 0x02,   ///< The pixel is a bad column.
+    PM_MASK_SAT     = 0x04,   ///< The pixel is saturated.
+    PM_MASK_BAD     = 0x08,   ///< The pixel is low
+    PM_MASK_FLAT    = 0x10,   ///< The pixel is non-positive in the flat-field.
+    PM_MASK_MARK    = 0x20,   ///< The pixel is marked as temporarily ignored
+    PM_MASK_EXT1    = 0x40,   ///< This mask value is not used
+    PM_MASK_EXT2    = 0x80,   ///< This mask value is not used
 } pmMaskValue;
 
-/** Macro to find maximum of two numbers */
-#define MAX(A,B)((A)>=(B)?(A):(B))
-
-/** Macro to find minimum of two numbers */
-#define MIN(A,B)((A)<=(B)?(A):(B))
-
+bool pmReadoutSetMask(pmReadout *readout // Readout for which to set mask
+                     );
+bool pmReadoutSetWeight(pmReadout *readout // Readout for which to set weight
+                       );
+bool pmCellSetMaskWeight(pmCell *cell // Cell for which to set weights
+                        );
 
 /** Execute bad pixels module.
Index: /branches/rel10_ifa/psModules/src/objects/models/pmModel_QGAUSS.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/models/pmModel_QGAUSS.c	(revision 6899)
@@ -26,7 +26,27 @@
     psF32 py = PAR[5]*Y;
     psF32 z  = 0.5*PS_SQR(px) + 0.5*PS_SQR(py) + PAR[6]*X*Y;
+    if (z <= 0) {
+        if (deriv) {
+            psF32 *dPAR = deriv->data.F32;
+            dPAR[0] = 0.0;
+            dPAR[1] = 0.0;
+            dPAR[2] = 0.0;
+            dPAR[3] = 0.0;
+            dPAR[4] = 0.0;
+            dPAR[5] = 0.0;
+            dPAR[6] = 0.0;
+            dPAR[7] = 0.0;
+        }
+        return(0.0);
+    }
+
     psF32 zp = pow(z,1.25);
+    if (isnan(zp))
+        psAbort ("psMinLMM", "nan in zp");
 
     psF32 r  = 1.0 / (1 + PAR[7]*z + z*zp);
+    if (isnan(r))
+        psAbort ("psMinLMM", "nan in r");
+
     // test: psF32 r  = 1.0 / (1 + PAR[7]*z + PS_SQR(z));
     psF32 r1 = PAR[1]*r;
@@ -111,5 +131,6 @@
 psF64 pmModelFlux_QGAUSS(const psVector *params)
 {
-    float f, norm, z;
+    float z;
+    float norm;
 
     psF32 *PAR = params->data.F32;
@@ -122,11 +143,31 @@
 
     // the area needs to be multiplied by the integral of f(z)
+    // XXX this integral can be done faster and more accurately
     norm = 0.0;
-    for (z = 0.05; z < 50; z += 0.1) {
+
+    # define DZ 0.1
+
+    # if 0
+
+    float f;
+for (z = 0.5*DZ; z < 50; z += DZ) {
         f = 1.0 / (1 + PAR[7]*z + pow(z, 2.25));
         // test: f = 1.0 / (1 + PAR[7]*z + PS_SQR(z));
         norm += f;
     }
-    norm *= 0.1;
+    norm *= DZ;
+    # else
+
+        float f0 = 1.0;
+    float f1, f2;
+    for (z = DZ; z < 50; z += DZ) {
+        f1 = 1.0 / (1 + PAR[7]*z + pow(z, 2.25));
+        z += DZ;
+        f2 = 1.0 / (1 + PAR[7]*z + pow(z, 2.25));
+        norm += f0 + 4*f1 + f2;
+        f0 = f2;
+    }
+    norm *= DZ / 3.0;
+    # endif
 
     psF64 Flux = PAR[1] * Area * norm;
Index: /branches/rel10_ifa/psModules/src/objects/pmModelGroup.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmModelGroup.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmModelGroup.c	(revision 6899)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.2.4.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 03:14:23 $
+ *  @version $Revision: 1.2.4.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
Index: /branches/rel10_ifa/psModules/src/objects/pmPSFtry.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmPSFtry.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmPSFtry.c	(revision 6899)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.4.4.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-13 06:18:46 $
+ *  @version $Revision: 1.4.4.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -15,4 +15,5 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmMaskBadPixels.h"
 #include "pmPeaks.h"
 #include "pmMoments.h"
@@ -112,7 +113,7 @@
         // fit model as EXT, not PSF
 
-        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_SOURCE_MASK_MARKED);
+        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_MASK_MARK);
         status = pmSourceFitModel (source, model, PM_SOURCE_FIT_EXT);
-        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PM_SOURCE_MASK_MARKED);
+        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", NOT_U8(PM_MASK_MARK));
 
         // exclude the poor fits
@@ -146,5 +147,5 @@
         y = source->peak->y;
 
-        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_SOURCE_MASK_MARKED);
+        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_MASK_MARK);
         status = pmSourceFitModel (source, modelPSF, PM_SOURCE_FIT_PSF);
 
@@ -176,5 +177,5 @@
 
 next_source:
-        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PM_SOURCE_MASK_MARKED);
+        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", NOT_U8(PM_MASK_MARK));
 
     }
Index: /branches/rel10_ifa/psModules/src/objects/pmSource.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSource.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmSource.c	(revision 6899)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-14 21:43:59 $
+ *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmMaskBadPixels.h"
 #include "pmPeaks.h"
 #include "pmMoments.h"
@@ -141,5 +142,5 @@
     psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
 
-    # define NPIX 10
+    // XXX SCALE is the bin size in pixels for the PSF clump search
     # define SCALE 0.1
 
@@ -164,4 +165,8 @@
         if (!status)
             SY_MAX = 10.0;
+        psF32 AR_MAX = psMetadataLookupF32 (&status, metadata, "MOMENTS_AR_MAX");
+        if (!status)
+            AR_MAX = 10.0;
+        psF32 AR_MIN = 1.0 / AR_MAX;
 
         // construct a sigma-plane image
@@ -185,5 +190,14 @@
 
             // Sx,Sy are limited at 0.  a peak at 0,0 is artificial
-            if ((fabs(tmpSrc->moments->Sx) < FLT_EPSILON) && (fabs(tmpSrc->moments->Sy) < FLT_EPSILON)) {
+            if (fabs(tmpSrc->moments->Sx) < 0.05) {
+                continue;
+            }
+            if (fabs(tmpSrc->moments->Sy) < 0.05) {
+                continue;
+            }
+            if ((tmpSrc->moments->Sx / tmpSrc->moments->Sy) > AR_MAX) {
+                continue;
+            }
+            if ((tmpSrc->moments->Sx / tmpSrc->moments->Sy) < AR_MIN) {
                 continue;
             }
@@ -341,5 +355,5 @@
         // inner = psRegionForSquare (tmpSrc->peak->x - tmpSrc->mask->col0, tmpSrc->peak->y - tmpSrc->mask->row0, 2);
         inner = psRegionForSquare (tmpSrc->peak->x, tmpSrc->peak->y, 2);
-        int Nsatpix = psImageCountPixelMask (tmpSrc->mask, inner, PM_SOURCE_MASK_SATURATED);
+        int Nsatpix = psImageCountPixelMask (tmpSrc->mask, inner, PM_MASK_SAT);
 
         // saturated star (size consistent with PSF or larger)
Index: /branches/rel10_ifa/psModules/src/objects/pmSource.h
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSource.h	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmSource.h	(revision 6899)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-14 21:43:59 $
+ *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -21,4 +21,6 @@
  * values with specific meanings that other functions can add to or define?
  */
+
+/*
 typedef enum {
     PM_SOURCE_MASK_CLEAR     = 0x00,
@@ -27,4 +29,5 @@
     PM_SOURCE_MASK_MARKED    = 0x08,
 } psphotMaskValues;
+*/
 
 /** pmSourceType enumeration
Index: /branches/rel10_ifa/psModules/src/objects/pmSourcePhotometry.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSourcePhotometry.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmSourcePhotometry.c	(revision 6899)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 17:10:08 $
+ *  @version $Revision: 1.1.2.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -16,4 +16,5 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmMaskBadPixels.h"
 #include "pmPeaks.h"
 #include "pmMoments.h"
@@ -117,5 +118,5 @@
 
     // set aperture mask circle to model radius
-    psImageKeepCircle (source->mask, x, y, model->radiusTMP, "OR", PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, model->radiusTMP, "OR", PM_MASK_MARK);
 
     // measure the weight of included pixels
@@ -139,5 +140,5 @@
 
     // unmask aperture
-    psImageKeepCircle (source->mask, x, y, model->radiusTMP, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageKeepCircle (source->mask, x, y, model->radiusTMP, "AND", NOT_U8(PM_MASK_MARK));
 
     // subtract object, leave local sky
Index: /branches/rel10_ifa/psModules/src/objects/pmSourceSky.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSourceSky.c	(revision 6898)
+++ /branches/rel10_ifa/psModules/src/objects/pmSourceSky.c	(revision 6899)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 03:14:23 $
+ *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-19 06:31:55 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmMaskBadPixels.h"
 #include "pmPeaks.h"
 #include "pmMoments.h"
@@ -56,8 +57,9 @@
     srcRegion = psRegionForImage(mask, srcRegion);
 
-    psImageMaskRegion(mask, srcRegion, "OR", PM_SOURCE_MASK_MARKED);
+    psImageMaskRegion(mask, srcRegion, "OR", PM_MASK_MARK);
     psStats *myStats = psStatsAlloc(statsOptions);
     myStats = psImageStats(myStats, image, mask, 0xff);
-    psImageMaskRegion(mask, srcRegion, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageMaskRegion(mask, srcRegion, "AND", NOT_U8(PM_MASK_EXT2));
+
 
     psF64 tmpF64;
@@ -99,8 +101,8 @@
     srcRegion = psRegionForImage(mask, srcRegion);
 
-    psImageMaskRegion(mask, srcRegion, "OR", PM_SOURCE_MASK_MARKED);
+    psImageMaskRegion(mask, srcRegion, "OR", PM_MASK_MARK);
     psStats *myStats = psStatsAlloc(statsOptions);
     myStats = psImageStats(myStats, image, mask, 0xff);
-    psImageMaskRegion(mask, srcRegion, "AND", ~PM_SOURCE_MASK_MARKED);
+    psImageMaskRegion(mask, srcRegion, "AND", NOT_U8(PM_MASK_MARK));
 
     psF64 tmpF64;
