Index: /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.c
===================================================================
--- /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.c	(revision 23405)
+++ /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.c	(revision 23406)
@@ -5,4 +5,8 @@
 #include "ppImage.h"
 
+// XXX I originally coded this to create a new pmFPAfile, but in retrospect it makes more sense
+// to treat this function as an operation on the input image
+
+// XXX make the choice of stats optional
 bool ppImageCheckCTE(pmConfig *config, ppImageOptions *options, pmFPAview *view)
 {
@@ -34,29 +38,21 @@
     psImageBinningSetSkip(binning, image);
 
-    pmCell *inCell  = pmFPAfileThisCell (config->files, view, "PPIMAGE.INPUT");
-    pmCell *outCell = pmFPAfileThisCell (config->files, view, "PPIMAGE.CTEMAP");
-    if (!pmCellCopyStructure(outCell, inCell, binning->nXbin, binning->nYbin)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to copy cell structure.");
-        return false;
-    }
+    psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN);
+    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
+    psImageBackground (stats, NULL, image, mask, 0xffff, rng);
+    float cellMedian = stats->robustMedian;
+    psFree (stats);
 
-    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPIMAGE.CTEMAP");
-    psImage *output = outRO->image;
+    // stats = psStatsAlloc (PS_STAT_SAMPLE_STDEV);
+    // psStats *statsDefaults = psStatsAlloc (PS_STAT_SAMPLE_STDEV);
 
-    // Don't care about the bias: get rid of it, if present
-    psFree(outRO->bias);
-    outRO->bias = psListAlloc(NULL);
-    psMetadataItem *biassec = psMetadataLookup(outCell->concepts, "CELL.BIASSEC");
-    psFree(biassec->data.V);
-    biassec->data.V = psListAlloc(NULL);
-
-    psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
-    psStats *statsDefaults = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
+    stats = psStatsAlloc (PS_STAT_ROBUST_STDEV);
+    psStats *statsDefaults = psStatsAlloc (PS_STAT_ROBUST_STDEV);
 
     // measure median and variance for subimages
     psRegion ruffRegion = {0,0,0,0};
     psRegion fineRegion = {0,0,0,0};
