Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 19658)
+++ trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 19942)
@@ -16,5 +16,4 @@
 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 );
 use PS::IPP::Metadata::List qw( parse_md_list );
@@ -68,26 +67,15 @@
 &my_die("Unrecognised detrend type: $det_type", $det_id, $exp_id, $PS_EXIT_PROG_ERROR) unless defined $recipe;
 
-# values to extract from output metadata and the stats to calculate
-# XXX -bg_mean_stdev should take rms of bg_mean_stdev if bg_mean_stdev != 0 (A)
-# XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
-# XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
-my $STATS =
-   [
-       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
-       { name => "bg",             type => "mean",  flag => "-bg",            dtype => "float" },
-       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev", dtype => "float" },
-       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev",      dtype => "float" },
-       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
-   ];
-my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
-
 # Look for programs we need
 my $missing_tools;
 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 $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
     exit($PS_EXIT_CONFIG_ERROR);
 }
+
+my $cmdflags;
 
 # Get list of component files
@@ -112,7 +100,24 @@
         &my_die("Unable to parse metadata list", $det_id, $exp_id, $PS_EXIT_PROG_ERROR);
 
-    # parse the stats in the metadata
-    unless ($stats->parse($metadata)) {
-        &my_die("Unable to find all values in statistics output.\n", $det_id, $exp_id, $PS_EXIT_PROG_ERROR);
+    # since I can't figure out how to do input and output within PERL, I'm writing to a temp file
+    my ($statFile, $statName) = tempfile( "/tmp/$exp_tag.detproc.$det_id.stats.XXXX", UNLINK => !$save_temps );
+    print "saving stats to $statName\n";
+    foreach my $line (@$stdout_buf) {
+        print $statFile $line;
+    }
+    close $statFile;
+
+    # parse the stats in the metadata file
+    $command = "$ppStatsFromMetadata $statName - DETREND_PROCESS_EXP";
+    ( $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);
+        warn("Unable to perform ppStatsFromMetadata: $error_code\n");
+        exit($error_code);
+    }
+
+    foreach my $line (@$stdout_buf) {
+        $cmdflags .= " $line";
     }
 }
@@ -173,5 +178,5 @@
 $command .= " -recip $recipe -path_base $outroot";
 $command .= " -dbname $dbname" if defined $dbname;
-$command .= $stats->cmdflags();
+$command .= " $cmdflags";
 
 # Add the processed file to the database
