Index: trunk/ippScripts/scripts/phase5_subtract.pl
===================================================================
--- trunk/ippScripts/scripts/phase5_subtract.pl	(revision 11830)
+++ trunk/ippScripts/scripts/phase5_subtract.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($p5_id, $camera, $dbname, $workdir, $no_update);
+my ($p5_id, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'p5_id|d=s'         => \$p5_id, # Phase 5 identifier
@@ -39,5 +39,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
-    'no-update'         => \$no_update,
+    'no-update'         => \$no_update,	# Don't update the database?
+    'no-op'             => \$no_op, # Don't do any operations?
 ) or pod2usage( 2 );
 
@@ -61,7 +62,8 @@
 
 # Get list of components for subtraction
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 my $files;
 {
-    my $command = "$p5tool -inputscfile -p5_id $p4_id";
+    my $command = "$p5tool -inputscfile -p5_id $p5_id";
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -116,10 +118,11 @@
 
 my $outputName = $outputRoot . ".fits";
-# my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
-# my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
-# my $outputStats = $outputRoot . '.stats';
+my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot);
+my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot);
+my $outputStats = $outputRoot . '.stats';
 
 # Perform subtraction
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$pois $template $input $outputName"; # Command to run pois
 
@@ -134,19 +137,14 @@
 #    &my_die("Couldn't find expected output file: $bin2Name",    $p5_id, $PS_EXIT_SYS_ERROR) unless -f $bin2Name;
 #    &my_die("Couldn't find expected output file: $outputStats", $p5_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
-}
-
-# Get the statistics on the residual image
-if (0) { ### Disabled because pois doesn't output stats yet
-    my $stats;			# Statistics from ppImage
-    {
+
+    # Get the statistics on the residual image
+    if (0) { ### Disabled because pois doesn't output stats yet
 	my $statsFile;		# File handle
-	open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
+	open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $p5_id, $PS_EXIT_SYS_ERROR);
 	my @contents = <$statsFile>; # Contents of file
-    close $statsFile;
-	my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+	close $statsFile;
 	my $metadata = $mdcParser->parse(join "", @contents) or
-	    &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
-	$stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-	$stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to parse metadata config doc", $p5_id, $PS_EXIT_PROG_ERROR);
+	$stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $p5_id, $PS_EXIT_PROG_ERROR);
     }
 }
@@ -154,15 +152,15 @@
 # Add the processed file to the database
 $outputName = $ipprc->convert_filename_relative( $outputName );
+
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+
 unless ($no_update) {
 
     # Add the subtraction result
     {
-	my $command = "$p5tool -adddiffscfile -p5_id $p5_id -uri $outputName -b1_uri $outputRoot"; # Command to run p5tool
+	my $command = "$p5tool -adddiffscfile -p5_id $p5_id -uri $outputName -b1_uri $outputRoot";
+	$command .= " -bg $bg -bg_stdev $bg_stdev";
 	$command .= " -dbname $dbname" if defined $dbname;
-	
-	if (0) { ### Disabled because no statistics yet
-	    $command .= " -bg " . $stats->bg_mean();
-	    $command .= " -bg_stdev " . $stats->bg_stdev();
-	}
 	
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -170,5 +168,5 @@
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform p5tool -adddiffscfile: $error_code", $p5_id, $iter, $exp_tag, $class_id, $error_code);
+	    &my_die("Unable to perform p5tool -adddiffscfile: $error_code", $p5_id, $error_code);
 	}
 	
@@ -199,5 +197,5 @@
 
     warn($msg);
-    if ($det_id and $iter and $exp_tag and not $no_update) {
+    if ($p5_id and not $no_update) {
 	my $command = "$p5tool -updaterun -p5_id $p5_id -state stop -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
