Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8379)
+++ trunk/ippTools/src/dettool.c	(revision 8383)
@@ -2193,4 +2193,86 @@
 static bool toresidMode(pxConfig *config)
 {
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    /*
+which returns a list of processed imfiles (with corresponding detrend
+id, iteration, class id, uri, type) for which the appropriate stacked
+imfile has been normalised, and which have not been masked out by
+detResidImfileAnalysis; it also returns stackedImfiles that have
+"-pleasenormalize" set to false.
+*/
+
+    // select detNormalizedStatImfile.*
+    // by:
+    // where det_id, iteration, class_id is not in detNormalizedImfile
+
+    psString query = psStringCopy(
+        "SELECT" 
+        "   detRun.iteration as iteration,"
+        "   detProcessedImfile.*"
+        " FROM detRun"
+        " LEFT JOIN detInputExp"
+        "   ON detRun.position = detInputExp.det_id"
+        "   AND detRun.iteration = detInputExp.iteration"
+        " LEFT JOIN detProcessedImfile"
+        "   ON detRun.position = detProcessedImfile.det_id"
+        "   AND detInputExp.exp_id = detProcessedImfile.exp_id"
+        " LEFT JOIN detNormalizedImfile"
+        "   ON detRun.position = detNormalizedImfile.det_id"
+        "   AND detRun.iteration = detNormalizedImfile.iteration"
+        "   AND detProcessedImfile.class_id = detNormalizedImfile.class_id"
+        " LEFT JOIN detResidImfile"
+        "   ON detRun.position = detResidImfile.det_id"
+        "   AND detRun.iteration = detResidImfile.iteration"
+        "   AND detProcessedImfile.class_id = detResidImfile.class_id"
+        " WHERE"
+        "   detResidImfile.det_id IS NULL"  
+        "   AND detResidImfile.iteration IS NULL"  
+        "   AnD detResidImfile.class_id IS NULL"  
+        ); 
+
+    // XXX does it make sens to accept any search params?
+#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");
+            psFree(output);
+            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;
 }
