Index: /trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9504)
+++ /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__
Index: /trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9504)
+++ /trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9505)
@@ -19,5 +19,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $exp_tag, $det_type, $no_update, $reject);
+my ($det_id, $iter, $exp_tag, $det_type, $camera, $filter, $no_update, $reject);
 GetOptions(
 	   'det_id|d=s'        => \$det_id,
@@ -26,4 +26,6 @@
 	   'det_type|t=s'      => \$det_type,
 	   'no-update'         => \$no_update,
+	   'camera=s'          => \$camera,
+	   'filter=s'          => \$filter,
 	   'reject'            => \$reject
 	   ) or pod2usage( 2 );
@@ -31,70 +33,14 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-	  -msg => "Required options: --det_id --iteration --exp_tag --det_type",
+	  -msg => "Required options: --det_id --iteration --exp_tag --det_type --camera",
 	  -exitval => 3,
 	  ) unless defined $det_id
     and defined $iter
     and defined $exp_tag
-    and defined $det_type;
+    and defined $det_type
+    and defined $camera;
 
 use constant RECIPE1 => 'PPIMAGE_J1'; # Recipe to use for ppImage to make JPEGs
 use constant RECIPE2 => 'PPIMAGE_J2'; # Recipe to use for ppImage to make JPEGs
-
-#### XXXXX these values must come from the config system, and may depend on filter!!!
-# XXX it is valid to reject on more than one criterion
-
-# The expected mean, as a function of detrend type
-use constant EXPECTED_MEAN => {
-    'bias' => 0,		# Bias should be zero
-    'dark' => 0,		# Dark should be zero
-    'shutter' => undef,		# Shutter could be anything (depends on exposure level)
-    'flat' => 0		        # Flat could be anything (depends on exposure level)
-    };
-
-# Rejection threshold for the mean
-# This measures how close it is to what's expected
-use constant REJECT_IMFILE_MEAN => {
-    'bias' => 0,		# Should be fairly flat; some CRs
-    'dark' => 0,		# Lots of CRs
-    'shutter' => undef,		# Can't define expected value (depends on exposure level)
-    'flat' => 0		# Can't define expected value (depends on exposure level)
-    };
-
-# Rejection threshold for the standard deviation, in ADUs
-# This measures how much variation there is in each imfile
-use constant REJECT_IMFILE_STDEV => {
-    'bias' => 15,		# Should be fairly flat; some CRs
-    'dark' => 0,		# Lots of CRs
-    'shutter' => undef,		# Can be significant structure
-    'flat' => 0	       	# Stars and galaxies
-    };
-
-# Rejection threshold for the mean of the exposure, in terms of the standard deviation of the exposure
-# This measures how close it is to what's expected
-use constant REJECT_EXPOSURE_MEAN => {
-    'bias' => 0,		# Should be little variation between chips
-    'dark' => 0,		# Could be some glow on some chips
-    'shutter' => undef,		# Can't define expected value (depends on exposure level)
-    'flat' => 0		# Can't define expected value (depends on exposure level)
-    };
-
-# Rejection threshold for the stdev of the exposure, in ADUs
-# This measures how much variation there is across the imfiles
-use constant REJECT_EXPOSURE_STDEV => {
-    'bias' => 15,		# Should be little variation between chips
-    'dark' => 0,		# Could be some glow on some chips
-    'shutter' => undef,		# Can be significant structure
-    'flat' => 0		        # Could be features on some chips, but all should be about the same
-    };
-
-# Rejection threshold for the stdev of the exposure, in ADUs
-# This measures how much variation there is across the imfiles
-use constant REJECT_EXPOSURE_MEAN_STDEV => {
-    'bias' => 0,		# Should be little variation between chips
-    'dark' => 0,		# Could be some glow on some chips
-    'shutter' => undef,		# Can be significant structure
-    'flat' => 0		        # Could be features on some chips, but all should be about the same
-    };
-
 
 # Look for programs we need
@@ -131,6 +77,6 @@
 open my $list2File, '>' . $list2Name;
 foreach my $file (@$files) {
-    print $list1File File::Spec->rel2abs( $file->{b1_uri}, $ipprc->workdir() ) . "\n";
-    print $list2File File::Spec->rel2abs( $file->{b2_uri}, $ipprc->workdir() ) . "\n";
+    print $list1File (File::Spec->rel2abs( $file->{b1_uri}, $ipprc->workdir() ) . "\n");
+    print $list2File (File::Spec->rel2abs( $file->{b2_uri}, $ipprc->workdir() ) . "\n");
     push @means, $file->{bg};
     ## calculate the root-mean-square of the bd_stdevs
@@ -160,17 +106,12 @@
 }
 
