Index: /trunk/ippScripts/scripts/phase2.pl
===================================================================
--- /trunk/ippScripts/scripts/phase2.pl	(revision 8619)
+++ /trunk/ippScripts/scripts/phase2.pl	(revision 8619)
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use IPC::Cmd qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+use Data::Dumper;
+
+use constant RECIPE => 'PPIMAGE_OBDF.config'; # Recipe to use
+use constant PREFIX => 'obdf';	# Prefix to attach to filenames
+use constant DELETE_STATS => 0;	# Delete the statistics file when done?
+
+# Parse the command-line arguments
+if (scalar @ARGV != 4) {
+    die "Perform phase 2 processing.\n\n" .
+	"Usage: $0 EXP_ID CLASS_ID INPUT.fits OUTPUT_ROOT\n\n";
+}
+my $expId = shift @ARGV;	# Exposure ID
+my $classId = shift @ARGV;	# Class ID
+my $input = shift @ARGV;	# Input FITS file
+my $output = shift @ARGV;	# Output root name
+
+# Look for programs we need
+my $missing_tools;
+my $p2tool = can_run('p2tool') or (warn "Can't find p2tool" and $missing_tools = 1);
+my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
+die "Can't find required tools.\n" if $missing_tools;
+
+### Output file name --- must match camera configuration!
+my $outputRoot =  PREFIX . '_' . $output;
+my $outputName = $outputRoot . '.' . $classId ;
+my $outputImage = $outputName . '.fit';
+my $outputStats = $outputName . '.stats';
+
+# Run ppImage
+{
+    my $command = "$ppImage -file $input $outputRoot -recipe PPIMAGE " . RECIPE .
+	" -stat $outputStats"; # Command to run ppImage
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform ppImage on $input: $error_code\n" if not $success;
+    die "Couldn't find expected output file: $outputImage\n" if not -f $outputImage;
+}
+
+# Get the statistics on the processed image
+my $stats;			# Statistics from ppImage
+{
+    my $statsFile;		# File handle
+    open $statsFile, "$outputStats" or die "Can't open statistics file $outputStats: $!\n";
+    my @contents = <$statsFile>; # Contents of file
+    close $statsFile;
+    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+    my $metadata = $mdcParser->parse(join "", @contents); # Metadata from the stats
+    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+}
+
+# Add the processed file to the database
+{
+    my $command = "$p2tool -processed -exp_id $expId -class_id $classId -recip $recipe " .
+	"-uri $outputImage"; # Command to run dettool
+    $command .= " -bg " . $stats->bg_mean();
+    $command .= " -bg_stdev " . $stats->bg_stdev();
+    $command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform p2tool -processed for $expId/$classId: $error_code\n"
+	if not $success;
+}
+
+unlink "$outputStats" if DELETE_STATS;
+
+__END__
