Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 13989)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 14009)
@@ -16,6 +16,6 @@
 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::Metadata::Stats;
 
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
@@ -41,5 +41,5 @@
     'camera|c=s'        => \$camera,
     'dbname|d=s'        => \$dbname, # Database name
-    'workdir|w=s'       => \$workdir,	# Working directory, for output files
+    'workdir|w=s'       => \$workdir, # Working directory, for output files
     'reduction=s'       => \$reduction,	# Reduction class for processing
     'no-update'         => \$no_update,
@@ -49,5 +49,6 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --det_id --iteration --class_id --det_type --camera", -exitval => 3 ) 
+pod2usage( -msg => "Required options: --det_id --iteration --class_id --det_type --camera", 
+	   -exitval => 3) 
     unless defined $det_id
     and defined $iter
@@ -64,4 +65,17 @@
 $reduction = "DETREND" unless defined $reduction;
 my $recipe = $ipprc->reduction($reduction, uc($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          CHIPTOOL FLAG
+       { name => "bg",             type => "mean",  flag => "-bg" },
+       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev" },
+       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
+       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
+   ];
 
 # Look for programs we need
@@ -74,11 +88,18 @@
 }
 
-my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
+
+my $outputRoot  = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
+my $outputStack = $outputRoot . '.fits'; # Output name
+my $outputStats = $outputRoot . '.stats'; # Statistics name
 
 # Get list of files to stack
 my $files;			# Array of files to be stacked
 {
-    my $command = "$dettool -processedimfile -det_id $det_id -class_id $class_id -included"; # Command to run
+    my $command = "$dettool -processedimfile -included";
+    $command .= " -det_id $det_id";
+    $command .= " -class_id $class_id";
     $command .= " -dbname $dbname" if defined $dbname;
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
@@ -87,4 +108,6 @@
 	&my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $iter, $class_id, $error_code);
     }
+
+    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
 	&my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
@@ -93,21 +116,15 @@
 }
 
-$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
-
-my $outputRoot = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
-my $outputStack = $outputRoot . '.fits'; # Output name
-my $outputStats = $outputRoot . '.stats'; # Statistics name
+my $command = "$ppMerge $outputStack"; # Command to run
+foreach my $file (@$files) {
+    $command .= ' ' . $file->{uri};
+}
+$command .= " -recipe PPMERGE $recipe";
+$command .= ' -type ' . uc($det_type); # Type of stacking to perform
+$command .= " -stats $outputStats";	# Statistics output filename
+$command .= " -recipe PPSTATS CHIPSTATS";
 
 # Stack the files
-my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
 unless ($no_op) {
-    my $command = "$ppMerge $outputStack"; # Command to run
-    foreach my $file (@$files) {
-	$command .= ' ' . $file->{uri};
-    }
-    $command .= " -recipe PPMERGE $recipe";
-    $command .= ' -type ' . uc($det_type); # Type of stacking to perform
-    $command .= " -stats $outputStats";	# Statistics output filename
-    $command .= " -recipe PPSTATS CHIPSTATS";
     
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -129,23 +146,27 @@
     my $metadata = $mdcParser->parse($contents) or
 	&my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+
+    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     $stats->parse($metadata)  or
 	&my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
 }
 
-my $bg            = $stats->bg_mean();
-my $bg_stdev      = $stats->bg_stdev();
-my $bg_mean_stdev = $stats->bg_mean_stdev();
+# Command to update the database
+$command  = "$dettool -addstacked";
+$command .= " -det_id $det_id -iteration $iter";
+$command .= " -class_id $class_id";
+$command .= " -uri $outputStack";
+$command .= " -recip $recipe";
+$command .= " -dbname $dbname" if defined $dbname;
+
+# add in the elements from the selected stats above
+foreach my $entry (@$STATS) {
+    my $value = $entry->{value};
+    my $flag = $entry->{flag};
+    $command .= " $flag $value";
+}
 
 # Add the resultant into the database
 unless ($no_update) {
-    my $command = "$dettool -addstacked";
-    $command .= " -det_id $det_id -iteration $iter";
-    $command .= " -class_id $class_id";
-    $command .= " -uri $outputStack";
-    $command .= " -recip $recipe";
-    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
-    $command .= " -dbname $dbname" if defined $dbname;
-
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
@@ -155,6 +176,7 @@
 	exit($error_code);
     }
-}
-
+} else {
+    print "skipping command: $command\n";
+}
 
 sub my_die
@@ -168,5 +190,9 @@
     carp($msg);
     if ($det_id and $iter and $class_id and not $no_update) {
-	my $command = "$dettool -addstacked -det_id $det_id -iteration $iter -class_id $class_id -code $exit_code";
+	my $command = "$dettool -addstacked";
+	$command .= " -det_id $det_id";
+	$command .= " -iteration $iter";
+	$command .= " -class_id $class_id";
+	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
 ###        system ($command);
