Index: trunk/ippScripts/scripts/detrend_create_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_create_resid.pl	(revision 8734)
+++ 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__
Index: trunk/ippScripts/scripts/detrend_process.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process.pl	(revision 8734)
+++ trunk/ippScripts/scripts/detrend_process.pl	(revision 8763)
@@ -4,9 +4,35 @@
 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::Stats;
-use Data::Dumper;
 
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($det_id, $exp_id, $class, $class_id, $det_type, $input_uri, $output_uri);
+GetOptions(
+    'det_id|d=s'        => \$det_id,
+    'exp_id|e=s'        => \$exp_id,
+    'class=s'           => \$class,     # unused
+    'class_id|i=s'      => \$class_id,
+    'det_type|t=s'      => \$det_type,
+    '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 --exp_id --class_id --det_type --input_uri --output_uri",
+    -exitval => 3,
+) unless defined $det_id
+    and defined $exp_id
+    and defined $class_id
+    and defined $det_type
+    and defined $input_uri
+    and defined $output_uri;
 
 # Recipes to use, as a function of the detrend type
@@ -26,16 +52,4 @@
 use constant DELETE_STATS => 0;	# Delete the statistics file when done?
 
-# Parse the command-line arguments
-if (scalar @ARGV != 6) {
-    die "Perform detrend processing at the imfile level.\n\n" .
-	"Usage: $0 DET_ID EXP_ID CLASS_ID DETREND_TYPE INPUT.fits OUTPUT_ROOT\n\n";
-}
-my $detId = shift @ARGV;	# Detrend ID
-my $expId = shift @ARGV;	# Exposure ID
-my $classId = shift @ARGV;	# Class ID
-my $detType = shift @ARGV;	# Detrend type
-my $input = shift @ARGV;	# Input FITS file
-my $output = shift @ARGV;	# Output root name
-
 # Look for programs we need
 my $missing_tools;
@@ -45,11 +59,11 @@
 
 # Recipe to use in processing
-my $recipe = RECIPES->{$detType};
-die "Unrecognised detrend type: $detType\n" if not defined $recipe;
-die "Unrecognised detrend type: $detType\n" if not defined PREFIX->{$detType};
+my $recipe = RECIPES->{$det_type};
+die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
+die "Unrecognised detrend type: $det_type\n" if not defined PREFIX->{$det_type};
 
 ### Output file name --- must match camera configuration!
-my $outputRoot =  PREFIX->{$detType} . '_' . $output;
-my $outputName = $outputRoot . '.' . $classId ;
+my $outputRoot =  PREFIX->{$det_type} . '_' . $output_uri;
+my $outputName = $outputRoot . '.' . $class_id ;
 my $outputImage = $outputName . '.fit';
 my $outputStats = $outputName . '.stats';
@@ -57,9 +71,9 @@
 # Run ppImage
 {
-    my $command = "$ppImage -file $input $outputRoot -recipe PPIMAGE $recipe" .
+    my $command = "$ppImage -file $input_uri $outputRoot -recipe PPIMAGE $recipe" .
 	" -stat $outputStats"; # 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: $outputImage\n" if not -f $outputImage;
 }
@@ -81,6 +95,6 @@
 # Add the processed file to the database
 {
-    my $command = "$dettool -addprocessed -det_id $detId -exp_id $expId " .
-	"-class_id $classId -recip $recipe -uri $outputImage"; # Command to run dettool
+    my $command = "$dettool -addprocessed -det_id $det_id -exp_id $exp_id " .
+	"-class_id $class_id -recip $recipe -uri $outputImage"; # Command to run dettool
     $command .= " -bg " . $stats->bg_mean();
     $command .= " -bg_stdev " . $stats->bg_stdev();
@@ -89,5 +103,5 @@
     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;
 }
Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 8734)
+++ 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';
Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 8734)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.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 );
@@ -8,5 +11,24 @@
 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, $exp_id, $det_type);
+GetOptions(
+    'det_id|d=s'        => \$det_id,
+    'iteration=s'       => \$iter,
+    'exp_id|e=s'        => \$exp_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 --exp_id --det_type",
+    -exitval => 3,
+) unless defined $det_id
+    and defined $iter
+    and defined $exp_id
+    and defined $det_type;
 
 use constant RECIPE => 'PPIMAGE_J'; # Recipe to use for ppImage to make JPEGs
