Index: /branches/eam_branch_20080706/ippTools/src/dettool.c
===================================================================
--- /branches/eam_branch_20080706/ippTools/src/dettool.c	(revision 18479)
+++ /branches/eam_branch_20080706/ippTools/src/dettool.c	(revision 18480)
@@ -18,17 +18,4 @@
  */
 
-#ifdef HAVB_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <inttypes.h>
-
-#include <ippdb.h>
-
-#include "pxtools.h"
 #include "dettool.h"
 
@@ -98,5 +85,5 @@
 //static psArray *searchInputImfiles(pxConfig *config, const char *det_id);
 static detInputExpRow *rawDetrenTodetInputExpRow(rawExpRow *rawExp, psS64 det_id, psS32 iteration);
-static psArray *searchRawImfiles(pxConfig *config, psMetadata *where);
+static psArray *searchRawImfiles(pxConfig *config);
 static bool startNewIteration(pxConfig *config, psS64 det_id);
 static psS32 incrementIteration(pxConfig *config, psS64 det_id);
@@ -812,14 +799,4 @@
     }
 
-    if (camera) {
-        psFree(detRun->camera);
-        detRun->camera = psStringCopy(camera);
-    }
-
-    if (telescope) {
-        psFree(detRun->telescope);
-        detRun->telescope = psStringCopy(telescope);
-    }
-
     if (exp_type) {
         psFree(detRun->exp_type);
@@ -1630,5 +1607,4 @@
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        psFree(where);
         return false;
     }
@@ -1841,5 +1817,5 @@
 
 // this is NOT a command-line mode : it is used by 'addstackedMode'
-static psArray *searchRawImfiles(pxConfig *config, psMetadata *where)
+static psArray *searchRawImfiles(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, NULL);
@@ -2460,5 +2436,5 @@
 
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detNormalizedImfile");
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detNormalizedImfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -2534,14 +2510,9 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-
+    
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
+    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    // Required if code == 0
-    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false);
-
-    // optional
+    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false); // Required if code == 0
     PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
@@ -2554,7 +2525,4 @@
     PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
 
-    // default values
-    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
-
     psString query = pxDataGet("dettool_tonormalizedexp.sql");
     if (!query) {
@@ -2563,8 +2531,6 @@
     }
 
-    psStringAppend(&query,
-            " WHERE det_id = %s"
-            " AND iteration = %d", det_id, iteration);
-
+    // XXX in some other places, we put the arguments in the .sql and supply them to RunQuery
+    psStringAppend(&query, " WHERE det_id = %s AND iteration = %d", det_id, iteration);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -2615,24 +2581,25 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-recip",  "recipe", "==");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
-    psString query = psStringCopy(
-        "SELECT"
-        "   detNormalizedExp.*"
-        " FROM detNormalizedExp"
-        " JOIN detRun"
-        "   USING(det_id, iteration)"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereSQL(config->where, NULL);
+    psString query = pxDataGet("dettool_normalizedexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereSQL(where, NULL);
         psStringAppend(&query, " %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
@@ -2687,4 +2654,9 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_S16(config->args, where, "-code",      "fault", "==");
+
     psString query = pxDataGet("dettool_revertnormalizedexp.sql");
     if (!query) {
@@ -2693,9 +2665,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detNormalizedExp");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detNormalizedExp");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -2727,13 +2700,4 @@
         return false;
     }
-
-    // XXX does it make sense to accept any search params?
-#if 0
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-#endif
 
     // treat limit == 0 as "no limit"
@@ -2779,16 +2743,12 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false);
-    PXOPT_LOOKUP_STR(exp_id, config->args, "-exp_id", true, false);
-
+    
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
+    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
+    PXOPT_LOOKUP_STR(exp_id, config->args, "-exp_id", true, false); // required
+    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false); // required
     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    // Required if code == 0
-    PXOPT_LOOKUP_STR(uri, config->args, "-uri", (code == 0), false);
-    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false);
-
-    // optional
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", (code == 0), false); // Required if code == 0
+    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false); // Required if code == 0
     PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
@@ -2797,5 +2757,4 @@
     PXOPT_LOOKUP_F64(bg_kurtosis, config->args, "-bg_kurtosis", false, false);
     PXOPT_LOOKUP_F64(bin_stdev, config->args, "-bin_stdev", false, false);
-
     PXOPT_LOOKUP_F64(fringe_0, config->args, "-fringe_0", false, false);
     PXOPT_LOOKUP_F64(fringe_1, config->args, "-fringe_1", false, false);
@@ -2804,5 +2763,4 @@
     PXOPT_LOOKUP_F64(fringe_resid_1, config->args, "-fringe_resid_1", false, false);
     PXOPT_LOOKUP_F64(fringe_resid_2, config->args, "-fringe_resid_2", false, false);
-
     PXOPT_LOOKUP_F64(user_1, config->args, "-user_1", false, false);
     PXOPT_LOOKUP_F64(user_2, config->args, "-user_2", false, false);
@@ -2811,7 +2769,4 @@
     PXOPT_LOOKUP_F64(user_5, config->args, "-user_5", false, false);
     PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
