Index: trunk/ippTools/src/warptool.c
===================================================================
--- trunk/ippTools/src/warptool.c	(revision 11765)
+++ trunk/ippTools/src/warptool.c	(revision 11766)
@@ -470,30 +470,33 @@
 static bool tooverlapMode(pxConfig *config)
 {
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "   p4InputExp.p4_id,"
-        "   p2ProcessedImfile.*,"
-        "   p4Run.workdir"
-        " FROM p4Run"
-        " JOIN p4InputExp"
-        "   USING(p4_id)"
-        " LEFT JOIN p4Scfile"
-        "   ON p4Run.p4_id = p4Scfile.p4_id"
-        "   AND exp_tag, p3_version, class_id)"
-        " JOIN p3ProcessedExp"
-        "   ON p4InputImfile.exp_tag = p3ProcessedExp.exp_tag"
-        "   AND p4InputImfile.p3_version = p3ProcessedExp.p3_version"
-        " JOIN p2ProcessedImfile"
-        "   ON p4InputImfile.exp_tag = p2ProcessedImfile.exp_tag"
-        "   AND p3ProcessedExp.p2_version = p2ProcessedImfile.p2_version"
-        "   AND p4InputImfile.class_id = p2ProcessedImfile.class_id"
-        " WHERE"
-        "   p4Run.state = 'run'"
-        "   AND p4WarpedImfile.p4_id IS NULL"
-        "   AND p4WarpedImfile.exp_tag IS NULL"
-        "   AND p4WarpedImfile.p3_version IS NULL"
-        "   AND p4WarpedImfile.class_id IS NULL"
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    bool status = false;
+    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
+    if (!status) {
+        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
+        return false;
+    }
+
+    // find all rawImfiles matching the default query
+    psString query = psStringCopy( 
+        "SELECT\n"
+        "   p3ProcessedExp.*\n"
+        " FROM p4Run\n"
+        " JOIN p4InputExp\n"
+        "   USING(p4_id)\n"
+        " JOIN p3ProcessedExp\n"
+        "   ON p4InputExp.exp_tag = p3ProcessedExp.exp_tag\n"
+        "   AND p4InputExp.p3_version = p3ProcessedExp.p3_version\n"
+        " LEFT JOIN p4SkyCellMap\n"
+        "   ON p4InputExp.p4_id = p4SkyCellMap.p4_id\n"
+        "   AND p4InputExp.exp_tag = p4SkyCellMap.exp_tag\n"
+        "   AND p4InputExp.p3_version = p4SkyCellMap.p3_version\n"
+        " WHERE\n"
+        "   p4Run.state = 'run'\n" 
+        "   AND p3ProcessedExp.fault = 0\n"
+        "   AND p4SkyCellMap.p4_id IS NULL\n"
+        "   AND p4SkyCellMap.exp_tag IS NULL\n"
+        "   AND p4SkyCellMap.p3_version IS NULL\n"
     );
 
@@ -504,4 +507,11 @@
     }
 
+    // treat limit == 0 as "no limit"
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -513,9 +523,18 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+        psErrorCode err = psErrorCodeLast();
+        switch (err) {
+            case PS_ERR_DB_CLIENT:
+                psError(PXTOOLS_ERR_SYS, false, "database error");
+            case PS_ERR_DB_SERVER:
+                psError(PXTOOLS_ERR_PROG, false, "database error");
+            default:
+                psError(PXTOOLS_ERR_PROG, false, "unknown error");
+        }
+
         return false;
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4InputImfile rows found");
+        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
         psFree(output);
         return true;
@@ -532,9 +551,11 @@
     }
 
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
+    if (psArrayLength(output)) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            return false;
+        }
     }
 
@@ -543,5 +564,4 @@
     return true;
 }
-
 
 static bool addoverlapMode(pxConfig *config)
