Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 15786)
+++ trunk/ippTools/src/dettool.c	(revision 15828)
@@ -128,9 +128,11 @@
         MODECASE(DETTOOL_MODE_DEFINEBYQUERY,    definebyqueryMode);
         MODECASE(DETTOOL_MODE_DEFINEBYDETRUN,   definebydetrunMode);
-        MODECASE(DETTOOL_MODE_MAKECORRECTION,   makecorrectionMode);
         MODECASE(DETTOOL_MODE_RUNS,             runsMode);
         MODECASE(DETTOOL_MODE_CHILDLESSRUN,     childlessrunMode);
         MODECASE(DETTOOL_MODE_INPUT,            inputMode);
         MODECASE(DETTOOL_MODE_RAW,              rawMode);
+        // correction
+        MODECASE(DETTOOL_MODE_TOCORRECTEXP,      tocorrectexpMode);
+        MODECASE(DETTOOL_MODE_TOCORRECTIMFILE,   tocorrectimfileMode);
         // imfile
         MODECASE(DETTOOL_MODE_TOPROCESSEDIMFILE,toprocessedimfileMode);
Index: trunk/ippTools/src/dettool.h
===================================================================
--- trunk/ippTools/src/dettool.h	(revision 15786)
+++ trunk/ippTools/src/dettool.h	(revision 15828)
@@ -30,4 +30,6 @@
     DETTOOL_MODE_DEFINEBYDETRUN,
     DETTOOL_MODE_MAKECORRECTION,
+    DETTOOL_MODE_TOCORRECTEXP,
+    DETTOOL_MODE_TOCORRECTIMFILE,
     DETTOOL_MODE_RUNS,
     DETTOOL_MODE_CHILDLESSRUN,
@@ -80,4 +82,6 @@
 
 bool makecorrectionMode(pxConfig *config);
+bool tocorrectexpMode(pxConfig *config);
+bool tocorrectimfileMode(pxConfig *config);
 
 #endif // DETTOOL_H
Index: trunk/ippTools/src/dettoolConfig.c
===================================================================
--- trunk/ippTools/src/dettoolConfig.c	(revision 15786)
+++ trunk/ippTools/src/dettoolConfig.c	(revision 15828)
@@ -274,4 +274,22 @@
     psMetadataAddStr(makecorrectionArgs, PS_LIST_TAIL, "-det_id",  0,
             "det ID to be corrected (required)", NULL);
+    psMetadataAddBool(makecorrectionArgs, PS_LIST_TAIL, "-simple", 0,
+            "use the simple output format", false);
+
+    // -tocorrectexp
+    psMetadata *tocorrectexpArgs = psMetadataAlloc();
+    psMetadataAddU64(tocorrectexpArgs, PS_LIST_TAIL, "-limit",  0,
+            "limit result set to N items", 0);
+    psMetadataAddBool(tocorrectexpArgs, PS_LIST_TAIL, "-simple",  0,
+            "use the simple output format", false);
+
+    // -tocorrectimfile
+    psMetadata *tocorrectimfileArgs = psMetadataAlloc();
+    psMetadataAddStr(tocorrectimfileArgs, PS_LIST_TAIL, "-det_id", 0,
+            "search for detrend ID", NULL);
+    psMetadataAddU64(tocorrectimfileArgs, PS_LIST_TAIL, "-limit",  0,
+            "limit result set to N items", 0);
+    psMetadataAddBool(tocorrectimfileArgs, PS_LIST_TAIL, "-simple",  0,
+            "use the simple output format", false);
 
     // -runs
@@ -1070,4 +1088,6 @@
     PXTOOL_ADD_MODE("-definebydetrun",  "", DETTOOL_MODE_DEFINEBYDETRUN, definebydetrunArgs);
     PXTOOL_ADD_MODE("-makecorrection",  "", DETTOOL_MODE_MAKECORRECTION, makecorrectionArgs);
+    PXTOOL_ADD_MODE("-tocorrectexp",    "", DETTOOL_MODE_TOCORRECTEXP, tocorrectexpArgs);
+    PXTOOL_ADD_MODE("-tocorrectimfile", "", DETTOOL_MODE_TOCORRECTIMFILE, tocorrectimfileArgs);
     PXTOOL_ADD_MODE("-raw",             "", DETTOOL_MODE_RAW,           rawArgs);
     PXTOOL_ADD_MODE("-runs",            "", DETTOOL_MODE_RUNS,          runsArgs);
Index: trunk/ippTools/src/dettool_correction.c
===================================================================
--- trunk/ippTools/src/dettool_correction.c	(revision 15786)
+++ trunk/ippTools/src/dettool_correction.c	(revision 15828)
@@ -95,8 +95,32 @@
     psFree(runs);
 
-
-    // point of no return for det_id creation
+    // print the new detRun
+    psS64 new_det_id = psDBLastInsertID(config->dbh);
+
     if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    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);
+    }
+    if (!detRuns) {
+        psError(PS_ERR_UNKNOWN, false, "can't find the detRun we just created");
+        return false;
+    }
+    // sanity check results
+    if (psArrayLength(detRuns) != 1) {
+        psAbort("found more then one detRun matching det_id %" PRId64 "(this should not happen)", new_det_id);
+        return false;
+    }
+
+    if (!convertIdToStr(detRuns)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
+        psFree(detRuns);
         return false;
     }
@@ -112,29 +136,4 @@
     }
 
-#if 0
-    // print the new det_id
-    psArray *detRuns = NULL;
-    {
-        psMetadata *where = psMetadataAlloc();
-        psMetadataAddS64(where, PS_LIST_TAIL, "det_id", 0, "==", det_id);
-        detRuns = psDBSelectRows(config->dbh, "detRun", where, 0);
-        psFree(where);
-    }
-    if (!detRuns) {
-        psError(PS_ERR_UNKNOWN, false, "can't find the detRun we just created");
-        return false;
-    }
-    // sanity check results
-    if (psArrayLength(detRuns) != 1) {
-        psAbort("found more then one detRun matching det_id %" PRId64 " (this should not happen)", det_id);
-        return false;
-    }
-
-    if (!convertIdToStr(detRuns)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
-        psFree(detRuns);
-        return false;
-    }
-
     // negative simple so the default is true
     if (!ippdbPrintMetadatas(stdout, detRuns, "detRun", !simple)) {
@@ -144,6 +143,139 @@
     }
     psFree(detRuns);
-#endif
 
     return true;
 }
+
+bool tocorrectexpMode(pxConfig *config)
+{
+    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;
+    }
+
+    psString query = pxDataGet("dettool_tocorrectexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    // 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");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("dettool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    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)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
+    return true;
+}
+
+bool tocorrectimfileMode(pxConfig *config)
+{
+    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;
+    }
+
+    psString query = pxDataGet("dettool_tocorrectimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "det1");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    // 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");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("dettool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    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)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
+    return true;
+}
