Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 9118)
+++ /trunk/ippTools/src/dettool.c	(revision 9119)
@@ -2582,4 +2582,101 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
+  
+    // select detProcessedImfile.det_id
+    // select detRun.iteration
+    // select detRun.det_type
+    // select detProcessedImfile.exp_tag
+    // by:
+    // find the current iteration bassed on det_id
+    // find all exp_tags in the current det_id/iteration from detInputExp
+    // find all rawImfiles in the current exp_tags
+    // compare to detProcessedImfiles by det_id/exp_tag
+    // found how many imfile there are in each class_id
+    // and:
+    // det_id is not in detProcessedExp;
+    // iteration is not in detProcessedExp;
+
+    psString query = psStringCopy(
+        " SELECT DISTINCT"
+        "    detRun.position as det_id,"
+        "    detRun.iteration,"
+        "    detRun.det_type,"
+        "    rawDetrendExp.camera,"
+        "    rawDetrendExp.telescope,"
+        "    rawDetrendExp.exp_type,"
+        "    rawDetrendExp.imfiles"
+        " FROM detRun"
+        " JOIN detInputExp"
+        "    ON detRun.position = detInputExp.det_id"
+        "    AND detRun.iteration = detInputExp.iteration"
+        " JOIN rawDetrendExp"
+        "    ON detInputExp.exp_tag = rawDetrendExp.exp_tag"
+        " JOIN detNormalizedImfile"
+        "    ON detInputExp.det_id = detNormalizedImfile.det_id"
+        "    AND detInputExp.iteration = detNormalizedImfile.iteration"
+        " LEFT JOIN rawImfile"
+        "    ON detInputExp.exp_tag = rawImfile.exp_tag"
+        "    AND detNormalizedImfile.class_id = rawImfile.class_id"
+        " LEFT JOIN detNormalizedExp"
+        "    ON detInputExp.det_id = detNormalizedExp.det_id"
+        "    AND detInputExp.iteration = detNormalizedExp.iteration"
+        " WHERE"
+        "    detNormalizedExp.det_id IS NULL"
+        "    AND detNormalizedExp.iteration IS NULL"
+        "    AND detInputExp.include = 1"
+        " GROUP BY"
+        "    detNormalizedImfile.iteration,"
+        "    detRun.position"
+        " HAVING"
+        "    COUNT(detNormalizedImfile.class_id) = COUNT(rawImfile.class_id)"
+        );
+
+    // XXX does it make sens to accept any search params?
+#if 0
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+#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) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+    }
+    if (!psArrayLength(output)) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no rawImfile rows found");
+        psFree(output);
+        return true;
+    }
+
+    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, "detProcessedImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
 
     return true;
