Index: trunk/ppImage/src/ppImageDetrendNonLinear.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendNonLinear.c	(revision 5857)
+++ trunk/ppImage/src/ppImageDetrendNonLinear.c	(revision 5858)
@@ -1,71 +1,65 @@
 # include "ppImage.h"
 
-bool ppDetrendNonLinear (pmCell *cell, pmReadout *readout, psMetadata *recipe) {
+bool ppDetrendNonLinearPolynomial (pmReadout *input, psMetadataItem *dataItem) {
 
-    psString name;
-    psVector *coeff;
-    psMetadata *options;
+    // These are the polynomial coefficients
+    psVector *coeff = dataItem->data.V; // The coefficient vector
+    if (coeff->type.type != PS_TYPE_F64) {
+	psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64); // F64 version
+	psFree (coeff);
+	coeff = temp;
+    }
+    psPolynomial1D *correction = psPolynomial1DAlloc(coeff->n - 1, PS_POLYNOMIAL_ORD);
+    psFree(correction->coeff);
+    correction->coeff = psMemIncrRefCounter(coeff->data.F64);
+    pmNonLinearityPolynomial(input, correction);
+    psFree(coeff);
+    psFree(correction);
+    return true;
+}    
 
-    psMetadataItem *dataItem = psMetadataLookup(recipe, "NONLIN.DATA");
-    if (! dataItem) {
-	psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction desired, but unable to "
-		 "find NONLIN.DATA in recipe --- ignored.\n");
+bool ppDetrendNonLinearLookup (pmReadout *input, psMetadataItem *dataItem) {
+
+    // This is a filename: lookup table
+    char *name = dataItem->data.V;       // Filename
+    psLookupTable *table = psLookupTableAlloc(name, "%f %f", 0);
+    if (psLookupTableRead(table) <= 0) {
+	psErrorStackPrint(stderr, "Unable to read non-linearity correction file "
+			  "%s --- ignored\n", name);
 	return false;
     } 
+#ifdef PRODUCTION
+    pmNonLinearityLookup(input, table);
+#else
+    psVector *influx = table->values->data[0];
+    psVector *outflux = table->values->data[1];
+    pmNonLinearityLookup(input, influx, outflux);
+#endif
+    psFree(table);
+    return true;
+}    
 