-    for (int iy = 0; iy < output->numRows; iy++) {
-        for (int ix = 0; ix < output->numCols; ix++) {
+    for (int iy = 0; iy < binning->nYruff; iy++) {
+        for (int ix = 0; ix < binning->nXruff; ix++) {
 
             // convert the ruff grid cell to the equivalent fine grid cell
@@ -84,8 +80,16 @@
             statsDefaults->tmpData = NULL;
 
-            psImageStats (stats, subset, submask, 0);
+            psImageStats (stats, subset, submask, 0xffff);
 
-            // XXX need to apply the gain as well
-            output->data.F32[iy][ix] = PS_SQR(stats->sampleStdev) / stats->sampleMedian;
+	    // XXX need to apply the gain as well
+	    float normVariance = PS_SQR(stats->robustStdev) / cellMedian;
+	    // float normVariance = PS_SQR(stats->sampleStdev) / cellMedian;
+
+	    // apply resulting value to the input pixels
+	    for (int jy = fineRegion.y0; jy < fineRegion.y1; jy++) {
+	      for (int jx = fineRegion.x0; jx < fineRegion.x1; jx++) {
+		image->data.F32[jy][jx] = normVariance;
+	      }
+	    }
 
             psFree (subset);
@@ -94,4 +98,5 @@
     }
 
+    psFree (rng);
     psFree (binning);
     psFree (stats);
Index: /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.v1.c
===================================================================
--- /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.v1.c	(revision 23406)
+++ /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageCheckCTE.v1.c	(revision 23406)
@@ -0,0 +1,101 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "ppImage.h"
+
+bool ppImageCheckCTE(pmConfig *config, ppImageOptions *options, pmFPAview *view)
+{
+    bool status;
+
+    // this step is complete optional.
+    if (!options->checkCTE) {
+        return true;
+    }
+
+    // add recipe options supplied on command line
+    psMetadata *recipe  = psMetadataLookupPtr(&status, config->recipes, RECIPE_NAME);
+
+    // find the currently selected readout
+    pmReadout *inReadout = pmFPAfileThisReadout(config->files, view, "PPIMAGE.INPUT");
+
+    psImage *image    = inReadout->image;
+    psImage *mask     = inReadout->mask;
+    // psImage *variance = inReadout->variance;
+
+    // I have the fine image size, I know the binning factor, determine the ruff image size
+    psImageBinning *binning = psImageBinningAlloc();
+    binning->nXfine = image->numCols;
+    binning->nYfine = image->numRows;
+    binning->nXbin  = psMetadataLookupS32 (&status, recipe, "CTE.XBIN");
+    binning->nYbin  = psMetadataLookupS32 (&status, recipe, "CTE.YBIN");
+
+    psImageBinningSetRuffSize(binning, PS_IMAGE_BINNING_CENTER);
+    psImageBinningSetSkip(binning, image);
+
+    pmCell *inCell  = pmFPAfileThisCell (config->files, view, "PPIMAGE.INPUT");
+    pmCell *outCell = pmFPAfileThisCell (config->files, view, "PPIMAGE.CTEMAP");
+    if (!pmCellCopyStructure(outCell, inCell, binning->nXbin, binning->nYbin)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to copy cell structure.");
+        return false;
+    }
+
+    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPIMAGE.CTEMAP");
+    psImage *output = outRO->image;
+
+    // Don't care about the bias: get rid of it, if present
+    psFree(outRO->bias);
+    outRO->bias = psListAlloc(NULL);
+    psMetadataItem *biassec = psMetadataLookup(outCell->concepts, "CELL.BIASSEC");
+    psFree(biassec->data.V);
+    biassec->data.V = psListAlloc(NULL);
+
+    psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
+    psStats *statsDefaults = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
+
+    // measure median and variance for subimages
+    psRegion ruffRegion = {0,0,0,0};
+    psRegion fineRegion = {0,0,0,0};
+    for (int iy = 0; iy < output->numRows; iy++) {
+        for (int ix = 0; ix < output->numCols; ix++) {
+
+            // convert the ruff grid cell to the equivalent fine grid cell
+            ruffRegion = psRegionSet (ix, ix + 1, iy, iy + 1);
+            fineRegion = psImageBinningSetFineRegion (binning, ruffRegion);
+            fineRegion = psRegionForImage (image, fineRegion);
+            if (fineRegion.x0 >= image->numCols || fineRegion.x1 >= image->numCols ||
+                fineRegion.y0 >= image->numRows || fineRegion.y1 >= image->numRows) {
+                continue;
+            }
+
+            psImage *subset  = psImageSubset (image, fineRegion);
+            if (!subset->numCols || !subset->numRows) {
+                psFree (subset);
+                continue;
+            }
+            psImage *submask = NULL;
+            if (mask) {
+                submask = psImageSubset (mask, fineRegion);
+            }
+
+            // reset the default values
+            statsDefaults->tmpData = stats->tmpData; // XXX this is fairly hackish: tmpData is internal storage for stats; the assign drops the reference...
+            *stats = *statsDefaults;
+            statsDefaults->tmpData = NULL;
+
+            psImageStats (stats, subset, submask, 0);
+
+            // XXX need to apply the gain as well
+            output->data.F32[iy][ix] = PS_SQR(stats->sampleStdev) / stats->sampleMedian;
+
+            psFree (subset);
+            psFree (submask);
+        }
+    }
+
+    psFree (binning);
+    psFree (stats);
+    psFree (statsDefaults);
+
+    return true;
+}
Index: /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageParseCamera.c
===================================================================
--- /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageParseCamera.c	(revision 23405)
+++ /branches/eam_branches/eam_branch_20090312/ppImage/src/ppImageParseCamera.c	(revision 23406)
@@ -154,5 +154,5 @@
     }
 
-    if (options->checkCTE) {
+    if (options->checkCTE && false) {
         int DX = psMetadataLookupS32 (&status, recipe, "CTE.XBIN");
         int DY = psMetadataLookupS32 (&status, recipe, "CTE.YBIN");
