Index: /trunk/ippScripts/scripts/detrend_calc_norm.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_calc_norm.pl	(revision 8997)
+++ /trunk/ippScripts/scripts/detrend_calc_norm.pl	(revision 8998)
@@ -4,5 +4,6 @@
 use strict;
 
-use IPC::Cmd qw( can_run run );
+use IPC::Cmd qw( can_run );
+use IPC::Run qw ( run );
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::List qw( parse_md_list );
@@ -13,20 +14,24 @@
 
 
-use constant STATISTIC => 'bg';	# Background statistic to use from the database
-
 # Parse command-line arguments
-my ($detId,   # Detrend id
-    $iter      # Iteration
+my ($detId,			# Detrend id
+    $iter,			# Iteration
+    $camera,			# Camera name
+    $detType			# Detrend type
     );
 GetOptions(
 	'det_id|d=s'	=> \$detId,
 	'iteration|i=s'	=> \$iter,
+	'camera|c=s'    => \$camera,
+	'det_type|t=s'  => \$detType
 	) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options --det_id --iteration",
+pod2usage( -msg => "Required options --det_id --iteration --camera --det_type",
 	   -exitval => 3,
-	 ) unless defined $detId
-	      and defined $iter;
+	   ) unless defined $detId
+    and defined $iter
+    and defined $camera
+    and defined $detType;
 
 
@@ -45,11 +50,11 @@
 {
     my $command = "$dettool -processed -unmask -det_id $detId -iteration $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;
+    my @command = split /\s+/, $command;
+    my ( $stdin, $stdout, $stderr ); # Buffers for running program
+    run \@command, \$stdin, \$stdout, \$stderr or
+	die "Unable to perform dettool -processed -unmask on detrend $detId/$iter: $?";
     
     # Parse the output
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf)
+    my $metadata = $mdcParser->parse(join "", @$stdout)
         or die "unable to parse metadata config doc";
     $files = parse_md_list($metadata);
@@ -67,25 +72,24 @@
 }
 
-my $normName = 'normCalc_' . $detId . '_' . $iter . '.mdc'; # Name for ppNormCalc input file
-my $normFile;			# File handle for ppNormCalc input
-open $normFile, ">" . $normName;
+# Generate the input for ppNormCalc
+my $normData;			# Normalisation data
 foreach my $expId (keys %matrix) {
-    print $normFile "$expId\tMETADATA\n";
+    $normData .= "$expId\tMETADATA\n";
     foreach my $classId (keys %{$matrix{$expId}}) {
-	print $normFile "\t" . $classId . "\tF32\t" . $matrix{$expId}->{$classId} . "\n";
+	$normData .= "\t" . $classId . "\tF32\t" . $matrix{$expId}->{$classId} . "\n";
     }
-    print $normFile "END\n\n";
+    $normData .= "END\n\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)
+    my ( $stdout, $stderr ); # Buffers for running program
+    my @command = split /\s+/, $ppNormCalc;
+    run \@command, \$normData, \$stdout, \$stderr or
+	die "Unable to perform ppNormCalc: $?";
+
+    # Parse the output
+    $norms = $mdcParser->parse(join "", @$stdout)
         or die "unable to parse metadata config doc";
 }
@@ -99,8 +103,9 @@
 	my $command = "$dettool -addnormstat -det_id $detId -iteration $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 dettool -addnormstat: $error_code\n"
-	    if not $success;
+	my @command = split /\s+/, $command;
+
+	my ( $stdin, $stdout, $stderr ); # Buffers for running program
+	run \@command, \$stdin, \$stdout, \$stderr or
+	    die "Unable to perform dettool -addnormstat for $className: $?";
     }
 }
