Index: trunk/ippScripts/scripts/detrend_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid.pl	(revision 13973)
+++ trunk/ippScripts/scripts/detrend_resid.pl	(revision 14009)
@@ -17,5 +17,4 @@
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::Stats;
-use Data::Dumper;
 
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
@@ -33,6 +32,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $exp_tag, $class_id, $det_type, $detrend,
-        $input_uri, $camera, $mode, $dbname, $workdir, $reduction, $no_update, $no_op);
+my ($det_id, $iter, $exp_tag, $class_id, $det_type, $detrend, $input_uri, $camera, $mode, $dbname, $workdir, $reduction, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -53,8 +51,7 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage(
-    -msg => "Required options: --det_id --iteration --exp_tag --class_id --det_type --camera --input_uri --mode --detrend (not for 'verify' mode)",
-    -exitval => 3,
-) unless defined $det_id
+pod2usage( -msg => "Required options: --det_id --iteration --exp_tag --class_id --det_type --camera --input_uri --mode --detrend (not for 'verify' mode)",
+	   -exitval => 3)
+    unless defined $det_id
     and defined $iter
     and defined $exp_tag
@@ -68,4 +65,5 @@
 $ipprc->define_camera($camera);
 
+# Recipes to use as a function of detrend type and mode
 $reduction = 'DETREND' unless defined $reduction;
 my $recipe;			# Name of recipe to use
@@ -78,4 +76,23 @@
 }
 $recipe = $ipprc->reduction($reduction, $recipe);
+
+# 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" },
+       { name => "FRINGE_0",           type => "mean",  flag => "-fringe_0" },
+       { name => "FRINGE_ERR_0",       type => "rms",   flag => "-fringe_1" },
+       { name => "FRINGE_0",           type => "stdev", flag => "-fringe_2" },
+       { name => "FRINGE_RESID_0",     type => "mean",  flag => "-user_1" },
+       { name => "FRINGE_RESID_ERR_0", type => "rms",   flag => "-user_2" },
+       { name => "FRINGE_RESID_0",     type => "stdev", flag => "-user_3" },
+   ];
+my $BINNED_STATS = 
+   [   
+       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bin_stdev" },
+   ];
 
 # Flags to specify the particular detrend to use
@@ -106,12 +123,11 @@
 
 my $outputRoot = $ipprc->file_prepare( "$exp_tag/$exp_tag.detresid.$det_id.$iter", $workdir, $input_uri );
-my $outputName = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
-my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
-my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
-my $outputStats = $outputRoot . '.' . $class_id . '.stats';
+
+my $outputName  = $ipprc->filename("PPIMAGE.OUTPUT", $outputRoot, $class_id);
+my $bin1Name    = $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
+my $bin2Name    = $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
+my $outputStats = $ipprc->filename("PPIMAGE.STATS", $outputRoot, $class_id);
 
 # Run ppImage & ppStats
-my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-my $binnedStats = PS::IPP::Metadata::Stats->new(); # Stats parser
 unless ($no_op) {
     my $command = "$ppImage -file $input_uri $outputRoot";
@@ -144,7 +160,10 @@
     close $statsFile;
 
-    # Parse the statistics on the residual image
+    # 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_tag, $class_id, $PS_EXIT_PROG_ERROR);
+
+    # Parse the statistics on the residual image
+    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, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 
@@ -160,33 +179,29 @@
     # parse the binned image statistics
     my $binnedMetadata = $mdcParser->parse(join "", @$stdout_buf) or &my_die("Unable to parse metadata output", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+
+    my $binnedStats = PS::IPP::Metadata::Stats->new($BINNED_STATS); # Stats parser
     $binnedStats->parse($binnedMetadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
+# Command to update the database
+my $command = "$dettool -addresidimfile";
+$command .= " -det_id $det_id";
+$command .= " -iteration $iter";
+$command .= " -exp_tag $exp_tag";
+$command .= " -class_id $class_id";
+$command .= " -recip $recipe";
+$command .= " -uri $outputName";
+$command .= " -path_base $outputRoot";
+$command .= " -dbname $dbname" if defined $dbname;
+
+# add in the elements from the selected stats above
+foreach my $entry (@$STATS @$BINNED_STATS) {
+    my $value = $entry->{value};
+    my $flag = $entry->{flag};
+    $command .= " $flag $value";
+}
+
 # Add the processed file to the database
-# these value must be defined (NAN if not valid), or we raise a programming error
-my $bg            = $stats->bg_mean();
-my $bg_stdev      = $stats->bg_stdev();
-my $bg_mean_stdev = $stats->bg_mean_stdev();
-my $bin_stdev	  = $binnedStats->bg_stdev();
-my $fringe_0 	  = ${$stats->fringe_mean()}[0];
-my $fringe_1 	  = ${$stats->fringe_err()}[0];
-my $fringe_2 	  = ${$stats->fringe_mean_stdev()}[0];
-my $dfringe_0 	  = ${$stats->dfringe_mean()}[0];
-my $dfringe_1 	  = ${$stats->dfringe_err()}[0];
-my $dfringe_2 	  = ${$stats->dfringe_mean_stdev()}[0];
-
-print "bg_mean_stdev: " . $bg_mean_stdev . "\n";
-print "stats : bg_mean_stdev: " . $stats->bg_mean_stdev() . "\n";
-
 unless ($no_update) {
-    my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter";
-    $command .= " -exp_tag $exp_tag -class_id $class_id";
-    $command .= " -recip $recipe -uri $outputName -path_base $outputRoot";
-    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
-    $command .= " -bin_stdev $bin_stdev";
-    $command .= " -fringe_0 $fringe_0 -fringe_1 $fringe_1 -fringe_2 $fringe_2";
-    $command .= " -user_1 $dfringe_0 -user_2 $dfringe_1 -user_3 $dfringe_2";
-    $command .= " -dbname $dbname" if defined $dbname;
-
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -196,4 +211,6 @@
 	exit($error_code);
     }
+} else {
+    print "skipping command: $command\n";
 }
 
@@ -209,5 +226,10 @@
     carp($msg);
     if ($det_id and $iter and $exp_tag and not $no_update) {
-	my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	my $command = "$dettool -addresidimfile";
+	$command .= " -det_id $det_id";
+	$command .= " -iteration $iter";
+	$command .= " -exp_tag $exp_tag";
+	$command .= " -class_id $class_id";
+	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
 ###        system ($command);
