Index: trunk/psModules/src/imcombine/pmReadoutCombine.c
===================================================================
--- trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 5170)
+++ trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 5516)
@@ -5,6 +5,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-28 20:43:52 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-15 20:09:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -374,2 +374,89 @@
     return(output);
 }
+
+
+/* This function measures the robust median at each of the minimum and maximum
+ * coordinates and determines the difference and mean of the two values. The size
+ * of the box used to make the measurement at each point is specified by the
+ * configuration variable FRINGE_SQUARE_RADIUS. From the collection of
+ * differences, the robust median is calculated, and returned as part of the
+ * fringe statistics. For each fringe point, the values of delta and midValue are
+ * also assigned and available to the user on return.
+ */
+
+psStats *pmFringeStats(
+    psArray *fringePoints,
+    psImage *image,
+    psMetadata *config)
+{
+    PS_ASSERT_PTR_NON_NULL(fringePoints, NULL);
+    //    for (psS32 i = 0 ; i < fringePoints->n ; i++) {
+    //        if (!psMemCheckFringePoint((pmFringePoint *) fringePoints->data[i])) {
+    //            psError(PS_ERR_UNKNOWN, true, "fringePoints->data[%d] is not of type pmFringePoint.\n");
+    //            return(NULL);
+    //        }
+    //    }
+    PS_ASSERT_IMAGE_NON_NULL(image, NULL);
+    PS_ASSERT_IMAGE_NON_EMPTY(image, NULL);
+    PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, NULL);
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+
+    psBool rc;
+    psF32 frSquareRadius = psMetadataLookupF32(&rc, config, "FRINGE_SQUARE_RADIUS");
+    if (!rc) {
+        psError(PS_ERR_UNKNOWN, true, "Could not determing the fringe radius from the metadata.\n");
+    }
+
+    psRegion minRegion;
+    psRegion maxRegion;
+    psStats *minStats = psStatsAlloc(PS_STAT_ROBUST_MEAN);
+    psStats *maxStats = psStatsAlloc(PS_STAT_ROBUST_MEAN);
+    psStats *diffStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN);
+    psVector *diffs = psVectorAlloc(fringePoints->n, PS_TYPE_F32);
+
+    //
+    // Loop through each fringe point.  Determine the robust mean around
+    // the min and max for that fringe point.  Save the difference between
+    // those two numbers in psVector diffs.
+    //
+    // XXX: Ensure you have the radius correct.  (add 1 to x1 and y1?)
+    //
+    for (psS32 i = 0 ; i < fringePoints->n ; i++) {
+        pmFringePoint *fp = (pmFringePoint *) fringePoints->data[i];
+        minRegion.x0 = fp->xMin - frSquareRadius;
+        minRegion.x1 = fp->xMin + frSquareRadius;
+        minRegion.y0 = fp->yMin - frSquareRadius;
+        minRegion.y1 = fp->yMin + frSquareRadius;
+        psImage *minSubImage = psImageSubset(image, minRegion);
+        minStats = psImageStats(minStats, minSubImage, NULL, 0);
+
+        maxRegion.x0 = fp->xMax - frSquareRadius;
+        maxRegion.x1 = fp->xMax + frSquareRadius;
+        maxRegion.y0 = fp->yMax - frSquareRadius;
+        maxRegion.y1 = fp->yMax + frSquareRadius;
+        psImage *maxSubImage = psImageSubset(image, maxRegion);
+        maxStats = psImageStats(maxStats, maxSubImage, NULL, 0);
+
+        if ((minStats == NULL) || (maxStats == NULL)) {
+            psError(PS_ERR_UNKNOWN, true, "Could not determine robust mean on subimage.\n");
+            psFree(minStats);
+            psFree(maxStats);
+            return(NULL);
+        }
+
+        fp->midValue = 0.5 * (maxStats->robustMean + minStats->robustMean);
+        fp->delta = maxStats->robustMean - minStats->robustMean;
+        diffs->data.F32[i] = fp->delta;
+    }
+    psFree(minStats);
+    psFree(maxStats);
+
+    diffStats = psVectorStats(diffStats, diffs, NULL, NULL, 0);
+    psFree(diffs);
+    if (diffStats == NULL) {
+        psError(PS_ERR_UNKNOWN, true, "Could not determine robust median of the differences.\n");
+        return(NULL);
+    }
+    return(diffStats);
+}
+
