Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 24682)
+++ trunk/ippTools/share/Makefile.am	(revision 24683)
@@ -159,4 +159,8 @@
      magicdstool_toremove.sql \
      magicdstool_torestore.sql \
+     magicdstool_torevert_raw.sql \
+     magicdstool_torevert_chip.sql \
+     magicdstool_torevert_warp.sql \
+     magicdstool_torevert_diff.sql \
      pstamptool_addjob_otherjob.sql \
      pstamptool_addjob_stampjob.sql \
Index: trunk/ippTools/share/magicdstool_completed_runs.sql
===================================================================
--- trunk/ippTools/share/magicdstool_completed_runs.sql	(revision 24682)
+++ trunk/ippTools/share/magicdstool_completed_runs.sql	(revision 24683)
@@ -16,5 +16,5 @@
     GROUP BY
         magicDSRun.magic_ds_id,
-        rawImfile.exp_id
+        stage_id
     HAVING
         COUNT(rawImfile.class_id) = COUNT(magicDSFile.component)
@@ -35,8 +35,25 @@
     GROUP BY
         magic_ds_id,
-        chip_id
+        stage_id
     HAVING
         COUNT(chipProcessedImfile.class_id) = COUNT(magicDSFile.component)
         AND SUM(magicDSFile.fault) = 0
+UNION
+-- camera stage
+SELECT
+    magicDSRun.magic_ds_id
+    FROM magicDSRun
+    JOIN camProcessedExp ON stage_id = camProcessedExp.cam_id
+    LEFT JOIN magicDSFile
+        ON magicDSFile.magic_ds_id = magicDSRun.magic_ds_id
+        AND magicDSFile.component IS NOT NULL
+    WHERE
+        magicDSRun.state = 'new'
+        AND magicDSRun.stage = 'camera'
+        AND camProcessedExp.quality = 0
+        AND magicDSFile.fault = 0
+    GROUP BY
+        magic_ds_id,
+        stage_id
 UNION
 -- warp stage
@@ -55,5 +72,5 @@
     GROUP BY
         magicDSRun.magic_ds_id,
-        warp_id
+        stage_id
     HAVING
         COUNT(warpSkyfile.skycell_id) = COUNT(magicDSFile.component)
Index: trunk/ippTools/share/magicdstool_todestreak.sql
===================================================================
--- trunk/ippTools/share/magicdstool_todestreak.sql	(revision 24682)
+++ trunk/ippTools/share/magicdstool_todestreak.sql	(revision 24683)
@@ -67,4 +67,42 @@
     AND chipProcessedImfile.fault = 0
     AND chipProcessedImfile.quality = 0
+    AND magicDSFile.component IS NULL
+UNION
+  -- camProcessedExp
+SELECT DISTINCT
+    magicDSRun.magic_ds_id,
+    magicDSRun.magic_id,
+    chipRun.exp_id,
+    magicDSRun.label,
+    camera,
+    magicMask.uri as streaks_uri,
+    stage,
+    stage_id,
+    'exposure' as component,
+    NULL AS uri,
+    camProcessedExp.path_base,
+    magicRun.inverse,
+    camProcessedExp.path_base as cam_path_base,
+    outroot,
+    recoveryroot,
+    re_place,
+    remove
+FROM magicDSRun
+JOIN magicMask USING (magic_id)
+JOIN magicRun USING(magic_id)
+JOIN camRun ON magicDSRun.stage_id = camRun.cam_id
+JOIN camProcessedExp ON camRun.cam_id = camProcessedExp.cam_id
+JOIN chipRun USING(chip_id)
+JOIN rawExp ON chipRun.exp_id = rawExp.exp_id
+LEFT JOIN magicDSFile
+    ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
+WHERE
+    magicDSRun.state = 'new'
+    AND magicDSRun.stage = 'camera'
+    AND camRun.state = 'full'
+    AND chipRun.state = 'full'
+    AND chipRun.magicked
+    AND camProcessedExp.fault = 0
+    AND camProcessedExp.quality = 0
     AND magicDSFile.component IS NULL
 UNION
