Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 8617)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 8621)
@@ -56,4 +56,5 @@
 # Stack the files
 my $output = $detType . '_' . $classId . '_' . $detId . '_' . $iter . '.fit'; # Output name
+my $outputStats = $detType . '_' . $classId . '_' . $detId . '_' . $iter . '.stats'; # Statistics name
 {
     my $command = "$ppMerge $output"; # Command to run
@@ -64,4 +65,5 @@
     $command .= " -recipe PPMERGE $recipe";
     $command .= ' -type ' . uc($detType); # Type of stacking to perform
+    $command .= " -stats $outputStats";	# Statistics output filename
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -69,11 +71,21 @@
     die "Unable to perform ppMerge: $error_code\n" if not $success;
     die "Unable to find expected output file: $output\n" if not -f $output;
+    die "Unable to find expected output file: $outputStats\n" if not -f $outputStats;
 }
 
-### XXX: Need to add statistics
-my $bg_mean = 0;
-my $bg_stdev = 0;
-my $bg_mean_stdev = 0;
+### Could do some sort of JPEG generation here.
 
+# Get the statistics on the stacked 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 resultant into the database
@@ -81,7 +93,7 @@
     my $command = "$dettool -addstacked -det_id $detId -iteration $iter -class_id $classId" .
 	" -uri $output -recip $recipe";	# Command to run
-    $command .= ' -bg ' . $bg_mean;
-    $command .= ' -bg_stdev ' . $bg_stdev;
-    $command .= ' -bg_mean_stdev ' . $bg_mean_stdev;
+    $command .= ' -bg ' . $stats->bg_mean();
+    $command .= ' -bg_stdev ' . $stats->bg_stdev();
+    $command .= ' -bg_mean_stdev ' . $stats->bg_mean_stdev();
     $command .= ' -pleasenormalize' if NORMALISE()->{$detType};
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