-
-    // default values
-    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
 
     if (!detResidImfileInsert(config->dbh,
@@ -2854,30 +2809,22 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-class_id",  "class_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-recip",      "recipe", "==");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
-    // select detResidImfile.*
-    // by:
-    // where det_id, iteration, exp_id is not in detResidExp;
-
-    psString query = psStringCopy(
-        "SELECT"
-        "   detRun.det_type,"
-        "   detRun.mode,"
-        "   detResidImfile.*,"
-        "   rawExp.exp_time"
-        " FROM detResidImfile"
-        " JOIN detRun"
-        "   USING(det_id, iteration)"
-        " JOIN rawExp"
-        "   USING(exp_id)"
-        " WHERE"
-    );
-    // NOTE the above WHERE is completed with the following line:
-
+    psString query = pxDataGet("dettool_residimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    // NOTE: the query ends in a WHERE, add the required restriction on detRun.state
     char *value = NULL;
-
-    // add the two required restrictions: detRun.state and detRun.mode
     bool status;
     if ((value = psMetadataLookupStr(&status, config->args, "-select_state"))) {
@@ -2887,9 +2834,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detResidImfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detResidImfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
@@ -2943,4 +2891,11 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-exp_id",  "exp_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-class_id",  "class_id", "==");
+    PXOPT_COPY_S16(config->args, where, "-code",      "fault", "==");
+
     psString query = pxDataGet("dettool_revertresidimfile.sql");
     if (!query) {
@@ -2949,9 +2904,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detResidImfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detResidImfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -2971,14 +2927,13 @@
 
 
+/*
+  The SQL returns a list of exposures for which all component class ids have had residuals
+  created.  This list includes the detrend id, iteration, exposure id, detrend type, and
+  whether the exposure was included in the stack for this iteration.
+*/
+
 static bool toresidexpMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    /*
-which returns a list of exposures for which all component class ids
-have had residuals created.  This list includes the detrend id,
-iteration, exposure id, detrend type, and whether the exposure was
-included in the stack for this iteration.
-    */
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -2990,13 +2945,4 @@
         return false;
     }
-
-    // XXX does it make sense to accept any search params?
-#if 0
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-#endif
 
     // treat limit == 0 as "no limit"
@@ -3037,18 +2983,25 @@
 }
 
+/*
+  The SQL returns a list of exposures for which all component class ids have had residuals
+  created.  This list includes the detrend id, iteration, exposure id, detrend type, and
+  whether the exposure was included in the stack for this iteration.
+*/
+
 static bool addresidexpMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-    PXOPT_LOOKUP_STR(exp_id, config->args, "-exp_id", true, false);
-
+    // limit search by det_id, iteration, exp_id
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id", "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-exp_id", "exp_id", "==");
+
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
+    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
+    PXOPT_LOOKUP_STR(exp_id, config->args, "-exp_id", true, false); // required
     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    // Required if code == 0
-    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false);
-
-    // optional
+    PXOPT_LOOKUP_STR(recipe, config->args, "-recip", (code == 0), false); // Required if code == 0
     PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
@@ -3057,5 +3010,4 @@
     PXOPT_LOOKUP_F64(bg_kurtosis, config->args, "-bg_kurtosis", false, false);
     PXOPT_LOOKUP_F64(bin_stdev, config->args, "-bin_stdev", false, false);
-
     PXOPT_LOOKUP_F64(fringe_0, config->args, "-fringe_0", false, false);
     PXOPT_LOOKUP_F64(fringe_1, config->args, "-fringe_1", false, false);
@@ -3064,5 +3016,4 @@
     PXOPT_LOOKUP_F64(fringe_resid_1, config->args, "-fringe_resid_1", false, false);
     PXOPT_LOOKUP_F64(fringe_resid_2, config->args, "-fringe_resid_2", false, false);
-
     PXOPT_LOOKUP_F64(user_1, config->args, "-user_1", false, false);
     PXOPT_LOOKUP_F64(user_2, config->args, "-user_2", false, false);
@@ -3071,15 +3022,5 @@
     PXOPT_LOOKUP_F64(user_5, config->args, "-user_5", false, false);
     PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
-
-    // default values
-    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false);
     PXOPT_LOOKUP_BOOL(reject, config->args, "-reject", false);
-
-    /*
-which returns a list of exposures for which all component class ids
-have had residuals created.  This list includes the detrend id,
-iteration, exposure id, detrend type, and whether the exposure was
-included in the stack for this iteration.
-    */
 
     psString query = pxDataGet("dettool_toresidexp.sql");
@@ -3089,21 +3030,10 @@
     }
 
