Index: /branches/czw_branch/20100817/ppImage/src/ppImageArguments.c
===================================================================
--- /branches/czw_branch/20100817/ppImage/src/ppImageArguments.c	(revision 29678)
+++ /branches/czw_branch/20100817/ppImage/src/ppImageArguments.c	(revision 29679)
@@ -24,4 +24,5 @@
     fprintf(stderr, "\t-mask/-masklist: Mask image.\n");
     fprintf(stderr, "\t-fringe/-fringelist: Fringe image and data.\n");
+    fprintf(stderr, "\t-linearity/-linearlist: linearity correction file.\n");
     fprintf(stderr, "\n");
     exit (2);
@@ -120,4 +121,5 @@
     pmConfigFileSetsMD (config->arguments, &argc, argv, "MASK", "-mask", "-masklist");
     pmConfigFileSetsMD (config->arguments, &argc, argv, "FRINGE", "-fringe", "-fringelist");
+    pmConfigFileSetsMD (config->arguments, &argc, argv, "LINEARITY", "-linearity", "-linearlist");
 
     // chip selection is used to limit chips to be processed
Index: /branches/czw_branch/20100817/ppImage/src/ppImageDetrendNonLinear.c
===================================================================
--- /branches/czw_branch/20100817/ppImage/src/ppImageDetrendNonLinear.c	(revision 29678)
+++ /branches/czw_branch/20100817/ppImage/src/ppImageDetrendNonLinear.c	(revision 29679)
@@ -4,5 +4,4 @@
 
 #include "ppImage.h"
-
 
 bool ppImageDetrendNonLinearPolynomial(pmReadout *input, psMetadataItem *dataItem) {
@@ -47,97 +46,104 @@
 
 bool ppImageDetrendNonLinear(pmReadout *input, pmFPAview *detview, pmConfig  *config) {
-  bool status;
+    bool status;
 
-  pmFPAfile *linearity_file = psMetadataLookupPtr(&status,config->files,"PPIMAGE.LINEARITY");
-  psFits *linearity_fits = linearity_file->fits;
+    pmFPAfile *linearity_file = psMetadataLookupPtr(&status,config->files,"PPIMAGE.LINEARITY");
+    psFits *linearity_fits = linearity_file->fits;
 
-  if (!psFitsMoveExtName(linearity_fits,psMetadataLookupStr(&status,input->parent->concepts,"CELL.NAME"))) {
-    psError(PS_ERR_IO, false, "Unable to move to non-linearity table %s",psMetadataLookupStr(&status,input->parent->concepts,"CELL.NAME"));
-    return(false);
-  }
+    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);
-  }
+    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. 
+    // 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");
-    return(false);
-  }	    
+    if (!pmNonLinearityApply(input,table)) {
+	psError(PS_ERR_UNKNOWN, false, "Unable to apply non-linearity corrections.\n");
+	psFree (table);
+	return(false);
+    }	    
+    psFree (table);
 
+    return true;
+}
 
-  return(true);
+bool ppImageDetrendNonLinear_Original(pmReadout *input, ppImageOptions *options) {
+
+    psMetadataItem *concept;
+    pmCell *cell = input->parent;
+
+    switch (options->nonLinearType) {
+      case PS_DATA_VECTOR:
+        ppImageDetrendNonLinearPolynomial (input, options->nonLinearData);
+        return true;
+
+      case PS_DATA_STRING:
+        ppImageDetrendNonLinearLookup (input, options->nonLinearData);
+        return true;
+
+      case PS_DATA_METADATA:
+        // Go looking for the value in the hierarchy
+        concept = psMetadataLookup(cell->concepts, options->nonLinearSource);
+        if (! concept) {
+            pmChip *chip = cell->parent;// Parent chip
+            concept = psMetadataLookup(chip->concepts, options->nonLinearSource);
+            if (! concept) {
+                pmFPA *fpa = chip->parent; // Parent FPA
+                concept = psMetadataLookup(fpa->concepts, options->nonLinearSource);
+                if (! concept) {
+                    psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
+                             "for non-linearity correction --- ignored.\n", (char *)options->nonLinearSource);
+                    return false;
+                }
+            }
+        }
+
+        if (concept->type != PS_DATA_STRING) {
+            psLogMsg("phase2", PS_LOG_WARN, "Type for concept %p isn't STRING, as"
+                     " expected for non-linearity correction --- ignored.\n",
+                     concept);
+            return false;
+        }
+
+        // Get the value of the concept
+        psString conceptValue = concept->data.V;
+        psMetadata *folder = (psMetadata *)options->nonLinearData->data.V;
+        psMetadataItem *optionItem = psMetadataLookup(folder, conceptValue);
+        if (!optionItem) {
+            psLogMsg("phase2", PS_LOG_WARN, "Unable to find %s in NONLIN.DATA"
+                     " --- ignored.\n", conceptValue);
+            return false;
+        }
+
+        switch (optionItem->type) {
+          case PS_DATA_VECTOR:
+            ppImageDetrendNonLinearPolynomial (input, optionItem);
+            return true;
+          case PS_DATA_STRING:
+            ppImageDetrendNonLinearLookup (input, optionItem);
+            return true;
+          default:
+            psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction "
+                     "desired but unable to interpret NONLIN.DATA for %s"
+                     " --- ignored\n", conceptValue);
+            return false;
+        }
+      default:
+        psAbort("Invalid options->nonLinearType");
+    }
+    return true;
 }
