Index: /trunk/psModules/src/detrend/pmDetrendDB.c
===================================================================
--- /trunk/psModules/src/detrend/pmDetrendDB.c	(revision 9432)
+++ /trunk/psModules/src/detrend/pmDetrendDB.c	(revision 9433)
@@ -8,4 +8,6 @@
 #include "pmFPA.h"
 #include "pmDetrendDB.h"
+#include "psPipe.h"
+#include "psIOBuffer.h"
 
 // ************* detrend select functions **************
@@ -25,5 +27,5 @@
 
 // define basic options for a new detrend database query
-pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(char *camera, psTime time, pmDetrendType type)
+pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(const char *camera, psTime time, pmDetrendType type)
 {
 
@@ -32,12 +34,12 @@
 
     // basic options required by every query
-    options->camera = psMemIncrRefCounter(camera);
+    options->camera = psStringCopy (camera);
     options->time   = time;
     options->type   = type;
 
     // these other options depend on the type of detrend data
-    options->filter = NULL;
-    options->exptime = 0.0;
-    options->airmass = 0.0;
+    options->filter = NULL;  //
+    options->exptime = -1.0; // the undefined value (safe since exptime >= 0)
+    options->airmass = -1.0; // the undefined value (safe since airmass >= 1)
 
     return options;
@@ -90,54 +92,95 @@
 // detselect -camera (camera) -time (time) -type (type) [others]
 // returns: (type) (class) (exp_flag) DONE
-pmDetrendSelectResults *pmDetrendSelect (pmDetrendSelectOptions *options)
-{
-    bool status;
+pmDetrendSelectResults *pmDetrendSelect (const pmDetrendSelectOptions *options)
+{
+    int status, exit_status;
     char *line = NULL;
-
     char *time = psTimeToISO (&options->time);
     char *type = pmDetrendTypeToString (options->type);
 
+    psArray *array = NULL;
+    pmDetrendSelectResults *results = pmDetrendSelectResultsAlloc();
+
     // generate the detselect command
-    psStringAppend (&line, "detselect -camera %s -time %s -type %s", options->camera, time, type);
-    psFree (time);
-    psFree (type);
+    // psStringAppend (&line, "detselect -inst %s -time %s -det_type %s", options->camera, time, type);
+    // 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);
+    if (options->filter) {
+        psStringAppend (&line, " -filter %s", options->filter);
+    }
+    if (options->exptime > -1.0) {
+        psStringAppend (&line, " -exptime %f", options->exptime);
+    }
+    if (options->airmass > -1.0) {
+        psStringAppend (&line, " -airmass %f", options->airmass);
+    }
+
+    psTrace("psModules.detrend", 5, "running %s", line);
 
     // use psPipe to exec the command, wait for response
     psIOBuffer *buffer = psIOBufferAlloc (512);
     psPipe *pipe = psPipeOpen (line);
+    if (!pipe) {
+        psError (PS_ERR_IO, false, "error calling command %s", line);
+        goto failure;
+    }
+
     status = psIOBufferReadEmpty (buffer, 100, pipe->stdout);
     if (!status) {
         psError (PS_ERR_IO, false, "detselect is not responding");
-        psFree (buffer);
-        psFree (pipe);
-        psFree (line);
-        return NULL;
-    }
-    psPipeClose (pipe);
-    psFree (pipe);
-    psFree (line);
+        goto failure;
+    }
+    exit_status = psPipeClose (pipe);
+    if (exit_status) {
+        psError (PS_ERR_IO, false, "error running detselect");
+        goto failure;
+    }
+
+    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
-    psArray *array = psStringSplitArray (buffer->data, " ", false);
-    psFree (buffer);
-
-    if (!array)
-        return NULL;
-    if (!array->n)
-        return NULL;
-    if (!strcasecmp(array->data[0], "ERROR"))
-        return NULL;
-
-    pmFPALevel level = pmFPALevelFromName (array->data[1]);
-    if (level == PM_FPA_LEVEL_NONE)
-        return NULL;
-
-    pmDetrendSelectResults *results = pmDetrendSelectResultsAlloc();
+    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;
-    results->detID = psStringCopy (array->data[2]);
-
-    psFree (array);
+    psStringAppend (&results->detID, " -det_id %s -iteration %s ", (char *)array->data[0], (char *)array->data[1]);
+
+    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 (type);
+    psFree (time);
     return results;
+
+failure:
+    psFree (array);
+    psFree (results);
+    psFree (pipe);
+    psFree (buffer);
+    psFree (line);
+    psFree (type);
+    psFree (time);
+    return NULL;
 }
 
@@ -146,42 +189,68 @@
 // detselect -select -detID (detID) -classID (classID)
 // returns: (detID) (classID) (filename) DONE
-char *pmDetrendFile (char *detID, char *classID)
-{
+char *pmDetrendFile (const char *detID, const char *classID)
+{
+    PS_ASSERT_PTR_NON_NULL(detID, NULL);
+    PS_ASSERT_PTR_NON_NULL(classID, NULL);
+
     bool status;
     char *line = NULL;
+    psArray *array = NULL;
 
     // generate the detselect command
-    psStringAppend (&line, "detselect -select -detID %s -classID %s", detID, classID);
+    psStringAppend (&line, "detselect -select -simple %s -class_id %s", detID, classID);
+    psTrace("psModules.detrend", 5, "running %s", line);
 
     // use psPipe to exec the command, wait for response
     psIOBuffer *buffer = psIOBufferAlloc (512);
     psPipe *pipe = psPipeOpen (line);
+    if (!pipe) {
+        psError (PS_ERR_IO, false, "error calling command %s", line);
+        goto failure;
+    }
     status = psIOBufferReadEmpty (buffer, 100, pipe->stdout);
     if (!status) {
         psError (PS_ERR_IO, false, "detselect is not responding");
-        psFree (buffer);
-        psFree (pipe);
-        psFree (line);
-        return NULL;
-    }
-    psPipeClose (pipe);
-    psFree (pipe);
-    psFree (line);
+        goto failure;
+    }
+    status = psPipeClose (pipe);
+    if (status) {
+        psError (PS_ERR_IO, false, "error running detselect");
+        goto failure;
+    }
+
+    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
-    psArray *array = psStringSplitArray (buffer->data, " ", false);
-    psFree (buffer);
-
-    if (!array)
-        return NULL;
-    if (!array->n)
-        return NULL;
-    if (!strcasecmp(array->data[0], "ERROR"))
-        return NULL;
-
-    char *result = psStringCopy (array->data[2]);
-
-    psFree (array);
+    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[4]);
+    psTrace("psModules.detrend", 5, "detrend file: %s\n", result);
+
+    psFree (array);
+    psFree (pipe);
+    psFree (buffer);
+    psFree (line);
     return result;
-}
+
+failure:
+    psFree (array);
+    psFree (pipe);
+    psFree (buffer);
+    psFree (line);
+    return NULL;
+}
Index: /trunk/psModules/src/detrend/pmDetrendDB.h
===================================================================
--- /trunk/psModules/src/detrend/pmDetrendDB.h	(revision 9432)
+++ /trunk/psModules/src/detrend/pmDetrendDB.h	(revision 9433)
@@ -14,6 +14,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-07-01 00:00:11 $
+*  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-10-10 01:02:25 $
 *
 *  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