-    {
-        // build a query to search by det_id, iteration, exp_id
-        psMetadata *where = psMetadataAlloc();
-
-        PXOPT_COPY_STR(config->args, where, "-det_id", "det_id", "==");
-        PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
-        PXOPT_COPY_STR(config->args, where, "-exp_id", "exp_id", "==");
-
-
-        // there's not
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psFree(where);
-        if (whereClause) {
-            psStringAppend(&query, " WHERE %s", whereClause);
-            psFree(whereClause);
-        }
-    }
+    if (psListLength(where->list)) {
+	psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+	psStringAppend(&query, " WHERE %s", whereClause);
+	psFree(whereClause);
+    }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -3164,27 +3094,27 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-exp_id",    "exp_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-recip",     "recipe", "==");
+
+    PXOPT_LOOKUP_U64(limit,    config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple", false);
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
-
-    psString query = psStringCopy(
-        "SELECT"
-        "   detRun.mode,"
-        "   detResidExp.*,"
-        "   detInputExp.include"
-        " FROM detRun"
-        " JOIN detInputExp"
-        "   USING(det_id, iteration)"
-        " JOIN detResidExp"
-        "   USING(det_id, iteration, exp_id)"
-        " WHERE"
-        "   detRun.state = 'run'"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detResidExp");
+    PXOPT_LOOKUP_BOOL(reject,  config->args, "-reject", false);
+
+    psString query = pxDataGet("dettool_residexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detResidExp");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
@@ -3196,4 +3126,10 @@
     }
 
+    // list only accepted rows
+    // XXX the usage of this flag is unclear : -reject means "accepted"?
+    if (reject) {
+        psStringAppend(&query, " %s", "AND detResidExp.accept != 0");
+    } 
+
     // treat limit == 0 as "no limit"
     if (limit) {
@@ -3238,4 +3174,10 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-exp_id",    "exp_id", "==");
+    PXOPT_COPY_S16(config->args, where, "-code",      "fault", "==");
+
     psString query = pxDataGet("dettool_revertresidexp.sql");
     if (!query) {
@@ -3244,9 +3186,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detResidExp");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detResidExp");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -3266,4 +3209,50 @@
 
 
+static bool updateresidexpMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_BOOL(reject, config->args, "-reject", false);
+
+    // build a query to search by det_id, iteration, exp_id
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id", "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-exp_id", "exp_id", "==");
+
+    // find the values we're going to set
+    // copy everything but det_id, iteration, & exp_id from the args and
+    // remove the '-' prefix
+    psMetadata *set = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, set, "-recip", "recipe", "==");
+    PXOPT_COPY_F64(config->args, set, "-bg", "bg", "==");
+    PXOPT_COPY_F64(config->args, set, "-bg_stdev", "bg_stdev", "==");
+    PXOPT_COPY_F64(config->args, set, "-bg_mean_stdev", "bg_mean_stdev", "==");
+    PXOPT_COPY_STR(config->args, set, "-path_base", "path_base", "==");
+
+    // this can't be PXOPT_ macro-ized as reject is !'d
+    if (!psMetadataAddBool(set, PS_LIST_TAIL, "accept", 0, "==", !reject)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item accept");
+        psFree(set);
+        psFree(where);
+        return false;
+    }
+
+    long changed = psDBUpdateRows(config->dbh, "detResidExp", where, set);
+    psFree(set);
+    psFree(where);
+
+    if (changed < 0) {
+        psError(PS_ERR_UNKNOWN, false, "no rows were updated");
+        return false;
+    }
+
+    return true;
+}
+
+/* The SQL returns a list of detrend runs (with detrend id, iteration and detrend type) which
+ * have completed all residexps.
+ */
+
 static bool todetrunsummaryMode(pxConfig *config)
 {
@@ -3272,8 +3261,4 @@
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    /* which returns a list of detrend runs (with detrend id, iteration and
-     * detrend type) which have completed all residexps.
-     */
 
     psString query = pxDataGet("dettool_todetrunsummary.sql");
@@ -3282,13 +3267,4 @@
         return false;
     }
-
-    // XXX does it make sense to accept any search params?
-#if 0
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-#endif
 
     // treat limit == 0 as "no limit"
@@ -3329,56 +3305,58 @@
 }
 
-static bool updateresidexpMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    PXOPT_LOOKUP_BOOL(reject, config->args, "-reject", false);
-
+// this function is used to generate the detrunSummary entries used below
+// XXX why is this a separate function?  roll it back into adddetrunsummary?
+static detRunSummaryRow *mdToDetRunSummary(pxConfig *config, psMetadata *row)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    bool status = false;
+    // from row
+    psS64 det_id = psMetadataLookupS64(&status, row, "det_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for det_id");
+        return false;
+    }
+    psS32 iteration = psMetadataLookupS32(&status, row, "iteration");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for iteration");
+        return false;
+    }
+
+    // optional
+    PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
+    PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
+    PXOPT_LOOKUP_F64(bg_mean_stdev, config->args, "-bg_mean_stdev", false, false);
+
+    // default values
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
+    PXOPT_LOOKUP_BOOL(accept, config->args, "-accept", false);
+
+    return detRunSummaryRowAlloc(
+            det_id,
+            iteration,
+            bg,
+            bg_stdev,
+            bg_mean_stdev,
+            accept,
+            code
+        );
+}
+
+static bool adddetrunsummaryMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+    
     // build a query to search by det_id, iteration, exp_id
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_STR(config->args, where, "-det_id", "det_id", "==");
     PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