Index: trunk/ippTools/share/magicdstool_torevert_chip.sql
===================================================================
--- trunk/ippTools/share/magicdstool_torevert_chip.sql	(revision 24683)
+++ trunk/ippTools/share/magicdstool_torevert_chip.sql	(revision 24683)
@@ -0,0 +1,20 @@
+SELECT
+    magic_ds_id,
+    re_place,
+    camera,
+    stage,
+    stage_id,
+    component,
+    outroot,
+    chipProcessedImfile.path_base,
+    camProcessedExp.path_base AS cam_path_base,
+    0 AS bytes,
+    0 AS md5sum
+FROM magicDSRun
+    JOIN magicDSFile using(magic_ds_id)
+    JOIN camProcessedExp using(cam_id)
+    JOIN chipProcessedImfile ON (stage_id = chipProcessedImfile.chip_id AND component = class_id)
+    JOIN rawExp using(exp_id)
+WHERE magicDSRun.stage = 'chip'
+    AND magicDSRun.state = 'new'
+    AND magicDSFile.fault > 0
Index: trunk/ippTools/share/magicdstool_torevert_diff.sql
===================================================================
--- trunk/ippTools/share/magicdstool_torevert_diff.sql	(revision 24683)
+++ trunk/ippTools/share/magicdstool_torevert_diff.sql	(revision 24683)
@@ -0,0 +1,20 @@
+SELECT
+    magic_ds_id,
+    re_place,
+    camera,
+    stage,
+    stage_id,
+    component,
+    outroot,
+    diffSkyfile.path_base,
+    NULL AS cam_path_base,
+    0 AS bytes,
+    0 AS md5sum
+FROM magicDSRun
+    JOIN magicRun using(magic_id)
+    JOIN magicDSFile USING(magic_ds_id)
+    JOIN diffSkyfile ON (stage_id = diffSkyfile.diff_id AND component = skycell_id)
+    JOIN rawExp USING(exp_id)
+WHERE magicDSRun.stage = 'diff'
+    AND magicDSRun.state = 'new'
+    AND magicDSFile.fault > 0
Index: trunk/ippTools/share/magicdstool_torevert_raw.sql
===================================================================
--- trunk/ippTools/share/magicdstool_torevert_raw.sql	(revision 24683)
+++ trunk/ippTools/share/magicdstool_torevert_raw.sql	(revision 24683)
@@ -0,0 +1,20 @@
+SELECT
+    magic_ds_id,
+    re_place,
+    rawImfile.camera,
+    stage,
+    stage_id,
+    component,
+    outroot,
+    rawImfile.uri AS path_base,
+    NULL AS cam_path_base,
+    bytes,
+    md5sum
+FROM magicDSRun
+    JOIN magicDSFile using(magic_ds_id)
+    JOIN rawImfile ON (stage_id = exp_id AND component = class_id)
+    JOIN summitImfile ON (rawImfile.exp_name = summitImfile.exp_name 
+                      AND rawImfile.tmp_class_id = summitImfile.class_id)
+WHERE magicDSRun.stage = 'raw'
+    AND magicDSRun.state = 'new'
+    AND magicDSFile.fault > 0
Index: trunk/ippTools/share/magicdstool_torevert_warp.sql
===================================================================
--- trunk/ippTools/share/magicdstool_torevert_warp.sql	(revision 24683)
+++ trunk/ippTools/share/magicdstool_torevert_warp.sql	(revision 24683)
@@ -0,0 +1,23 @@
+SELECT
+    magic_ds_id,
+    re_place,
+    camera,
+    stage,
+    stage_id,
+    component,
+    outroot,
+    warpSkyfile.path_base,
+    "NULL" AS cam_path_base,
+    0 AS bytes,
+    0 AS md5sum
+FROM magicDSRun
+    JOIN magicDSFile USING(magic_ds_id)
+    JOIN warpSkyfile ON (stage_id = warp_id AND component = skycell_id)
+    JOIN warpRun USING(warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun ON fakeRun.cam_id = camRun.cam_id
+    JOIN chipRun USING(chip_id)
+    JOIN rawExp USING(exp_id)
+WHERE magicDSRun.stage = 'warp'
+    AND magicDSRun.state = 'new'
+    AND magicDSFile.fault > 0
Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 24682)
+++ trunk/ippTools/src/magicdstool.c	(revision 24683)
@@ -40,4 +40,5 @@
 static bool toremoveMode(pxConfig *config);
 static bool torestoreMode(pxConfig *config);
+static bool torevertMode(pxConfig *config);
 
 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
@@ -72,4 +73,5 @@
         MODECASE(MAGICDSTOOL_MODE_TOREMOVE,            toremoveMode);
         MODECASE(MAGICDSTOOL_MODE_TORESTORE,           torestoreMode);