-# Check the existence of the required rejection levels as a function of detrend type
-die "Unknown expected mean for detrend type $det_type\n"
-    if not exists EXPECTED_MEAN->{$det_type};
-die "Unknown imfile mean rejection level for detrend type $det_type\n"
-    if not exists REJECT_IMFILE_MEAN->{$det_type};
-die "Unknown imfile stdev rejection level for detrend type $det_type\n"
-    if not exists REJECT_IMFILE_STDEV->{$det_type};
-die "Unknown exposure mean rejection level for detrend type $det_type\n"
-    if not exists REJECT_EXPOSURE_MEAN->{$det_type};
-die "Unknown exposure stdev rejection level for detrend type $det_type\n"
-    if not exists REJECT_EXPOSURE_STDEV->{$det_type};
-die "Unknown exposure mean stdev rejection level for detrend type $det_type\n"
-    if not exists REJECT_EXPOSURE_MEAN_STDEV->{$det_type};
+$ipprc->define_camera( $camera);
+my $expected = rejection_limit( 'EXPECTED', $det_type, $filter ); # Expected mean
+# Rejection thresholds
+my $reject_imfile_mean   = rejection_limit( 'IMFILE.MEAN',   $det_type, $filter );
+my $reject_imfile_stdev  = rejection_limit( 'IMFILE.STDEV',  $det_type, $filter );
+my $reject_exp_mean      = rejection_limit( 'EXP.MEAN',      $det_type, $filter );
+my $reject_exp_stdev     = rejection_limit( 'EXP.STDEV',     $det_type, $filter );
+my $reject_exp_meanstdev = rejection_limit( 'EXP.MEANSTDEV', $det_type, $filter );
 
 # Reject based on the stats of the imfiles
@@ -179,11 +120,11 @@
 for (my $i = 0; $i < scalar @means; $i++) {
     my $mean = $means[$i];	# Mean for this imfile
-    $mean -= EXPECTED_MEAN->{$det_type} if defined EXPECTED_MEAN->{$det_type};
+    $mean -= $expected;
     my $stdev = sqrt($variances[$i]);	# Stdev for this imfile
 
-    if (REJECT_IMFILE_MEAN->{$det_type}) {
-	if (abs($mean) > REJECT_IMFILE_MEAN->{$det_type}) {
+    if ($reject_imfile_mean) {
+	if (abs($mean) > $reject_imfile_mean) {
 	    print "Rejecting exposure based on bad imfile mean for imfile $i: " .
-		$mean . " vs " . REJECT_IMFILE_MEAN->{$det_type} . "\n";
+		$mean . " vs " . $reject_imfile_mean . "\n";
 	    $reject = 1;
 	    last;
@@ -192,8 +133,8 @@
 	print "no rejection for imfile mean\n";
     }
-    if (REJECT_IMFILE_STDEV->{$det_type}) {
-	if ($stdev > REJECT_IMFILE_STDEV->{$det_type}) {
+    if ($reject_imfile_stdev) {
+	if ($stdev > $reject_imfile_stdev) {
 	    print "Rejecting exposure based on bad imfile stdev for imfile $i: " .
-		$stdev . " vs " . REJECT_IMFILE_STDEV->{$det_type} . "\n";
+		$stdev . " vs " . $reject_imfile_stdev . "\n";
 	    $reject = 1;
 	    last;
@@ -221,8 +162,8 @@
 ## Reject based on the exposure ensemble stats
 # reject if the exposure ensemble mean is deviant
-if (REJECT_EXPOSURE_MEAN->{$det_type}) {
-    if (abs($mean) > REJECT_EXPOSURE_MEAN->{$det_type}) {
+if ($reject_exp_mean) {
+    if (abs($mean) > $reject_exp_mean) {
 	print "Rejecting exposure based on bad mean: " . ($mean / $stdev) . " vs " .
-	    REJECT_EXPOSURE_MEAN->{$det_type} . "\n";
+	    $reject_exp_mean . "\n";
 	$reject = 1;
     } 
@@ -231,8 +172,8 @@
 }
 # reject if the exposure ensemble stdev is deviant
-if (REJECT_EXPOSURE_STDEV->{$det_type}) {
-    if ($stdev > REJECT_EXPOSURE_STDEV->{$det_type}) {
+if ($reject_exp_stdev) {
+    if ($stdev > $reject_exp_stdev) {
 	print "Rejecting exposure based on bad mean stdev: " . $stdev . " vs " .
-	    REJECT_EXPOSURE_STDEV->{$det_type} . "\n";
+	    $reject_exp_stdev . "\n";
 	$reject = 1;
     }
@@ -241,8 +182,8 @@
 }
 # reject if the exposure ensemble mean stdev is deviant
-if (REJECT_EXPOSURE_MEAN_STDEV->{$det_type}) {
-    if ($meanStdev > REJECT_EXPOSURE_MEAN_STDEV->{$det_type}) {
+if ($reject_exp_meanstdev) {
+    if ($meanStdev > $reject_exp_meanstdev) {
 	print "Rejecting exposure based on bad mean stdev: " . $meanStdev . " vs " .
-	    REJECT_EXPOSURE_MEAN_STDEV->{$det_type} . "\n";
+	    $reject_exp_meanstdev . "\n";
 	$reject = 1;
     }
@@ -268,4 +209,24 @@
 
 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__
