Index: trunk/ippScripts/scripts/magic_destreak.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak.pl	(revision 26554)
+++ trunk/ippScripts/scripts/magic_destreak.pl	(revision 27301)
@@ -32,4 +32,5 @@
 my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
 my $censorObjects = can_run('censorObjects') or (warn "Can't find censorObjects" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 if ($missing_tools) {
@@ -39,5 +40,5 @@
 
 # Parse the command-line arguments
-my ($magic_ds_id, $camera, $streaks, $inv_streaks, $exp_id, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base);
+my ($magic_ds_id, $camera, $streaks, $inv_streaks, $exp_id, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base, $cam_reduction);
 my ($outroot, $recoveryroot);
 my ($replace, $release);
@@ -56,4 +57,5 @@
            'path_base=s'    => \$path_base,  # path_base of the input
            'cam_path_base=s'=> \$cam_path_base,  # path_base from camera stage (for chip and raw)
+           'cam_reduction=s'=> \$cam_reduction,  # reduction class from camera stage (for chip and raw)
            'outroot=s'      => \$outroot,     # "directory" for temporary images (may be nebulous)
            'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels
@@ -84,4 +86,6 @@
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 $ipprc->redirect_output($logfile) or my_die( "Unable to redirect output", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR ) if $logfile;
+my $recipe_psastro = $ipprc->reduction($cam_reduction, 'PSASTRO'); # Recipe to use
+&my_die("Unrecognised PSASTRO recipe", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
 
 my ($skycell_args, $class_id, $skycell_id);
@@ -97,5 +101,5 @@
 } elsif ($stage ne "camera") {
     &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
-}    
+}
 $inv_streaks = undef if defined($inv_streaks) and ($inv_streaks eq "NULL");
 
@@ -114,6 +118,6 @@
     if ($stage eq 'camera') {
         my $nebulousServer = metadataLookupStr( $ipprc->{_siteConfig}, 'NEB_SERVER' );
-        &my_die("cannot find NEB_SERVER in site configuration", 
-                                        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) 
+        &my_die("cannot find NEB_SERVER in site configuration",
+                                        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR)
             if !$ nebulousServer;
 
@@ -233,16 +237,37 @@
         $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
     } elsif ($stage eq "chip") {
+
+        # 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 $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');
+        }
+
         # we use the mask output from the camera stage for input and replace
         # the output of the chip stage with that mask as well.
         $image  = $ipprc->filename("PPIMAGE.CHIP", $path_base, $class_id);
-        $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
-        $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
         $weight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
         $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
-        if (!$ipprc->file_exists($mask)) {
-            carp("camera mask file $mask for $component not found. Continuing using mask from chip stage.");
-            $mask = $ch_mask;
-            $ch_mask = undef;
-        }
+
+        if ($dynamicMasks) {
+            $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
+            $ch_mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
+        } else {
+            $mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
+        }
+
         # XXX: should we censor the sources as well? For now we're leaving them out of the distribution bundles
         # because they confuse people
@@ -262,5 +287,5 @@
 
         if ($inv_streaks) {
-            # create a temporary file containing the contents of the 
+            # create a temporary file containing the contents of the
             # two streaks files
             ($allstreaks_fh, $allstreaks_name) = tempfile ("/tmp/all.streaks.XXXX",
@@ -357,5 +382,5 @@
         my $quality = $file->{quality};
         if (!$quality) {
-            my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 
+            my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id);
             if (! $ipprc->file_exists($mask)) {
                 # camera mask doesn't exist for this chip. Fall back to the chip mask
@@ -379,5 +404,5 @@
                 &my_die("Unable to perform censorObjects: $error_code", $magic_ds_id, $component, $error_code);
             }
-            my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base); 
+            my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base);
             &my_die("expected output file $output not found ", $magic_ds_id, $component, $PS_EXIT_DATA_ERROR)
                 unless $ipprc->file_exists($output);
@@ -481,5 +506,5 @@
     }
 
-    close $fout 
+    close $fout
         or my_die("failed to close combined streaks file", $magic_ds_id,
                      $component, $PS_EXIT_UNKNOWN_ERROR);