-    PXOPT_COPY_STR(config->args, where, "-exp_id", "exp_id", "==");
-
-    // find the values we're going to set
-    // copy everything but det_id, iteration, & exp_id from the args and
-    // remove the '-' prefix
-    psMetadata *set = psMetadataAlloc();
-    // XXX if PXOPT_COPY_* errors here the where md will leak
-    PXOPT_COPY_STR(config->args, set, "-recip", "recipe", "==");
-    PXOPT_COPY_F64(config->args, set, "-bg", "bg", "==");
-    PXOPT_COPY_F64(config->args, set, "-bg_stdev", "bg_stdev", "==");
-    PXOPT_COPY_F64(config->args, set, "-bg_mean_stdev", "bg_mean_stdev", "==");
-    PXOPT_COPY_STR(config->args, set, "-path_base", "path_base", "==");
-
-    // this can't be PXOPT_ macro-ized as reject is !'d
-    if (!psMetadataAddBool(set, PS_LIST_TAIL, "accept", 0, "==", !reject)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item accept");
-        psFree(set);
-        psFree(where);
-        return false;
-    }
-
-    long changed = psDBUpdateRows(config->dbh, "detResidExp", where, set);
-    psFree(set);
-    psFree(where);
-
-    if (changed < 0) {
-        psError(PS_ERR_UNKNOWN, false, "no rows were updated");
-        return false;
-    }
-
-    return true;
-}
-
-static bool adddetrunsummaryMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-
-    // optional
+
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
     PXOPT_LOOKUP_BOOL(again, config->args, "-again", false);
+
+    // The values supplied as arguments on the command (eg, -bg) are parsed 
+    // by mdToDetRunSummary below. 
+    // XXX why is there ever more than one?
 
     psString query = pxDataGet("dettool_find_completed_runs.sql");
@@ -3388,18 +3366,10 @@
     }
 
-    // build a query to search by det_id, iteration, exp_id
-    psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-det_id", "det_id", "==");
-    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
-
-    {
-        // there's not
+    if (psListLength(where->list)) {
         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psFree(where);
-        if (whereClause) {
-            psStringAppend(&query, " WHERE %s", whereClause);
-            psFree(whereClause);
-        }
-    }
+	psStringAppend(&query, " WHERE %s", whereClause);
+	psFree(whereClause);
+    }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -3428,4 +3398,5 @@
     }
 
+    // XXX why is there ever more than one result here?
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *row = output->data[i];
@@ -3456,4 +3427,5 @@
 
     // XXX this logic does not deal with the case of -code being set
+    // XXX it should be an error for -again and -code to both be set
     if (again) {
         if (!startNewIteration(config, (psS64)atoll(det_id))) {
@@ -3482,43 +3454,11 @@
 }
 
-static detRunSummaryRow *mdToDetRunSummary(pxConfig *config, psMetadata *row)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    // from row
-    psS64 det_id = psMetadataLookupS64(&status, row, "det_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for det_id");
-        return false;
-    }
-    psS32 iteration = psMetadataLookupS32(&status, row, "iteration");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for iteration");
-        return false;
-    }
-
-    // optional
-    PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
-    PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
-    PXOPT_LOOKUP_F64(bg_mean_stdev, config->args, "-bg_mean_stdev", false, false);
-    // default values
-    PXOPT_LOOKUP_BOOL(accept, config->args, "-accept", false);
-    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    return detRunSummaryRowAlloc(
-            det_id,
-            iteration,
-            bg,
-            bg_stdev,
-            bg_mean_stdev,
-            accept,
-            code
-        );
-}
-
 static bool detrunsummaryMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -3526,21 +3466,17 @@
     PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
-    psString query = psStringCopy(
-        "SELECT DISTINCT\n"
-        "   detRunSummary.*,\n"
-        "   detRun.det_type,\n"
-        "   detRun.mode\n"
-        " FROM detRun\n"
-        " JOIN detRunSummary\n"
-        "   USING(det_id, iteration)\n"
-        " WHERE\n"
-        "   detRun.state = 'run'\n"
-        );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
+    psString query = pxDataGet("dettool_detrunsummary.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psFree(where);
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "NULL");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (faulted) {
@@ -3594,4 +3530,9 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_id",    "det_id", "==");
+    PXOPT_COPY_S32(config->args, where, "-iteration", "iteration", "==");
+    PXOPT_COPY_STR(config->args, where, "-code",      "fault", "==");
+
     psString query = pxDataGet("dettool_revertdetrunsummary.sql");
     if (!query) {
@@ -3600,9 +3541,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detRunSummary");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "detRunSummary");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -3625,8 +3567,6 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-
+    
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
     PXOPT_LOOKUP_BOOL(accept, config->args, "-accept", false);
     PXOPT_LOOKUP_BOOL(reject, config->args, "-reject", false);
@@ -3656,11 +3596,8 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // det_id is required
-    // XXX this isn't strictly nessicary but not having the det_id complicates
-    // incrementing the iteration number
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
     PXOPT_LOOKUP_BOOL(again, config->args, "-again", false);
     PXOPT_LOOKUP_STR(state, config->args, "-state", false, false);
+
     // either -rerun or -state must be specified
     if (!(again || state)) {
@@ -3669,5 +3606,5 @@
     }
     if (again && state) {
-        psError(PS_ERR_UNKNOWN, true, "either -again or -state must be specified");
+        psError(PS_ERR_UNKNOWN, true, "-accept and -reject are exclusive");
         return false;
     }
@@ -3688,4 +3625,5 @@
 }
 
+// used by updatedetrunMode 
 static bool startNewIteration(pxConfig *config, psS64 det_id)
 {
@@ -3698,7 +3636,5 @@
     }
 
-    // XXX this query was not restricted by det_id, resulting
-    // in an inconsistent UPDATE below.  I added this AND clause
-    // though there may be a cleaner method (EAM 2006.10.08)
+    // restrict the update only to the specified det_id
     psStringAppend(&query, " WHERE det_id = %" PRId64 , det_id);
 
@@ -3953,9 +3889,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // required
-    PXOPT_LOOKUP_STR(det_type, config->args, "-det_type", true, false);
-    PXOPT_LOOKUP_STR(filelevel, config->args, "-filelevel", false, false);
-
-    // optional
+    PXOPT_LOOKUP_STR(det_type, config->args, "-det_type", true, false); // required
+    PXOPT_LOOKUP_STR(filelevel, config->args, "-filelevel", true, false); // required
     PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
     PXOPT_LOOKUP_STR(camera, config->args, "-inst", false, false);
@@ -3980,6 +3913,4 @@
     PXOPT_LOOKUP_STR(parent, config->args, "-parent", false, false);
     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
-
-    // default
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
@@ -3993,5 +3924,5 @@
                       0,            // the iteration is fixed at 0
                       det_type,
-                      "register",   // mode
+                      "register",   // mode is required to be "register"
                       "register",   // state
                       filelevel,
@@ -4064,14 +3995,12 @@
 }
 
