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