-  /* bool ppImageDetrendNonLinear(pmReadout *input, ppImageOptions *options) { */
-/*     psMetadataItem *concept; */
-/*     pmCell *cell = input->parent; */
 
-/*     switch (options->nonLinearType) { */
-/*       case PS_DATA_VECTOR: */
-/*         ppImageDetrendNonLinearPolynomial (input, options->nonLinearData); */
-/*         return true; */
-
-/*       case PS_DATA_STRING: */
-/*         ppImageDetrendNonLinearLookup (input, options->nonLinearData); */
-/*         return true; */
-
-/*       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); */
-/*         if (! concept) { */
-/*             pmChip *chip = cell->parent;// Parent chip */
-/*             concept = psMetadataLookup(chip->concepts, options->nonLinearSource); */
-/*             if (! concept) { */
-/*                 pmFPA *fpa = chip->parent; // Parent FPA */
-/*                 concept = psMetadataLookup(fpa->concepts, options->nonLinearSource); */
-/*                 if (! concept) { */
-/*                     psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s " */
-/*                              "for non-linearity correction --- ignored.\n", (char *)options->nonLinearSource); */
-/*                     return false; */
-/*                 } */
-/*             } */
-/*         } */
-
-/*         if (concept->type != PS_DATA_STRING) { */
-/*             psLogMsg("phase2", PS_LOG_WARN, "Type for concept %p isn't STRING, as" */
-/*                      " expected for non-linearity correction --- ignored.\n", */
-/*                      concept); */
-/*             return false; */
-/*         } */
-
-/*         // Get the value of the concept */
-/*         psString conceptValue = concept->data.V; */
-/*         psMetadata *folder = (psMetadata *)options->nonLinearData->data.V; */
-/*         psMetadataItem *optionItem = psMetadataLookup(folder, conceptValue); */
-/*         if (!optionItem) { */
-/*             psLogMsg("phase2", PS_LOG_WARN, "Unable to find %s in NONLIN.DATA" */
-/*                      " --- ignored.\n", conceptValue); */
-/*             return false; */
-/*         } */
-
-/*         switch (optionItem->type) { */
-/*           case PS_DATA_VECTOR: */
-/*             ppImageDetrendNonLinearPolynomial (input, optionItem); */
-/*             return true; */
-/*           case PS_DATA_STRING: */
-/*             ppImageDetrendNonLinearLookup (input, optionItem); */
-/*             return true; */
-/*           default: */
-/*             psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction " */
-/*                      "desired but unable to interpret NONLIN.DATA for %s" */
-/*                      " --- ignored\n", conceptValue); */
-/*             return false; */
-/*         } */
-/*       default: */
-/*         psAbort("Invalid options->nonLinearType"); */
-/*     } */
-/*     return true; */
-/* } */
-
Index: /branches/czw_branch/20100817/ppImage/src/ppImageOptions.c
===================================================================
--- /branches/czw_branch/20100817/ppImage/src/ppImageOptions.c	(revision 29678)
+++ /branches/czw_branch/20100817/ppImage/src/ppImageOptions.c	(revision 29679)
@@ -8,6 +8,6 @@
 {
     psFree(options->overscan);
-    psFree(options->nonLinearData);
-    psFree(options->nonLinearSource);
+    // psFree(options->nonLinearData);
+    // psFree(options->nonLinearSource);
 }
 
@@ -130,6 +130,5 @@
         psMetadataItem *dataItem = psMetadataLookup(recipe, "NONLIN.DATA");
         if (! dataItem) {
-            psLogMsg(__func__, PS_LOG_ERROR, "Non-linearity correction desired, but unable to "
-                     "find NONLIN.DATA in recipe %s.", RECIPE_NAME);
+            psLogMsg("ppImage", PS_LOG_ERROR, "Non-linearity correction desired, but unable to find NONLIN.DATA in recipe %s.", RECIPE_NAME);
             exit(EXIT_FAILURE);
         }
@@ -147,17 +146,12 @@
             // This is a menu; we need the key
           case PS_DATA_METADATA:
-            {
-                bool status;
-                options->nonLinearSource = psMetadataLookupStr(&status, recipe, "NONLIN.SOURCE");
-                if (! status || ! options->nonLinearSource) {
-                    psLogMsg(__func__, PS_LOG_ERROR, "Non-linearity correction desired, but unable to "
-                            "find NONLIN.SOURCE in recipe %s.", RECIPE_NAME);
-                    exit(EXIT_FAILURE);
-                }
-            }
+	    options->nonLinearSource = psMetadataLookupStr(&status, recipe, "NONLIN.SOURCE");
+	    if (! status || ! options->nonLinearSource) {
+		psLogMsg("ppImage", PS_LOG_ERROR, "Non-linearity correction desired, but unable to find NONLIN.SOURCE in recipe %s.", RECIPE_NAME);
+		exit(EXIT_FAILURE);
+	    }
             break;
           default:
-            psLogMsg(__func__, PS_LOG_ERROR, "Non-linearity correction desired, but "
-                    "NONLIN.DATA is of invalid type in recipe %s.", RECIPE_NAME);
+            psLogMsg("ppImage", PS_LOG_ERROR, "Non-linearity correction desired, but NONLIN.DATA is of invalid type in recipe %s.", RECIPE_NAME);
             exit(EXIT_FAILURE);
         }
