Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8334)
+++ trunk/ippTools/src/dettool.c	(revision 8344)
@@ -116,5 +116,5 @@
 
     if (config->where) {
-        psString whereClaus = psDBGenerateWhereConditionSQL(config->where);
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where, "rawDetrendExp");
         psStringAppend(&query, " AND %s", whereClaus);
         psFree(whereClaus);
@@ -751,4 +751,7 @@
     // get det_id from detInputExp.det_id
     // get det_type from detRun.position
+    //
+    // det_id is in detInputExp
+    // exp_id is in detInputExp
     // det_id is not in detProccessedImfile
     // exp_id is not in detProccessedImfile
@@ -763,17 +766,17 @@
                 " AND rawImfile.exp_id = detProcessedImfile.exp_id"
                 " AND rawImfile.class_id = detProcessedImfile.class_id"
-            " WHERE detProcessedImfile.det_id IS NULL AND"
-            " detProcessedImfile.exp_id IS NULL AND"
-            " detProcessedImfile.class_id IS NULL"
+            " WHERE"
+            " detInputExp.det_id IS NOT NULL"
+            " AND detInputExp.exp_id IS NOT NULL"
+            " AND detProcessedImfile.det_id IS NULL"
+            " AND detProcessedImfile.exp_id IS NULL"
+            " AND detProcessedImfile.class_id IS NULL"
         ); 
 
-    // XXX search criterian are disabled
-#if 0
     if (config->where) {
-        psString whereClaus = psDBGenerateWhereConditionSQL(config->where);
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where, "rawImfile");
         psStringAppend(&query, " AND %s", whereClaus);
         psFree(whereClaus);
     }
-#endif
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -986,15 +989,4 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    /*
-which returns a list of stacks that are ready to be performed.  This
-list includes the detrend id, iteration, class id and detrend types
-(note that class id is unique per detrend id).  In terms of the
-database, the command returns the list where the detrend id, iteration
-and class id triplet do not have an entry in detStackedImfile; the
-results are masked by entries in detResidExpAnalysis
-*/
-
-//select detProcessedImfile.det_id, detRun.iteration, detProcessedImfile.class_id from detProcessedImfile LEFT JOIN detRun ON detProcessedImfile.det_id = detRun.position LEFT JOIN rawImfile ON detprocessedImfile.exp_id
 
     // select detRun.iteration
@@ -1021,6 +1013,4 @@
         "    ON detRun.position = detInputExp.det_id"
         "    AND detRun.iteration = detInputExp.iteration"
-        " LEFT JOIN rawImfile"
-        "    ON detInputExp.exp_id = rawImfile.exp_id"
         " LEFT JOIN detProcessedImfile"
         "    ON detInputExp.det_id = detProcessedImfile.det_id"
@@ -1030,13 +1020,19 @@
         "    AND detInputExp.iteration = detStackedImfile.iteration"
         "    AND detProcessedImfile.class_id = detStackedImfile.class_id"
+        " LEFT JOIN rawImfile"
+        "    ON detInputExp.exp_id = rawImfile.exp_id"
+        "    AND detProcessedImfile.class_id = rawImfile.class_id"
         " WHERE"
         "    detStackedImfile.det_id IS NULL"
         "    AND detStackedImfile.iteration IS NULL"
         "    AND detStackedImfile.class_id IS NULL"
+        " GROUP BY"
+        "    detProcessedImfile.class_id,"
+        "    rawImfile.class_id"
         " HAVING"
         "    COUNT(detProcessedImfile.class_id) = COUNT(rawImfile.class_id)"
         ); 
 
-    // XXX search criterian are disabled
+    // XXX does it make sens to accept any search params?
 #if 0
     if (config->where) {
@@ -1086,5 +1082,63 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // select detRun.det_type
+    // select detProcessedImfile
+    psString query = psStringCopy(
+        " SELECT"
+        "   detRun.det_type,"
+        "   detProcessedImfile.*"
+        " FROM detProcessedImfile"
+        " JOIN detRun"
+        " ON detProcessedImfile.det_id = detRun.position"
+        ); 
+
+    // XXX search criterian are disabled
+#if 1
+    if (config->where) {
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where, "detProcessedImfile");
+        psStringAppend(&query, " AND %s", whereClaus);
+        psFree(whereClaus);
+    }
+#endif
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    } 
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
+        return false;
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            return false;
+        }
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "rawDetrendImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
     
+    return true;
+}
+
+#if 0
+{
     // find all matching detProcessedImfiles where query should be
     // pre-generated the entry for iteration must be pruned from the default
@@ -1339,4 +1393,5 @@
     return true;
 }
+#endif
 
 static bool addstackedMode(pxConfig *config)
