Index: trunk/ippScripts/scripts/magic_destreak_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 27256)
+++ trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 27301)
@@ -30,4 +30,5 @@
 my $missing_tools;
 my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -36,5 +37,5 @@
 
 # Parse the command-line arguments
-my ($magic_ds_id, $camera, $force);
+my ($magic_ds_id, $camera);
 my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
 
@@ -42,5 +43,4 @@
            'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
            'camera=s'       => \$camera,     # camera for evaluating file rules
-           'force'          => \$force,      # force deletion
            'save-temps'     => \$save_temps, # Save temporary files?
            'dbname=s'       => \$dbname,     # Database name
@@ -87,10 +87,8 @@
 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, magicRun.inverse"
-         . " FROM magicDSRun JOIN magicRun USING(magic_id) JOIN rawExp USING(exp_id) LEFT JOIN camProcessedExp USING(cam_id)"
+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 $q2 = "SELECT * from magicDSFile where magic_ds_id = $magic_ds_id";
-
-# my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
 my $stmt1 = $dbh->prepare($q1);
@@ -104,10 +102,13 @@
 my $stage_id = $run->{stage_id};
 my $cam_path_base = $run->{cam_path_base};
+my $cam_reduction = $run->{cam_reduction};
 my $inverse = $run->{inverse};
-my $replace = $run->{re_place};
 
 
 &my_die("unexpected run state found: $state", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) if $state ne "goto_cleaned";
-&my_die("clean not allowed for raw stage, use goto_restore", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) if ($stage eq "raw"); # XXX: add this once we implement and $replace;
+&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;
 
 
@@ -126,29 +127,49 @@
 
         if ($stage eq "chip") {
-            # we use the mask output from the camera stage for input and replace
-            # the output of the chip stage with that mask as well.
-            my $cammask   = $cam_path_base ? $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $component) : undef;
+            # Check to see if we're using dynamic masks
+            my $dynamicMasks;               # Use dynamic masks?
+            {
+                # Get the PSASTRO recipe
+                my $command = "$ppConfigDump -camera $camera -recipe PSASTRO $recipe_psastro -dump-recipe PSASTRO -";
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbose);
+                unless ($success) {
+                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                    &my_die("Unable to perform ppConfigDump: $error_code", $magic_ds_id, $component,
+                            $PS_EXIT_CONFIG_ERROR);
+                }
+                my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+                my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
+                    &my_die("Unable to parse metadata config doc", $magic_ds_id, $component,
+                            $PS_EXIT_CONFIG_ERROR);
+
+                $dynamicMasks = metadataLookupBool($recipeData, 'REFSTAR_MASK');
+            }
 
             if ($backup_path_base) {
                 $bimage  = $ipprc->filename("PPIMAGE.CHIP", $backup_path_base, $component);
-                $bch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $backup_path_base, $component);
+
+                if ($dynamicMasks) {
+                    my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $component);
+                    # This is kludgey but correct
+                    $bmask = dirname($backup_path_base) . "/SR_" . basename($mask);
+                    $bch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $backup_path_base, $component);
+                } else {
+                    $bmask = $ipprc->filename("PPIMAGE.CHIP.MASK", $backup_path_base, $component);
+                }
                 $bweight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $backup_path_base, $component);
-                # This is kludgey but correct
-                
-                my $prefix;
-                if ($backup_path_base =~ /\.SR/) {
-                    $prefix = "";
+            }
+            if ($recovery_path_base) {
+                $rimage  = $ipprc->filename("PPIMAGE.CHIP", $recovery_path_base, $component);
+
+                if ($dynamicMasks) {
+                    my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $component);
+                    # This is kludgey but correct
+                    $rmask = dirname($recovery_path_base) . "/" . basename($mask);
+                    $rch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $recovery_path_base, $component);
                 } else {
-                    $prefix = "SR_";
-                }
-
-                $bmask   = dirname($backup_path_base) . "/" . $prefix . basename($cammask) if $cammask;
-            }
-            if ($recovery_path_base) {
-                $rimage  = $ipprc->filename("PPIMAGE.CHIP", $recovery_path_base, $component);
-                $rch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $recovery_path_base, $component);
+                    $rmask = $ipprc->filename("PPIMAGE.CHIP.MASK", $recovery_path_base, $component);
+                }
                 $rweight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $recovery_path_base, $component);
-                # This is kludgey but correct
-                $rmask   = dirname($recovery_path_base) . "/" . basename($cammask) if $cammask;;
             }
         } elsif ($stage eq "camera") {
@@ -162,5 +183,5 @@
                 $bweight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $backup_path_base);
                 $bsources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $backup_path_base);
-            } 
+            }
             if ($recovery_path_base) {
                 $rimage  = $ipprc->filename("PSWARP.OUTPUT", $recovery_path_base);
@@ -168,5 +189,5 @@
                 $rweight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $recovery_path_base);
                 $rsources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $recovery_path_base);
-            } 
+            }
         } elsif ($stage eq "diff") {
             my $name = "PPSUB.OUTPUT";
@@ -228,6 +249,6 @@
                 if (!$no_update) {
                     print STDERR "deleting $file\n" if $verbose;
-                    $ipprc->file_delete($file, $force) or my_die("Failed to delete $file", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR);
-                } else {
+                    $ipprc->file_delete($file, 1) or my_die("Failed to delete $file", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR);
+                    } else {
                     print STDERR "skipping delete $file\n";
                 }
