Index: trunk/Ohana/src/opihi/cmd.data/threshold.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/threshold.c	(revision 42150)
+++ trunk/Ohana/src/opihi/cmd.data/threshold.c	(revision 42277)
@@ -1,3 +1,15 @@
 # include "data.h"
+
+int QUIET = FALSE;
+
+void _threshold_set_values (double value, int Nbin, double level) {
+
+  set_variable ("threshval", value);
+  set_int_variable ("threshbin", Nbin);
+  set_variable ("threshold", level);
+
+  if (!QUIET) gprint (GP_LOG, "theshold %f (bin %d is %f)\n", level, Nbin, value);
+  return;
+}
 
 int threshold (int argc, char **argv) {
@@ -6,7 +18,16 @@
   Vector *vecx, *vecy;
 
-  int QUIET = FALSE;
   if ((N = get_argument (argc, argv, "-q"))) {
     QUIET = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  int SATURATE = FALSE;
+  if ((N = get_argument (argc, argv, "-saturate"))) {
+    SATURATE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-sat"))) {
+    SATURATE = TRUE;
     remove_argument (N, &argc, argv);
   }
@@ -95,10 +116,22 @@
     // this algorithm assumes vecy[BinMax] > threshold, vecy[BinMin] < threshold
     if (vecy[0].elements.Flt[BinMin] > value) {
-      gprint (GP_ERR, "ERROR: all values above threshold\n");
-      return FALSE;
+      if (SATURATE) {
+	_threshold_set_values (vecy[0].elements.Flt[BinMin], BinMin, vecx[0].elements.Flt[BinMin]);
+	return TRUE;
+      } else {
+	gprint (GP_ERR, "ERROR: all values above threshold\n");
+	_threshold_set_values (NAN, -1, NAN);
+	return FALSE;
+      }
     }
     if (vecy[0].elements.Flt[BinMax] < value) {
-      gprint (GP_ERR, "ERROR: all values below threshold\n");
-      return FALSE;
+      if (SATURATE) {
+	_threshold_set_values (vecy[0].elements.Flt[BinMax], BinMax, vecx[0].elements.Flt[BinMax]);
+	return TRUE;
+      } else {
+	gprint (GP_ERR, "ERROR: all values below threshold\n");
+	_threshold_set_values (NAN, -1, NAN);
+	return FALSE;
+      }
     }
     while (Nhi - Nlo > 10) {
@@ -122,10 +155,22 @@
     // this algorithm assumes vecy[BinMin] > threshold, vecy[BinMax] < threshold
     if (vecy[0].elements.Flt[BinMin] < value) {
-      gprint (GP_ERR, "ERROR: all values below threshold\n");
-      return FALSE;
+      if (SATURATE) {
+	_threshold_set_values (vecy[0].elements.Flt[BinMin], BinMin, vecx[0].elements.Flt[BinMin]);
+	return TRUE;
+      } else {
+	gprint (GP_ERR, "ERROR: all values below threshold\n");
+	_threshold_set_values (NAN, -1, NAN);
+	return FALSE;
+      }
     }
     if (vecy[0].elements.Flt[BinMax] > value) {
-      gprint (GP_ERR, "ERROR: all values above threshold\n");
-      return FALSE;
+      if (SATURATE) {
+	_threshold_set_values (vecy[0].elements.Flt[BinMax], BinMax, vecx[0].elements.Flt[BinMax]);
+	return TRUE;
+      } else {
+	gprint (GP_ERR, "ERROR: all values above threshold\n");
+	_threshold_set_values (NAN, -1, NAN);
+	return FALSE;
+      }
     }
     while (Nhi - Nlo > 10) {
@@ -174,10 +219,5 @@
   }
 
-  set_variable ("threshval", y1);
-  set_int_variable ("threshbin", Nhi);
-  set_variable ("threshold", Xvalue);
-
-  if (!QUIET) gprint (GP_LOG, "theshold %f (bin %d is %f)\n", Xvalue, Nhi, y1);
-
+  _threshold_set_values (y1, Nhi, Xvalue);
   return (TRUE);
 }
