Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 14242)
+++ trunk/ippTools/share/Makefile.am	(revision 14243)
@@ -14,4 +14,5 @@
 	detselect_select.sql \
 	dettool_find_completed_runs.sql \
+	dettool_revertnormalizedimfile.sql \
 	dettool_normalizedstat.sql
 	dettool_raw.sql \
Index: trunk/ippTools/share/dettool_revertnormalizedimfile.sql
===================================================================
--- trunk/ippTools/share/dettool_revertnormalizedimfile.sql	(revision 14243)
+++ trunk/ippTools/share/dettool_revertnormalizedimfile.sql	(revision 14243)
@@ -0,0 +1,3 @@
+DELETE FROM detNormalizedImfile
+WHERE
+    fault != 0
Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 14242)
+++ trunk/ippTools/src/dettool.c	(revision 14243)
@@ -64,10 +64,14 @@
 static bool tonormalizeMode(pxConfig *config);
 static bool addnormalizedimfileMode(pxConfig *config);
+static bool normalizedimfileMode(pxConfig *config);
+static bool revertnormalizedimfileMode(pxConfig *config);
+// normalizedexp
 static bool tonormalizedexpMode(pxConfig *config);
 static bool addnormalizedexpMode(pxConfig *config);
 static bool normalizedexpMode(pxConfig *config);
+
+// residimfile
 static bool toresidimfileMode(pxConfig *config);
 static bool addresidimfileMode(pxConfig *config);
-static bool normalizedimfileMode(pxConfig *config);
 static bool toresidexpMode(pxConfig *config);
 static bool residimfileMode(pxConfig *config);
@@ -147,4 +151,6 @@
         MODECASE(DETTOOL_MODE_ADDNORMALIZEDIMFILE,addnormalizedimfileMode);
         MODECASE(DETTOOL_MODE_NORMALIZEDIMFILE, normalizedimfileMode);
+        MODECASE(DETTOOL_MODE_REVERTNORMALIZEDIMFILE, revertnormalizedimfileMode);
+        // normalizedexp
         MODECASE(DETTOOL_MODE_TONORMALIZEDEXP,  tonormalizedexpMode);
         MODECASE(DETTOOL_MODE_ADDNORMALIZEDEXP, addnormalizedexpMode);
@@ -3977,4 +3983,133 @@
 }
 
+
+static bool normalizedimfileMode(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;
+    }
+
+    bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -faulted");
+        return false;
+    }
+
+    psString query = psStringCopy(
+        "SELECT"
+        " detNormalizedImfile.*"
+        " FROM detNormalizedImfile"
+        " JOIN detRun"
+        "   USING(det_id, iteration)"
+        " WHERE"
+        "   detRun.state = 'run'"
+        "   AND detRun.mode = 'master'"
+    );
+
+    if (config->where) {
+        bool status;
+        int iteration = psMetadataLookupS32 (&status, config->where, "iteration");
+        if (status) {
+            psMetadataRemoveKey (config->where, "iteration");
+            psMetadataAddS32 (config->where, PS_LIST_TAIL, "iteration", 0, "==", iteration);
+        }
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detNormalizedImfile");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (faulted) {
+        // list only faulted rows
+        psStringAppend(&query, " %s", "AND detNormalizedImfile.fault != 0");
+    } else {
+        // don't list faulted rows
+        psStringAppend(&query, " %s", "AND detNormalizedImfile.fault = 0");
+    }
+
+    // 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, "detNormalizedImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
+    return true;
+}
+
+
+static bool revertnormalizedimfileMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psString query = pxDataGet("dettool_revertnormalizedimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detNormalizedImfile");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    if (psDBAffectedRows(config->dbh) < 1) {
+        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
+        return false;
+    }
+
+    return true;
+}
+
+
 static bool tonormalizedexpMode(pxConfig *config)
 {
@@ -4464,98 +4599,4 @@
 }
 