@@ -52,13 +74,4 @@
 
 
-# Parse command-line arguments
-die "Reject exposures based on the imfile.\n\n" .
-    "Usage: $0 DET_ID ITER EXP_ID DETREND_TYPE\n\n"
-    if scalar @ARGV != 4;
-my $detId = shift @ARGV;	# Detrend ID
-my $iter = shift @ARGV;		# Iteration number
-my $expId = shift @ARGV;	# Exposure ID
-my $detType = shift @ARGV;	# Detrend type
-
 # Look for programs we need
 my $missing_tools;
@@ -72,5 +85,5 @@
 my $files;			# Array of component files
 {
-    my $command = "$dettool -residimfile -det_id $detId -iteration $iter -exp_id $expId"; # Command to run
+    my $command = "$dettool -residimfile -det_id $det_id -iteration $iter -exp_id $exp_id"; # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -82,5 +95,5 @@
 
 # Generate the file list, and get the statistics
-my $outputName = $detType . '_' . $detId . '_' . $iter; # Root output name
+my $outputName = $det_type . '_' . $det_id . '_' . $iter; # Root output name
 my $list1Name = $outputName . '_jpeg1.list'; # Name for the input file list for binning 1
 my $list2Name = $outputName . '_jpeg2.list'; # Name for the input file list for binning 2
@@ -119,14 +132,14 @@
 
 # Check the existence of the required rejection levels as a function of detrend type
-die "Unknown expected mean for detrend type $detType\n"
-    if not exists EXPECTED_MEAN->{$detType};
-die "Unknown individual mean rejection level for detrend type $detType\n"
-    if not exists REJECT_INDIVIDUAL_MEAN->{$detType};
-die "Unknown individual stdev rejection level for detrend type $detType\n"
-    if not exists REJECT_INDIVIDUAL_STDEV->{$detType};
-die "Unknown sample mean rejection level for detrend type $detType\n"
-    if not exists REJECT_SAMPLE_MEAN->{$detType};
-die "Unknown sample stdev rejection level for detrend type $detType\n"
-    if not exists REJECT_SAMPLE_STDEV->{$detType};
+die "Unknown expected mean for detrend type $det_type\n"
+    if not exists EXPECTED_MEAN->{$det_type};
+die "Unknown individual mean rejection level for detrend type $det_type\n"
+    if not exists REJECT_INDIVIDUAL_MEAN->{$det_type};
+die "Unknown individual stdev rejection level for detrend type $det_type\n"
+    if not exists REJECT_INDIVIDUAL_STDEV->{$det_type};
+die "Unknown sample mean rejection level for detrend type $det_type\n"
+    if not exists REJECT_SAMPLE_MEAN->{$det_type};
+die "Unknown sample stdev rejection level for detrend type $det_type\n"
+    if not exists REJECT_SAMPLE_STDEV->{$det_type};
 
 # Reject based on the individual stats
@@ -135,18 +148,18 @@
 for (my $i = 0; $i < scalar @means; $i++) {
     my $mean = $means[$i];	# Mean for this component
-    $mean -= EXPECTED_MEAN->{$detType} if defined EXPECTED_MEAN->{$detType};
+    $mean -= EXPECTED_MEAN->{$det_type} if defined EXPECTED_MEAN->{$det_type};
     my $stdev = $stdevs[$i];	# Stdev for this component
 
-    if (defined REJECT_INDIVIDUAL_MEAN->{$detType}
+    if (defined REJECT_INDIVIDUAL_MEAN->{$det_type}
 	and $stdev > 0 and
-	$mean / $stdev > REJECT_INDIVIDUAL_MEAN->{$detType}) {
+	$mean / $stdev > REJECT_INDIVIDUAL_MEAN->{$det_type}) {
 	print "Rejecting exposure based on bad individual mean for component $i: " .
-	    ($mean / $stdev) . " vs " . REJECT_INDIVIDUAL_MEAN->{$detType} . "\n";
+	    ($mean / $stdev) . " vs " . REJECT_INDIVIDUAL_MEAN->{$det_type} . "\n";
 	$reject = 1;
 	last;
-    } elsif (defined REJECT_INDIVIDUAL_STDEV->{$detType} and
-	     $stdev > REJECT_INDIVIDUAL_STDEV->{$detType}) {
+    } elsif (defined REJECT_INDIVIDUAL_STDEV->{$det_type} and
+	     $stdev > REJECT_INDIVIDUAL_STDEV->{$det_type}) {
 	print "Rejecting exposure based on bad individual stdev for component $i: " .
-	    $stdev . " vs " . REJECT_INDIVIDUAL_STDEV->{$detType} . "\n";
+	    $stdev . " vs " . REJECT_INDIVIDUAL_STDEV->{$det_type} . "\n";
 	$reject = 1;
 	last;
@@ -165,14 +178,14 @@
 }
 my $meanStdev = $stdevStats->mean(); # Mean of the sample of stdevs
-if (defined REJECT_SAMPLE_MEAN->{$detType} and
+if (defined REJECT_SAMPLE_MEAN->{$det_type} and
     $stdev != 0 and
-    $mean / $stdev > REJECT_SAMPLE_MEAN->{$detType}) {
+    $mean / $stdev > REJECT_SAMPLE_MEAN->{$det_type}) {
     print "Rejecting exposure based on bad mean: " . ($mean / $stdev) . " vs " .
-	REJECT_SAMPLE_MEAN->{$detType} . "\n";
+	REJECT_SAMPLE_MEAN->{$det_type} . "\n";
     $reject = 1;
-} elsif (defined REJECT_SAMPLE_STDEV->{$detType} and
-	 $meanStdev > REJECT_SAMPLE_STDEV->{$detType}) {
+} elsif (defined REJECT_SAMPLE_STDEV->{$det_type} and
+	 $meanStdev > REJECT_SAMPLE_STDEV->{$det_type}) {
     print "Rejecting exposure based on bad mean stdev: " . $meanStdev . " vs " .
-	REJECT_SAMPLE_STDEV->{$detType} . "\n";
+	REJECT_SAMPLE_STDEV->{$det_type} . "\n";
     $reject = 1;
 }
@@ -180,5 +193,5 @@
 # Add the result into the database
 {
-    my $command = "$dettool -addresidexp -det_id $detId -iteration $iter -exp_id $expId " .
+    my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_id $exp_id " .
 	"-recip " . RECIPE() . " -b1_uri $jpeg1Name -b2_uri $jpeg2Name -bg $mean -bg_stdev $stdev " .
 	"-bg_mean_stdev $meanStdev";	# Command to run
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);
Index: trunk/ippScripts/scripts/phase0imfile.pl
===================================================================
--- trunk/ippScripts/scripts/phase0imfile.pl	(revision 8734)
+++ trunk/ippScripts/scripts/phase0imfile.pl	(revision 8763)
@@ -4,8 +4,36 @@
 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::Stats;
+use PS::IPP::Metadata::StatsFilter;
 use Data::Dumper;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($exp_id, $class, $class_id, $uri);
+
+GetOptions(
+    'exp_id|e=s'    => \$exp_id,
+    'class|c=s'     => \$class,
+    'class_id|i=s'  => \$class_id,
+    'uri|u=s'       => \$uri,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --exp_id --class --class_id --uri",
+    -exitval => 3,
+) unless defined $exp_id 
+    and defined $class 
+    and defined $class_id 
+    and defined $uri;
+
+# XXX the uri should be processed either here or by ppImage
+my $file = $uri;
 
 use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
@@ -40,15 +68,4 @@
 use constant P0TOOL_BG_MEAN_STDEV => '-bg_mean_stdev'; # Switch to add the bg mean stdev
 
-# Parse command-line arguments
-if (scalar @ARGV == 0 || scalar @ARGV > 3) {
-    die "Perform phase 0 processing at the imfile level.\n\n" .
-        "Usage: $0 EXP_ID CLASS_ID FILE.fits\n\n";
-}
-
-my $expid = shift @ARGV;        # Exposure identifier
-my $classid = shift @ARGV;        # Class identifier
-my $file = shift @ARGV;                # Input filename
-
-
 # Look for programs we need
 my $missing_tools;
@@ -63,5 +80,5 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => 1);
-    die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success;
+    die "Unable to perform ppStats on exposure id $exp_id: $error_code\n" if not $success;
     
     # Parse the output
@@ -69,4 +86,7 @@
     my $metadata = $mdcParser->parse(join "", @$stdout_buf)
             or die "unable to parse metadata config doc";
+    my $statsfilter = PS::IPP::Metadata::StatsFilter->new;
+    $metadata = $statsfilter->filter($metadata)
+        or die "failed to filter stats document";
     my @constants = keys %{CONSTANTS()}; # List of constants to parse out
     my @variables = keys %{VARIABLES()}; # List of variables to parse out
@@ -77,6 +97,6 @@
 # Push the results into the database
 {
-    my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $expid . " " .
-        P0TOOL_CLASSID() . " " . $classid; # Command to run p0tool
+    my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $exp_id . " " .
+        P0TOOL_CLASSID() . " " . $class_id; # Command to run p0tool
     
     foreach my $constant (keys %{CONSTANTS()}) {
