Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8330)
+++ trunk/ippTools/src/dettool.c	(revision 8333)
@@ -985,4 +985,96 @@
 static bool tostackMode(pxConfig *config)
 {
+    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
+    // select detProcessedImfile.det_id
+    // select detProcessedImfile.class_id
+    // by:
+    // find the current iteration bassed on det_id
+    // find all exp_ids in the current det_id/iteration from detInputExp
+    // find all rawImfiles in the current exp_ids
+    // compare to detProcessedImfiles by det_id/exp_id
+    // found how many imfile there are in each class_id
+    // and: 
+    // det_id is not in detStackedImfile;
+    // iteration is not in detStackedImfile;
+    // class_id is not in detStackedImfile;
+
+    psString query = psStringCopy(
+        " SELECT"
+        "    detProcessedImfile.det_id,"
+        "    detRun.iteration,"
+        "    detProcessedImfile.class_id"
+        " FROM detRun LEFT JOIN detInputExp"
+        "    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"
+        "    AND detInputExp.exp_id = detProcessedImfile.exp_id"
+        " LEFT JOIN detStackedImfile"
+        "    ON detInputExp.det_id = detStackedImfile.det_id"
+        "    AND detInputExp.iteration = detStackedImfile.iteration"
+        "    AND detProcessedImfile.class_id = detStackedImfile.class_id"
+        " WHERE detStackedImfile.det_id IS NULL"
+        "    AND detStackedImfile.iteration IS NULL"
+        "    AND detStackedImfile.class_id IS NULL"
+        " HAVING COUNT(detProcessedImfile.class_id) = COUNT(rawImfile.class_id)"
+        ); 
+
+    // XXX search criterian are disabled
+#if 0
+    if (config->where) {
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where);
+        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;
 }
