Index: trunk/ippScripts/scripts/detrend_resid_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid_imfile.pl	(revision 19621)
+++ trunk/ippScripts/scripts/detrend_resid_imfile.pl	(revision 19627)
@@ -16,6 +16,8 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
-use PS::IPP::Metadata::Stats;
 use PS::IPP::Config 1.01 qw( :standard );
+
+# XXX drop (moved to ppStatsFromMetadata)
+# use PS::IPP::Metadata::Stats;
 
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
@@ -88,28 +90,4 @@
 print "real recipe: $recipe\n";
 
-# values to extract from output metadata and the stats to calculate
-my $STATS =
-   [
-       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
-       { name => "ROBUST_MEDIAN",      type => "mean",  flag => "-bg",             dtype => "float" },
-       { name => "ROBUST_MEDIAN",      type => "stdev", flag => "-bg_mean_stdev",  dtype => "float" },
-       { name => "ROBUST_STDEV",       type => "rms",   flag => "-bg_stdev",       dtype => "float" },
-       { name => "SAMPLE_SKEWNESS",    type => "mean",  flag => "-bg_skewness",    dtype => "float" },
-       { name => "SAMPLE_KURTOSIS",    type => "mean",  flag => "-bg_kurtosis",    dtype => "float" },
-       { name => "FRINGE_0",           type => "mean",  flag => "-fringe_0",       dtype => "float" },
-       { name => "FRINGE_ERR_0",       type => "rms",   flag => "-fringe_1",       dtype => "float" },
-       { name => "FRINGE_0",           type => "stdev", flag => "-fringe_2",       dtype => "float" },
-       { name => "FRINGE_RESID_0",     type => "mean",  flag => "-fringe_resid_0", dtype => "float" },
-       { name => "FRINGE_RESID_ERR_0", type => "rms",   flag => "-fringe_resid_1", dtype => "float" },
-       { name => "FRINGE_RESID_0",     type => "stdev", flag => "-fringe_resid_2", dtype => "float" },
-   ];
-my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
-
-my $BINNED_STATS =
-   [
-       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bin_stdev" },
-   ];
-my $binnedStats = PS::IPP::Metadata::Stats->new($BINNED_STATS); # Stats parser
-
 # Flags to specify the particular detrend to use
 use constant DETRENDS => {
@@ -140,4 +118,5 @@
 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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -161,4 +140,6 @@
 my $outputStats = $ipprc->filename("PPIMAGE.STATS",  $outroot, $class_id);
 my $traceDest   = $ipprc->filename("TRACE.IMFILE",   $outroot, $class_id);
+
+my $cmdflags;
 
 # Run ppImage & ppStats
@@ -184,24 +165,24 @@
         &my_die("Unable to perform ppImage: $error_code", $det_id, $iter, $exp_id, $class_id, $error_code);
     }
+
     &my_die("Couldn't find expected output file: $outputName", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputName);
-    &my_die("Couldn't find expected output file: $outputStats", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStats);
     &my_die("Couldn't find expected output file: $bin1Name", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($bin1Name);
     &my_die("Couldn't find expected output file: $bin2Name", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($bin2Name);
 
-    # Load the raw output stats file
-    my $statsFile;              # File handle
-    open $statsFile, $ipprc->file_resolve($outputStats) or &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR);
-    my @contents = <$statsFile>; # Contents of file
-    close $statsFile;
-
-    # Parse the stats file contents into a metadata
-    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_id, $class_id, $PS_EXIT_PROG_ERROR);
-
-    # Parse the statistics on the residual image
-    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
+    my $outputStatsReal = $ipprc->file_resolve($outputStats);
+    &my_die("Couldn't find expected output file: $outputStats", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStatsReal;
+
+    # ppStatsFromMetadata $outputStats - DETREND_RESID_IMFILE
+    $command = "$ppStatsFromMetadata $outputStatsReal - DETREND_RESID_IMFILE";
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform ppStatsFromMetadata: $error_code", $det_id, $iter, $exp_id, $class_id, $error_code);
+    }
+    $cmdflags = $stdout_buf; chomp $cmdflags;
 
     # run ppStats on the binned image
-    $command = "$ppStats -recipe PPSTATS RESIDUAL $bin2Name";
+    $command = "$ppStats -recipe PPSTATS RESIDUAL $bin2Name | $ppStatsFromMetadata - - DETREND_RESID_IMFILE_BINNED";
     ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
@@ -210,10 +191,5 @@
         &my_die("Unable to perform ppStats: $error_code", $det_id, $iter, $exp_id, $class_id, $error_code);
     }
-
-    # Parse the output contents into a metadata
-    my $binnedMetadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata output", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
-
-    # parse the binned image statistics
-    $binnedStats->parse($binnedMetadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
+    $cmdflags .= $stdout_buf; chomp $cmdflags;
 }
 
@@ -230,6 +206,5 @@
 $command .= " -path_base $outroot";
 $command .= " -dbname $dbname" if defined $dbname;
-$command .= $stats->cmdflags();
-$command .= $binnedStats->cmdflags();
+$command .= $cmdflags;
 
 # Add the processed file to the database
