Index: trunk/ippScripts/scripts/magic_destreak.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak.pl	(revision 24553)
+++ trunk/ippScripts/scripts/magic_destreak.pl	(revision 24687)
@@ -29,4 +29,8 @@
 my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
 my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
+my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
+# XXX: replace with real name for tool
+my $censordetections = "";
+# my $censordetections = can_run('censordetections') or (warn "Can't find censordetections" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -89,5 +93,5 @@
 } elsif ($stage eq "diff") {
     $skycell_id = $component;
-} else {
+} elsif ($stage ne "camera") {
     &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
 }
@@ -159,81 +163,132 @@
 }
 
-# get skycell list if needed
-my ($sfh, $skycell_list);
-if ($skycell_args) {
-    my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
-    $command .= " -dbname $dbname" if defined $dbname;
-    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 magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
-    }
-
-    my $getskycells_output = join "", @$stdout_buf;
-    if ($getskycells_output) {
-        my $metadata = $mdcParser->parse($getskycells_output) or
-            &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
-
-        my $skycells = parse_md_list($metadata) or
-                &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
-
-        ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
-
-        foreach my $skycell (@$skycells) {
-            my $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base});
-            print $sfh "$skycell_uri\n";
-        }
-        close $sfh;
-    }
-}
-
-my ($image, $mask, $ch_mask, $weight, $astrom);
-
-if ($stage eq "raw") {
-    $image = $uri;
-    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
-    $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
-} elsif ($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.
-    $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);
-} elsif ($stage eq "warp") {
-    $image  = $ipprc->filename("PSWARP.OUTPUT", $path_base);
-    $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
-    $weight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $path_base);
-} elsif ($stage eq "diff") {
-    my $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images
-    $image  = $ipprc->filename($name, $path_base);
-    $mask   = $ipprc->filename("$name.MASK", $path_base);
-    $weight = $ipprc->filename("$name.VARIANCE", $path_base);
-}
-
-{
-    my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image";
-
-    $command .= " -class_id $class_id" if defined $class_id;
-    $command .= " -recovery $recoveryroot" if defined $recoveryroot;
-    $command .= " -astrom $astrom" if defined $astrom;
-    $command .= " -mask $mask" if defined $mask;
-    $command .= " -chip_mask $ch_mask" if defined $ch_mask;
-    $command .= " -weight $weight" if defined $weight;
-    $command .= " -skycelllist $skycell_list" if defined $skycell_list;
-    $command .= " -replace" if $replace;
-    $command .= " -release" if $release;
-    $command .= " -dbname $dbname" if defined $dbname;
-    unless (defined $no_op) {
+if ($stage ne "camera") {
+    # get skycell list if needed
+    my ($sfh, $skycell_list);
+    if ($skycell_args) {
+        my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
+        $command .= " -dbname $dbname" if defined $dbname;
         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 streaksremove: $error_code", $magic_ds_id, $component, $error_code);
-        }
-    } else {
-        print "skipping command $command\n";
+            &my_die("Unable to perform magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
+        }
+
+        my $getskycells_output = join "", @$stdout_buf;
+        if ($getskycells_output) {
+            my $metadata = $mdcParser->parse($getskycells_output) or
+                &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
+
+            my $skycells = parse_md_list($metadata) or
+                    &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
+
+            ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
+
+            foreach my $skycell (@$skycells) {
+                my $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base});
+                print $sfh "$skycell_uri\n";
+            }
+            close $sfh;
+        }
+    }
+
+    my ($image, $mask, $ch_mask, $weight, $astrom);
+
+    if ($stage eq "raw") {
+        $image = $uri;
+        $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
+        $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
+    } elsif ($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.
+        $image  = $ipprc->filename("PPIMAGE.CHIP", $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);
+    } elsif ($stage eq "warp") {
+        $image  = $ipprc->filename("PSWARP.OUTPUT", $path_base);
+        $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
+        $weight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $path_base);
+    } elsif ($stage eq "diff") {
+        my $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images
+        $image  = $ipprc->filename($name, $path_base);
+        $mask   = $ipprc->filename("$name.MASK", $path_base);
+        $weight = $ipprc->filename("$name.VARIANCE", $path_base);
+    }
+
+    {
+        my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image";
+
+        $command .= " -class_id $class_id" if defined $class_id;
+        $command .= " -recovery $recoveryroot" if defined $recoveryroot;
+        $command .= " -astrom $astrom" if defined $astrom;
+        $command .= " -mask $mask" if defined $mask;
+        $command .= " -chip_mask $ch_mask" if defined $ch_mask;
+        $command .= " -weight $weight" if defined $weight;
+        $command .= " -skycelllist $skycell_list" if defined $skycell_list;
+        $command .= " -replace" if $replace;
+        $command .= " -release" if $release;
+        $command .= " -dbname $dbname" if defined $dbname;
+        unless (defined $no_op) {
+            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 streaksremove: $error_code", $magic_ds_id, $component, $error_code);
+            }
+        } else {
+            print "skipping command $command\n";
+        }
+    }
+} else {
+    my $tempOutRoot = "/tmp/destreak";
+    my ($maskListFile,   $maskListName)   = tempfile( "$tempOutRoot.mask.list.XXXX",   UNLINK => !$save_temps);
+    my $files;
+    {
+        my $cam_id = $stage_id;
+        my $command = "$camtool -pendingimfile -cam_id $cam_id"; # Command to run
+        $command .= " -dbname $dbname" if defined $dbname;
+        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 camtool: $error_code", $cam_id, $error_code);
+        }
+        my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+            &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_PROG_ERROR);
+
+        # extract the metadata for the files into a hash list
+        $files = parse_md_list($metadata) or
+            &my_die("Unable to parse metadata list", $cam_id, $PS_EXIT_PROG_ERROR);
+    }
+    foreach my $file (@$files) {
+        my $class_id = $file->{class_id};
+        my $quality = $file->{quality};
+        if (!$quality) {
+            my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 
+            print $maskListFile "$mask\n";
+        }
+    }
+    close $maskListFile;
+
+    my $astrom = $ipprc->filename("PSASTRO.OUTPUT", $path_base);
+    {
+        my $output = $tmproot . basename($astrom);
+        my $command = "$censordetections -stage $stage -output $output";
+        $command .= " -astrom $astrom";
+        $command .= " -masklist $maskListName";
+        $command .= " -replace" if $replace;
+        $command .= " -dbname $dbname" if defined $dbname;
+        unless (defined $no_op) {
+            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 streaksremove: $error_code", $magic_ds_id, $component, $error_code);
+            }
+        } else {
+            print "skipping command $command\n";
+        }
     }
 }
