Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 8734)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 8763)
@@ -4,9 +4,31 @@
 use strict;
 
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
 use IPC::Cmd qw( can_run run );
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::List qw( parse_md_list );
 use PS::IPP::Metadata::Stats;
-use Data::Dumper;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($det_id, $iter, $class_id, $det_type);
+GetOptions(
+    'det_id|d=s'        => \$det_id,
+    'iteration=s'       => \$iter,
+    'class_id|i=s'      => \$class_id,
+    'det_type|t=s'      => \$det_type,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --det_id --iteration --class_id --det_type",
+    -exitval => 3,
+) unless defined $det_id
+    and defined $iter
+    and defined $class_id
+    and defined $det_type;
 
 # Recipes to use as a function of detrend type
@@ -24,15 +46,6 @@
     };    
 
-# 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
+die "Unrecognised detrend type: $det_type\n" if not defined RECIPES()->{$det_type};
+my $recipe = RECIPES()->{$det_type}; # Recipe to use in stacking
 
 # Look for programs we need
@@ -47,5 +60,5 @@
 my $files;			# Array of files to be stacked
 {
-    my $command = "$dettool -processed -det_id $detId -iteration $iter -class_id $classId"; # Command to run
+    my $command = "$dettool -processed -det_id $det_id -iteration $iter -class_id $class_id"; # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -57,6 +70,6 @@
 
 # Stack the files
-my $output = $detType . '_' . $classId . '_' . $detId . '_' . $iter . '.fit'; # Output name
-my $outputStats = $detType . '_' . $classId . '_' . $detId . '_' . $iter . '.stats'; # Statistics name
+my $output = $det_type . '_' . $class_id . '_' . $det_id . '_' . $iter . '.fit'; # Output name
+my $outputStats = $det_type . '_' . $class_id . '_' . $det_id . '_' . $iter . '.stats'; # Statistics name
 {
     my $command = "$ppMerge $output"; # Command to run
@@ -66,5 +79,5 @@
     }
     $command .= " -recipe PPMERGE $recipe";
-    $command .= ' -type ' . uc($detType); # Type of stacking to perform
+    $command .= ' -type ' . uc($det_type); # Type of stacking to perform
     $command .= " -stats $outputStats";	# Statistics output filename
 
@@ -95,10 +108,10 @@
 # Add the resultant into the database
 {
-    my $command = "$dettool -addstacked -det_id $detId -iteration $iter -class_id $classId" .
+    my $command = "$dettool -addstacked -det_id $det_id -iteration $iter -class_id $class_id" .
 	" -uri $output -recip $recipe";	# Command to run
     $command .= ' -bg ' . $stats->bg_mean();
     $command .= ' -bg_stdev ' . $stats->bg_stdev();
     $command .= ' -bg_mean_stdev ' . $stats->bg_mean_stdev();
-    $command .= ' -pleasenormalize' if NORMALISE()->{$detType};
+    $command .= ' -pleasenormalize' if NORMALISE()->{$det_type};
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
