Index: trunk/ippTools/src/difftool.c
===================================================================
--- trunk/ippTools/src/difftool.c	(revision 18336)
+++ trunk/ippTools/src/difftool.c	(revision 18561)
@@ -104,6 +104,6 @@
 
     // default
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
     diffRunRow *run = diffRunRowAlloc(
@@ -282,6 +282,15 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+
+    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-tess_id", "tess_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-kind", "kind", "==");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
 
     // find all rawImfiles matching the default query
@@ -292,9 +301,18 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereSQL(config->where, NULL);
-        psStringAppend(&query, " %s", whereClause);
+    if (template) {
+        // list only faulted rows
+        psStringAppend(&query, " %s", "WHERE diffInputSkyfile.template != 0");
+    } else {
+        // don't list faulted rows
+        psStringAppend(&query, " %s", "WHERE diffInputSkyfile.template = 0");
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -351,4 +369,7 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -360,9 +381,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "diffSkyfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "diffSkyfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -418,22 +440,9 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false);
-
-    // default to 0
+    
+    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false); // required
     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
-    if (code == 0 && !uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
-    if (code == 0 && !path_base) {
-        psError(PS_ERR_UNKNOWN, true, "-path_base is required");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", (code == 0), false);
+    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", (code == 0), false);
 
     // optional
@@ -441,9 +450,9 @@
     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
     PXOPT_LOOKUP_F32(dtime_diff, config->args, "-dtime_diff", false, false);
-    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
-    PXOPT_LOOKUP_F32(good_frac, config->args, "-good_frac", false, false);
     PXOPT_LOOKUP_S32(stamps_num, config->args, "-stamps_num", false, false);
     PXOPT_LOOKUP_F32(stamps_rms, config->args, "-stamps_rms", false, false);
     PXOPT_LOOKUP_S32(sources, config->args, "-sources", false, false);
+    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
+    PXOPT_LOOKUP_F32(good_frac, config->args, "-good_frac", false, false);
 
     if (!psDBTransaction(config->dbh)) {
@@ -495,4 +504,9 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-tess_id", "tess_id", "==");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -504,9 +518,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "diffSkyfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "diffSkyfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -563,4 +578,8 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
+    PXOPT_COPY_S16(config->args, where, "-code",     "fault", "==");
+
     psString query = pxDataGet("difftool_revertdiffskyfile.sql");
     if (!query) {
@@ -569,9 +588,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "diffSkyfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "diffSkyfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -741,12 +761,11 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // required options
-    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
-    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
-    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false);
-
-    // optional
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false); // required options
+    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false); // required options
+    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false); // required options
+    PXOPT_LOOKUP_S64(template_warp_id, config->args, "-template_warp_id", false, false);
     PXOPT_LOOKUP_S64(template_stack_id, config->args, "-template_stack_id", false, false);
-    PXOPT_LOOKUP_S64(template_warp_id, config->args, "-template_warp_id", false, false);
+    PXOPT_LOOKUP_S64(input_warp_id, config->args, "-input_warp_id", false, false);
+    PXOPT_LOOKUP_S64(input_stack_id, config->args, "-input_stack_id", false, false);
 
     if (template_stack_id && template_warp_id) {
@@ -760,6 +779,4 @@
     }
 
-    PXOPT_LOOKUP_S64(input_stack_id, config->args, "-input_stack_id", false, false);
-    PXOPT_LOOKUP_S64(input_warp_id, config->args, "-input_warp_id", false, false);
     if (input_stack_id && input_warp_id) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Only one input can be defined.");
@@ -787,10 +804,15 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // required options
-    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
-
-    // default
+    psMetadata *where = psMetadataAlloc();
+
+    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-tess_id", "tess_id", "==");
+    PXOPT_COPY_STR(config->args, where,  "-kind", "kind", "==");
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false); // required options
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
+    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
     // find all things to queue
@@ -801,9 +823,18 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereSQL(config->where, NULL);
-        psStringAppend(&query, " %s", whereClause);
+    if (template) {
+        // list only faulted rows
+        psStringAppend(&query, " %s", "WHERE diffInputSkyfile.template != 0");
+    } else {
+        // don't list faulted rows
+        psStringAppend(&query, " %s", "WHERE diffInputSkyfile.template = 0");
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
