Index: /trunk/ippScripts/scripts/diff_skycell.pl
===================================================================
--- /trunk/ippScripts/scripts/diff_skycell.pl	(revision 23437)
+++ /trunk/ippScripts/scripts/diff_skycell.pl	(revision 23438)
@@ -95,4 +95,6 @@
 my $tess_id;                    # Tesselation identifier
 my $camera;                     # Camera
+my $magicked_0;
+my $magicked_1;
 foreach my $file (@$files) {
     if (defined $file->{template} and $file->{template}) {
@@ -103,4 +105,6 @@
             $templateVariance = "PPSTACK.OUTPUT.VARIANCE";
             $templateSources = "PSPHOT.OUT.CMF.MEF";  ## this must be consistent with the value in stack_skycell.pl:161
+            # template is a stack so it doesn't need to be magicked
+            $magicked_1 = 1;
             ## use an explicit stack name for psphot output objects
         } else {
@@ -108,8 +112,10 @@
             $templateVariance = "PSWARP.OUTPUT.VARIANCE";
             $templateSources = "PSWARP.OUTPUT.SOURCES";
+            $magicked_1 = $file->{magicked};
         }
     } else {
         $input = $file->{uri};
         $inputPath = $file->{path_base};
+        $magicked_0 = $file->{magicked};    # if input is a stack the output can't be "magicked"
         if ($file->{warp_id} == 0) {
             $inputMask = "PPSTACK.OUTPUT.MASK";
@@ -146,4 +152,11 @@
 &my_die("Unable to identify camera", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
 $ipprc->define_camera($camera);
+
+# Compute the magicked status of the output.
+# The output file will be considered magicked if the input has been magicked and the
+# template is either a stack or a warp that has been magicked.
+# note that difftool -inputskyfile outputs the magicked boolean as an int not T or F
+# because the output is constructed from a union of two selects
+my $magicked = $magicked_0 && $magicked_1;
 
 # Recipes to use based on reduction class
@@ -265,4 +278,5 @@
         my $command = "$difftool -adddiffskyfile -diff_id $diff_id -skycell_id $skycell_id -uri $outputName -path_base $outroot";
         $command .= " $cmdflags";
+        $command .= " -magicked" if $magicked;
         $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
         $command .= " -hostname $host" if defined $host;
Index: /trunk/ippTools/share/difftool_completed_runs.sql
===================================================================
--- /trunk/ippTools/share/difftool_completed_runs.sql	(revision 23437)
+++ /trunk/ippTools/share/difftool_completed_runs.sql	(revision 23438)
@@ -1,8 +1,10 @@
 SELECT DISTINCT
-    diff_id
+    diff_id,
+    all_magicked as magicked
 FROM (
     SELECT
         COUNT(diffInputSkyfile.skycell_id), COUNT(diffSkyfile.skycell_id),
-        diffSkyfile.*
+        diffSkyfile.*,
+        SUM(!diffSkyfile.magicked) = 0 as all_magicked
     FROM diffRun
     JOIN diffInputSkyfile USING(diff_id)
Index: /trunk/ippTools/src/difftool.c
===================================================================
--- /trunk/ippTools/src/difftool.c	(revision 23437)
+++ /trunk/ippTools/src/difftool.c	(revision 23438)
@@ -48,5 +48,5 @@
 static bool importrunMode(pxConfig *config);
 
-static bool setdiffRunState(pxConfig *config, psS64 diff_id, const char *state);
+static bool setdiffRunState(pxConfig *config, psS64 diff_id, const char *state, bool magicked);
 static bool diffRunComplete(pxConfig *config);
 
@@ -169,5 +169,5 @@
     if (state) {
         // set detRun.state to state
-        return setdiffRunState(config, diff_id, state);
+        return setdiffRunState(config, diff_id, state, false);
     }
 
@@ -283,5 +283,5 @@
 
     if (count == 2) {
-        if (!setdiffRunState(config, diff_id, "new")) {
+        if (!setdiffRunState(config, diff_id, "new", false)) {
             if (!psDBRollback(config->dbh)) {
                 psError(PS_ERR_UNKNOWN, false, "database error");
@@ -732,5 +732,5 @@
 
 
-static bool setdiffRunState(pxConfig *config, psS64 diff_id, const char *state)
+static bool setdiffRunState(pxConfig *config, psS64 diff_id, const char *state, bool magicked)
 {
     PS_ASSERT_PTR_NON_NULL(state, false);
@@ -742,6 +742,7 @@
     }
 
-    char *query = "UPDATE diffRun SET state = '%s' WHERE diff_id = %"PRId64;
-    if (!p_psDBRunQueryF(config->dbh, query, state, diff_id)) {
+    char *query = "UPDATE diffRun SET state = '%s', magicked = %d WHERE diff_id = %"PRId64;
+
+    if (!p_psDBRunQueryF(config->dbh, query, state, magicked, diff_id)) {
         psError(PS_ERR_UNKNOWN, false,
                 "failed to change state for diff_id %"PRId64, diff_id);
@@ -1169,5 +1170,5 @@
         query = NULL;
 
-        if (!setdiffRunState(config, run->diff_id, "new")) {
+        if (!setdiffRunState(config, run->diff_id, "new", false)) {
             psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
                 run->diff_id);
@@ -1432,7 +1433,8 @@
 
         psS64 diff_id = psMetadataLookupS64(NULL, row, "diff_id");
+        bool magicked = psMetadataLookupBool(NULL, row, "magicked");
 
         // set diffRun.state to 'stop'
-        if (!setdiffRunState(config, diff_id, "full")) {
+        if (!setdiffRunState(config, diff_id, "full", magicked)) {
             psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
                 diff_id);
Index: /trunk/ippTools/src/magicdstool.c
===================================================================
--- /trunk/ippTools/src/magicdstool.c	(revision 23437)
+++ /trunk/ippTools/src/magicdstool.c	(revision 23438)
@@ -40,5 +40,5 @@
 
 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
-static bool magicDSRunComplete(pxConfig *config);
+static bool magicDSRunComplete(pxConfig *config, bool setmagicked);
 static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id);
 
@@ -528,4 +528,66 @@
 }
 
+static bool
+setRunMagicked(pxConfig *config, psS64 magic_ds_id)
+{
+    // first query the magicDSRun to find the stage and the stage_id
+    psString query = "SELECT stage, stage_id from magicDSRun where magic_ds_id = %" PRId64;
+
+    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psError(PS_ERR_UNKNOWN, true, "magicDSRun not found for magic_ds_id %" PRId64, magic_ds_id);
+        psFree(output);
+        return false;
+    }
+    if (psArrayLength(output) > 1) {
+        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found %ld for magic_ds_id %" PRId64,
+            psArrayLength(output), magic_ds_id);
+        psFree(output);
+        return false;
+    }
+    psMetadata *row = output->data[0];
+
+    psString stage = psMetadataLookupStr(NULL, row, "stage");
+    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
+
+
+    // chose the appropriate query based on the stage
+    if (!strcmp(stage, "raw")) {
+        query = "UPDATE rawExp SET magicked = 1 where exp_id = %" PRId64;
+    } else if (!strcmp(stage, "chip")) {
+        query = "UPDATE chipRun SET magicked = 1 where chip_id = %" PRId64;
+    } else if (!strcmp(stage, "warp")) {
+        query = "UPDATE warpRun SET magicked = 1 where warp_id = %" PRId64;
+    } else if (!strcmp(stage, "diff")) {
+        query = "UPDATE diffRun SET magicked = 1 where diff_id = %" PRId64;
+    } else {
+        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
+        psFree(output);
+        return false;
+    }
+    if (!p_psDBRunQueryF(config->dbh, query, stage_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    psFree(output);
+
+    psU64 affected = psDBAffectedRows(config->dbh);
+    if (affected != 1) {
+        psError(PS_ERR_UNKNOWN, false, "should have affected 1 row");
+        return false;
+    }
+
+    return true;
+}
+
 static bool adddestreakedfileMode(pxConfig *config)
 {
@@ -553,6 +615,10 @@
 
     if (setmagicked) {
+        // set the image file's magicked flag
         if (!setMagicked(config, magic_ds_id, component)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
             return false;
         }
@@ -568,5 +634,5 @@
     }
 
-    if (!magicDSRunComplete(config)) {
+    if (!magicDSRunComplete(config, setmagicked)) {
             // rollback
         if (!psDBRollback(config->dbh)) {
@@ -593,5 +659,5 @@
 
     if (!strcmp(stage, "diff")) {
-        // don't need these ids for diff stage
+        // don't need these ids for diff stage because diff_id is in the magicRun
         *stage_id = 0;
         *cam_id = 0;
@@ -654,5 +720,5 @@
 }
 
-static bool magicDSRunComplete(pxConfig *config)
+static bool magicDSRunComplete(pxConfig *config, bool setmagicked)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -686,4 +752,11 @@
 
         psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id");
+
+        // if requested, set stageRun.magicked
+        if (setmagicked && !setRunMagicked(config, magic_ds_id)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to change stageRun.magicked for magic_ds_id: %" PRId64,
+                magic_ds_id);
+            return false;
+        }
 
         // set magicDSRun.state to 'full'
