Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 14047)
+++ trunk/ippTools/src/dettool.c	(revision 14059)
@@ -3228,60 +3228,9 @@
     }
 
-    // select detRun.det_id (det_id)
-    // select detRun.iteration
-    // by:
-    // find the current iteration bassed on det_id
-    // find all exp_ids in the current det_id/iteration from detInputExp
-    // sort to detInputExp.imfiles to find the largest value per det_id/iter
-    // compare imfiles to the number of detStackedImfiles by class_id
-    // and:
-    // ???
-    // det_id is not in detStackedImfile;
-    // iteration is not in detStackedImfile;
-    // class_id is not in detStackedImfile;
-
-    psString query = psStringCopy(
-        "SELECT"
-        "   det_id,"
-        "   det_type,"
-        "   iteration,"
-        "   camera,"
-        "   workdir"
-        " FROM"
-        "(SELECT DISTINCT"
-        "   detRun.det_id,"
-        "   detRun.det_type,"
-        "   detRun.iteration,"
-        "   detRun.workdir,"
-        "   rawExp.camera,"
-        "   rawExp.imfiles,"
-        "   detStackedImfile.class_id"
-        " FROM detRun"
-        " JOIN detInputExp"
-        "   ON detRun.det_id = detInputExp.det_id"
-        "   AND detRun.iteration = detInputExp.iteration"
-        " JOIN rawExp"
-        "   ON detInputExp.exp_id = rawExp.exp_id"
-        " JOIN detStackedImfile"
-        "   ON detInputExp.det_id = detStackedImfile.det_id"
-        "   AND detInputExp.iteration = detStackedImfile.iteration"
-        " LEFT JOIN detNormalizedStatImfile"
-        "   ON detStackedImfile.det_id = detNormalizedStatImfile.det_id"
-        "   AND detStackedImfile.iteration = detNormalizedStatImfile.iteration"
-        "   AND detStackedImfile.class_id = detNormalizedStatImfile.class_id"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-        "   AND detStackedImfile.fault = 0"
-        "   AND detNormalizedStatImfile.det_id IS NULL"
-        "   AND detNormalizedStatImfile.iteration IS NULL"
-        "   AND detNormalizedStatImfile.class_id IS NULL"
-        " GROUP BY"
-        "   rawExp.exp_id,"
-        "   detRun.iteration,"
-        "   detRun.det_id"
-        " HAVING MAX(rawExp.imfiles) = COUNT(detStackedImfile.class_id)"
-        ") as tonormalizedstat"
-        );
+    psString query = pxDataGet("dettool_tonormalizedstat.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     // XXX does it make sense to accept any search params?
@@ -3803,42 +3752,9 @@
     }
 
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "    detRun.det_id as det_id,"
-        "    detRun.iteration,"
-        "    detRun.det_type,"
-        "    detRun.workdir,"
-        "    rawExp.camera,"
-        "    rawExp.telescope,"
-        "    rawExp.exp_type,"
-        "    rawExp.imfiles"
-        " FROM detRun"
-        " JOIN detInputExp"
-        "    ON detRun.det_id = detInputExp.det_id"
-        "    AND detRun.iteration = detInputExp.iteration"
-        " JOIN rawExp"
-        "    ON detInputExp.exp_id = rawExp.exp_id"
-        " JOIN detNormalizedImfile"
-        "    ON detInputExp.det_id = detNormalizedImfile.det_id"
-        "    AND detInputExp.iteration = detNormalizedImfile.iteration"
-        " LEFT JOIN rawImfile"
-        "    ON detInputExp.exp_id = rawImfile.exp_id"
-        "    AND detNormalizedImfile.class_id = rawImfile.class_id"
-        " LEFT JOIN detNormalizedExp"
-        "    ON detInputExp.det_id = detNormalizedExp.det_id"
-        "    AND detInputExp.iteration = detNormalizedExp.iteration"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-        "   AND detNormalizedImfile.fault = 0"
-        "   AND detNormalizedExp.det_id IS NULL"
-        "   AND detNormalizedExp.iteration IS NULL"
-        "   AND detInputExp.include = 1"
-        " GROUP BY"
-        "    detNormalizedImfile.iteration,"
-        "    detRun.det_id"
-        " HAVING"
-        "    COUNT(detNormalizedImfile.class_id) = COUNT(rawImfile.class_id)"
-    );
+    psString query = pxDataGet("dettool_tonormalizedexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     // XXX does it make sense to accept any search params?
@@ -3993,44 +3909,16 @@
     }
 
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "    detRun.det_id as det_id,"
-        "    detRun.iteration,"
-        "    detRun.det_type,"
-        "    rawExp.camera,"
-        "    rawExp.telescope,"
-        "    rawExp.exp_type,"
-        "    rawExp.imfiles"
-        " FROM detRun"
-        " JOIN detInputExp"
-        "    ON detRun.det_id = detInputExp.det_id"
-        "    AND detRun.iteration = detInputExp.iteration"
-        " JOIN rawExp"
-        "    ON detInputExp.exp_id = rawExp.exp_id"
-        " JOIN detNormalizedImfile"
-        "    ON detInputExp.det_id = detNormalizedImfile.det_id"
-        "    AND detInputExp.iteration = detNormalizedImfile.iteration"
-        " LEFT JOIN rawImfile"
-        "    ON detInputExp.exp_id = rawImfile.exp_id"
-        "    AND detNormalizedImfile.class_id = rawImfile.class_id"
-        " LEFT JOIN detNormalizedExp"
-        "    ON detInputExp.det_id = detNormalizedExp.det_id"
-        "    AND detInputExp.iteration = detNormalizedExp.iteration"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-        "   AND detNormalizedExp.det_id IS NULL"
-        "   AND detNormalizedExp.iteration IS NULL"
-        "   AND detInputExp.include = 1"
-        "   AND detRun.det_id = %s"
-        "   AND detNormalizedImfile.iteration = %d"
-        " GROUP BY"
-        "    detNormalizedImfile.iteration,"
-        "    detRun.det_id"
-        " HAVING"
-        "    COUNT(detNormalizedImfile.class_id) = COUNT(rawImfile.class_id)"
-        );
-
-    if (!p_psDBRunQuery(config->dbh, query, det_id, iteration)) {
+    psString query = pxDataGet("dettool_tonormalizedexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    psStringAppend(&query,
+            "WHERE det_id = %s"
+            " AND iteration = %d", det_id, iteration);
+
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
@@ -4993,44 +4881,9 @@
     // detResidImfile.{det_id, iteration, exp_id} is not in detResidExp
 
-    psString query = psStringCopy(
-        "SELECT DISTINCT"
-        "   det_id,"
-        "   iteration,"
-        "   exp_id,"
-        "   include"
-        " FROM"
-        "   (SELECT DISTINCT"
-        "       detRun.det_id AS det_id,"
-        "       detRun.iteration,"
-        "       detRun.det_type,"
-        "       detInputExp.exp_id,"
-        "       detInputExp.include,"
-        "       rawExp.imfiles"
-        "   FROM detRun"
-        "   JOIN detInputExp"
-        "       USING(det_id, iteration)"
-        "   JOIN rawExp"
-        "       ON detInputExp.exp_id = rawExp.exp_id"
-        "   JOIN detResidImfile"
-        "       ON detRun.det_id = detResidImfile.det_id"
-        "       AND detRun.iteration = detResidImfile.iteration"
-        "       AND detInputExp.exp_id = detResidImfile.exp_id"
-        "   LEFT JOIN detResidExp"
-        "       ON detResidImfile.det_id = detResidExp.det_id"
-        "       AND detResidImfile.iteration = detResidExp.iteration"
-        "       AND detResidImfile.exp_id = detResidExp.exp_id"
-        "   WHERE"
-        "       detRun.state = 'run'"
-        "       AND detResidExp.det_id IS NULL"
-        "       AND detResidExp.iteration IS NULL"
-        "       AND detResidExp.exp_id IS NULL"
-        "   GROUP BY"
-        "       detInputExp.exp_id,"
-        "       detRun.iteration,"
-        "       detRun.det_id"
-        "   HAVING"
-        "       rawExp.imfiles = COUNT(detResidImfile.class_id)"
-        " ) AS toresidexp"
-        );
+    psString query = pxDataGet("dettool_toresidexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     {
Index: trunk/ippTools/src/regtool.c
===================================================================
--- trunk/ippTools/src/regtool.c	(revision 14047)
+++ trunk/ippTools/src/regtool.c	(revision 14059)
@@ -897,14 +897,4 @@
     }
 
-    psS32 imfiles = psMetadataLookupS32(&status, config->args, "-imfiles");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -imfiles");
-        return false;
-    }
-    if (!imfiles) {
-        psError(PS_ERR_UNKNOWN, true, "-imfiles is required");
-        return false;
-    }
-
     psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
     if (!status) {
@@ -1074,5 +1064,4 @@
         exp_tag,
         exp_type,
-        imfiles,
         filelevel,
         exp->workdir,
Index: trunk/ippTools/src/regtoolConfig.c
===================================================================
--- trunk/ippTools/src/regtoolConfig.c	(revision 14047)
+++ trunk/ippTools/src/regtoolConfig.c	(revision 14059)
@@ -153,6 +153,4 @@
     psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-exp_type",  0,
         "search by exposure type", NULL);
-    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-imfiles",  0,
-        "search for exps with N imfiles", NULL);
     psMetadataAddU64(pendingexpArgs, PS_LIST_TAIL, "-limit",  0,
         "limit result set to N items", 0);
@@ -176,6 +174,4 @@
     psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-exp_type",  0,
         "define exposure type", NULL);
-    psMetadataAddS32(addprocessedexpArgs, PS_LIST_TAIL, "-imfiles",  0,
-        "define the number of imfiles in this exp (required)", 0);
     psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-filelevel",  0,
         "define the data partitioning level of this file", NULL);
@@ -307,15 +303,4 @@
     addWhereStr(telescope);
     addWhereStr(exp_type);
-    {
-        int imfiles = 0; 
-        bool status = false;
-        if ((imfiles = psMetadataLookupS32(&status, config->args, "-imfiles"))) {
-            if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "imfiles", 0, "==", imfiles)) {
-                psError(PXTOOLS_ERR_PROG, false, "failed to add item imfiles");
-                psFree(config);
-                return NULL;
-            }
-        }
-    }
     addWhereStr(class);
     addWhereStr(class_id);
