Index: branches/eam_branches/ipp-20100823/ippScripts/scripts/automate_stacks.pl
===================================================================
--- branches/eam_branches/ipp-20100823/ippScripts/scripts/automate_stacks.pl	(revision 29016)
+++ branches/eam_branches/ipp-20100823/ippScripts/scripts/automate_stacks.pl	(revision 29124)
@@ -123,5 +123,6 @@
     defined $queue_chips or defined $queue_stacks or $queue_sweetspot or $queue_detrends or $queue_dqstats or 
     defined $check_chips or defined $check_stacks or $check_sweetspot or $check_detrends or $check_dqstats or
-    defined $test_mode or defined $clean_old or defined $check_mode;
+    defined $test_mode or defined $clean_old or defined $check_mode or
+    defined $confirm_stacks;
 
 # Configurable parameters from our config file.
@@ -1271,11 +1272,17 @@
 		    print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n"
 		}
+		if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
+		    $metadata_out{nsState} = 'STACKING';
+		}
+		    
 		next;
 	    }
-	    if (defined($pretend)) {
-		add_to_macro_list('check_confirm_stacks',$stackable_list{$target},$date,$target);
-	    }
-	    else {
-		add_to_macro_list('confirm_stacks',$stackable_list{$target},$date,$target);
+	    if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
+		if (defined($pretend)) {
+		    add_to_macro_list('check_confirm_stacks',$stackable_list{$target},$date,$target);
+		}
+		else {
+		    add_to_macro_list('confirm_stacks',$stackable_list{$target},$date,$target);
+		}
 	    }
 	}
Index: branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_bundle.pl
===================================================================
--- branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_bundle.pl	(revision 29016)
+++ branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_bundle.pl	(revision 29124)
@@ -154,5 +154,5 @@
 #   2. magic is not required for this distRun
 #   3. the processing for the component produced no images (warp or diff with bad quality for example)
-my $nan_masked_pixels = ! ($clean || (($stage eq "camera") || ($stage eq 'fake') || ($stage eq 'stack')) || $no_magic || $poor_quality);
+my $nan_masked_pixels = ! ($clean || (($stage eq "camera") || ($stage eq 'fake') || ($stage eq 'stack') || ($stage eq 'sky')) || $no_magic || $poor_quality);
 
 my ($image, $mask, $variance);
Index: branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_defineruns.pl
===================================================================
--- branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_defineruns.pl	(revision 29016)
+++ branches/eam_branches/ipp-20100823/ippScripts/scripts/dist_defineruns.pl	(revision 29124)
@@ -83,5 +83,5 @@
     push @stages, $stage;
 } else {
-    @stages = qw( raw chip chip_bg camera fake warp warp_bg diff stack SSdiff);
+    @stages = qw( raw chip chip_bg camera fake warp warp_bg diff stack SSdiff sky);
 }
 
Index: branches/eam_branches/ipp-20100823/ippScripts/scripts/magic_destreak_cleanup.pl
===================================================================
--- branches/eam_branches/ipp-20100823/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29016)
+++ branches/eam_branches/ipp-20100823/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29124)
@@ -38,5 +38,5 @@
 
 # Parse the command-line arguments
-my ($magic_ds_id, $camera);
+my ($magic_ds_id, $camera, $stage);
 my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
 
@@ -44,4 +44,5 @@
            'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
            'camera=s'       => \$camera,     # camera for evaluating file rules
+           'stage=s'        => \$stage,     # camera for evaluating file rules
            'save-temps'     => \$save_temps, # Save temporary files?
            'dbname=s'       => \$dbname,     # Database name
@@ -56,7 +57,6 @@
            -exitval => 3) unless
     defined $magic_ds_id and
-    defined $camera;
-#    defined $stage and
-#    defined $stage_id;
+    defined $camera and
+    defined $stage;
 
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
@@ -79,7 +79,14 @@
 my $dbh = DBI->connect($dsn, $dbuser, $dbpassword) or die "Cannot connect to mysql server\n";
 
