Index: /trunk/psModules/src/detrend/pmNonLinear.c
===================================================================
--- /trunk/psModules/src/detrend/pmNonLinear.c	(revision 12455)
+++ /trunk/psModules/src/detrend/pmNonLinear.c	(revision 12456)
@@ -25,4 +25,22 @@
 }
 
+// set the bin closest to the corresponding value.  
+#define PS_BIN_FOR_VALUE(RESULT, VECTOR, VALUE) { \
+       	psVectorBinaryDisectResult result; \
+       	psScalar tmpScalar; \
+       	tmpScalar.type.type = PS_TYPE_F32; \
+	tmpScalar.data.F32 = (VALUE); \
+	RESULT = psVectorBinaryDisect (&result, VECTOR, &tmpScalar); \
+	switch (result) { \
+	  case PS_BINARY_DISECT_PASS: \
+            break; \
+	  case PS_BINARY_DISECT_OUTSIDE_RANGE: \
+            numPixels ++; \
+	    break; \
+	  case PS_BINARY_DISECT_INVALID_INPUT: \
+	  case PS_BINARY_DISECT_INVALID_TYPE: \
+	    psAbort ("programming error"); \
+	    break; \
+        } }
 
 pmReadout *pmNonLinearityLookup(pmReadout *inputReadout, const psVector *inFlux, const psVector *outFlux)
@@ -50,35 +68,19 @@
 
     psImage *image = inputReadout->image; // Input image
-    psScalar x;                         // Value at pixel, for p_psVectorBinDisect
-    x.type.type = PS_TYPE_F32;
     int numPixels = 0;                  // Number of pixels outside the range
+    int binNum;
 
     for (int i = 0; i < image->numRows; i++) {
         for (int j = 0; j < image->numCols; j++) {
-            x.data.F32 = image->data.F32[i][j];
-            int binNum = p_psVectorBinDisect((psVector *)inFlux, &x); // Bin below the value
+	    float value = image->data.F32[i][j];
+            PS_BIN_FOR_VALUE(binNum, inFlux, value);
 
-            if (binNum == -2) {
-                // We get here if x is below the table lookup range.
-                image->data.F32[i][j] = outFlux->data.F32[0];
-                numPixels++;
-            } else if (binNum == -1) {
-                // We get here if x is above the table lookup range.
-                image->data.F32[i][j] = outFlux->data.F32[tableSize-1];
-                numPixels++;
-            } else if (binNum < -2) {
-                // We get here if there was some other problem.
-                psError(PS_ERR_UNKNOWN, true,
-                        "pmNonLinearityLookup(): Could not perform p_psVectorBinDisect().  "
-                        "Returning inputReadout image.");
-                return inputReadout;
-                numPixels++;
-            } else {
-                // Perform linear interpolation.
-                float slope = (outFlux->data.F32[binNum + 1] - outFlux->data.F32[binNum]) /
-                              (inFlux->data.F32[binNum + 1] - inFlux->data.F32[binNum]);
-                image->data.F32[i][j] = outFlux->data.F32[binNum] +
-                                        (x.data.F32 - inFlux->data.F32[binNum]) * slope;
-            }
+	    // Perform linear interpolation.
+	    // XXX this will result in non-sensical results if inFlux contains equal-value
+	    // bins.  either enforce d(inFlux)/d(binNum) > 0 or see psStats.c PS_BIN_INTERPOLATE
+	    float slope = 
+		(outFlux->data.F32[binNum + 1] - outFlux->data.F32[binNum]) /
+		(inFlux->data.F32[binNum + 1] - inFlux->data.F32[binNum]);
+	    image->data.F32[i][j] = slope*(value - inFlux->data.F32[binNum]) + outFlux->data.F32[binNum];
         }
     }
