Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9454)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9457)
@@ -21,17 +21,17 @@
 my ($det_id, $iter, $exp_tag, $det_type, $no_update, $reject);
 GetOptions(
-    'det_id|d=s'        => \$det_id,
-    'iteration=s'       => \$iter,
-    'exp_tag|e=s'       => \$exp_tag,
-    'det_type|t=s'      => \$det_type,
-    'no-update'         => \$no_update,
-    'reject'            => \$reject
-) or pod2usage( 2 );
+	   'det_id|d=s'        => \$det_id,
+	   'iteration=s'       => \$iter,
+	   'exp_tag|e=s'       => \$exp_tag,
+	   'det_type|t=s'      => \$det_type,
+	   'no-update'         => \$no_update,
+	   'reject'            => \$reject
+	   ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --det_id --iteration --exp_tag --det_type",
-    -exitval => 3,
-) unless defined $det_id
+	  -msg => "Required options: --det_id --iteration --exp_tag --det_type",
+	  -exitval => 3,
+	  ) unless defined $det_id
     and defined $iter
     and defined $exp_tag
@@ -42,4 +42,6 @@
 
 #### 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 => {
@@ -50,8 +52,7 @@
     };
 
-# Rejection threshold for the mean, in terms of the standard deviation
+# Rejection threshold for the mean
 # This measures how close it is to what's expected
