Index: trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 13989)
+++ trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 14009)
@@ -48,8 +48,7 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage(
-    -msg => "Required options: --det_id --exp_tag --class_id --det_type --input_uri --camera",
-    -exitval => 3,
-) unless defined $det_id
+pod2usage( -msg => "Required options: --det_id --exp_tag --class_id --det_type --input_uri --camera",
+	   -exitval => 3)
+    unless defined $det_id
     and defined $exp_tag
     and defined $class_id
@@ -61,6 +60,16 @@
 $ipprc->define_camera($camera);
 
+# Recipes to use as a function of detrend type
 $reduction = "DETREND" unless defined $reduction;
 my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_PROCESS'); # Recipe name to use
+
+# 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" },
+       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev" },
+       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev" },
+   ];
 
 # Look for programs we need
@@ -74,14 +83,16 @@
 $ppImage .= " -dbname $dbname" if defined $dbname;
 
+&my_die("Couldn't find input file: $input_uri\n", $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
+
 $workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
 
-my $outputRoot = $ipprc->file_prepare( "$exp_tag/$exp_tag.detproc.$det_id", $workdir, $input_uri );
+my $outputRoot  = $ipprc->file_prepare( "$exp_tag/$exp_tag.detproc.$det_id", $workdir, $input_uri );
+
 my $outputImage = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
-my $outputBin1 = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
-my $outputBin2 = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
-my $outputStats = $outputRoot . '.' . $class_id . '.stats';
+my $outputBin1  = $ipprc->filename("PPIMAGE.BIN1",   $outputRoot, $class_id);
+my $outputBin2  = $ipprc->filename("PPIMAGE.BIN2",   $outputRoot, $class_id);
+my $outputStats = $ipprc->filename("PPIMAGE.STATS",  $outputRoot, $class_id);
 
 # Run ppImage
-my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
 unless ($no_op) {
     my $command = "$ppImage -file $input_uri $outputRoot";
@@ -96,4 +107,5 @@
 	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $class_id, $error_code);
     }
+
     &my_die("Couldn't find expected output file: $outputImage", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputImage);
     &my_die("Couldn't find expected output file: $outputStats", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStats);
@@ -106,25 +118,33 @@
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
+
+    # parse the statistics MDC file
     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", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+
+    # extract the stats from the metadata
+    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, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
-my $bg            = $stats->bg_mean();
-my $bg_stdev      = $stats->bg_stdev();
-my $bg_mean_stdev = $stats->bg_mean_stdev();
+# command to update database
+my $command = "$dettool -addprocessedimfile";
+$command .= " -det_id $det_id";
+$command .= " -exp_tag $exp_tag";
+$command .= " -class_id $class_id";
+$command .= " -recip $reduction";
+$command .= " -uri $outputImage -path_base $outputRoot";
+$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 processed file to the database
 unless ($no_update) {
-    my $command = "$dettool -addprocessedimfile";
-    $command .= " -det_id $det_id";
-    $command .= " -exp_tag $exp_tag";
-    $command .= " -class_id $class_id";
-    $command .= " -recip $recipe";
-    $command .= " -uri $outputImage -path_base $outputRoot";
-    $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 => 1);
@@ -134,4 +154,6 @@
 	exit($error_code);
     }
+} else {
+    print "skipping command: $command\n";
 }
 
@@ -146,5 +168,9 @@
     carp($msg);
     if ($det_id and $exp_tag and $class_id and not $no_update) {
-	my $command = "$dettool -addprocessedimfile -det_id $det_id -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	my $command = "$dettool -addprocessedimfile";
+	$command .= " -det_id $det_id";
+	$command .= " -exp_tag $exp_tag"; 
+	$command .= " -class_id $class_id";
+	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
 ###        system ($command);
