Index: /trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9456)
+++ /trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9457)
@@ -78,5 +78,5 @@
 my @expTags;			# Array of exposure IDs
 my @means;			# Array of means
-my @stdevs;			# Array of stdevs
+my @variances;			# Array of variances
 my @meanStdevs;			# Array of mean stdevs
 my @accept;			# Array of accept flags
@@ -91,6 +91,6 @@
     push @expTags, $exposure->{exp_tag};
     push @means, $exposure->{bg};
-    push @stdevs, $exposure->{bg_stdev};
-    push @meanStdevs, $exposure->{bg_mean_stdev};
+    push @variances, ($exposure->{bg_stdev}*$exposure->{bg_stdev});
+    push @meanStdevs, $exposure->{bg_mean_stdev}; ### XXX are we keeping this or stdev(mean)?
     push @accept, $exposure->{accept};
     push @include, $exposure->{include};
@@ -98,6 +98,6 @@
 my $meanStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
 $meanStats->add_data(@means);
-my $stdevStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-$stdevStats->add_data(@stdevs);
+my $variancestats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+$variancestats->add_data(@variances);
 my $meanStdevStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
 $meanStdevStats->add_data(@meanStdevs);
@@ -111,4 +111,12 @@
     if not exists REJECT_MEAN_STDEV->{$det_type};
 
+# rejections based on comparison with ensemble statistics
+my $mean = $meanStats->mean();
+my $meanStdev = $meanStats->standard_deviation();
+if (not defined $meanStdev) { $meanStdev = 0; }
+
+my $var = $variances->mean();
+my $varStdev = $variances->standard_deviation();
+
 # Go through again to do rejection, and update the database for each exposure
 my $numChanges = 0;		# Number of exposures with changed status
@@ -117,37 +125,35 @@
     my $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
     my $reject = 0;		# Reject this exposure?
-    my $not_reject = 0;		# Reject this exposure?
+
     if (not $accept[$i]) {
 	# Rejected this at an earlier stage
 	print "Rejecting $expTag based on earlier determination.\n";
 	$reject = 1;
-    } elsif (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->{$det_type}) {
-	print "Rejecting $expTag based on bad mean: " .
-	    (($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation()) .
-	    " vs " . REJECT_MEAN->{$det_type} . "\n";
-	$not_reject = 1;
-    } 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->{$det_type}) {
-	print "Rejecting $expTag based on bad stdev: " .
-	    (($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation()) .
-	    " vs " . REJECT_STDEV->{$det_type} . "\n";
-	$not_reject = 1;
-    } 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->{$det_type}) {
-	print "Rejecting $expTag based on bad stdev: " .
-	    (($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation()) .
-	    " vs " . REJECT_MEAN_STDEV->{$det_type} . "\n";
-	$not_reject = 1;
-    }
-    
+	goto UPDATE;
+    } 
+    if (REJECT_MEAN->{$det_type} && ($meanStdev > 0)) {
+	my $nSigma = abs($means[$i] - $mean) / $meanStdev;
+	if ($nSigma > REJECT_MEAN->{$det_type}) {
+	    print "Rejecting $expTag based on outlier mean value: " .
+		"$means[$i] is $nSigma vs " . REJECT_MEAN->{$det_type} . "\n";
+	    $reject = 1;
+	    goto UPDATE;
+	}
+    } else {
+	print "no rejection for exposure mean\n";
+    }
+    if (defined REJECT_STDEV->{$det_type} && ($varStdev >0)) {
+	my $nSigma = abs($var[$i] - $var) / $varStdev;
+	if ($nSigma > REJECT_STDEV->{$det_type}) {
+	    print "Rejecting $expTag based on outlier stdev: " .
+		sqrt($variances[$i]) " is $nSigma vs " . REJECT_STDEV->{$det_type} . "\n";
+	    $reject = 1;
+	    goto UPDATE;
+	}
+    } else {
+	print "no rejection for exposure stdev\n";
+    }
+    
+  UPDATE:
     if ($reject) {
 	$command .= ' -reject';
@@ -159,5 +165,5 @@
 	$numChanges++;
     }
-    
+
     unless ($no_update) {
 	# Update 
@@ -182,7 +188,7 @@
 # Put the result into the database
 unless ($no_update) {
-    my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter " .
-	"-bg " . $meanStats->mean() . " -bg_stdev " . $stdevStats->mean() .
-	" -bg_mean_stdev " . $meanStdevStats->mean();
+    my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter" .
+	" -bg " . $mean . " -bg_stdev " . sqrt($var) .
+	" -bg_mean_stdev " . $meanStdev;
     $command .= " -accept" if $master;
     
Index: /trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 9456)
+++ /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";
 }
 
Index: /trunk/ippScripts/scripts/detrend_resid.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_resid.pl	(revision 9456)
+++ /trunk/ippScripts/scripts/detrend_resid.pl	(revision 9457)
@@ -115,4 +115,7 @@
 
 # Add the processed file to the database
+# XXX I think this has the names "bg_stdev" and "bg_mean_stdev" exchanged
+#     bg_stdev : standard deviation of the background
+#     bg_mean_stdev : standard deviation of the background means
 $outputName = File::Spec->abs2rel ($outputName, $ipprc->workdir() );
 $bin1Name = File::Spec->abs2rel( $bin1Name, $ipprc->workdir() );
@@ -123,11 +126,15 @@
 	"-b2_uri $bin2Name"; # Command to run dettool
     $command .= " -bg " . $stats->bg_mean();
+
+    # XXX note bg_stdev <--> bg_mean_stdev
     if (defined($stats->bg_stdev())) {
-	$command .= " -bg_stdev " . $stats->bg_stdev();
+	$command .= " -bg_mean_stdev " . $stats->bg_stdev();
     } else {
 	# May be undefined if there is only a single imfile
 	$command .= ' -bg_stdev 0';
     }
-    $command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
+
+    # XXX note bg_stdev <--> bg_mean_stdev
+    $command .= " -bg_stdev " . $stats->bg_mean_stdev();
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