-# XXX it is valid to reject on more than one criterion
-use constant REJECT_COMPONENT_MEAN => {
+use constant REJECT_IMFILE_MEAN => {
     'bias' => 0,		# Should be fairly flat; some CRs
     'dark' => 0,		# Lots of CRs
@@ -61,6 +62,6 @@
 
 # Rejection threshold for the standard deviation, in ADUs
-# This measures how much variation there is in each imfile component
-use constant REJECT_COMPONENT_STDEV => {
+# This measures how much variation there is in each imfile
+use constant REJECT_IMFILE_STDEV => {
     'bias' => 0,		# Should be fairly flat; some CRs
     'dark' => 0,		# Lots of CRs
@@ -69,7 +70,7 @@
     };
 
-# Rejection threshold for the mean of the sample, in terms of the standard deviation of the sample
+# 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_SAMPLE_MEAN => {
+use constant REJECT_EXPOSURE_MEAN => {
     'bias' => 0,		# Should be little variation between chips
     'dark' => 0,		# Could be some glow on some chips
@@ -78,7 +79,7 @@
     };
 
-# Rejection threshold for the stdev of the sample, in ADUs
-# This measures how much variation there is across the imfile components
-use constant REJECT_SAMPLE_STDEV => {
+# 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' => 0,		# Should be little variation between chips
     'dark' => 0,		# Could be some glow on some chips
@@ -87,7 +88,7 @@
     };
 
-# Rejection threshold for the stdev of the sample, in ADUs
-# This measures how much variation there is across the imfile components
-use constant REJECT_SAMPLE_MEAN_STDEV => {
+# 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
@@ -105,6 +106,6 @@
 my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 
-# Get list of component files
-my $files;			# Array of component files
+# Get list of imfile files
+my $files;			# Array of imfile files
 {
     my $command = "$dettool -residimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag"; # Command to run
@@ -126,5 +127,5 @@
 my $list2Name = $outputRoot . '.b2.list'; # Name for the input file list for binning 2
 my @means;			# Array of means
-my @stdevs;			# Array of stdevs
+my @variances;			# Array of variances
 open my $list1File, '>' . $list1Name;
 open my $list2File, '>' . $list2Name;
@@ -134,5 +135,5 @@
     push @means, $file->{bg};
     ## calculate the root-mean-square of the bd_stdevs
-    push @stdevs, $file->{bg_mean_stdev}*$file->{bg_mean_stdev};
+    push @variances, $file->{bg_stdev}*$file->{bg_stdev};
 }
 close $list1File;
@@ -162,74 +163,90 @@
 die "Unknown expected mean for detrend type $det_type\n"
     if not exists EXPECTED_MEAN->{$det_type};
-die "Unknown component mean rejection level for detrend type $det_type\n"
-    if not exists REJECT_COMPONENT_MEAN->{$det_type};
-die "Unknown component stdev rejection level for detrend type $det_type\n"
-    if not exists REJECT_COMPONENT_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 stats of the component imfile
-die "Number of means and number of stdevs differ!\n" if scalar @means != scalar @stdevs;
+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};
+
+# Reject based on the stats of the imfiles
+# it is VALID to reject on more than one criterion
+die "Number of means and number of variances differ!\n" if scalar @means != scalar @variances;
 for (my $i = 0; $i < scalar @means; $i++) {
-    my $mean = $means[$i];	# Mean for this component
+    my $mean = $means[$i];	# Mean for this imfile
     $mean -= EXPECTED_MEAN->{$det_type} if defined EXPECTED_MEAN->{$det_type};
-    my $stdev = sqrt($stdevs[$i]);	# Stdev for this component
-
-    ## IT IS VALID to reject on more than one criterion
-    if (REJECT_COMPONENT_MEAN->{$det_type} && ($stdev > 0) ) {
-	if ($mean / $stdev > REJECT_COMPONENT_MEAN->{$det_type}) {
-	    print "Rejecting exposure based on bad component mean for component $i: " .
-		($mean / $stdev) . " vs " . REJECT_COMPONENT_MEAN->{$det_type} . "\n";
+    my $stdev = sqrt($variances[$i]);	# Stdev for this imfile
+
+    if (REJECT_IMFILE_MEAN->{$det_type}) {
+	if (abs($mean) > REJECT_IMFILE_MEAN->{$det_type}) {
+	    print "Rejecting exposure based on bad imfile mean for imfile $i: " .
+		$mean . " vs " . REJECT_IMFILE_MEAN->{$det_type} . "\n";
 	    $reject = 1;
 	    last;
 	}
-    } 
-    if (REJECT_COMPONENT_STDEV->{$det_type}) {
-	if ($stdev > REJECT_COMPONENT_STDEV->{$det_type}) {
-	    print "Rejecting exposure based on bad component stdev for component $i: " .
-		$stdev . " vs " . REJECT_COMPONENT_STDEV->{$det_type} . "\n";
+    }  else {
+	print "no rejection for imfile mean\n";
+    }
+    if (REJECT_IMFILE_STDEV->{$det_type}) {
+	if ($stdev > REJECT_IMFILE_STDEV->{$det_type}) {
+	    print "Rejecting exposure based on bad imfile stdev for imfile $i: " .
+		$stdev . " vs " . REJECT_IMFILE_STDEV->{$det_type} . "\n";
 	    $reject = 1;
 	    last;
 	}
+    } else {
+	print "no rejection for imfile stdev\n";
     }
 }
 
-# calculate the imfile ensemble statistics
+# calculate the exposure ensemble statistics
 my $meanStats = Statistics::Descriptive::Sparse->new();	# Statistics calculator for means
 $meanStats->add_data(@means);
-my $stdevStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for stdevs
-$stdevStats->add_data(@stdevs);
-my $mean = $meanStats->mean();	# Mean of the sample of means
-my $meanStdev = $meanStats->standard_deviation(); # Stdev of the sample of means
-if (not defined $stdev) {
-    $stdev = 0;
-}
-my $stdev = sqrt($stdevStats->mean()); # Root-Mean-Square of the sample of stdevs
-print "calculating root-mean-square of the sample stdevs: $meanStdev\n";
-
-## Reject based on the imfile ensemble stats
-# reject if the imfile ensemble 
-if (REJECT_SAMPLE_MEAN->{$det_type} && ($stdev > 0)) {
-    if ($mean / $stdev > REJECT_SAMPLE_MEAN->{$det_type}) {
+my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for variances
+$varianceStats->add_data(@variances);
+
+my $mean = $meanStats->mean();	# Mean of the imfile means
+my $meanStdev = $meanStats->standard_deviation(); # Stdev of the imfile means
+if (not defined $meanStdev) {
+    # this is the case for Nimfile == 1
+    $meanStdev = 0;
+}
+my $stdev = sqrt($varianceStats->mean()); # Root-Mean-Square of the imfile stdevs (root mean of variances)
+print "exposure mean $mean, stdev $stdev, mean stdev $meanStdev\n";
+
+## 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}) {
 	print "Rejecting exposure based on bad mean: " . ($mean / $stdev) . " vs " .
-	    REJECT_SAMPLE_MEAN->{$det_type} . "\n";
+	    REJECT_EXPOSURE_MEAN->{$det_type} . "\n";
 	$reject = 1;
     } 
-}
-if (REJECT_SAMPLE_STDEV->{$det_type}) {
-    if ($stdev > REJECT_SAMPLE_STDEV->{$det_type}) {
+} else {
+    print "no rejection for imfile mean\n";
+}
+# reject if the exposure ensemble stdev is deviant
+if (REJECT_EXPOSURE_STDEV->{$det_type}) {
+    if ($stdev > REJECT_EXPOSURE_STDEV->{$det_type}) {
 	print "Rejecting exposure based on bad mean stdev: " . $stdev . " vs " .
-	    REJECT_SAMPLE_STDEV->{$det_type} . "\n";
+	    REJECT_EXPOSURE_STDEV->{$det_type} . "\n";
 	$reject = 1;
     }
-}
-if (REJECT_SAMPLE_MEAN_STDEV->{$det_type}) {
-    if ($meanStdev > REJECT_SAMPLE_MEAN_STDEV->{$det_type}) {
+} else {
+    print "no rejection for imfile stdev\n";
+}
+# reject if the exposure ensemble mean stdev is deviant
+if (REJECT_EXPOSURE_MEAN_STDEV->{$det_type}) {
+    if ($meanStdev > REJECT_EXPOSURE_MEAN_STDEV->{$det_type}) {
 	print "Rejecting exposure based on bad mean stdev: " . $meanStdev . " vs " .
-	    REJECT_SAMPLE_MEAN_STDEV->{$det_type} . "\n";
+	    REJECT_EXPOSURE_MEAN_STDEV->{$det_type} . "\n";
 	$reject = 1;
     }
+} else {
+    print "no rejection for imfile mean stdev\n";
 }
 
