Index: trunk/psModules/src/detrend/pmDetrendDB.c
===================================================================
--- trunk/psModules/src/detrend/pmDetrendDB.c	(revision 10102)
+++ trunk/psModules/src/detrend/pmDetrendDB.c	(revision 10725)
@@ -101,7 +101,4 @@
     unsigned int nFail;
 
-    psArray *array = NULL;
-    psMetadata *answer = NULL;
-
     pmDetrendSelectResults *results = pmDetrendSelectResultsAlloc();
 
@@ -110,5 +107,10 @@
     // XXX we need to put in the filter and other restrictions based on the recipe
     // detselect -search -simple returns: DET_ID ITERATION CLASS TYPE
-    psStringAppend (&line, "detselect -search -simple -inst %s -det_type %s", options->camera, type);
+    // XXXXX ***** MAJOR HACK ***** Josh dropped INST from detrun, so I am using 'object' for
+    // now ('object' is not relevant to detrun!!!)
+    // XXX this operation is not applying the time-based selections yet.
+    psStringAppend (&line, "detselect -search -inst %s -det_type %s", options->camera, type);
+
+    // psStringAppend (&line, "detselect -search -simple -object %s -det_type %s", options->camera, type);
     if (options->filter) {
         psStringAppend (&line, " -filter %s", options->filter);
@@ -144,53 +146,38 @@
     psTrace("psModules.detrend", 5, "got answer: %s\n", buffer->data);
 
-    array = NULL;
-    answer = NULL;
     nFail = 0;
-    # define USE_MDPARSE 0
-    # if (USE_MDPARSE)
-
-        answer = psMetadataConfigParse (NULL, &nFail, buffer->data, false);
-    detExp = psMetadataLookupPtr ();
-    char *levelName = psMetadataLookupStr ();
-    # endif
-
-    // XXX need to parse the response more robustly
-    // XXX for now, assume a single line
-    // XXX we should expect a metadata back (if we call without -simple)
-    // XXX we should run psMetatdataParseConfig () on the
-    array = psStringSplitArray (buffer->data, " ", false);
-
-    if (!array) {
-        psError(PS_ERR_IO, false, "failed to split detselect answer %s\n", buffer->data);
-        goto failure;
-    }
-    if (array->n != 4) {
-        psError(PS_ERR_IO, false, "invalid number of fields in detselect answer %s (%ld)\n", buffer->data, array->n);
-        goto failure;
-    }
-
-    pmFPALevel level = pmFPALevelFromName (array->data[2]);
-    if (level == PM_FPA_LEVEL_NONE) {
-        psError(PS_ERR_IO, false, "invalid file level (%s) from detselect\n", (char *)array->data[2]);
-        goto failure;
-    }
-
-    results->level = level;
-    psStringAppend (&results->detID, " -det_id %s -iteration %s ", (char *)array->data[0], (char *)array->data[1]);
+    psMetadata *answer = psMetadataConfigParse (NULL, &nFail, buffer->data, false);
+    if (!answer) {
+        psError(PS_ERR_IO, false, "failed to parse response from detselect\n");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response (%d bytes):\n %s\n", buffer->n, buffer->data);
+        goto failure;
+    }
+
+    psMetadata *md = psMetadataLookupPtr (NULL, answer, "detExp");
+    if (!md) {
+        psError(PS_ERR_IO, false, "detselect response is missing 'detExp' Metadata\n");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response:\n %s\n", buffer->data);
+        goto failure;
+    }
+
+    bool mdstatus;
+    results->level = 0;
+    int detID = psMetadataLookupS32 (&mdstatus, md, "det_id");
+    int iteration  = psMetadataLookupS32 (&mdstatus, md, "iteration");
+
+    results->detID = NULL; // it should be NULL already from the Alloc above
+    psStringAppend (&results->detID, " -det_id %d -iteration %d ", detID, iteration);
 
     psTrace("psModules.detrend", 5, "generated detID %s\n", results->detID);
-    psTrace("psModules.detrend", 5, "fileLevel is %d (%s)\n", results->level, (char *)array->data[2]);
-    psTrace("psModules.detrend", 5, "selected type is %s\n", (char *)array->data[3]);
-
-    psFree (array);
-    psFree (pipe);
-    psFree (buffer);
-    psFree (line);
+
+    psFree (answer);
+    psFree (buffer);
+    psFree (pipe);
+    psFree (line);
+    psFree (time);
     psFree (type);
-    psFree (time);
     return results;
 
 failure:
-    psFree (array);
     psFree (results);
     psFree (pipe);
@@ -208,4 +195,6 @@
 char *pmDetrendFile (const char *detID, const char *classID)
 {
+    unsigned int nFail;
+
     PS_ASSERT_PTR_NON_NULL(detID, NULL);
     PS_ASSERT_PTR_NON_NULL(classID, NULL);
@@ -216,5 +205,5 @@
 
     // generate the detselect command
-    psStringAppend (&line, "detselect -select -simple %s -class_id %s", detID, classID);
+    psStringAppend (&line, "detselect -select %s -class_id %s", detID, classID);
     psTrace("psModules.detrend", 5, "running %s", line);
 
@@ -239,25 +228,21 @@
     psTrace("psModules.detrend", 5, "got answer: %s\n", buffer->data);
 
-    // XXX need to parse the response more robustly
-    // XXX for now, assume a single line
-    array = psStringSplitArray (buffer->data, " ", false);
-
-    if (!array) {
-        psError(PS_ERR_IO, false, "failed to split detselect answer %s\n", buffer->data);
-        goto failure;
-    }
-    if (array->n == 0) {
-        psError(PS_ERR_IO, true, "empty result set from detselect\n");
-        goto failure;
-    }
-    if (array->n < 5) {
-        psError(PS_ERR_IO, true, "invalid result set from detselect %s\n", buffer->data);
-        goto failure;
-    }
-
-    char *result = psStringCopy (array->data[3]);
+    psMetadata *answer = psMetadataConfigParse (NULL, &nFail, buffer->data, false);
+    if (!answer) {
+        psError(PS_ERR_IO, false, "failed to parse response from detselect\n");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response (%d bytes):\n %s\n", buffer->n, buffer->data);
+        goto failure;
+    }
+    psMetadata *md = psMetadataLookupPtr (NULL, answer, "detNormalizedImfile");
+    if (!md) {
+        psError(PS_ERR_IO, false, "detselect response is missing 'detNormalizedImfile' Metadata\n");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response:\n %s\n", buffer->data);
+        goto failure;
+    }
+
+    char *result = psStringCopy (psMetadataLookupStr (NULL, md, "uri"));
     psTrace("psModules.detrend", 5, "detrend file: %s\n", result);
 
-    psFree (array);
+    psFree (answer);
     psFree (pipe);
     psFree (buffer);