+        MODECASE(MAGICDSTOOL_MODE_TOREVERT,            torevertMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -502,4 +504,9 @@
     psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
 
+    if (!strcmp(stage, "camera")) {
+        // no there is no magicked column in camProcessedExp
+        psFree(output);
+        return true;
+    }
 
     // chose the appropriate query based on the stage
@@ -570,4 +577,6 @@
     } else if (!strcmp(stage, "chip")) {
         query = "UPDATE chipRun SET magicked = %" PRId64 " where chip_id = %" PRId64;
+    } else if (!strcmp(stage, "camera")) {
+        query = "UPDATE camRun SET magicked = %" PRId64 " where cam_id = %" PRId64;
     } else if (!strcmp(stage, "warp")) {
         query = "UPDATE warpRun SET magicked = %" PRId64 " where warp_id = %" PRId64;
@@ -674,6 +683,8 @@
     } else if (!strcmp(stage, "chip")) {
         stageNum = 1;
+    } else if (!strcmp(stage, "camera")) {
+        stageNum = 2;
     } else if (!strcmp(stage, "warp")) {
-        stageNum = 2;
+        stageNum = 3;
     } else {
         psError(PXTOOLS_ERR_DATA, true, "%s is not a valid value for stage", stage);
@@ -718,4 +729,6 @@
         *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
     } else if (stageNum == 2) {
+        *stage_id = *cam_id;
+    } else if (stageNum == 3) {
         *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
     }
@@ -1039,2 +1052,85 @@
     return true;
 }
+
+
+static bool torevertMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
+
+    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
+    pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString sql_file = NULL;
+    psStringAppend(&sql_file, "magicdstool_torevert_%s.sql", stage);
+
+    psString query = pxDataGet(sql_file);
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file);
+        psFree(sql_file);
+        return false;
+    }
+    psFree(sql_file);
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    // 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) {
+        psErrorCode err = psErrorCodeLast();
+        switch (err) {
+            case PS_ERR_DB_CLIENT:
+                psError(PXTOOLS_ERR_SYS, false, "database error");
+            case PS_ERR_DB_SERVER:
+                psError(PXTOOLS_ERR_PROG, false, "database error");
+            default:
+                psError(PXTOOLS_ERR_PROG, false, "unknown error");
+        }
+
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    if (psArrayLength(output)) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "torevert", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            return false;
+        }
+    }
+
+    psFree(output);
+
+    return true;
+}
+
+
Index: trunk/ippTools/src/magicdstool.h
===================================================================
--- trunk/ippTools/src/magicdstool.h	(revision 24682)
+++ trunk/ippTools/src/magicdstool.h	(revision 24683)
@@ -34,4 +34,5 @@
     MAGICDSTOOL_MODE_TOREMOVE,
     MAGICDSTOOL_MODE_TORESTORE,
+    MAGICDSTOOL_MODE_TOREVERT,
 } MAGICDStoolMode;
 
Index: trunk/ippTools/src/magicdstoolConfig.c
===================================================================
--- trunk/ippTools/src/magicdstoolConfig.c	(revision 24682)
+++ trunk/ippTools/src/magicdstoolConfig.c	(revision 24683)
@@ -130,4 +130,13 @@
     psMetadataAddBool(torestoreArgs, PS_LIST_TAIL, "-simple", 0, "use the simple output format", false);
 
+    // -torevert
+    psMetadata *torevertArgs = psMetadataAlloc();
+    psMetadataAddS64(torevertArgs, PS_LIST_TAIL, "-magic_ds_id", 0, "search by magic Destreak ID", 0);
+    psMetadataAddS64(torevertArgs, PS_LIST_TAIL, "-magic_id", 0, "search by magic ID", 0);
+    psMetadataAddStr(torevertArgs, PS_LIST_TAIL, "-stage", 0, "define output directory (required)", NULL);
+    psMetadataAddStr(torevertArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "define label", NULL);
+    psMetadataAddU64(torevertArgs, PS_LIST_TAIL, "-limit", 0, "limit result set to N items", 0);
+    psMetadataAddBool(torevertArgs, PS_LIST_TAIL, "-simple", 0, "use the simple output format", false);
+
     psFree(now);
 
@@ -153,4 +162,6 @@
     PXOPT_ADD_MODE("-torestore", "images pending restore of exicsed streak pixels",
                     MAGICDSTOOL_MODE_TORESTORE, torestoreArgs);
+    PXOPT_ADD_MODE("-torevert", "faulted images to revert",
+                    MAGICDSTOOL_MODE_TOREVERT, torevertArgs);
 
     if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