+// NOTE : this function is also used by addcorrectimfileMode
 bool register_detrend_imfileMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false);
-    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false);
-    PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false);
-
-    // optional
+    
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
+    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false); // required
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false); // required
     PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
Index: /branches/eam_branch_20080706/ippTools/src/dettool.h
===================================================================
--- /branches/eam_branch_20080706/ippTools/src/dettool.h	(revision 18479)
+++ /branches/eam_branch_20080706/ippTools/src/dettool.h	(revision 18480)
@@ -17,4 +17,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #ifndef DETTOOL_H
Index: /branches/eam_branch_20080706/ippTools/src/dettoolConfig.c
===================================================================
--- /branches/eam_branch_20080706/ippTools/src/dettoolConfig.c	(revision 18479)
+++ /branches/eam_branch_20080706/ippTools/src/dettoolConfig.c	(revision 18480)
@@ -18,11 +18,4 @@
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <psmodules.h>
-
-#include "pxtools.h"
 #include "dettool.h"
 
@@ -167,5 +160,5 @@
     psMetadataAddStr(definebydetrunArgs, PS_LIST_TAIL, "-set_input_end", 0,       "require input exp to be in this period", NULL);
 
-    // XXX EAM : it looks like the corresponding dettool functions don't exist:
+    // *** these are in dettool_correction.c ***
     // -makecorrection
     psMetadata *makecorrectionArgs = psMetadataAlloc();
@@ -224,5 +217,5 @@
     psMetadataAddStr(inputArgs, PS_LIST_TAIL, "-det_id", 0,            "search for detrend ID", NULL);
     psMetadataAddS32(inputArgs, PS_LIST_TAIL, "-iteration",  0,            "define iteration number", 0);
