Index: trunk/psModules/src/detrend/pmNonLinear.c
===================================================================
--- trunk/psModules/src/detrend/pmNonLinear.c	(revision 8669)
+++ trunk/psModules/src/detrend/pmNonLinear.c	(revision 9617)
@@ -1,22 +1,2 @@
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// XXX WARNING: I have completely replaced this file with an OLD VERSION (that works) instead of the
-// one that was being worked on.
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-/** @file  pmNonLinear.c
- *
- *  Provides polynomial or table lookup non-linearity corrections to readouts.
- *
- *  @author GLG, MHPCC
- *
- *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-08-29 21:39:44 $
- *
- *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
- *
- *  XXX: The SDR is silent about image types.  Only F32 was implemented.
- *
- */
-
 #if HAVE_CONFIG_H
 #include <config.h>
@@ -24,18 +4,10 @@
 
 #include <stdio.h>
-#include <math.h>
 #include <pslib.h>
 
+#include "pmFPA.h"
 #include "pmNonLinear.h"
 
-/******************************************************************************
-pmNonLinearityLookup(): This routine will take an pmReadout image as input
-and a 1-D polynomial.  For each pixel in the input image, the polynomial will
-be evaluated at that pixels value, and the image pixel will then be set to
-that value.
- *****************************************************************************/
-
-pmReadout *pmNonLinearityPolynomial(pmReadout *inputReadout,
-                                    const psPolynomial1D *input1DPoly)
+pmReadout *pmNonLinearityPolynomial(pmReadout *inputReadout, const psPolynomial1D *input1DPoly)
 {
     PS_ASSERT_PTR_NON_NULL(inputReadout, NULL);
@@ -44,33 +16,23 @@
     PS_ASSERT_PTR_NON_NULL(input1DPoly, NULL);
 
-    psS32 i;
-    psS32 j;
-
-    for (i=0;i<inputReadout->image->numRows;i++) {
-        for (j=0;j<inputReadout->image->numCols;j++) {
-            inputReadout->image->data.F32[i][j] = psPolynomial1DEval(input1DPoly, inputReadout->image->data.F32[i][j]);
+    psImage *image = inputReadout->image; // Image to correct
+    for (int i = 0; i < image->numRows; i++) {
+        for (int j = 0; j < image->numCols; j++) {
+            image->data.F32[i][j] = psPolynomial1DEval(input1DPoly, image->data.F32[i][j]);
         }
     }
-    return(inputReadout);
+    return inputReadout;
 }
 
 
-/******************************************************************************
-pmNonLinearityLookup(): This routine will take an pmReadout image as input
-and two input vectors, which constitute a lookup table.  For each pixel in
-the input image, that pixels value will be determined in the input vector
-inFluxe, and the corresponding value in outFlux.  The image pixel will then
-be set to the value from outFlux.
- *****************************************************************************/
-pmReadout *pmNonLinearityLookup(pmReadout *inputReadout,
-                                const psVector *inFlux,
-                                const psVector *outFlux)
+pmReadout *pmNonLinearityLookup(pmReadout *inputReadout, const psVector *inFlux, const psVector *outFlux)
 {
-    PS_ASSERT_PTR_NON_NULL(inputReadout,NULL);
-    PS_ASSERT_PTR_NON_NULL(inputReadout->image,NULL);
+    PS_ASSERT_PTR_NON_NULL(inputReadout, NULL);
+    PS_ASSERT_PTR_NON_NULL(inputReadout->image, NULL);
     PS_ASSERT_IMAGE_TYPE(inputReadout->image, PS_TYPE_F32, NULL);
-    PS_ASSERT_PTR_NON_NULL(inFlux,NULL);
+    PS_ASSERT_PTR_NON_NULL(inFlux, NULL);
     if (inFlux->n < 2) {
-        psError(PS_ERR_UNKNOWN,true, "pmNonLinearityLookup(): input vector less than 2 elements.  Returning inputReadout image.");
+        psError(PS_ERR_UNKNOWN, true,
+                "pmNonLinearityLookup(): input vector less than 2 elements.  Returning inputReadout image.");
         return(inputReadout);
     }
@@ -80,43 +42,42 @@
         tableSize = PS_MIN(inFlux->n, outFlux->n);
         psLogMsg(__func__, PS_LOG_WARN,
-                 "WARNING: pmNonLinear.c: pmNonLinearityLookup(): input vectors have different sizes (%ld, %ld)\n", inFlux->n, outFlux->n);
+                 "WARNING: pmNonLinear.c: pmNonLinearityLookup(): "
+                 "input vectors have different sizes (%ld, %ld)\n",
+                 inFlux->n, outFlux->n);
     }
     PS_ASSERT_VECTOR_TYPE(inFlux, PS_TYPE_F32, NULL);
     PS_ASSERT_VECTOR_TYPE(outFlux, PS_TYPE_F32, NULL);
 
-    psS32 i;
-    psS32 j;
-    psS32 binNum;
-    psScalar x;
-    psS32 numPixels = 0;
-    psF32 slope;
+    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
 
-    x.type.type = PS_TYPE_F32;
-    for (i=0;i<inputReadout->image->numRows;i++) {
-        for (j=0;j<inputReadout->image->numCols;j++) {
-            x.data.F32 = inputReadout->image->data.F32[i][j];
-            binNum = p_psVectorBinDisect((psVector *)inFlux, &x);
+    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
 
             if (binNum == -2) {
                 // We get here if x is below the table lookup range.
-                inputReadout->image->data.F32[i][j] = outFlux->data.F32[0];
+                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.
-                inputReadout->image->data.F32[i][j] = outFlux->data.F32[tableSize-1];
+                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);
+                psError(PS_ERR_UNKNOWN, true,
+                        "pmNonLinearityLookup(): Could not perform p_psVectorBinDisect().  "
+                        "Returning inputReadout image.");
+                return inputReadout;
                 numPixels++;
             } else {
                 // Perform linear interpolation.
-                slope = (outFlux->data.F32[binNum+1] - outFlux->data.F32[binNum]) /
-                        (inFlux->data.F32[binNum+1]  - inFlux->data.F32[binNum]);
-                inputReadout->image->data.F32[i][j] = outFlux->data.F32[binNum] +
-                                                      ((x.data.F32 - inFlux->data.F32[binNum]) * slope);
+                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;
             }
         }
@@ -126,4 +87,4 @@
                  "WARNING: pmNonLinear.c: pmNonLinearityLookup(): %d pixels outside table.", numPixels);
     }
-    return(inputReadout);
+    return inputReadout;
 }
