Index: /trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_stack.pl	(revision 8359)
+++ /trunk/ippScripts/scripts/detrend_stack.pl	(revision 8359)
@@ -0,0 +1,84 @@
+#!/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;
+
+# Recipes to use as a function of detrend type
+use constant RECIPES => {
+    'bias' => 'PPMERGE_BIAS',
+    'dark' => 'PPMERGE_DARK',
+    'flat' => 'PPMERGE_FLAT'
+    };
+
+# Do we want to normalise this, or not?
+use constant NORMALISE => {
+    'bias' => 0,
+    'dark' => 0,
+    'flat' => 1
+    };    
+
+# Parse command-line arguments
+die "Stack detrends.\n\n" .
+    "Usage: $0 DET_ID ITER CLASS_ID DETREND_TYPE\n\n"
+    if scalar @ARGV != 4;
+my $detId = shift @ARGV;	# Detrend ID
+my $iter = shift @ARGV;		# Iteration number
+my $classId = shift @ARGV;	# Class ID
+my $detType = shift @ARGV;	# Detrend type
+
+die "Unrecognised detrend type: $detType\n" if not defined RECIPES->{$detType};
+my $recipe = RECIPES->{$detType}; # Recipe to use in stacking
+
+# Look for programs we need
+my $missing_tools;
+my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
+my $ppMerge = can_run('ppMerge') or (warn "Can't find ppMerge" 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 list of files to stack
+my $files;			# Array of files to be stacked
+{
+    my $command = "$dettool -processed -det_id $detId -iter $iter -class_id $classId"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform dettool -processed: $error_code\n" if not $success;
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata
+    $files = parse_md_list($metadata);
+}
+
+# Stack the files
+my $output = $detType . '_' . $classId . '_' . $detId . '_' . $iter; # Output name
+{
+    my $command = "ppMerge $output"; # Command to run
+    foreach my $file (@$files) {
+	my $uri = $file->{uri};	# URI for input file
+	$command .= ' ' . $uri;
+    }
+    $command .= " -recipe PPMERGE $recipe";
+    $command .= ' -type' . uc($detType); # Type of stacking to perform
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform dettool -processed: $error_code\n" if not $success;
+    die "Unable to find expected output file: $output\n" if not -f $output;
+}
+
+# Add the resultant into the database
+{
+    my $command = "dettool -addstacked -det_id $detId -iter $iter -class_id $classId" .
+	" -uri $output";	# Command to run
+    $command .= ' -pleasenormalize' if NORMALIZE()->{$detType};
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform dettool -addstacked: $error_code\n" if not $success;
+}
+
+
+__END__