-static bool normalizedimfileMode(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;
-    }
-
-    bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -faulted");
-        return false;
-    }
-
-    psString query = psStringCopy(
-        "SELECT"
-        " detNormalizedImfile.*"
-        " FROM detNormalizedImfile"
-        " JOIN detRun"
-        "   USING(det_id, iteration)"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-    );
-
-    if (config->where) {
-        bool status;
-        int iteration = psMetadataLookupS32 (&status, config->where, "iteration");
-        if (status) {
-            psMetadataRemoveKey (config->where, "iteration");
-            psMetadataAddS32 (config->where, PS_LIST_TAIL, "iteration", 0, "==", iteration);
-        }
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detNormalizedImfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    if (faulted) {
-        // list only faulted rows
-        psStringAppend(&query, " %s", "AND detNormalizedImfile.fault != 0");
-    } else {
-        // don't list faulted rows
-        psStringAppend(&query, " %s", "AND detNormalizedImfile.fault = 0");
-    }
-
-    // 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, "detNormalizedImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
 
 static bool addresidimfileMode(pxConfig *config)
Index: trunk/ippTools/src/dettool.h
===================================================================
--- trunk/ippTools/src/dettool.h	(revision 14242)
+++ trunk/ippTools/src/dettool.h	(revision 14243)
@@ -52,4 +52,5 @@
     DETTOOL_MODE_ADDNORMALIZEDIMFILE,
     DETTOOL_MODE_NORMALIZEDIMFILE,
+    DETTOOL_MODE_REVERTNORMALIZEDIMFILE,
     DETTOOL_MODE_TONORMALIZEDEXP,
     DETTOOL_MODE_ADDNORMALIZEDEXP,
Index: trunk/ippTools/src/dettoolConfig.c
===================================================================
--- trunk/ippTools/src/dettoolConfig.c	(revision 14242)
+++ trunk/ippTools/src/dettoolConfig.c	(revision 14243)
@@ -606,4 +606,32 @@
             "set fault code", 0);
 
+    // -normalizedimfile
+    psMetadata *normalizedimfileArgs = psMetadataAlloc();
+    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-det_id",  0,
+            "search for detrend ID", NULL);
+    psMetadataAddS32(normalizedimfileArgs, PS_LIST_TAIL, "-iteration",  0,
+            "search for iteration number", 0);
+    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
+            "search for class ID", NULL);
+    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-recip",  0,
+            "search for recipe", NULL);
+    psMetadataAddU64(normalizedimfileArgs, PS_LIST_TAIL, "-limit",  0,
+            "limit result set to N items", 0);
+    psMetadataAddBool(normalizedimfileArgs, PS_LIST_TAIL, "-faulted",  0,
+            "only return imfiles with a fault status set", false);
+    psMetadataAddBool(normalizedimfileArgs, PS_LIST_TAIL, "-simple",  0,
+            "use the simple output format", false);
+
+    // -revertnormalizedimfile
+    psMetadata *revertnormalizedimfileArgs = psMetadataAlloc();
+    psMetadataAddStr(revertnormalizedimfileArgs, PS_LIST_TAIL, "-det_id", 0,
+            "search by detrend ID (required)", NULL);
+    psMetadataAddS32(revertnormalizedimfileArgs, PS_LIST_TAIL, "-iteration", 0,
+            "search by iteration number", 0);
+    psMetadataAddStr(revertnormalizedimfileArgs, PS_LIST_TAIL, "-class_id", 0,
+            "search by class ID", NULL);
+    psMetadataAddS16(revertnormalizedimfileArgs, PS_LIST_TAIL, "-code",  0,
+            "search by fault code", 0);
+
     // -tonormalizedexp
     psMetadata *tonormalizedexpArgs = psMetadataAlloc();
@@ -672,21 +700,4 @@
             "limit result set to N items", 0);
     psMetadataAddBool(toresidimfileArgs, PS_LIST_TAIL, "-simple",  0,
-            "use the simple output format", false);
-
-    // -normalizedimfile
-    psMetadata *normalizedimfileArgs = psMetadataAlloc();
-    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-det_id",  0,
-            "search for detrend ID", NULL);
-    psMetadataAddS32(normalizedimfileArgs, PS_LIST_TAIL, "-iteration",  0,
-            "search for iteration number", 0);
-    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
-            "search for class ID", NULL);
-    psMetadataAddStr(normalizedimfileArgs, PS_LIST_TAIL, "-recip",  0,
-            "search for recipe", NULL);
-    psMetadataAddU64(normalizedimfileArgs, PS_LIST_TAIL, "-limit",  0,
-            "limit result set to N items", 0);
-    psMetadataAddBool(normalizedimfileArgs, PS_LIST_TAIL, "-faulted",  0,
-            "only return imfiles with a fault status set", false);
-    psMetadataAddBool(normalizedimfileArgs, PS_LIST_TAIL, "-simple",  0,
             "use the simple output format", false);
 
@@ -1009,4 +1020,5 @@
     PXTOOL_ADD_MODE("-addnormalizedimfile", "", DETTOOL_MODE_ADDNORMALIZEDIMFILE,addnormalizedimfileArgs);
     PXTOOL_ADD_MODE("-normalizedimfile","", DETTOOL_MODE_NORMALIZEDIMFILE, normalizedimfileArgs);
+    PXTOOL_ADD_MODE("-revertnormalizedimfile","", DETTOOL_MODE_REVERTNORMALIZEDIMFILE, normalizedimfileArgs);
     PXTOOL_ADD_MODE("-tonormalizedexp", "", DETTOOL_MODE_TONORMALIZEDEXP, tonormalizedexpArgs);
     PXTOOL_ADD_MODE("-addnormalizedexp", "", DETTOOL_MODE_ADDNORMALIZEDEXP, addnormalizedexpArgs);
