Index: /trunk/ippTools/src/camtool.c
===================================================================
--- /trunk/ippTools/src/camtool.c	(revision 9272)
+++ /trunk/ippTools/src/camtool.c	(revision 9273)
@@ -51,10 +51,4 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // return only exps that:
-    // are not in rawScienceExp 
-    // are not in rawDetrendExp 
-    // have ALL of their imfiles in rawImfile (by count)
-    // and have no associated imfiles left in newImfile
 
     psString query = psStringCopy("SELECT * FROM p3PendingExp");
@@ -111,23 +105,23 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // select newImfiles that:
-    // exp_tag is in newExp
-    // don't have their exp_tag in rawScienceExp 
-    // don't have their exp_tag in rawDetrendExp
-    // XXX having the same exp_tag in newExp and raw*Exp is probably an error
-    // that should be checked for
-    char *query =
-        "SELECT newImfile.* FROM newImfile"
-        " LEFT JOIN newExp USING(exp_tag)"
-        " LEFT JOIN rawScienceExp USING(exp_tag)"
-        " LEFT JOIN rawDetrendExp USING (exp_tag)"
-        " WHERE newExp.exp_tag is NOT NULL"
-        " AND rawScienceExp.exp_tag IS NULL"
-        " AND rawDetrendExp.exp_tag IS NULL";
+    psString query = psStringCopy(
+        "SELECT p2ProcessedImfile.*"
+        " FROM p3PendingExp"
+        " JOIN p2ProcessedImfile"
+        " USING(exp_tag, p2_version)"
+    );
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereSQL(config->where, "p2ProcessedImfile");
+        psStringAppend(&query, " %s", whereClause);
+        psFree(whereClause);
+    }
 
     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);
@@ -138,5 +132,5 @@
     if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending newImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no pending p2ProcessedImfile rows found");
         psFree(output);
         return true;
@@ -154,5 +148,5 @@
 
     // negate simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "newImfile", !simple)) {
+    if (!ippdbPrintMetadatas(stdout, output, "p2ProcessedImfile", !simple)) {
         psError(PS_ERR_UNKNOWN, false, "failed to print array");
         psFree(output);
