Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 13612)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 13618)
@@ -120,4 +120,5 @@
 my @means;			# Array of means
 my @variances;			# Array of variances
+my @binVariances;		# Array of binned variances
 my @meanStdevs;			# Array of mean stdevs
 my @names;			# Array of names (class_id)
@@ -129,4 +130,5 @@
     ## calculate the root-mean-square of the bd_stdevs
     push @variances, $file->{bg_stdev}*$file->{bg_stdev};
+    push @binVariances, $file->{bin_stdev}*$file->{bin_stdev};
     push @names, $file->{class_id};
 }
@@ -180,8 +182,10 @@
 my $reject_imfile_meanstdev = rejection_limit( 'IMFILE.MEANSTDEV', $det_type, $filter );
 my $reject_imfile_sn        = rejection_limit( 'IMFILE.SN',        $det_type, $filter );
+my $reject_imfile_bin_sn    = rejection_limit( 'IMFILE.BIN.SN',    $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 );
 my $reject_exp_sn           = rejection_limit( 'EXP.SN',           $det_type, $filter );
+my $reject_exp_bin_sn       = rejection_limit( 'EXP.BIN.SN',       $det_type, $filter );
 
 my $logName = caturi( $workdir, "$exp_tag.detreject.$det_id.$iter.log" ); # Name for log
@@ -199,4 +203,5 @@
     $mean -= $expected;
     my $stdev = sqrt($variances[$i]);	# Stdev for this imfile
+    my $binStdev = sqrt($binVariances[$i]);	# Stdev for this imfile
     my $name = $names[$i];
 
@@ -247,4 +252,15 @@
     } else {
 	print $logFile "No rejection on imfile S/N for $name\n";
+    }
+    if ($reject_imfile_bin_sn) {
+	if ($mean < $binStdev * $reject_imfile_bin_sn) {
+	    print $logFile "Rejecting exposure based on bad imfile binned S/N for $name: ";
+	    $reject = 1;
+	} else {
+	    print $logFile "Imfile binned S/N for $name meets requirements: ";
+	}
+	print $logFile "mean: $mean vs binStdev*SNlimit: " . $binStdev * $reject_imfile_bin_sn . "\n";
+    } else {
+	print $logFile "No rejection on imfile binned S/N for $name\n";
     }
 }
@@ -255,4 +271,6 @@
 my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for variances
 $varianceStats->add_data(@variances);
+my $binVarianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator for variances
+$binVarianceStats->add_data(@binVariances);
 
 my $mean = $meanStats->mean();	# Mean of the imfile means
@@ -263,4 +281,5 @@
 }
 my $stdev = sqrt($varianceStats->mean()); # Root-Mean-Square of the imfile stdevs (root mean of variances)
+my $binStdev = sqrt($binVarianceStats->mean()); # Root-Mean-Square of the imfile stdevs (root mean of variances)
 my $exp_sn = 0.0;
 if ($stdev > 0) { $exp_sn = $mean / $stdev; }
@@ -318,4 +337,16 @@
 	print $logFile "No rejection for exp S/N\n";
     }
+    # reject if the signal-to-noise is insufficient
+    if ($reject_exp_bin_sn) {
+	if (abs($mean) < abs($binStdev * $reject_exp_bin_sn)) {
+	    print $logFile "Rejecting exposure based on poor binned S/N: \n";
+	    $reject = 1;
+	} else {
+	    print $logFile "Exposure binned S/N meets requirements: \n";
+	}
+	print $logFile "signal: $mean vs noise: $binStdev (s/n limit is: $reject_exp_bin_sn)\n";
+    } else {
+	print $logFile "No rejection for exp binned S/N\n";
+    }
 
     close $logFile;
@@ -326,9 +357,10 @@
 my $bg_stdev = $stdev;
 my $bg_mean_stdev = $meanStdev;
+my $bin_stdev = $binStdev;
 
 unless ($no_update) {
     my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag";
     $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot ";
-    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev -bin_stdev $bin_stdev";
     $command .= ' -reject' if $reject;
     $command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/detrend_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid.pl	(revision 13612)
+++ trunk/ippScripts/scripts/detrend_resid.pl	(revision 13618)
@@ -105,4 +105,5 @@
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
+my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1);
 if ($missing_tools) { 
     warn("Can't find required tools.");
@@ -123,7 +124,9 @@
 my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
 my $outputStats = $outputRoot . '.' . $class_id . '.stats';
+my $outputBinnedStats = $outputRoot . '.' . $class_id . '.bin.stats';
 
 # Run ppImage
 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+my $binnedStats = PS::IPP::Metadata::Stats->new(); # Stats parser
 unless ($no_op) {
     my $command = "$ppImage -file $input_uri $outputRoot -recipe PPIMAGE $recipe" .
@@ -154,8 +157,26 @@
     close $statsFile;
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @contents) or
-	&my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+    my $metadata = $mdcParser->parse(join "", @contents) or &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+    # $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+
+    # run ppStats on the binned image
+    $command = "$ppStats $bin2Name $outputBinnedStats";
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppStats: $error_code", $det_id, $iter, $exp_tag, $class_id, $error_code);
+    }
+
+    &my_die("Couldn't find expected output file: $outputBinnedStats", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputBinnedStats);
+
+    open $statsFile, $ipprc->file_resolve($outputBinnedStats) or &my_die("Can't open statistics file $outputBinnedStats: $!", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
+    @contents = <$statsFile>; # Contents of file
+    close $statsFile;
+    my $binnedMetadata = $mdcParser->parse(join "", @contents) or &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+    # $binnedStats = PS::IPP::Metadata::Stats->new(); # Stats parser
+    $binnedStats->parse($binnedMetadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -164,9 +185,12 @@
 my $bg_stdev = ($stats->bg_stdev() or 'NAN');
 my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+my $bin_stdev = ($binnedStats->bg_stdev() or 'NAN');
 
 unless ($no_update) {
-    my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag " .
-	"-class_id $class_id -recip $recipe -uri $outputName -path_base $outputRoot"; # Command to run dettool
+    my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter";
+    $command .= " -exp_tag $exp_tag -class_id $class_id";
+    $command .= " -recip $recipe -uri $outputName -path_base $outputRoot";
     $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
+    $command .= " -bin_stdev $bin_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
 
@@ -179,6 +203,4 @@
     }
 }
-
-
 
 sub my_die
