Index: trunk/ippScripts/scripts/detrend_create_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_create_resid.pl	(revision 8715)
+++ trunk/ippScripts/scripts/detrend_create_resid.pl	(revision 8763)
@@ -3,4 +3,7 @@
 use warnings;
 use strict;
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
 
 use IPC::Cmd qw( can_run run );
@@ -9,4 +12,32 @@
 use Data::Dumper;
 
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($det_id, $iter, $exp_id, $class_id, $det_type, $detrend,
+        $input_uri, $output_uri);
+GetOptions(
+    'det_id|d=s'        => \$det_id,
+    'iteration=s'       => \$iter,
+    'exp_id|e=s'        => \$exp_id,
+    'class_id|i=s'      => \$class_id,
+    'det_type|t=s'      => \$det_type,
+    'detrend=s'         => \$detrend,
+    'input_uri|u=s'     => \$input_uri,
+    'output_uri|o=s'    => \$output_uri,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --det_id --iteration --exp_id --class_id --det_type --detrend --input_uri --output_uri",
+    -exitval => 3,
+) unless defined $det_id
+    and defined $iter
+    and defined $exp_id
+    and defined $class_id
+    and defined $det_type
+    and defined $detrend
+    and defined $input_uri
+    and defined $output_uri;
 
 # Recipes to use, as a function of the detrend type
@@ -34,18 +65,4 @@
 use constant DELETE_STATS => 0;	# Delete the statistics file when done?
 
-# Parse the command-line arguments
-if (scalar @ARGV != 8) {
-    die "Apply a stacked detrend image to an individual detrend.\n\n" .
-	"Usage: $0 DET_ID ITERATION EXP_ID CLASS_ID DETREND_TYPE DETREND.fits INPUT.fits OUTPUT_ROOT\n\n";
-}
-my $detId = shift @ARGV;	# Detrend ID
-my $iter = shift @ARGV;		# Iteration
-my $expId = shift @ARGV;	# Exposure ID
-my $classId = shift @ARGV;	# Class ID
-my $detType = shift @ARGV;	# Detrend type
-my $detrend = shift @ARGV;	# The detrend frame to apply
-my $input = shift @ARGV;	# Input FITS file
-my $output = shift @ARGV;	# Output root name
-
 # Look for programs we need
 my $missing_tools;
@@ -55,27 +72,27 @@
 
 # Recipe to use in processing
-my $recipe = RECIPES->{$detType};
-die "Unrecognised detrend type: $detType\n" if not defined $recipe;
+my $recipe = RECIPES->{$det_type};
+die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
 # Detrend to use in processing
-my $detFlag = DETRENDS->{$detType};
-die "Unrecognised detrend type: $detType\n" if not defined $detFlag;
+my $detFlag = DETRENDS->{$det_type};
+die "Unrecognised detrend type: $det_type\n" if not defined $detFlag;
 # Prefix to use for filename
-my $prefix = PREFIX->{$detType};
-die  "Unrecognised detrend type: $detType\n" if not defined $prefix;
+my $prefix = PREFIX->{$det_type};
+die  "Unrecognised detrend type: $det_type\n" if not defined $prefix;
 
 ### Output file names --- must match camera configuration!
-my $outputRoot = $prefix . '_' . $output;
-my $outputName = $outputRoot . '.' . $classId . '.fit';
-my $outputStats = $outputRoot . '.' . $classId . '.stats';
-my $bin1Name = $outputRoot . '.' . $classId . '.b1.fit';
-my $bin2Name =  $outputRoot . '.' . $classId . '.b2.fit';
+my $outputRoot = $prefix . '_' . $output_uri;
+my $outputName = $outputRoot . '.' . $class_id . '.fit';
+my $outputStats = $outputRoot . '.' . $class_id . '.stats';
+my $bin1Name = $outputRoot . '.' . $class_id . '.b1.fit';
+my $bin2Name =  $outputRoot . '.' . $class_id . '.b2.fit';
 
 # Run ppImage
 {
-    my $command = "$ppImage -file $input $outputRoot -recipe PPIMAGE $recipe" .
+    my $command = "$ppImage -file $input_uri $outputRoot -recipe PPIMAGE $recipe" .
 	" -stat $outputStats $detFlag $detrend"; # Command to run ppImage
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input: $error_code\n" if not $success;
+    die "Unable to perform ppImage on $input_uri: $error_code\n" if not $success;
     die "Couldn't find expected output file: $outputName\n" if not -f $outputName;
     die "Couldn't find expected output file: $bin1Name\n" if not -f $bin1Name;
@@ -99,6 +116,6 @@
 # Add the processed file to the database
 {
-    my $command = "$dettool -addresidimfile -det_id $detId -iteration $iter -exp_id $expId " .
-	"-class_id $classId -recip $recipe -uri $outputName -b1_uri $bin1Name " .
+    my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_id $exp_id " .
+	"-class_id $class_id -recip $recipe -uri $outputName -b1_uri $bin1Name " .
 	"-b2_uri $bin2Name"; # Command to run dettool
     $command .= " -bg " . $stats->bg_mean();
@@ -108,9 +125,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addprocessed for $detId/$expId/$classId: $error_code\n"
+    die "Unable to perform dettool -addprocessed for $det_id/$exp_id/$class_id: $error_code\n"
 	if not $success;
 }
 
-unlink "$output.stats" if DELETE_STATS;
+unlink "$output_uri.stats" if DELETE_STATS;
 
 __END__
