Index: trunk/psModules/src/detrend/pmFlatField.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatField.c	(revision 7275)
+++ trunk/psModules/src/detrend/pmFlatField.c	(revision 7278)
@@ -1,8 +1,2 @@
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// XXX WARNING: I have completely replaced this file with an OLD VERSION (that works) instead of the
-// one that was being worked on.
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
 /** @file  pmFlatField.c
  *
@@ -24,6 +18,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 18:10:08 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-02 00:55:23 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -46,6 +40,15 @@
 bool pmFlatField(pmReadout *in, const pmReadout *flat)
 {
-    int i = 0;
-    int j = 0;
+    PS_ASSERT_PTR_NON_NULL(in, false);
+    PS_ASSERT_PTR_NON_NULL(in->image, false);
+    PS_ASSERT_IMAGE_NON_EMPTY(in->image, false);
+    PS_ASSERT_PTR_NON_NULL(flat, false);
+    PS_ASSERT_PTR_NON_NULL(flat->image, false);
+    PS_ASSERT_IMAGE_NON_EMPTY(flat->image, false);
+    if (in->mask) {
+        PS_ASSERT_IMAGE_TYPE(in->mask, PS_TYPE_U8, false);
+    }
+    PS_ASSERT_IMAGE_TYPE(flat->image, in->image->type.type, false);
+
     int totOffCol = 0;
     int totOffRow = 0;
@@ -147,39 +150,33 @@
 
     // Macro for all PS types
-    #define PM_FLAT_DIVISION(TYPE)                                                                           \
-case PS_TYPE_##TYPE:                                                                                         \
-    /* Per Eugene's request, use two sets of loops: first to fill mask, second to avoid div with bad pix */  \
-    for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
-        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
-            if(flatImage->data.TYPE[j][i] <= 0.0) {                                                          \
-                /* Negative or zero flat pixels shall be masked in input image as  PM_MASK_FLAT */           \
-                if (inMask) {                                                                                \
-                    inMask->data.PS_TYPE_MASK_DATA[j][i] |= PM_MASK_FLAT;                                    \
-                }                                                                                            \
-                flatImage->data.TYPE[j][i] = 0.0;                                                            \
-            }                                                                                                \
-        }                                                                                                    \
-    }                                                                                                        \
-    for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
-        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
-            if(inMask && !inMask->data.PS_TYPE_MASK_DATA[j][i]) {                                            \
-                /* Module shall divide the input image by the flat-fielded image */                          \
-                inImage->data.TYPE[j][i] /= flatImage->data.TYPE[j][i];                                      \
-            }                                                                                                \
-        }                                                                                                    \
-    }                                                                                                        \
+    #define FLAT_DIVISION_CASE(TYPE) \
+case PS_TYPE_##TYPE: \
+    for (long j = totOffRow; j < inImage->numRows; j++) { \
+        for (long i = totOffCol; i < inImage->numCols; i++) { \
+            if (flatImage->data.TYPE[j][i] <= 0.0) { \
+                if (inMask) { \
+                    inMask->data.U8[j][i] |= PM_MASK_FLAT; \
+                } \
+                flatImage->data.TYPE[j][i] = 0.0; \
+            } else { \
+                if (!inMask || !inMask->data.U8[j][i]) { \
+                    inImage->data.TYPE[j][i] /= flatImage->data.TYPE[j][i]; \
+                } \
+            } \
+        } \
+    } \
     break;
 
     switch(inType) {
-        PM_FLAT_DIVISION(U8);
-        PM_FLAT_DIVISION(U16);
-        PM_FLAT_DIVISION(U32);
-        PM_FLAT_DIVISION(U64);
-        PM_FLAT_DIVISION(S8);
-        PM_FLAT_DIVISION(S16);
-        PM_FLAT_DIVISION(S32);
-        PM_FLAT_DIVISION(S64);
-        PM_FLAT_DIVISION(F32);
-        PM_FLAT_DIVISION(F64);
+        FLAT_DIVISION_CASE(U8);
+        FLAT_DIVISION_CASE(U16);
+        FLAT_DIVISION_CASE(U32);
+        FLAT_DIVISION_CASE(U64);
+        FLAT_DIVISION_CASE(S8);
+        FLAT_DIVISION_CASE(S16);
+        FLAT_DIVISION_CASE(S32);
+        FLAT_DIVISION_CASE(S64);
+        FLAT_DIVISION_CASE(F32);
+        FLAT_DIVISION_CASE(F64);
     default:
         psError( PS_ERR_BAD_PARAMETER_TYPE, true,
Index: trunk/psModules/src/detrend/pmFlatField.h
===================================================================
--- trunk/psModules/src/detrend/pmFlatField.h	(revision 7275)
+++ trunk/psModules/src/detrend/pmFlatField.h	(revision 7278)
@@ -1,8 +1,2 @@
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// XXX WARNING: I have completely replaced this file with an OLD VERSION (that works) instead of the
-// one that was being worked on.
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
 /** @file  pmFlatField.h
  *
@@ -24,6 +18,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 18:01:05 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-02 00:55:23 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
Index: trunk/psModules/src/detrend/pmFlatNormalize.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatNormalize.c	(revision 7275)
+++ trunk/psModules/src/detrend/pmFlatNormalize.c	(revision 7278)
@@ -15,4 +15,7 @@
                          )
 {
+    PS_ASSERT_PTR_NON_NULL(chipGains, NULL);
+    PS_ASSERT_PTR_NON_NULL(fluxLevels, NULL);
+
     int numSources = fluxLevels->numRows; // Number of integrations
     int numChips = fluxLevels->numCols; // Number of chips with which each integration is made
Index: trunk/psModules/src/detrend/pmFringeStats.c
===================================================================
--- trunk/psModules/src/detrend/pmFringeStats.c	(revision 7275)
+++ trunk/psModules/src/detrend/pmFringeStats.c	(revision 7278)
@@ -3,6 +3,6 @@
  *  @author Eugene Magnier, IfA
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-17 00:55:35 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-02 00:55:23 $
  *
  *  Copyright 2004 IfA
@@ -61,4 +61,7 @@
 bool pmFringeRegionsCreatePoints(pmFringeRegions *fringe, const psImage *image)
 {
+    PS_ASSERT_PTR_NON_NULL(fringe, false);
+    PS_ASSERT_PTR_NON_NULL(image, false);
+    PS_ASSERT_IMAGE_NON_EMPTY(image, false);
 
     double frnd;
@@ -106,4 +109,6 @@
 pmFringeStats *pmFringeStatsAlloc(pmFringeRegions *regions)
 {
+    PS_ASSERT_PTR_NON_NULL(regions, false);
+
     pmFringeStats *stats = psAlloc(sizeof(pmFringeStats));
     (void)psMemSetDeallocator(stats, (psFreeFunc)fringeStatsFree);
@@ -120,4 +125,9 @@
 pmFringeStats *pmFringeStatsMeasure(pmFringeRegions *fringe, pmReadout *readout, psMaskType maskVal)
 {
+    PS_ASSERT_PTR_NON_NULL(fringe, NULL);
+    PS_ASSERT_PTR_NON_NULL(readout, NULL);
+    PS_ASSERT_PTR_NON_NULL(readout->image, NULL);
+    PS_ASSERT_IMAGE_NON_EMPTY(readout->image, NULL);
+
     if (!fringe->x || !fringe->y) {
         // create the fringe vectors for this image
@@ -364,4 +374,9 @@
                                     unsigned int nIter, float keepFrac)
 {
+    PS_ASSERT_PTR_NON_NULL(science, NULL);
+    PS_ASSERT_PTR_NON_NULL(fringes, NULL);
+    PS_ASSERT_INT_POSITIVE(fringes->n, NULL);
+    PS_ASSERT_INT_POSITIVE(nIter, NULL);
+
     pmFringeRegions *regions = science->regions; // The fringe regions
     int numRegions = regions->nRequested; // Number of regions
@@ -439,4 +454,13 @@
                          unsigned int nIter, float keepFrac)
 {
+    PS_ASSERT_PTR_NON_NULL(readout, NULL);
+    PS_ASSERT_PTR_NON_NULL(readout->image, NULL);
+    PS_ASSERT_IMAGE_NON_EMPTY(readout->image, NULL);
+    PS_ASSERT_PTR_NON_NULL(fringes, NULL);
+    PS_ASSERT_PTR_NON_NULL(fringeImages, NULL);
+    PS_ASSERT_PTR_NON_NULL(fringeStats, NULL);
+    PS_ASSERT_INT_EQUAL(fringeImages->n, fringeStats->n, NULL);
+    PS_ASSERT_INT_POSITIVE(nIter, NULL);
+
     // measure the fringe stats for the science frame and solve for the scales
     pmFringeStats *scienceStats = pmFringeStatsMeasure(fringes, readout, maskVal);
Index: trunk/psModules/src/detrend/pmSubtractBias.c
===================================================================
--- trunk/psModules/src/detrend/pmSubtractBias.c	(revision 7275)
+++ trunk/psModules/src/detrend/pmSubtractBias.c	(revision 7278)
@@ -11,6 +11,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-01 01:56:29 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-02 00:55:23 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -146,24 +146,4 @@
 
 
-#if 0
-/******************************************************************************
-ImageSubtractScalar(): subtract a scalar from the input image.
- 
-XXX: Use a psLib function for this.
- 
-XXX: This should
- *****************************************************************************/
-static psImage *ImageSubtractScalar(psImage *image,
-                                    psF32 scalar)
-{
-    for (psS32 i=0;i<image->numRows;i++) {
-        for (psS32 j=0;j<image->numCols;j++) {
-            image->data.F32[i][j]-= scalar;
-        }
-    }
-    return(image);
-}
-#endif
-
 /******************************************************************************
 GenNewStatOptions(): this routine will take as input the options member of the
@@ -175,4 +155,6 @@
 static psStatsOptions GenNewStatOptions(const psStats *stat)
 {
+    assert(stat);
+
     psS32 numOptions = 0;
     psStatsOptions opt = 0;
@@ -210,116 +192,10 @@
     if (numOptions != 1) {
         psLogMsg(__func__, PS_LOG_WARN,
-                 "WARNING: pmSubtractBias.c: GenNewStatOptions(): Too many statistics options have been specified\n");
-    }
-    return(opt);
-}
-
-
-
-#if 0
-/******************************************************************************
-ScaleOverscanVector(): this routine takes as input an arbitrary vector,
-creates a new vector of length n, and fills the new vector with the
-interpolated values of the old vector.  The type of interpolation is:
-    PM_FIT_POLYNOMIAL: fit a polynomial to the entire input vector data.
-    PM_FIT_SPLINE: fit splines to the input vector data.
-XXX: Doesn't it make more sense to do polynomial interpolation on a few
-elements of the input vector, rather than fit a polynomial to the entire
-vector?
- *****************************************************************************/
-static psVector *ScaleOverscanVector(psVector *overscanVector,
-                                     psS32 n,
-                                     void *fitSpec,
-                                     pmFit fit)
-{
-    psTrace(".psModule.pmSubtracBias.ScaleOverscanVector", 4,
-            "---- ScaleOverscanVector() begin (%d -> %d) ----\n", overscanVector->n, n);
-    //    PS_VECTOR_PRINT_F32(overscanVector);
-
-    if (NULL == overscanVector) {
-        return(overscanVector);
-    }
-
-    // Allocate the new vector.
-    psVector *newVec = psVectorAlloc(n, PS_TYPE_F32);
-
-    //
-    // If the new vector is the same size as the old, simply copy the data.
-    //
-    if (n == overscanVector->n) {
-        for (psS32 i = 0 ; i < n ; i++) {
-            newVec->data.F32[i] = overscanVector->data.F32[i];
-        }
-        return(newVec);
-    }
-    psPolynomial1D *myPoly;
-    psSpline1D *mySpline;
-    psF32 x;
-    psS32 i;
-    if (fit == PM_FIT_POLYNOMIAL) {
-        // Fit a polynomial to the old overscan vector.
-        myPoly = (psPolynomial1D *) fitSpec;
-        PS_ASSERT_POLY_NON_NULL(myPoly, NULL);
-        myPoly = psVectorFitPolynomial1D(myPoly, NULL, 0, overscanVector, NULL, NULL);
-        if (myPoly == NULL) {
-            psError(PS_ERR_UNKNOWN, false, "ScaleOverscanVector()(1): Could not fit a polynomial to the psVector.\n");
-            return(NULL);
-        }
-
-        // For each element of the new vector, convert the x-ordinate to that
-        // of the old vector, use the fitted polynomial to determine the
-        // interpolated value at that point, and set the new vector.
-        for (i=0;i<n;i++) {
-            x = ((psF32) i) * ((psF32) overscanVector->n) / ((psF32) n);
-            newVec->data.F32[i] = psPolynomial1DEval(myPoly, x);
-        }
-    } else if (fit == PM_FIT_SPLINE) {
-        psS32 mustFreeSpline = 0;
-        // Fit a spline to the old overscan vector.
-        mySpline = (psSpline1D *) fitSpec;
-        // XXX: Does it make any sense to have a psSpline argument?
-        if (mySpline == NULL) {
-            mustFreeSpline = 1;
-        }
-
-        //
-        // NOTE: Since the X arg in the psVectorFitSpline1D() function is NULL,
-        // splines endpoints will be from 0.0 to overscanVector->n-1.  Must scale
-        // properly when doing the spline eval.
-        //
-        //        mySpline = psVectorFitSpline1D(mySpline, NULL, overscanVector, NULL);
-        mySpline = psVectorFitSpline1D(NULL, overscanVector);
-        if (mySpline == NULL) {
-            psError(PS_ERR_UNKNOWN, false, "ScaleOverscanVector()(2): Could not fit a spline to the psVector.\n");
-            return(NULL);
-        }
-        //        PS_PRINT_SPLINE(mySpline);
-
-        // For each element of the new vector, convert the x-ordinate to that
-        // of the old vector, use the fitted polynomial to determine the
-        // interpolated value at that point, and set the new vector.
-        for (i=0;i<n;i++) {
-            // Scale to [0 : overscanVector->n - 1]
-            x = ((psF32) i) * ((psF32) (overscanVector->n-1)) / ((psF32) n);
-            newVec->data.F32[i] = psSpline1DEval(mySpline, x);
-        }
-        if (mustFreeSpline ==1) {
-            psFree(mySpline);
-        }
-        //        PS_VECTOR_PRINT_F32(newVec);
-
-
-    } else {
-        psError(PS_ERR_UNKNOWN, true, "unknown fit type.  Returning NULL.\n");
-        psFree(newVec);
-        return(NULL);
-    }
-
-    psTrace(".psModule.pmSubtracBias.ScaleOverscanVector", 4,
-            "---- ScaleOverscanVector() exit ----\n");
-    return(newVec);
-}
-
-#endif
+                 "WARNING: pmSubtractBias.c: GenNewStatOptions(): Too many statistics options "
+                 "have been specified\n");
+    }
+    return opt;
+}
+
 
 // Produce an overscan vector from an array of pixels
