Index: trunk/ippScripts/scripts/detrend_calc_norm.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_calc_norm.pl	(revision 8419)
+++ trunk/ippScripts/scripts/detrend_calc_norm.pl	(revision 8419)
@@ -0,0 +1,92 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use IPC::Cmd qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+use Data::Dumper;
+
+use constant STATISTIC => 'bg';	# Background statistic to use from the database
+
+
+# Parse the command-line
+die "Calculate the detrend normalisations.\n\n" .
+    "Usage: $0 DET_ID ITER\n\n"
+    if scalar @ARGV != 2;
+my $detId = shift @ARGV;	# Detrend ID
+my $iter = shift @ARGV;		# Iteration
+
+
+# Look for programs we need
+my $missing_tools;
+my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
+my $ppNormCalc = can_run('ppNormCalc') or (warn "Can't find ppNormCalc" and $missing_tools = 1);
+die "Can't find required tools.\n" if $missing_tools;
+
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+# Get the list of inputs
+my $files;			# The input files
+{
+    my $command = "$dettool -processed -unmask -det_id $detId -iter $iter"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform dettool -processed -unmask on detrend $detId/$iter: $error_code\n"
+	if not $success;
+    
+    # Parse the output
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata
+    $files = parse_md_list($metadata);
+}
+
+my %matrix; # Matrix of statistics as a function of exposures and classes
+foreach my $file (@$files) {
+    my $expId = $file->{exp_id}; # Exposure ID
+    my $classId = $file->{class_id}; # Class ID
+    my $stat = $file->{STATISTIC}; # Statistic of interest
+
+    # Create matrix elements
+    $matrix{$expId} = {} if not defined $matrix{$expId};
+    $matrix{$expId}->{$classId} = $stat;
+}
+
+my $normName = 'normCalc_' . $detId . '_' . $iter . '.mdc'; # Name for ppNormCalc input file
+my $normFile;			# File handle for ppNormCalc input
+open $normFile, ">" . $normName;
+foreach my $expId (keys %matrix) {
+    print $normFile "$expId\tMETADATA\n";
+    foreach my $classId (keys %{$matrix{$expId}}) {
+	print $normFile "\t$classId\tF32\t" . $matrix{$expId}->{$classId} . "\n";
+    }
+    print $normFile "\n";
+}
+close $normFile;
+
+
+# Run ppNormCalc
+my $norms;
+{
+    my $command = "$ppNormCalc $normName"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform ppNormCalc: $error_code\n"
+	if not $success;
+    $norms = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata
+}
+
+# Process output normalisations
+{
+    foreach my $normItem (@$norms) {
+	my $className = $normItem->{name}; # Name of component
+	my $normalisation = $normItem->{value}; # Normalisation for component
+
+	my $command = "$dettool -addnormstat -det_id $detId -iter $iter -class_id $className ".
+	    "-norm $normalisation"; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	die "Unable to perform ppNormCalc: $error_code\n"
+	    if not $success;
+    }
+}
