Index: trunk/psModules/src/detrend/pmMaskBadPixels.c
===================================================================
--- trunk/psModules/src/detrend/pmMaskBadPixels.c	(revision 14935)
+++ trunk/psModules/src/detrend/pmMaskBadPixels.c	(revision 15865)
@@ -5,4 +5,5 @@
 #include <stdio.h>
 #include <math.h>
+#include <strings.h>
 #include <pslib.h>
 
@@ -125,4 +126,6 @@
     psFree(stats);
 
+    psTrace ("psModules.detrend", 3, "suspect: %f +/- %f\n", median, stdev);
+
     if (!out) {
         out = psImageAlloc(image->numCols, image->numRows, PS_TYPE_S32);
@@ -142,6 +145,5 @@
 }
 
-
-psImage *pmMaskIdentifyBadPixels(const psImage *suspects, float thresh, psMaskType maskVal)
+psImage *pmMaskIdentifyBadPixels(const psImage *suspects, psMaskType maskVal, int nTotal, float thresh, pmMaskIdentifyMode mode)
 {
     PS_ASSERT_IMAGE_NON_NULL(suspects, NULL);
@@ -150,5 +152,15 @@
 
     float limit = NAN;                  // Limit for masking
-    if (thresh > 0) {
+
+    switch (mode) {
+      case PM_MASK_ID_VALUE:
+        limit = thresh;
+	break;
+
+      case PM_MASK_ID_FRACTION:
+        limit = thresh * nTotal;
+	break;
+
+      case PM_MASK_ID_SIGMA: {
         psStats *stats = psStatsAlloc(PS_STAT_CLIPPED_STDEV); // Statistics
         stats->clipSigma = 5.0;
@@ -160,6 +172,10 @@
         }
         limit = thresh * stats->clippedStdev;
-        psFree(stats);
-    } else if (thresh < 0) {
+	psTrace ("psModules.detrend", 3, "bad: %f -> %f\n", stats->clippedStdev, limit);
+        psFree(stats);
+	break;
+      }
+
+      case PM_MASK_ID_POISSON: {
         psStats *stats = psStatsAlloc(PS_STAT_MAX); // Statistics
         if (!psImageStats(stats, suspects, NULL, 0)) {
@@ -189,7 +205,9 @@
         limit = max + 1.0 - thresh * sqrtf((float)max + 1.0);
 
-    } else {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                "Threshold (%f) must be a positive or negative real number.\n", thresh);
+	psTrace ("psModules.detrend", 3, "bad: mode: %d, stdev: %f, limit: %f\n", max, sqrtf((float)max + 1.0), limit);
+	break;
+      }
+      default:
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Invalid mask identify mode");
         return NULL;
     }
@@ -212,4 +230,6 @@
     }
 
+    psTrace ("psModules.detrend", 3, "bad pixel threshold: %f", limit);
+
     for (int y = 0; y < suspects->numRows; y++) {
         for (int x = 0; x < suspects->numCols; x++) {
@@ -222,2 +242,19 @@
     return badpix;
 }
+
+pmMaskIdentifyMode pmMaskIdentifyModeFromString (const char *string) {
+
+    if (!strcasecmp (string, "VALUE")) {
+      return PM_MASK_ID_VALUE;
+    } 
+    if (!strcasecmp (string, "FRACTION")) {
+      return PM_MASK_ID_FRACTION;
+    } 
+    if (!strcasecmp (string, "SIGMA")) {
+      return PM_MASK_ID_SIGMA;
+    } 
+    if (!strcasecmp (string, "POISSON")) {
+      return PM_MASK_ID_POISSON;
+    } 
+    return PM_MASK_ID_NONE;
+}
