Index: trunk/ppImage/src/ppImageDetrendNonLinear.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendNonLinear.c	(revision 11702)
+++ trunk/ppImage/src/ppImageDetrendNonLinear.c	(revision 29833)
@@ -44,5 +44,42 @@
 }
 
-bool ppImageDetrendNonLinear(pmReadout *input, ppImageOptions *options) {
+
+bool ppImageDetrendNonLinear(pmReadout *input, pmFPAview *detview, pmConfig  *config) {
+    bool status;
+
+    pmFPAfile *linearity_file = psMetadataLookupPtr(&status,config->files,"PPIMAGE.LINEARITY");
+    psFits *linearity_fits = linearity_file->fits;
+
+    char *extname = psMetadataLookupStr(&status,input->parent->concepts,"CELL.NAME");
+    if (!extname) {
+	psError(PS_ERR_IO, false, "missing CELL.NAME in concepts");
+	return(false);
+    }
+
+    if (!psFitsMoveExtName(linearity_fits,extname)) {
+	psError(PS_ERR_IO, false, "Unable to move to non-linearity table %s", extname);
+	return(false);
+    }
+  
+    psArray *table = psFitsReadTable(linearity_fits);
+    if (!table) {
+	psError(PS_ERR_IO, false, "Unable to read non-linearity table.\n");
+	return(false);
+    }
+
+    // It might be better to pack lookup table here...
+    // Why? I only use that lookup table once for the single cell it matches. 
+  
+    if (!pmNonLinearityApply(input,table)) {
+	psError(PS_ERR_UNKNOWN, false, "Unable to apply non-linearity corrections.\n");
+	psFree (table);
+	return(false);
+    }	    
+    psFree (table);
+
+    return true;
+}
+
+bool ppImageDetrendNonLinear_Original(pmReadout *input, ppImageOptions *options) {
 
     psMetadataItem *concept;
@@ -59,6 +96,4 @@
 
       case PS_DATA_METADATA:
-        // XXX EAM: this is somewhat confusing : let's wrap in a function when i understand it
-
         // Go looking for the value in the hierarchy
         concept = psMetadataLookup(cell->concepts, options->nonLinearSource);