@@ -329,4 +205,8 @@
                                )
 {
+    assert(overscanOpts);
+    assert(pixels);
+    assert(myStats);
+
     // Reduce the overscans
     psVector *reduced = psVectorAlloc(pixels->n, PS_TYPE_F32); // Overscan for each row
@@ -416,7 +296,16 @@
     psTrace(".psModule.pmSubtracBias.pmSubtractBias", 4,
             "---- pmSubtractBias() begin ----\n");
-    PS_ASSERT_READOUT_NON_NULL(in, NULL);
-    PS_ASSERT_READOUT_NON_EMPTY(in, NULL);
-    PS_ASSERT_READOUT_TYPE(in, PS_TYPE_F32, NULL);
+    PS_ASSERT_PTR_NON_NULL(in, NULL);
+    PS_ASSERT_IMAGE_NON_NULL(in->image, NULL);
+    PS_ASSERT_IMAGE_TYPE(in->image, PS_TYPE_F32, NULL);
+    PS_ASSERT_PTR_NON_NULL(overscanOpts, NULL);
+    if (bias) {
+        PS_ASSERT_IMAGE_NON_NULL(bias->image, NULL);
+        PS_ASSERT_IMAGE_TYPE(bias->image, PS_TYPE_F32, NULL);
+    }
+    if (dark) {
+        PS_ASSERT_IMAGE_NON_NULL(dark->image, NULL);
+        PS_ASSERT_IMAGE_TYPE(dark->image, PS_TYPE_F32, NULL);
+    }
 
     psImage *image = in->image;         // The input image