-my $q1 = "SELECT magicDSRun.*, camera, camProcessedExp.path_base AS cam_path_base, camRun.reduction AS cam_reduction, magicRun.inverse"
-         . " FROM magicDSRun JOIN magicRun USING(magic_id) JOIN rawExp USING(exp_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN camRun USING(cam_id)"
-         . " WHERE magic_ds_id = $magic_ds_id";
+my $q1;
+
+if ($stage ne 'diff') {
+    $q1 = "SELECT magicDSRun.*, camera, camProcessedExp.path_base AS cam_path_base, camRun.reduction AS cam_reduction"
+     . " FROM magicDSRun JOIN magicRun USING(magic_id) JOIN rawExp USING(exp_id) LEFT JOIN camProcessedExp USING(cam_id) LEFT JOIN camRun USING(cam_id)";
+} else {
+    $q1 = "SELECT magicDSRun.*, diffRun.diff_mode FROM magicDSRun JOIN diffRun ON stage_id = diffRun.diff_id AND stage = 'diff'";
+}
+$q1 .= " WHERE magic_ds_id = $magic_ds_id";
+
 my $q2 = "SELECT * from magicDSFile where magic_ds_id = $magic_ds_id";
 
@@ -89,12 +96,13 @@
 &my_die ("Unable to find magicDSRun $magic_ds_id", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) if !$nrows;
 my $run = $stmt1->fetchrow_hashref();
+$stmt1->finish();
 
 my $state = $run->{state};
-my $stage = $run->{stage};
 my $stage_id = $run->{stage_id};
 my $cam_path_base = $run->{cam_path_base};
 my $cam_reduction = $run->{cam_reduction};
 $cam_reduction = 'DEFAULT' if !$cam_reduction or ($cam_reduction eq 'NULL');
-my $inverse = $run->{inverse};
+
+my $warp_warp = ($stage eq 'diff' and $run->{diff_mode} eq 1);
 
 
@@ -105,6 +113,9 @@
 &my_die("clean not allowed for raw stage, use goto_restore", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) if $stage eq "raw";
 
-my $recipe_psastro = $ipprc->reduction($cam_reduction, 'PSASTRO'); # Recipe to use
-&my_die("Unrecognised PSASTRO recipe", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
+my $recipe_psastro;
+if ($stage eq 'chip') {
+    $recipe_psastro = $ipprc->reduction($cam_reduction, 'PSASTRO'); # Recipe to use
+    &my_die("Unrecognised PSASTRO recipe", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
+}
 
 
@@ -217,5 +228,5 @@
         delete_files($rimage, $rmask, $rweight, $rsources, $rastrom, $bimage, $bmask, $bweight, $bsources, $bastrom, $bch_mask, $rch_mask);
 
-        if ($stage eq "diff" and $inverse) {
+        if ($stage eq "diff" and $warp_warp) {
             my $name = "PPSUB.INVERSE";
             if ($backup_path_base) {
Index: branches/eam_branches/ipp-20100823/ippScripts/scripts/staticsky.pl
===================================================================
--- branches/eam_branches/ipp-20100823/ippScripts/scripts/staticsky.pl	(revision 29016)
+++ branches/eam_branches/ipp-20100823/ippScripts/scripts/staticsky.pl	(revision 29124)
@@ -71,4 +71,5 @@
 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $sky_id, $PS_EXIT_SYS_ERROR ) if $redirect;
 
+my $traceDest      = $ipprc->filename("TRACE.EXP", $outroot);
 # my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_STATICSKY);
 
@@ -164,5 +165,5 @@
     $command .= " -recipe PSPHOT $recipe_psphot";
     $command .= " -dumpconfig $configuration";
-    # $command .= " -tracedest $traceDest -log $logDest";
+    $command .= " -tracedest $traceDest -log $logDest";
     # $command .= " -dbname $dbname" if defined $dbname;
     # $command .= " -image_id $diff_skyfile_id" if defined $diff_skyfile_id;
