Index: trunk/ippScripts/scripts/magic_destreak.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak.pl	(revision 20704)
+++ trunk/ippScripts/scripts/magic_destreak.pl	(revision 20704)
@@ -0,0 +1,241 @@
+#!/usr/bin/env perl
+
+use Carp;
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Temp qw( tempfile );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use Astro::FITS::CFITSIO qw( :constants );
+Astro::FITS::CFITSIO::PerlyUnpacking(1);
+
+use PS::IPP::Config 1.01 qw( :standard );
+
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Parse the command-line arguments
+my ($magic_id, $camera, $streaks, $stage, $class_id, $skycell_id, $tmpdir, $recoverydir);
+my ($replace, $remove);
+my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
+
+GetOptions(
+           'magic_id=s'     => \$magic_id,   # Magic identifier
+           'camera=s'       => \$camera,
+           'streaks=s'      => \$streaks,    # file containing the list of streaks
+           'stage=s'        => \$stage,      # raw, chip, warp, or diff
+           'class_id=s'     => \$class_id,
+           'skycell_id=s'   => \$skycell_id,
+           'tmpdir=s'       => \$tmpdir,     # "directory" for temporary images (may be nebulous)
+           'recoverydir=s'  => \$recoverydir,# "directory" for saving the images of excised pixels
+           'replace'        => \$replace,    # replace the input images with the results.
+           'remove'         => \$remove,     # remove the original images when done YIKES!
+           'save-temps'     => \$save_temps, # Save temporary files?
+           'dbname=s'       => \$dbname,     # Database name
+           'verbose'        => \$verbose,    # Print stuff?
+           'no-update'      => \$no_update,  # Don't update the database?
+           'no-op'          => \$no_op,      # Don't do any operations?
+           'logfile=s'      => \$logfile,
+           ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --magic_id --camera --streaks --stage --tmpdir",
+           -exitval => 3) unless
+    defined $magic_id and
+    defined $camera and
+    defined $streaks and
+    defined $stage and
+    defined $tmpdir;
+    
+if (!defined($class_id) and (($stage eq "raw") or ($stage eq "chip"))) {
+    carp ("--class_id is required for stage $stage");
+    exit  $PS_EXIT_PROG_ERROR;
+} elsif (!defined($skycell_id) and (($stage eq "warp") or ($stage eq "diff"))) {
+    carp ("--skycell_id is required for stage $stage");
+    exit  $PS_EXIT_PROG_ERROR;
+}
+
+$ipprc->redirect_output($logfile) if $logfile;
+
+$ipprc->define_camera($camera);
+
+
+# Look for programs we need
+my $missing_tools;
+my $magictool      = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
+my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+my $magictool_args;
+my $skycell_args;
+my $component;
+if ($stage eq "raw") {
+    $magictool_args = "-rawimfile -class_id $class_id";
+    $skycell_args = " -class_id $class_id";
+    $component = $class_id;
+} elsif ($stage eq "chip") {
+    $magictool_args = "-chipprocessedimfile -class_id $class_id";
+    $skycell_args = " -class_id $class_id";
+    $component = $class_id;
+} elsif ($stage eq "warp") {
+    $magictool_args = "-warpskyfile -skycell_id $skycell_id";
+    $skycell_args = " -skycell_id $skycell_id";
+    $component = $skycell_id;
+} elsif ($stage eq "diff") {
+    $magictool_args = "-diffskyfile -skycell_id $skycell_id";
+    $component = $skycell_id;
+} else {
+    carp("invalid stage value found: $stage\n");
+    exit  $PS_EXIT_PROG_ERROR;
+}
+
+### Get the input information
+my $input;          
+{
+    my $command = "$magictool -magic_id $magic_id $magictool_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 magictool $magictool_args: $error_code", $magic_id, $component, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR);
+
+    my $input_list = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR);
+
+    $input = $input_list->[0];
+}
+
+# get skycell list if needed
+my ($sfh, $skycell_list);          
+if ($skycell_args) {
+    my $command = "$magictool -magic_id $magic_id -diffskyfile $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 magictool -diffskyfile $skycell_args: $error_code", $magic_id, $component, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR);
+
+    my $input_list = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR);
+    ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
+
+    foreach my $skycell (@$input_list) {
+        print $sfh "$skycell->{uri}\n"
+    }
+    close $sfh;
+}
+
+my $image = $input->{uri};
+my $path_base = $input->{path_base};
+
+my ($mask, $weight, $astrom);
+
+if ($stage eq "raw") {
+    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $input->{cam_path_base});
+} elsif ($stage eq "chip") {
+    $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id);
+    $weight = $ipprc->filename("PPIMAGE.CHIP.WEIGHT", $path_base, $class_id);
+    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $input->{cam_path_base});
+} elsif ($stage eq "warp") {
+    $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
+    $weight = $ipprc->filename("PSWARP.OUTPUT.WEIGHT", $path_base);
+} elsif ($stage eq "diff") {
+    $mask   = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base);
+    $weight = $ipprc->filename("PPSUB.OUTPUT.WEIGHT", $path_base);
+}
+
+{
+    my $command = "$streaksremove -stage $stage -tmproot $tmpdir -streaks $streaks -image $image";
+    $command .= " -class_id $class_id" if defined $class_id;
+    $command .= " -recovery $recoverydir" if defined $recoverydir;
+    $command .= " -astrom $astrom" if defined $astrom;
+    $command .= " -mask $mask" if defined $mask;
+    $command .= " -weight $weight" if defined $weight;
+    $command .= " -skycelllist $skycell_list" if defined $skycell_list;
+    $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_id, $component, $error_code);
+        }
+    } else {
+        print "skipping command $command\n";
+    }
+}
+
+### Input result into database
+#{
+#    my $command = "$magictool -addresult";
+#    $command   .= " -magic_id $magic_id";
+#    $command   .= " -node $node";
+#    $command   .= " -uri $outroot";
+#    $command   .= " -dbname $dbname" if defined $dbname;
+#
+#    # Add the processed file to the database
+#    unless ($no_update) {
+#        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 magictool -addresult: $error_code", $magic_id, $node, $error_code);
+#        }
+#    } else {
+#        print "Skipping command: $command\n";
+#    }
+#}
+
+
+
+### Pau.
+
+
+sub file_check
+{
+    my $file = shift;           # Name of file
+    &my_die("Unable to find output file: $file", $magic_id, $component, $PS_EXIT_SYS_ERROR) unless
+        $ipprc->file_exists($file);
+}
+
+sub my_die
+{
+    my $msg = shift;            # Warning message on die
+    my $magic_id = shift;       # Magic identifier
+    my $component = shift;      # class_id or skycell_id
+    my $exit_code = shift;      # Exit code to add
+
+    carp($msg);
+    exit $exit_code;
+}
+
+__END__