@@ -47,5 +47,5 @@
 typedef struct
 {
-    char *detID;   // identifier of detrend image
+    char *detID;   // identifier of detrend run
     pmFPALevel level;   // level in FPA hierarchy of individual file
 }
@@ -61,38 +61,8 @@
 psString pmDetrendTypeToString (pmDetrendType type);
 
-pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(char *camera, psTime time, pmDetrendType type);
+pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(const char *camera, psTime time, pmDetrendType type);
 pmDetrendSelectResults *pmDetrendSelectResultsAlloc();
-pmDetrendSelectResults *pmDetrendSelect (pmDetrendSelectOptions *options);
-char *pmDetrendFile (char *detID, char *classID);
-
-// move these to pslib??
-typedef struct
-{
-    int stdin;
-    int stdout;
-    int stderr;
-}
-psPipe;
-
-typedef struct
-{
-    char *data;
-    int nAlloc;    // current size of allocated buffer
-    int nReset;    // size to set buffer after flush
-    int nBlock;    // number of bytes to try to read at a time
-    int n;    // current size of filled data
-}
-psIOBuffer;
-
-// psIOBuffer functions
-psIOBuffer *psIOBufferAlloc (int nBuffer);
-bool psIOBufferFlush (psIOBuffer *buffer);
-int psIOBufferRead (psIOBuffer *buffer, int fd);
-int psIOBufferReadEmpty (psIOBuffer *buffer, int maxRetries, int fd);
-
-// psPipe functions
-psPipe *psPipeAlloc ();
-psPipe *psPipeOpen (char *command);
-bool psPipeClose (psPipe *pipe);
+pmDetrendSelectResults *pmDetrendSelect (const pmDetrendSelectOptions *options);
+char *pmDetrendFile (const char *detID, const char *classID);
 
 # endif