-    switch (dataItem->type) {
+bool ppDetrendNonLinear (pmCell *cell, pmReadout *input, ppOptions *options) {
+
+    psMetadataItem *concept;
+
+    switch (options->nonLinearType) {
       case PS_DATA_VECTOR:
-	// These are the polynomial coefficients
-	coeff = dataItem->data.V; // The coefficient vector
-	if (coeff->type.type != PS_TYPE_F64) {
-	    psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64); // F64 version
-	    coeff = temp;
-	}
-	psPolynomial1D *correction = psPolynomial1DAlloc(coeff->n - 1,
-							 PS_POLYNOMIAL_ORD);
-	psFree(correction->coeff);
-	correction->coeff = psMemIncrRefCounter(coeff->data.F64);
-	(void)pmNonLinearityPolynomial(inputReadout, correction);
-	psFree(coeff);
-	psFree(correction);
+	ppDetrendNonLinearPolynomial (input, options->nonLinearData);
 	return true;
 
       case PS_DATA_STRING:
-	// This is a filename
-	name = dataItem->data.V;       // Filename
-	psLookupTable *table = psLookupTableAlloc(name, "%f %f", 0);
-	if (psLookupTableRead(table) <= 0) {
-	    psErrorStackPrint(stderr, "Unable to read non-linearity correction file "
-			      "%s --- ignored\n", name);
-	    return false;
-	} 
-#ifdef PRODUCTION
-	(void)pmNonLinearityLookup(inputReadout, table);
-#else
-	psVector *influx = table->values->data[0];
-	psVector *outflux = table->values->data[1];
-	(void)pmNonLinearityLookup(inputReadout, table->values->data[0],
-				   table->values->data[1]);
-#endif
-	psFree(table);
+	ppDetrendNonLinearLookup (input, options->nonLinearData);
 	return true;
 
       case PS_DATA_METADATA:
-	// This is a menu
-	options = dataItem->data.V; // Options with concept values as keys
-	bool mdok = false; // Success of MD lookup
-	psString concept = psMetadataLookupStr(&mdok, recipe, "NONLIN.SOURCE");
-	if (! mdok || ! concept) {
-	    psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction desired, but "
-		     "unable to find NONLIN.SOURCE in recipe --- ignored.\n");
+	// XXX this is somewhat confusing : let's wrap in a function when i understand it
+	concept = pmCellGetConcept(cell, options->nonLinearSource);
+	if (! concept) {
+	    psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
+		     "for non-linearity correction --- ignored.\n", options->nonLinearSource);
 	    return false;
 	} 
-	psMetadataItem *conceptValueItem = pmCellGetConcept(inputCell, concept);
-	if (! conceptValueItem) {
-	    psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
-		     "for non-linearity correction --- ignored.\n", concept);
-	    return false;
-	} 
-	if (conceptValueItem->type != PS_DATA_STRING) {
+	if (concept->type != PS_DATA_STRING) {
 	    psLogMsg("phase2", PS_LOG_WARN, "Type for concept %s isn't STRING, as"
 		     " expected for non-linearity correction --- ignored.\n",
@@ -74,7 +68,8 @@
 	} 
 
-	psString conceptValue = conceptValueItem->data.V;
 	// Get the value of the concept
-	psMetadataItem *optionItem = psMetadataLookup(options, conceptValue);
+	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"
@@ -82,52 +77,22 @@
 	    return false;
 	} 
-	if (optionItem->type == PS_DATA_VECTOR) {
-	    // These are the polynomial coefficients
-	    coeff = optionItem->data.V; // The coefficient vector
-	    if (coeff->type.type != PS_TYPE_F64) {
-		psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64);
-		coeff = temp;
-	    }
-	    // Polynomial correction
-	    psPolynomial1D *correction = psPolynomial1DAlloc(coeff->n - 1,
-							     PS_POLYNOMIAL_ORD);
-	    psFree(correction->coeff);
-	    correction->coeff = psMemIncrRefCounter(coeff->data.F64);
-	    (void)pmNonLinearityPolynomial(inputReadout, correction);
-	    psFree(coeff);
-	    psFree(correction);
+
+	switch (optionItem->type) {
+	  case PS_DATA_VECTOR:
+	    ppDetrendNonLinearPolynomial (input, optionItem);
 	    return true;
-	} 
-	if (optionItem->type == PS_DATA_STRING) {
-	    // This is a filename
-	    psString tableName = optionItem->data.V; // The filename
-	    psLookupTable *table = psLookupTableAlloc(tableName, "%f %f", 0);
-	    int numLines = 0; // Number of lines read from table
-	    if ((numLines = psLookupTableRead(table)) <= 0) {
-		psErrorStackPrint(stderr, "Unable to read non-linearity "
-				  "correction file %s --- ignored\n",
-				  tableName);
-		return false;
-	    } 
-#ifdef PRODUCTION
-	    (void)pmNonLinearityLookup(inputReadout, table);
-#else
-	    printf("XXX: Non-linearity correction from lookup table not "
-		   "yet implemented.\n");
-#endif
-	    psFree(table);
+	  case PS_DATA_STRING:
+	    ppDetrendNonLinearLookup (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;
 	}
-
-	psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction "
-		 "desired but unable to interpret NONLIN.DATA for %s"
-		 " --- ignored\n", conceptValue);
-	return false;
-  
       default:
-	psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction desired, but "
-		 "NONLIN.DATA is of invalid type --- ignored.\n");
+	psAbort("phase2", "Invalid options->nonLinearType");
     }
-    return false;
+    return true;
 }
 
