Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9458)
+++ 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__
