Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 19374)
+++ trunk/ippScripts/scripts/detrend_stack.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::Metadata::List qw( parse_md_list );
 use PS::IPP::Config 1.01 qw( :standard );
@@ -67,21 +66,9 @@
 my $recipe = $ipprc->reduction($reduction, $det_type . '_STACK'); # Recipe name to use
 
-# 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            DETTOOL FLAG
-       { name => "ROBUST_MEDIAN",  type => "clipmean",  flag => "-bg",             dtype => "float" },
-       { name => "ROBUST_MEDIAN",  type => "clipstdev", flag => "-bg_mean_stdev",  dtype => "float" },
-       { name => "ROBUST_STDEV",   type => "rms",       flag => "-bg_stdev",       dtype => "float" },
-   ];
-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 $ppMerge = can_run('ppMerge') or (warn "Can't find ppMerge" 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.");
@@ -112,23 +99,25 @@
 
 # The stats recipe depends on the detrend type
-my $STATRECIPES = {'FLATMASK'         => 'CHIPSTATS',
-                   'DARKMASK'         => 'CHIPSTATS',
-                   'MASK'             => 'CHIPSTATS',
-                   'BIAS'             => 'CHIPSTATS',
+my $STATRECIPES = {'FLATMASK'         => 'DETSTATS',
+                   'DARKMASK'         => 'DETSTATS',
+                   'MASK'             => 'DETSTATS',
+                   'BIAS'             => 'DETSTATS',
                    'DARK'             => 'DARKSTATS',
                    'DARK_PREMASK'     => 'DARKSTATS',
                    'SHUTTER'          => 'DARKSTATS',
-                   'FLAT_PREMASK'     => 'CHIPSTATS',
-                   'DOMEFLAT_PREMASK' => 'CHIPSTATS',
-                   'SKYFLAT_PREMASK'  => 'CHIPSTATS',
-                   'FLAT_RAW'         => 'CHIPSTATS',
-                   'DOMEFLAT_RAW'     => 'CHIPSTATS',
-                   'SKYFLAT_RAW'      => 'CHIPSTATS',
-                   'FLAT'             => 'CHIPSTATS',
-                   'DOMEFLAT'         => 'CHIPSTATS',
-                   'SKYFLAT'          => 'CHIPSTATS',
-                   'FRINGE'           => 'CHIPSTATS',
+                   'FLAT_PREMASK'     => 'DETSTATS',
+                   'DOMEFLAT_PREMASK' => 'DETSTATS',
+                   'SKYFLAT_PREMASK'  => 'DETSTATS',
+                   'FLAT_RAW'         => 'DETSTATS',
+                   'DOMEFLAT_RAW'     => 'DETSTATS',
+                   'SKYFLAT_RAW'      => 'DETSTATS',
+                   'FLAT'             => 'DETSTATS',
+                   'DOMEFLAT'         => 'DETSTATS',
+                   'SKYFLAT'          => 'DETSTATS',
+                   'FRINGE'           => 'DETSTATS',
               };
 my $statrecipe = $STATRECIPES->{$det_type}; # File rule for output
+
+my $cmdflags;
 
 # Get list of files to stack
@@ -217,18 +206,20 @@
     &my_die("Unable to find expected output file: $outputCount\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputCount);
     &my_die("Unable to find expected output file: $outputSigma\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputSigma);
-    &my_die("Unable to find expected output file: $outputStats\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStats);
-
-    # Get the statistics on the stacked image
-    open(my $statsFile, $ipprc->file_resolve("$outputStats")) or
-        &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
-    my $contents = do { local $/; <$statsFile> }; # Contents of file
-    close($statsFile);
-
-    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
-    my $metadata = $mdcParser->parse($contents) or
-        &my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
-
-    $stats->parse($metadata)  or
-        &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
+
+    my $outputStatsReal = $ipprc->file_resolve($outputStats);
+    &my_die("Couldn't find expected output file: $outputStats", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStatsReal;
+
+    # parse stats from metadata
+    $command = "$ppStatsFromMetadata $outputStatsReal - DETREND_STACK";
+    ( $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, $class_id, $error_code);
+    }
+    foreach my $line (@$stdout_buf) {
+	$cmdflags .= " $line";
+    }
+    chomp $cmdflags;
 }
 
@@ -241,5 +232,5 @@
 $command .= " -recip $recipe";
 $command .= " -dbname $dbname" if defined $dbname;
-$command .= $stats->cmdflags();
+$command .= " $cmdflags";
 
 # Add the resultant into the database
