Index: trunk/ippScripts/scripts/magic_destreak_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29068)
+++ trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29097)
@@ -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) {