-    psMetadataAddS64(inputArgs, PS_LIST_TAIL, "-exp_id",  0,            "search for exp ID", NULL);
+    psMetadataAddS64(inputArgs, PS_LIST_TAIL, "-exp_id",  0,            "search for exp ID", 0);
     psMetadataAddBool(inputArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
@@ -428,7 +421,4 @@
     psMetadataAddS16(revertnormalizedimfileArgs, PS_LIST_TAIL, "-code",  0,            "search by fault code", 0);
 
-    XXX EAM : cleaned to this point
-	break into separate files
-
     // -tonormalizedexp
     psMetadata *tonormalizedexpArgs = psMetadataAlloc();
@@ -440,4 +430,5 @@
     psMetadataAddStr(addnormalizedexpArgs, PS_LIST_TAIL, "-det_id",  0,            "define detrend ID (required)", NULL);
     psMetadataAddS32(addnormalizedexpArgs, PS_LIST_TAIL, "-iteration",  0,            "define iteration number", 0);
+    psMetadataAddS16(addnormalizedexpArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
     psMetadataAddStr(addnormalizedexpArgs, PS_LIST_TAIL, "-recip",  0,            "search for recipe", NULL);
     psMetadataAddF64(addnormalizedexpArgs, PS_LIST_TAIL, "-bg",  0,            "define exposure background", NAN);
@@ -450,15 +441,10 @@
     psMetadataAddF64(addnormalizedexpArgs, PS_LIST_TAIL, "-user_5",  0,            "define user statistic (5)", NAN);
     psMetadataAddStr(addnormalizedexpArgs, PS_LIST_TAIL, "-path_base",  0,            "define base output location", NULL);
-    psMetadataAddS16(addnormalizedexpArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
 
     // -normalizedexp
     psMetadata *normalizedexpArgs = psMetadataAlloc();
-    psMetadataAddStr(normalizedexpArgs, PS_LIST_TAIL, "-det_id",  0,            "define detrend ID", NULL);
-    psMetadataAddS32(normalizedexpArgs, PS_LIST_TAIL, "-iteration",  0,            "define iteration number", 0);
+    psMetadataAddStr(normalizedexpArgs, PS_LIST_TAIL, "-det_id",  0,            "search by detrend ID", NULL);
+    psMetadataAddS32(normalizedexpArgs, PS_LIST_TAIL, "-iteration",  0,            "search by iteration number", 0);
     psMetadataAddStr(normalizedexpArgs, PS_LIST_TAIL, "-recip",  0,            "search for recipe", NULL);
-    psMetadataAddF64(normalizedexpArgs, PS_LIST_TAIL, "-bg",  0,            "define exposure background", NAN);
-    psMetadataAddF64(normalizedexpArgs, PS_LIST_TAIL, "-bg_stdev",  0,            "define exposure background stdev", NAN);
-    psMetadataAddF64(normalizedexpArgs, PS_LIST_TAIL, "-bg_mean_stdev",  0,            "define exposure background mean stdev", NAN);
-    psMetadataAddStr(normalizedexpArgs, PS_LIST_TAIL, "-path_base",  0,            "define base output location", NULL);
     psMetadataAddU64(normalizedexpArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
     psMetadataAddBool(normalizedexpArgs, PS_LIST_TAIL, "-faulted",  0,            "only return imfiles with a fault status set", false);
@@ -475,9 +461,4 @@
     psMetadataAddU64(toresidimfileArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
     psMetadataAddBool(toresidimfileArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
-
-    // -toresidexp
-    psMetadata *toresidexpArgs = psMetadataAlloc();
-    psMetadataAddU64(toresidexpArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
-    psMetadataAddBool(toresidexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
     // -addresidimfile
@@ -487,4 +468,5 @@
     psMetadataAddStr(addresidimfileArgs, PS_LIST_TAIL, "-exp_id",  0,            "define detrend ID (required)", NULL);
     psMetadataAddStr(addresidimfileArgs, PS_LIST_TAIL, "-class_id",  0,            "define class ID (required)", NULL);
+    psMetadataAddS16(addresidimfileArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
     psMetadataAddStr(addresidimfileArgs, PS_LIST_TAIL, "-uri",  0,            "define resid file URI", NULL);
     psMetadataAddStr(addresidimfileArgs, PS_LIST_TAIL, "-recip",  0,            "define recipe", NULL);
@@ -507,5 +489,4 @@
     psMetadataAddF64(addresidimfileArgs, PS_LIST_TAIL, "-user_5",  0,            "define user statistic (5)", NAN);
     psMetadataAddStr(addresidimfileArgs, PS_LIST_TAIL, "-path_base",  0,            "define base output location", NULL);
-    psMetadataAddS16(addresidimfileArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
 
     // -residimfile
@@ -513,5 +494,5 @@
     psMetadataAddStr(residimfileArgs, PS_LIST_TAIL, "-det_id",  0,            "search for detrend ID", NULL);
     psMetadataAddS32(residimfileArgs, PS_LIST_TAIL, "-iteration",  0,            "search for iteration number", 0);
-    psMetadataAddStr(residimfileArgs, PS_LIST_TAIL, "-exp_id",  0,            "define detrend ID", NULL);
+    psMetadataAddStr(residimfileArgs, PS_LIST_TAIL, "-exp_id",  0,            "search by detrend ID", NULL);
     psMetadataAddStr(residimfileArgs, PS_LIST_TAIL, "-class_id",  0,            "search for class ID", NULL);
     psMetadataAddStr(residimfileArgs, PS_LIST_TAIL, "-recip",  0,            "search for recipe", NULL);
@@ -523,9 +504,14 @@
     // -revertresidimfile
     psMetadata *revertresidimfileArgs =  psMetadataAlloc();
-    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-det_id", 0,            "search by detrend ID (required)", NULL);
-    psMetadataAddS32(revertresidimfileArgs, PS_LIST_TAIL, "-iteration", 0,            "search by iteration number", 0);
-    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-exp_id",  0,            "search by detrend ID", NULL);
-    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-class_id",  0,            "search for class ID", NULL);
-    psMetadataAddS16(revertresidimfileArgs, PS_LIST_TAIL, "-code",  0,            "search by fault code", 0);
+    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-det_id", 0,          "search by detrend ID (required)", NULL);
+    psMetadataAddS32(revertresidimfileArgs, PS_LIST_TAIL, "-iteration", 0,       "search by iteration number", 0);
+    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-exp_id",  0,         "search by detrend ID", NULL);
+    psMetadataAddStr(revertresidimfileArgs, PS_LIST_TAIL, "-class_id",  0,       "search for class ID", NULL);
+    psMetadataAddS16(revertresidimfileArgs, PS_LIST_TAIL, "-code",  0,           "search by fault code", 0);
+
+    // -toresidexp
+    psMetadata *toresidexpArgs = psMetadataAlloc();
+    psMetadataAddU64(toresidexpArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
+    psMetadataAddBool(toresidexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
     // -addresidexp
@@ -534,4 +520,5 @@
     psMetadataAddS32(addresidexpArgs, PS_LIST_TAIL, "-iteration",  0,            "define iteration number", 0);
     psMetadataAddStr(addresidexpArgs, PS_LIST_TAIL, "-exp_id",  0,            "define detrend ID (required)", NULL);
+    psMetadataAddS16(addresidexpArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
     psMetadataAddStr(addresidexpArgs, PS_LIST_TAIL, "-recip",  0,            "define recipe", NULL);
     psMetadataAddF64(addresidexpArgs, PS_LIST_TAIL, "-bg",  0,            "define exposure background", NAN);
@@ -553,5 +540,4 @@
     psMetadataAddF64(addresidexpArgs, PS_LIST_TAIL, "-user_5",  0,            "define user statistic (5)", NAN);
     psMetadataAddStr(addresidexpArgs, PS_LIST_TAIL, "-path_base",  0,            "define base output location", NULL);
-    psMetadataAddS16(addresidexpArgs, PS_LIST_TAIL, "-code",  0,            "set fault code", 0);
     psMetadataAddBool(addresidexpArgs, PS_LIST_TAIL, "-reject",  0,            "exposure is not to be stacked in the next iteration", false);
 
@@ -562,7 +548,7 @@
     psMetadataAddStr(residexpArgs, PS_LIST_TAIL, "-exp_id",  0,            "search for exp ID", NULL);
     psMetadataAddStr(residexpArgs, PS_LIST_TAIL, "-recip",  0,            "search for recipe", NULL);
+    psMetadataAddBool(residexpArgs, PS_LIST_TAIL, "-reject",  0,            "search for acceptable residuals", false);
     psMetadataAddU64(residexpArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
     psMetadataAddBool(residexpArgs, PS_LIST_TAIL, "-faulted",  0,            "only return imfiles with a fault status set", false);
-    psMetadataAddBool(residexpArgs, PS_LIST_TAIL, "-reject",  0,            "search for acceptable residuals", false);
     psMetadataAddBool(residexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
@@ -573,9 +559,4 @@
     psMetadataAddStr(revertresidexpArgs, PS_LIST_TAIL, "-exp_id",  0,            "search by detrend ID", NULL);
     psMetadataAddS16(revertresidexpArgs, PS_LIST_TAIL, "-code",  0,            "search by fault code", 0);
-
-    // -todetrunsummary
-    psMetadata *todetrunsummaryArgs = psMetadataAlloc();
-    psMetadataAddBool(todetrunsummaryArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
-    psMetadataAddU64(todetrunsummaryArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
 
     // -updateresidexp
@@ -591,4 +572,9 @@
     psMetadataAddBool(updateresidexpArgs, PS_LIST_TAIL, "-reject",  0,            "exposure is not to be stacked in the next iteration", false);
 
+    // -todetrunsummary
+    psMetadata *todetrunsummaryArgs = psMetadataAlloc();
+    psMetadataAddBool(todetrunsummaryArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
+    psMetadataAddU64(todetrunsummaryArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
+
     // -adddetrunsummary
     psMetadata *adddetrunsummaryArgs = psMetadataAlloc();
@@ -605,9 +591,9 @@
     psMetadata *detrunsummaryArgs = psMetadataAlloc();
     psMetadataAddStr(detrunsummaryArgs, PS_LIST_TAIL, "-det_id",  0,            "search for detrend ID", NULL);
+    psMetadataAddS32(detrunsummaryArgs, PS_LIST_TAIL, "-iteration",  0,            "search for iteration number", 0);
     psMetadataAddU64(detrunsummaryArgs, PS_LIST_TAIL, "-limit",  0,                     "limit result set to N items", 0);
-    psMetadataAddS32(detrunsummaryArgs, PS_LIST_TAIL, "-iteration",  0,            "search for iteration number", 0);
     psMetadataAddBool(detrunsummaryArgs, PS_LIST_TAIL, "-faulted",  0,            "only return imfiles with a fault status set", false);
+    psMetadataAddBool(detrunsummaryArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
     psMetadataAddBool(detrunsummaryArgs, PS_LIST_TAIL, "-reject",  0,            "search for acceptable residuals", false);
-    psMetadataAddBool(detrunsummaryArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
     // -revertdetrunsummary
@@ -637,5 +623,4 @@
     psMetadata *register_detrendArgs = psMetadataAlloc();
     psMetadataAddStr(register_detrendArgs, PS_LIST_TAIL, "-det_type",  0,            "define the type of detrend run (required)", NULL);
-    psMetadataAddStr(register_detrendArgs, PS_LIST_TAIL, "-mode",  0,            "define the mode of this detrend run", "master");
     psMetadataAddStr(register_detrendArgs, PS_LIST_TAIL, "-filelevel",  0,            "define filelevel (required)", NULL);
     psMetadataAddStr(register_detrendArgs, PS_LIST_TAIL, "-workdir",  0,            "define workdir", NULL);
@@ -648,6 +633,6 @@
     psMetadataAddF32(register_detrendArgs, PS_LIST_TAIL, "-exp_time_min",  0,            "define min exposure time", NAN);
     psMetadataAddF32(register_detrendArgs, PS_LIST_TAIL, "-exp_time_max",  0,            "define max exposure time", NAN);
-    psMetadataAddF64(register_detrendArgs, PS_LIST_TAIL, "-ccd_temp_min",  0,            "define min ccd tempature", NAN);
-    psMetadataAddF64(register_detrendArgs, PS_LIST_TAIL, "-ccd_temp_max",  0,            "define max ccd tempature", NAN);
+    psMetadataAddF32(register_detrendArgs, PS_LIST_TAIL, "-ccd_temp_min",  0,            "define min ccd tempature", NAN);
+    psMetadataAddF32(register_detrendArgs, PS_LIST_TAIL, "-ccd_temp_max",  0,            "define max ccd tempature", NAN);
     psMetadataAddF64(register_detrendArgs, PS_LIST_TAIL, "-posang_min",  0,            "define min rotator position angle", NAN);
     psMetadataAddF64(register_detrendArgs, PS_LIST_TAIL, "-posang_max",  0,            "define max rotator position angle", NAN);
@@ -750,27 +735,4 @@
     psFree(modes);
 
-    // generate SQL where clause
-    config->where = psMetadataAlloc();
-
-    addWhereStr(det_id);
-    PXOPT_ADD_WHERE_S32(iteration);
-    PXOPT_ADD_WHERE_STR(det_type);
-    PXOPT_ADD_WHERE_STR(exp_id);
-    PXOPT_ADD_WHERE_STR(class_id);
-    PXOPT_ADD_WHERE_STR_ALIAS("-inst","camera");
-    PXOPT_ADD_WHERE_STR(telescope);
-    PXOPT_ADD_WHERE_STR(exp_type);
-    PXOPT_ADD_WHERE_S32(imfiles);
-    PXOPT_ADD_WHERE_STR(filter);
-    PXOPT_ADD_WHERE_STR(recipe);
-    PXOPT_ADD_WHERE_S32(guide_version);
-    PXOPT_ADD_WHERE_BOOL_ALIAS_INVERT("-reject", "accept");
-    PXOPT_ADD_WHERE_S16_ALIAS("-code", "fault");
-
-    if (config->where->list->n < 1) {
-        psFree(config->where);
-        config->where = NULL;
-    }
-
     // define Database handle, if used
     // do this last so we don't setup a connection before CLI options are
Index: /branches/eam_branch_20080706/ippTools/src/dettool_correction.c
===================================================================
--- /branches/eam_branch_20080706/ippTools/src/dettool_correction.c	(revision 18479)
+++ /branches/eam_branch_20080706/ippTools/src/dettool_correction.c	(revision 18480)
@@ -18,17 +18,4 @@
  */
 
-#ifdef HAVB_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <inttypes.h>
-
-#include <ippdb.h>
-
-#include "pxtools.h"
 #include "dettool.h"
 
@@ -37,16 +24,13 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status     = false;
-    psString det_id = psMetadataLookupStr(&status, config->args, "-det_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_id");
-        return false;
-    }
-    if (!det_id) {
-        psError(PS_ERR_UNKNOWN, true, "-det_id is required");
-        return false;
-    }
-
-    psArray *runs = detRunSelectRowObjects(config->dbh, config->where, 1);
+    PXOPT_LOOKUP_STR(det_id, config->args, "-det_id", true, false); // required
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); // optional
+
+    // build the needed where 
+    psMetadata *where = psMetadataAlloc();
+    psMetadataAddS64(where, PS_LIST_TAIL, "det_id", 0, "==", (psS64)atoll(det_id));
+    psArray *runs = detRunSelectRowObjects(config->dbh, where, 1);
+    psFree (where);
+
     if (!psArrayLength(runs)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -103,11 +87,9 @@
     }
 
-    psArray *detRuns = NULL;
-    {
-        psMetadata *where = psMetadataAlloc();
-        psMetadataAddS64(where, PS_LIST_TAIL, "det_id", 0, "==", new_det_id);
-        detRuns = psDBSelectRows(config->dbh, "detRun", where, 0);
-        psFree(where);
-    }
+    psMetadata *where_new = psMetadataAlloc();
+    psMetadataAddS64(where, PS_LIST_TAIL, "det_id", 0, "==", new_det_id);
+    psArray *detRuns = psDBSelectRows(config->dbh, "detRun", where_new, 0);
+    psFree(where_new);
+
     if (!detRuns) {
         psError(PS_ERR_UNKNOWN, false, "can't find the detRun we just created");
@@ -118,14 +100,4 @@
         psAbort("found more then one detRun matching det_id %" PRId64 "(this should not happen)", new_det_id);
         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");
-            return false;
-        }
     }
 
@@ -145,10 +117,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("dettool_tocorrectexp.sql");
@@ -183,14 +151,4 @@
     }
 
-    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, "detRun", !simple)) {
@@ -209,10 +167,9 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-det_type", "det_type", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("dettool_tocorrectimfile.sql");
@@ -222,9 +179,11 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "det1");
+    if (psListLength(where->list)) {
+	// NOTE the SQL uses an intermediate table 'det1' for this query
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "det1"); 
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree (where);
 
     // treat limit == 0 as "no limit"
@@ -253,14 +212,4 @@
     }
 
-    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, "detPendingCorrectImfile", !simple)) {
@@ -275,4 +224,5 @@
 }
 
+// NOTE : the command-line args are parsed by register_detrend_imfileMode
 bool addcorrectimfileMode(pxConfig *config)
 {
Index: /branches/eam_branch_20080706/ippTools/src/pxtools.h
===================================================================
--- /branches/eam_branch_20080706/ippTools/src/pxtools.h	(revision 18479)
+++ /branches/eam_branch_20080706/ippTools/src/pxtools.h	(revision 18480)
@@ -22,6 +22,12 @@
 
 #include <stdio.h>
+#include <string.h>   // for strcmp and strncmp
 #include <strings.h>  // for strcasecmp
 #include <unistd.h>   // for unlink
+
+// #include <stdlib.h>
+// #include <stdint.h>
+// #include <inttypes.h>
+
 #include <pslib.h>
 #include <psmodules.h>
