Index: branches/czw_branch/20100817/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- branches/czw_branch/20100817/ppImage/src/ppImageDetrendReadout.c	(revision 29679)
+++ branches/czw_branch/20100817/ppImage/src/ppImageDetrendReadout.c	(revision 29813)
@@ -51,4 +51,13 @@
     }
 
+    // Subtract the overscan
+    if (options->doOverscan) {
+      if (!pmOverscanSubtract (input, options->overscan)) {
+	psError(PS_ERR_UNKNOWN, false, "Unable to subtract overscan.");
+	psFree(detview);
+	return false;
+      }
+    }
+
     // Non-linearity correction
     if (options->doNonLin) {
@@ -59,5 +68,5 @@
 	return(false);
       }
-/*       ppImageDetrendNonLinear(detrend->input, input, options); */
+      /*       ppImageDetrendNonLinear(detrend->input, input, options); */
     }
 
@@ -80,7 +89,7 @@
     }
 
-    // Bias, dark and overscan subtraction are all merged.
-    if (options->doBias || options->doOverscan) {
-        if (!pmBiasSubtract(input, options->overscan, bias, oldDark, view)) {
+    // Bias and temperature-independent-dark subtraction are merged.
+    if (options->doBias) {
+        if (!pmBiasSubtract(input, bias, oldDark, view)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to subtract bias.");
             psFree(detview);
@@ -88,5 +97,5 @@
         }
     }
-
+    
     // Weight on the basis of pixel value needs to be done after the overscan has been subtracted
     if (options->doVarianceBuild) {
Index: branches/czw_branch/20100817/psModules/src/detrend/pmBias.c
===================================================================
--- branches/czw_branch/20100817/psModules/src/detrend/pmBias.c	(revision 29679)
+++ branches/czw_branch/20100817/psModules/src/detrend/pmBias.c	(revision 29813)
@@ -163,5 +163,5 @@
 }
 
-bool pmBiasSubtract(pmReadout *in, pmOverscanOptions *overscanOpts,
+bool pmBiasSubtract(pmReadout *in, 
                     pmReadout *bias, pmReadout *dark, const pmFPAview *view)
 {
@@ -184,8 +184,4 @@
 
     pmHDU *hdu = pmHDUFromReadout(in);  // HDU of interest
-
-    if (!pmOverscanSubtract (in, overscanOpts)) {
-        return false;
-    }
 
     // Bias frame subtraction
@@ -247,5 +243,5 @@
     psString timeString = psTimeToISO(time); // String with time
     psFree(time);
-    psStringPrepend(&timeString, "Overscan/bias/dark processing completed at ");
+    psStringPrepend(&timeString, "Bias/dark processing completed at ");
     psMetadataAddStr(hdu->header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK,
                      timeString, "");
Index: branches/czw_branch/20100817/psModules/src/detrend/pmBias.h
===================================================================
--- branches/czw_branch/20100817/psModules/src/detrend/pmBias.h	(revision 29679)
+++ branches/czw_branch/20100817/psModules/src/detrend/pmBias.h	(revision 29813)
@@ -21,5 +21,4 @@
 /// bias (if non-NULL) and dark (if non-NULL) scaled by the CELL.DARKTIME concept.
 bool pmBiasSubtract(pmReadout *in,      ///< Input readout, to be overscan/bias/dark corrected
-                    pmOverscanOptions *overscanOpts, ///< Options for overscan subtraction, or NULL
                     pmReadout *bias, ///< Bias to subtract, or NULL
                     pmReadout *dark, ///< Dark to scale and subtract, or NULL
Index: branches/czw_branch/20100817/psModules/src/detrend/pmNonLinear.c
===================================================================
--- branches/czw_branch/20100817/psModules/src/detrend/pmNonLinear.c	(revision 29679)
+++ branches/czw_branch/20100817/psModules/src/detrend/pmNonLinear.c	(revision 29813)
@@ -10,4 +10,5 @@
 #include "pmNonLinear.h"
 
+psF32 pmNonLinearityMeasureNoisy(psF32 flux, psVector *correction_fluxes, psVector *correction_factors);
 pmReadout *pmNonLinearityPolynomial(pmReadout *inputReadout, const psPolynomial1D *input1DPoly)
 {
@@ -201,5 +202,5 @@
 	int seq = -1;
 	if ((dir == 0) && (pos < image->numCols/2)) {
-	    seq = pos - 1;
+	  seq = pos ;
 	    if (seq < 0) continue;
 	    fluxVector = x_lo_flux->data[seq];
@@ -207,5 +208,5 @@
 	} 
 	if ((dir == 0) && (pos > image->numCols/2)) {
-	    seq = pos - 1 + numBorder - image->numCols;
+	    seq = pos + numBorder - image->numCols;
 	    if (seq >= image->numCols) continue;
 	    fluxVector = x_hi_flux->data[seq];
@@ -213,5 +214,5 @@
 	}
 	if ((dir == 1) && (pos < image->numRows/2)) {
-	    seq = pos - 1;
+	    seq = pos;
 	    if (seq < 0) continue;
 	    fluxVector = y_lo_flux->data[seq];
@@ -219,5 +220,5 @@
 	} 
 	if ((dir == 1) && (pos > image->numRows/2)) {
-	    seq = pos - 1 + numBorder - image->numRows;
+	    seq = pos + numBorder - image->numRows;
 	    if (seq >= image->numRows) continue;
 	    fluxVector = y_hi_flux->data[seq];
@@ -244,4 +245,5 @@
 	}
 	if (row_correction_fluxes == NULL) {
+	  row_correction_factors = default_row_correction_factors;
 	    row_correction_fluxes = default_row_correction_fluxes;
 	}
@@ -258,5 +260,6 @@
 	    }
 	    if (col_correction_fluxes == NULL) {
-		col_correction_fluxes = default_col_correction_fluxes;
+	      col_correction_factors = default_col_correction_factors;
+	      col_correction_fluxes = default_col_correction_fluxes;
 	    }
 
@@ -264,25 +267,38 @@
 	    psF32 factor_row = pmNonLinearityMeasure(image->data.F32[i][j], row_correction_fluxes,row_correction_factors);
 	    psF32 factor_col = pmNonLinearityMeasure(image->data.F32[i][j], col_correction_fluxes,col_correction_factors);
-
-	    if (((i <= 9)&&(j <= 9))||((i == 9)&&(j == 5))) { // Print out if we're looking at a test case.
-		psTrace("psModules.camera",6,"Linearity: %d %d %s %f %f %f %d %d\n",i,j,
+#if (0)
+	    if (((i == 200)&&(j == 200))||((i == 9)&&(j == 5))) { // Print out if we're looking at a test case.
+	      psF32 factor_row = pmNonLinearityMeasureNoisy(image->data.F32[i][j], row_correction_fluxes,row_correction_factors);
+	      psF32 factor_col = pmNonLinearityMeasureNoisy(image->data.F32[i][j], col_correction_fluxes,col_correction_factors);
+	      
+		psTrace("psModules.nonlin",6,"Linearity: %d %d %s %f %f %f %d %d\n",i,j,
 			psMetadataLookupStr(NULL,inputReadout->parent->concepts,"CELL.NAME"),
 			image->data.F32[i][j],factor_row,factor_col,numBorder,numSamples);
-		psTrace("psModules.camera",6,"Linearity: R: %d %d %d C: %d %d %d\n",
+		psTrace("psModules.nonlin",6,"Linearity: R: %d %d %d C: %d %d %d\n",
 			i,(i < numBorder),(image->numRows - i),
 			j,(j < numBorder),(image->numCols - j));
 
-# if (0)
-		psTrace("psModules.camera",6,"Linearity: V: ");
+		psTrace("psModules.nonlin",6,"Linearity: V: ");
 		for (int k = 0; k < numSamples; k++) {
-		    psTrace("psModules.camera",6,"(%f %f) ",
-			    col_correction_fluxes->data.F32[k],col_correction_factors->data.F32[k]);
+		    psTrace("psModules.nonlin",6,"(%f %f) (%f %f) DDDD> (%f %f) (%f %f)",
+			    col_correction_fluxes->data.F32[k],col_correction_factors->data.F32[k],
+			    row_correction_fluxes->data.F32[k],row_correction_factors->data.F32[k],
+			    default_col_correction_fluxes->data.F32[k],default_col_correction_factors->data.F32[k],
+			    default_row_correction_fluxes->data.F32[k],default_row_correction_factors->data.F32[k]);
 		}
-		psTrace("psModules.camera",6,"\n");
+		psTrace("psModules.nonlin",6,"\n");
+	    } // End Test case
+#endif
+	    // Apply correction to image data
+#if (0)
+	    if (((i == 200)&&(j == 200))||((i == 9)&&(j == 5))) { // Print out if we're looking at a test case.
+	      psTrace("psModules.nonlin",4,"Applied Linearity Correction: %s %d %d : %f %f -> %f\n",
+		      psMetadataLookupStr(NULL,inputReadout->parent->concepts,"CELL.NAME"),
+		      i,j,image->data.F32[i][j],(factor_row + factor_col) / 2.0,
+		      image->data.F32[i][j] + (factor_row + factor_col) / 2.0);
+	    }
 # endif
-	    } // End Test case
-
-	    // Apply correction to image data
-	    image->data.F32[i][j] = image->data.F32[i][j] * ( factor_row * factor_col ) ;
+	    image->data.F32[i][j] = image->data.F32[i][j] - ( factor_row + factor_col ) / 2.0;
+
 	} // End loop over columns
     } // End loop over rows
@@ -314,9 +330,9 @@
     bin = correction_fluxes->n - 1;
     if (flux < correction_fluxes->data.F32[0]) {
-	return(1.0);
-    }
-    if (flux > correction_fluxes->data.F32[bin]) {
-	return(1.0);
-    }
+	return(0.0);
+    }
+/*     if (flux > correction_fluxes->data.F32[bin]) { */
+/* 	return(0.0); */
+/*     } */
   
     for (int i = 0; i < correction_fluxes->n - 1; i++) {
@@ -338,12 +354,54 @@
 /*   PS_BIN_INTERPOLATE(result,correction_fluxes,correction_factors,bin,flux); */
     if (!isfinite(result)) {
-	result = 1.0;
-    }
-    if (result <= 0) {
-	result = 1.0;
-    }
+	result = 0.0;
+    }
+/*     if (result <= 0) { */
+/* 	result = 1.0; */
+/*     } */
     return(result);
 }
-  
-  
-  
+
+psF32 pmNonLinearityMeasureNoisy(psF32 flux, psVector *correction_fluxes, psVector *correction_factors) {
+    //  psS32 numPixels = 0;
+    psF32 result = 0;
+    psU32 bin = 0;
+
+    bin = correction_fluxes->n - 1;
+    psTrace("psModules.nonlin",6,"NLMN: %f %d %f %f\n",flux,bin,correction_fluxes->data.F32[0],correction_fluxes->data.F32[bin]);
+    if (flux < correction_fluxes->data.F32[0]) {
+	return(0.0);
+    }
+/*     if (flux > correction_fluxes->data.F32[bin]) { */
+/* 	return(0.0); */
+/*     } */
+
+    for (int i = 0; i < correction_fluxes->n - 1; i++) {
+      psTrace("psModules.nonlin",6,"NLMN: %f %d %f %f %f %f\n",flux,i,correction_fluxes->data.F32[i],correction_fluxes->data.F32[i],
+	      correction_factors->data.F32[i],correction_factors->data.F32[i+1]);
+	if ((flux >= correction_fluxes->data.F32[i])&&
+	    (flux <  correction_fluxes->data.F32[i+1])) {
+	    result = correction_factors->data.F32[i] +
+		(flux - correction_fluxes->data.F32[i]) *
+		((correction_factors->data.F32[i+1] - correction_factors->data.F32[i]) /
+		 (correction_fluxes->data.F32[i+1] - correction_fluxes->data.F32[i]));
+	    continue;
+	}
+    }
+
+/*   PS_BIN_FOR_VALUE(bin,correction_fluxes,flux); */
+/*   if ((bin < 0)||(bin > correction_fluxes->n)) { */
+/*     return(1.0); */
+/*   } */
+  
+/*   PS_BIN_INTERPOLATE(result,correction_fluxes,correction_factors,bin,flux); */
+    if (!isfinite(result)) {
+	result = 0.0;
+    }
+/*     if (result <= 0) { */
+/* 	result = 1.0; */
+/*     } */
+    return(result);
+}
+
+  
+  
