Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 14265)
+++ trunk/ippTools/src/dettool.c	(revision 14266)
@@ -41,10 +41,10 @@
 static bool inputMode(pxConfig *config);
 static bool rawMode(pxConfig *config);
-// imfile
+// processedimfile
 static bool toprocessedimfileMode(pxConfig *config);
 static bool addprocessedimfileMode(pxConfig *config);
 static bool processedimfileMode(pxConfig *config);
 static bool revertprocessedimfileMode(pxConfig *config);
-// exp
+// processedexp
 static bool toprocessedexpMode(pxConfig *config);
 static bool addprocessedexpMode(pxConfig *config);
@@ -74,6 +74,8 @@
 static bool toresidimfileMode(pxConfig *config);
 static bool addresidimfileMode(pxConfig *config);
+static bool residimfileMode(pxConfig *config);
+static bool revertresidimfileMode(pxConfig *config);
+// residexp
 static bool toresidexpMode(pxConfig *config);
-static bool residimfileMode(pxConfig *config);
 static bool addresidexpMode(pxConfig *config);
 static bool residexpMode(pxConfig *config);
@@ -161,14 +163,17 @@
         MODECASE(DETTOOL_MODE_ADDRESIDIMFILE,   addresidimfileMode);
         MODECASE(DETTOOL_MODE_RESIDIMFILE,      residimfileMode);
-
+        MODECASE(DETTOOL_MODE_REVERTRESIDIMFILE,revertresidimfileMode);
+        // residexp
         MODECASE(DETTOOL_MODE_TORESIDEXP,       toresidexpMode);
         MODECASE(DETTOOL_MODE_ADDRESIDEXP,      addresidexpMode);
         MODECASE(DETTOOL_MODE_RESIDEXP,         residexpMode);
+        MODECASE(DETTOOL_MODE_UPDATERESIDEXP,   updateresidexpMode);
+        // detrunsummary
         MODECASE(DETTOOL_MODE_TODETRUNSUMMARY,  todetrunsummaryMode);
-        MODECASE(DETTOOL_MODE_UPDATERESIDEXP,   updateresidexpMode);
         MODECASE(DETTOOL_MODE_ADDDETRUNSUMMARY, adddetrunsummaryMode);
         MODECASE(DETTOOL_MODE_DETRUNSUMMARY,    detrunsummaryMode);
         MODECASE(DETTOOL_MODE_UPDATEDETRUN,     updatedetrunMode);
         MODECASE(DETTOOL_MODE_RERUN,            rerunMode);
+        // register
         MODECASE(DETTOOL_MODE_REGISTER_DETREND, register_detrendMode);
         MODECASE(DETTOOL_MODE_REGISTER_DETREND_IMFILE, register_detrend_imfileMode);
@@ -5091,4 +5096,37 @@
 }
 
+
+static bool revertresidimfileMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psString query = pxDataGet("dettool_revertresidimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detResidImfile");
+        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 toresidexpMode(pxConfig *config)
 {
Index: trunk/ippTools/src/dettool.h
===================================================================
--- trunk/ippTools/src/dettool.h	(revision 14265)
+++ trunk/ippTools/src/dettool.h	(revision 14266)
@@ -60,4 +60,5 @@
     DETTOOL_MODE_ADDRESIDIMFILE,
     DETTOOL_MODE_RESIDIMFILE,
+    DETTOOL_MODE_REVERTRESIDIMFILE,
     DETTOOL_MODE_TORESIDEXP,
     DETTOOL_MODE_RESIDEXP,
Index: trunk/ippTools/src/dettoolConfig.c
===================================================================
--- trunk/ippTools/src/dettoolConfig.c	(revision 14265)
+++ trunk/ippTools/src/dettoolConfig.c	(revision 14266)
@@ -782,4 +782,17 @@
             "search for state", NULL);
 
+    // -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);
+
     // -addresidexp
     psMetadata *addresidexpArgs = psMetadataAlloc();
@@ -1035,6 +1048,7 @@
     PXTOOL_ADD_MODE("-revertnormalizedexp","", DETTOOL_MODE_REVERTNORMALIZEDEXP, revertnormalizedexpArgs);
     PXTOOL_ADD_MODE("-toresidimfile",   "", DETTOOL_MODE_TORESIDIMFILE, toresidimfileArgs);
-    PXTOOL_ADD_MODE("-addresidimfile",  "", DETTOOL_MODE_ADDRESIDIMFILE,  addresidimfileArgs);
-    PXTOOL_ADD_MODE("-residimfile",     "", DETTOOL_MODE_RESIDIMFILE,    residimfileArgs);
+    PXTOOL_ADD_MODE("-addresidimfile",  "", DETTOOL_MODE_ADDRESIDIMFILE, addresidimfileArgs);
+    PXTOOL_ADD_MODE("-residimfile",     "", DETTOOL_MODE_RESIDIMFILE, residimfileArgs);
+    PXTOOL_ADD_MODE("-revertresidimfile", "", DETTOOL_MODE_REVERTRESIDIMFILE, revertresidimfileArgs);
     PXTOOL_ADD_MODE("-toresidexp",      "", DETTOOL_MODE_TORESIDEXP,    toresidexpArgs);
     PXTOOL_ADD_MODE("-addresidexp",     "", DETTOOL_MODE_ADDRESIDEXP,  addresidexpArgs);
