Index: /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.c
===================================================================
--- /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.c	(revision 6758)
+++ /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.c	(revision 6758)
@@ -0,0 +1,126 @@
+/** @file  pmFringeStats.c
+ *
+ *  @author Eugene Magnier, IfA
+ *
+ *  @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-03 06:12:10 $
+ *
+ *  Copyright 2004 IfA
+ */
+
+#include "pslib.h"
+#include "pmFringeStats.h"
+
+static pmFringeStatsFree (pmFringeStats *fringe)
+{
+    psFree (fringe->x);
+    psFree (fringe->y);
+    psFree (fringe->f);
+    psFree (fringe->df);
+    psFree (fringe->mask);
+    return;
+}
+
+pmFringeStats *pmFringeStatsAlloc (int nPts, int dX, int dY, int nX, int nY)
+{
+    pmFringeStats *fringe = psAlloc(sizeof(pmFringeStats));
+    (void)psMemSetDeallocator(fringe, (psFreeFunc)pmFringeStatsFree);
+
+    fringe->x = NULL;
+    fringe->y = NULL;
+    fringe->f = NULL;
+    fringe->df = NULL;
+    fringe->mask = NULL;
+
+    fringe->nRequested = nPts;
+    fringe->nAccepted = 0;
+
+    fringe->dX = dX;
+    fringe->dY = dY;
+    fringe->nX = nX;
+    fringe->nY = nY;
+
+    return fringe;
+}
+
+bool pmFringeStatsCreatePoints (pmFringeStats *fringe, psImage *image)
+{
+
+    double frnd;
+    // create fringe->nRequested
+
+    psRandom *rnd = psRandomAlloc (PS_RANDOM_TAUS, 0);
+
+    fringe->x = psVectorRecycle (fringe->x, fringe->nRequested, PS_TYPE_F32);
+    fringe->y = psVectorRecycle (fringe->y, fringe->nRequested, PS_TYPE_F32);
+
+    nX = image->numCols;
+    nY = image->numRows;
+
+    psF32 *xPt = fringe->x->data.F32;
+    psF32 *yPt = fringe->y->data.F32;
+
+    dX = fringe->dX;
+    dY = fringe->dY;
+
+    // generate random points located within image bounds
+    for (int i = 0; i < fringe->nRequested; i++) {
+        frnd = psRandomUniform (rnd);
+        xPt[i] = (nX - 2*dX)* frnd + dX;
+        frnd = psRandomUniform (rnd);
+        yPt[i] = (nY - 2*dY)* frnd + dY;
+    }
+    return true;
+}
+
+bool pmFringeStatsMeasure(pmFringeStats *fringe, pmReadout *readout)
+{
+    if (fringe->x == NULL) {
+        // create the fringe vectors for this image
+        pmFringeStatsCreatePoints (fringe, readout->image);
+    }
+
+    PS_ASSERT_PTR_NON_NULL (fringe->x, false);
+    PS_ASSERT_PTR_NON_NULL (fringe->y, false);
+
+    psFree (fringe->f);
+    psFree (fringe->df);
+    psFree (fringe->mask);
+
+    fringe->f = psVectorRecycle (fringe->f, fringe->x->n, PS_TYPE_F32);
+    fringe->df = psVectorRecycle (fringe->df, fringe->x->n, PS_TYPE_F32);
+    fringe->mask = psVectorRecycle (fringe->mask, fringe->x->n, PS_TYPE_U8);
+
+    psF32 *xPt = fringe->x->data.F32;
+    psF32 *yPt = fringe->y->data.F32;
+    psF32 *fPt = fringe->f->data.F32;
+    psF32 *dfPt = fringe->df->data.F32;
+
+    int dX = fringe->dX;
+    int dY = fringe->dY;
+
+    psImage *image = readout->image;
+    psImage *mask  = readout->mask;
+
+    psStats *stats = psStatsAlloc (PS_STATS_MEDIAN | PS_STATS_STDEV);
+
+    for (int i = 0; i < fringe->x->n; i++) {
+        region = psRegionSet (xPt[i] - dX, yPt[i] - dY, xPt[i] + dX + 1, YPt[i] + dY + 1);
+        region = psRegionForImage (image, region);
+        raster = psImageSubset (image, region);
+        psImageStats (stats, image, mask, maskVal);
+
+        fPt[i] = stats->sampleMedian;
+        dfPt[i] = stats->sampleStdev;
+    }
+
+    return true;
+}
+psImage *pmFringeCorrect(
+    psImage *out,
+    psMetadata *info,
+    psImage *science,
+    psArray *fringeImage,
+    psArray *fringeStats
+);
+
Index: /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.h
===================================================================
--- /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.h	(revision 6758)
+++ /branches/rel10_ifa/psModules/src/detrend/pmFringeStats.h	(revision 6758)
@@ -0,0 +1,72 @@
+/** @file  pmFringeStats.h
+ *
+ *  @brief Measure Fringe statistics and apply correction
+ *
+ *  @author Eugene Magnier, IfA
+ *
+ *  @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-03 06:12:10 $
+ *
+ *  Copyright 2004 IfA, University of Hawaii
+ */
+
+# ifndef PM_FRINGE_STATS
+# define PM_FRINGE_STATS
+
+/** Fringe Stats structure to hold the fringe statistics
+ *   for a given image
+ */
+typedef struct
+{
+    psU32 nRequested;   // number of fringe points selected
+    psU32 nAccepted;   // number of fringe points not masked
+    psU32 dX;    // median box half-width
+    psU32 dY;    // median box half-height
+    psU32 nX;    // large-scale smoothing in x (col)
+    psU32 nY;    // large-scale smoothing in y (row)
+    psVector x;    // fringe point coordinates (col)
+    psVector y;    // fringe point coordinates (row)
+    psVector f;    // fringe point median
+    psVector df;   // fringe point stdev
+    psVector mask;   // fringe point on/off mask
+}
+pmFringeStats;
+
+pmFringeStats *pmFringeStatsAlloc (
+    int nPts,     // number of points to create
+    int dX,     // half-width of fringe boxes
+    int dY,     // half-height of fringe boxes
+    int nX,     // smoothing scale in x
+    int nY    // smoothing scale in y
+);
+
+/** Measure the fringe stats for an image
+ *
+ *  Given an input image and a fringe stats structure, measure the statistics for each of the fringe points on
+ *  the image.  If the fringe vectors are NULL, new vectors are created.
+ *
+ *  XXX should the structure carry the image dimensions for validation?
+ *
+ *  @return  bool: True or false for success or failure
+ */
+bool pmFringeStatsMeasure(
+    pmFringeStats *fringe,  ///< results are placed here
+    psImage *image   ///< measure fringes on this image
+);
+
+/** Fringe correct the science image
+ *
+ *  Given a science image, a set of fringe stats and a matched set of fringe images, correct the science image
+ *  for the fringe images.  
+ *
+ *  @return  psImage: corrected science image
+ */
+psImage *pmFringeCorrect(
+    psImage *out,   // place corrected image here (allocate if NULL)
+    psMetadata *info,   // place info about results here
+    psImage *science,   // image to be corrected
+    psArray *fringeImage,  // fringe images to use in correction
+    psArray *fringeStats  // fringe stats to use in correction
+);
+
+# endif
