Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 8715)
+++ trunk/ippScripts/scripts/detrend_reject_exp.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 Statistics::Descriptive;
-use Data::Dumper;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($det_id, $iter, $det_type);
+GetOptions(
+    'det_id|d=s'        => \$det_id,
+    'iteration=s'       => \$iter,
+    'det_type|t=s'      => \$det_type,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --det_id --exp_id --class_id --det_type --input_uri --output_uri",
+    -exitval => 3,
+) unless defined $det_id
+    and defined $iter
+    and defined $det_type;
+
+my $detType = shift @ARGV;	# Detrend type
 
 # Rejection threshold for the mean, in terms of the standard deviation
@@ -34,12 +56,4 @@
     };
 
-# Parse command-line arguments
-die "Reject exposures based on the sample of exposures.\n\n" .
-    "Usage: $0 DET_ID ITER DETREND_TYPE\n\n"
-    if scalar @ARGV != 3;
-my $detId = shift @ARGV;	# Detrend ID
-my $iter = shift @ARGV;		# Iteration number
-my $detType = shift @ARGV;	# Detrend type
-
 # Look for programs we need
 my $missing_tools;
@@ -51,5 +65,5 @@
 my $exposures;			# Array of exposures
 {
-    my $command = "$dettool -residexp -det_id $detId -iteration $iter"; # Command to run
+    my $command = "$dettool -residexp -det_id $det_id -iteration $iter"; # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -82,10 +96,10 @@
 
 # Check the existence of the required rejection levels as a function of detrend type
-die "Unknown mean rejection level for detrend type $detType\n"
-    if not exists REJECT_MEAN->{$detType};
-die "Unknown stdev rejection level for detrend type $detType\n"
-    if not exists REJECT_STDEV->{$detType};
-die "Unknown mean stdev rejection level for detrend type $detType\n"
-    if not exists REJECT_MEAN_STDEV->{$detType};
+die "Unknown mean rejection level for detrend type $det_type\n"
+    if not exists REJECT_MEAN->{$det_type};
+die "Unknown stdev rejection level for detrend type $det_type\n"
+    if not exists REJECT_STDEV->{$det_type};
+die "Unknown mean stdev rejection level for detrend type $det_type\n"
+    if not exists REJECT_MEAN_STDEV->{$det_type};
 
 # Go through again to do rejection, and update the database for each exposure
@@ -93,31 +107,31 @@
 for (my $i = 0; $i < scalar @means; $i++) {
     my $expId = $expIds[$i];	# Exposure ID
-    my $command = "$dettool -updateresidexp -det_id $detId -iteration $iter -exp_id $expId"; # Command to run
+    my $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_id $expId"; # Command to run
     my $reject = 0;		# Reject this exposure?
-    if (defined REJECT_MEAN->{$detType} and
+    if (defined REJECT_MEAN->{$det_type} and
 	defined $meanStats->standard_deviation() and
 	$meanStats->standard_deviation() > 0 and
-	($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$detType}) {
+	($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$det_type}) {
 	print "Rejecting $expId based on bad mean: " .
 	    (($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation()) .
-	    " vs " . REJECT_MEAN->{$detType} . "\n";
+	    " vs " . REJECT_MEAN->{$det_type} . "\n";
 	$reject = 1;
-    } elsif (defined REJECT_STDEV->{$detType} and
+    } elsif (defined REJECT_STDEV->{$det_type} and
 	     defined $stdevStats->standard_deviation() and
 	     $stdevStats->standard_deviation() > 0 and
 	     ($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation() >
-	     REJECT_STDEV->{$detType}) {
+	     REJECT_STDEV->{$det_type}) {
 	print "Rejecting $expId based on bad stdev: " .
 	    (($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation()) .
-	    " vs " . REJECT_STDEV->{$detType} . "\n";
+	    " vs " . REJECT_STDEV->{$det_type} . "\n";
 	$reject = 1;
-    } elsif (defined REJECT_MEAN_STDEV->{$detType} and
+    } elsif (defined REJECT_MEAN_STDEV->{$det_type} and
 	     defined $meanStdevStats->standard_deviation() and 
 	     $meanStdevStats->standard_deviation() > 0 and
 	     ($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation() >
-	     REJECT_MEAN_STDEV->{$detType}) {
+	     REJECT_MEAN_STDEV->{$det_type}) {
 	print "Rejecting $expId based on bad stdev: " .
 	    (($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation()) .
-	    " vs " . REJECT_MEAN_STDEV->{$detType} . "\n";
+	    " vs " . REJECT_MEAN_STDEV->{$det_type} . "\n";
 	$reject = 1;
     }
@@ -147,5 +161,5 @@
 # Put the result into the database
 {
-    my $command = "$dettool -adddetrunsummary -det_id $detId -iteration $iter " .
+    my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter " .
 	"-bg " . $meanStats->mean() . " -bg_stdev " . $stdevStats->mean() .
 	" -bg_mean_stdev " . $meanStdevStats->mean();
@@ -159,5 +173,5 @@
 # Re-run processing if required
 {
-    my $command = "$dettool -updatedetrun -det_id $detId";
+    my $command = "$dettool -updatedetrun -det_id $det_id";
     if ($stop) {
 	$command .= ' -stop';
