Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9458)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9505)
@@ -12,49 +12,29 @@
 use Statistics::Descriptive;
 
+use PS::IPP::Config;
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $det_type, $no_update);
+my ($det_id, $iter, $det_type, $camera, $filter, $no_update);
 GetOptions(
-    'det_id|d=s'        => \$det_id,
-    'iteration=s'       => \$iter,
-    'det_type|t=s'      => \$det_type,
-    'no-update'         => \$no_update
+	   'det_id|d=s'        => \$det_id,
+	   'iteration=s'       => \$iter,
+	   'det_type|t=s'      => \$det_type,
+	   'camera=s'          => \$camera,
+	   'filter=s'          => \$filter,
+	   'no-update'         => \$no_update
 ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --det_id --iteration --det_type",
+    -msg => "Required options: --det_id --iteration --det_type --camera",
     -exitval => 3,
 ) unless defined $det_id
     and defined $iter
-    and defined $det_type;
-
-# Rejection threshold for the mean, in terms of the standard deviation
-# This measures how close it is to what's expected
-use constant REJECT_MEAN => {
-    'bias' => 0,		# Should be fairly flat; some CRs
-    'dark' => 0,		# Lots of CRs
-    'shutter' => 10,		# Should be less than 10 sec
-    'flat' => undef		# Can't define expected value (depends on exposure level)
-    };
-
-# Rejection threshold for the standard deviation, in terms of the standard deviation of the stdevs
-# This measures how much variation there is within the components of an exposure, compared to "typical"
-use constant REJECT_STDEV => {
-    'bias' => 0, # Components should have the same degree of variation
-    'dark' => 0, # Components should have the same degree of variation
-    'shutter' => undef,		# Might be significant variation
-    'flat' => 0 # Components should have the same degree of variation
-    };
-
-# Rejection threshold for the mean of the stdev, in terms of the standard deviation of the mean of the stdevs
-# This measures how structured the images are, compared to "typical"
-use constant REJECT_MEAN_STDEV => {
-    'bias' => 0,		# All images should be equally structured
-    'dark' => 0,		# All images should be equally structured
-    'shutter' => 0,		# All images should be equally structured
-    'flat' => 0 		# All images should be equally structured
-    };
+    and defined $det_type
+    and defined $camera;
+
 
 # Look for programs we need
@@ -103,11 +83,9 @@
 $meanStdevStats->add_data(@meanStdevs);
 
-# Check the existence of the required rejection levels as a function of detrend type
-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};
+$ipprc->define_camera($camera);
+# Rejection thresholds
+my $reject_mean = rejection_limit( 'ENSEMBLE.MEAN', $det_type, $filter );
+my $reject_stdev = rejection_limit( 'ENSEMBLE.STDEV', $det_type, $filter );
+my $reject_meanstdev = rejection_limit( 'ENSEMBLE.MEANSTDEV', $det_type, $filter );
 
 # rejections based on comparison with ensemble statistics
@@ -124,7 +102,9 @@
 # Go through again to do rejection, and update the database for each exposure
 my $numChanges = 0;		# Number of exposures with changed status
+my $numReject = 0;		# Number of exposures rejected
+my $command;
 for (my $i = 0; $i < scalar @means; $i++) {
     my $expTag = $expTags[$i];	# Exposure ID
-    my $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
+    $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
     my $reject = 0;		# Reject this exposure?
 
@@ -134,10 +114,10 @@
 	$reject = 1;
 	goto UPDATE;
-    } 
-    if (REJECT_MEAN->{$det_type} && ($meanStdev > 0)) {
+    }
+    if ($reject_mean && ($meanStdev > 0)) {
 	my $nSigma = abs($means[$i] - $mean) / $meanStdev;
-	if ($nSigma > REJECT_MEAN->{$det_type}) {
+	if ($nSigma > $reject_mean) {
 	    print "Rejecting $expTag based on outlier mean value: " .
-		"$means[$i] is $nSigma vs " . REJECT_MEAN->{$det_type} . "\n";
+		"$means[$i] is $nSigma vs " . $reject_mean . "\n";
 	    $reject = 1;
 	    goto UPDATE;
@@ -146,9 +126,9 @@
 	print "no rejection for exposure mean\n";
     }
-    if (REJECT_STDEV->{$det_type} && ($varStdev > 0)) {
+    if ($reject_stdev && ($varStdev > 0)) {
 	my $nSigma = abs($variances[$i] - $var) / $varStdev;
-	if ($nSigma > REJECT_STDEV->{$det_type}) {
+	if ($nSigma > $reject_stdev) {
 	    print "Rejecting $expTag based on outlier stdev: " .
-		sqrt($variances[$i]) . " is $nSigma vs " . REJECT_STDEV->{$det_type} . "\n";
+		sqrt($variances[$i]) . " is $nSigma vs " . $reject_stdev . "\n";
 	    $reject = 1;
 	    goto UPDATE;
@@ -161,4 +141,5 @@
     if ($reject) {
 	$command .= ' -reject';
+	$numReject++;
     }
     
@@ -184,4 +165,10 @@
     $master = 0;
     $stop = 0;
+}
+
+# Rejecting everything --- stop before something bad happens!
+if ($numReject == scalar @means) {
+    $master = 0;
+    $stop = 1;
 }
 
@@ -217,3 +204,20 @@
 END { system("sync") == 0 or die "failed to execute sync: $!" }
 
+# Retrieve the requested rejection limit, dying if not extant
+sub rejection_limit
+{
+    my $name = shift;		# Rejection limit to 
+    my $type = shift;		# Type of exposure
+    my $filter = shift;		# Filter
+
+    my $value = $ipprc->rejection( $name, $det_type, $filter );
+    if (not defined $value) {
+	$filter = "(no filter)" if not defined $filter;
+	die "Unable to determine $name rejection limit for $det_type with $filter.\n";
+    }
+
+    return $value;
+}
+
+
 __END__
