Index: /trunk/ippScripts/scripts/detrend_norm_apply.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 9140)
+++ /trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 9141)
@@ -43,21 +43,43 @@
 
 
+use constant RECIPE => 'PPIMAGE_N'; # Recipe to use with ppImage
+
 # Look for programs we need
 my $missing_tools;
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
-my $ppArith = can_run('ppArith') or (warn "Can't find ppArith" 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 name
-my $output = $camera . '.' . $detType . '.norm.' . $detId . '.' . $iter . '.' . $classId . '.fits';
+my $outputRoot = $camera . '.' . $detType . '.norm.' . $detId . '.' . $iter; # Root output name
+my $output = $outputRoot . '.' . $classId . '.fits'; # Main output file
+my $b1name = $outputRoot . '.' . $classId . '.b1.fits';	# Output file with binning 1
+my $b2name = $outputRoot . '.' . $classId . '.b2.fits';	# Output file with binning 2
+my $statName = $outputRoot . '.' . $classId . '.stats'; # Statistics file
 
 # Run ppArith
-my $norms;
 {
-    my $command = "$ppArith -file1 $input -op * -constant $value -out $output"; # Command to run
+    my $command = "$ppImage -file $input $outputRoot -norm $value -stat $statName -recipe PPIMAGE " . RECIPE(); # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppArith: $error_code\n" if not $success;
+    die "Unable to perform ppImage: $error_code\n" if not $success;
     die "Can't find expected output file: $output\n" if not -e $output;
+    die "Can't find expected output file: $b1name\n" if not -e $b1name;
+    die "Can't find expected output file: $b2name\n" if not -e $b2name;
+    die "Can't find expected output file: $statName\n" if not -e $statName;
+}
+
+# Get the statistics on the normalised image
+my $stats;			# Statistics from ppImage
+{
+    my $statsFile;		# File handle
+    open $statsFile, $statsName or die "Can't open statistics file $statsName: $!\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)
+        or die "unable to parse metadata config";
+    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
 }
 
@@ -66,5 +88,14 @@
 unless ($no_update) {
     my $command = "$dettool -addnormalizedimfile -det_id $detId -iteration $iter -class_id $classId ".
-	"-uri $output"; # Command to run
+	"-uri $output -b1_uri $b1name -b2_uri $b2name"; # Command to run
+    # Add the statistics triplet
+    $command .= " -bg " . $stats->bg_mean();
+    if (defined($stats->bg_stdev())) {
+	$command .= " -bg_stdev " . $stats->bg_stdev();
+    } else {
+	# May be undefined if there's only a single imfile
+	$command .= " -bg_stdev 0";
+    }
+    $command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
