Index: trunk/Ohana/src/opihi/cmd.data/threshold.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/threshold.c	(revision 40623)
+++ trunk/Ohana/src/opihi/cmd.data/threshold.c	(revision 41164)
@@ -25,4 +25,14 @@
     remove_argument (N, &argc, argv);
     BinMax = atoi (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  float ValMin = NAN;
+  float ValMax = NAN;
+  if ((N = get_argument (argc, argv, "-vrange"))) {
+    remove_argument (N, &argc, argv);
+    ValMin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    ValMax = atof (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -64,4 +74,16 @@
   }
 
+  // ValMin, ValMax not compatible with BinMin, BinMax
+  if (isfinite(ValMin)) {
+    int binTest = ohana_bisection_double (vecx[0].elements.Flt, vecx[0].Nelements, ValMin);
+    BinMin = (vecx[0].elements.Flt[binTest+1] == ValMin) ? binTest : binTest + 1;
+    // XXX dangerous: check that this does not run off the end, etc
+  }
+  if (isfinite(ValMax)) {
+    int binTest = ohana_bisection_double (vecx[0].elements.Flt, vecx[0].Nelements, ValMax);
+    BinMax = (vecx[0].elements.Flt[binTest+1] == ValMax) ? binTest : binTest + 1;
+    // XXX dangerous: check that this does not run off the end, etc
+  }
+
   // use bisection to find the value
 
@@ -71,4 +93,13 @@
 
   if (!REVERSE) {
+    // 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 (vecy[0].elements.Flt[BinMax] < value) {
+      gprint (GP_ERR, "ERROR: all values below threshold\n");
+      return FALSE;
+    }
     while (Nhi - Nlo > 10) {
       N = 0.5*(Nlo + Nhi);
@@ -89,4 +120,13 @@
     }
   } else {
+    // 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 (vecy[0].elements.Flt[BinMax] > value) {
+      gprint (GP_ERR, "ERROR: all values above threshold\n");
+      return FALSE;
+    }
     while (Nhi - Nlo > 10) {
       N = 0.5*(Nlo + Nhi);
