Index: trunk/ippScripts/scripts/chip_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/chip_imfile.pl	(revision 13989)
+++ trunk/ippScripts/scripts/chip_imfile.pl	(revision 14009)
@@ -1,7 +1,7 @@
 #!/usr/bin/env perl
 
+use Carp;
 use warnings;
 use strict;
-use Carp;
 
 ## report the program and machine
@@ -11,8 +11,11 @@
 print "Starting script $0 on $host\n\n";
 
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::Stats;
-use Data::Dumper;
+
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
 		       $PS_EXIT_UNKNOWN_ERROR
@@ -30,41 +33,52 @@
 
 # Parse the command-line arguments
-my ($exp_id,			# Exposure identifier
-    $chip_id,			# Chiptool identifier
-    $class_id,			# Class identifier
-    $input,			# Input FITS file
-    $camera,			# Camera
-    $dbname,			# Database name
-    $workdir,			# Working directory, for output files
-    $reduction,			# Reduction class
-    $no_update,			# Don't update the database?
-    $no_op,			# Don't do any operations?
-    );
+my ($exp_id, $chip_id, $class_id, $input_uri, $camera, $dbname, $workdir, $reduction, $no_update, $no_op);
 GetOptions(
-	   'exp_id=s'      => \$exp_id,
-	   'chip_id=s'     => \$chip_id,
-	   'class_id=s'    => \$class_id,
-	   'uri|u=s'       => \$input,
-	   'camera|c=s'    => \$camera,
-	   'dbname|d=s'    => \$dbname, # Database name
-	   'workdir|w=s'   => \$workdir,
-	   'reduction=s'   => \$reduction,
-	   'no-update'     => \$no_update,
-	   'no-op'         => \$no_op,
+	   'exp_id=s'      => \$exp_id,    # Exposure identifier		       
+	   'chip_id=s'     => \$chip_id,   # Chiptool identifier		       
+	   'class_id=s'    => \$class_id,  # Class identifier		       
+	   'input_uri|u=s' => \$input_uri, # Input FITS file		       
+	   'camera|c=s'    => \$camera,	   # Camera			       
+	   'dbname|d=s'    => \$dbname,    # Database name
+	   'workdir|w=s'   => \$workdir,   # Working directory, for output files  
+	   'reduction=s'   => \$reduction, # Reduction class		       
+	   'no-update'     => \$no_update, # Don't update the database?	       
+	   'no-op'         => \$no_op,	   # Don't do any operations?             
 	   ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage(
-    -msg => "Required options: --exp_id --chip_id --class_id --uri --camera",
-    -exitval => 3,
-) unless defined $exp_id
+pod2usage( -msg => "Required options: --exp_id --chip_id --class_id --uri --camera",
+	   -exitval => 3) 
+    unless defined $exp_id
     and defined $chip_id 
     and defined $class_id 
-    and defined $input
+    and defined $input_uri
     and defined $camera;
 
 $ipprc->define_camera($camera);
+
+# Recipes to use based on reduction class
 $reduction = 'DEFAULT' unless defined $reduction;
 my $recipe = $ipprc->reduction($reduction, 'CHIP'); # Recipe 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" },
+       { name => "FRINGE_0",       type => "rms",   flag => "-fringe_0" },
+       { name => "FRINGE_RESID_0", type => "rms",   flag => "-fringe_1" },
+       { name => "FRINGE_ERR_0",   type => "rms",   flag => "-fringe_2" },
+       { name => "OVER_VAL",       type => "mean",  flag => "-bias" },
+       { name => "FWHM",           type => "mean",  flag => "-fwhm" },
+       { name => "APMIFIT",        type => "mean",  flag => "-ap_resid" },
+       { name => "DAPMIFIT",       type => "rms",   flag => "-ap_resid_stdev" },
+       { name => "CERROR",         type => "rms",   flag => "-sigma_ra" },
+       { name => "CERROR",         type => "rms",   flag => "-sigma_dec" },
+       { name => "NSTARS",         type => "sum",   flag => "-n_stars" },
+       { name => "NASTRO",         type => "sum",   flag => "-n_astrom" },
+   ];
 
 # Look for programs we need
@@ -76,28 +90,24 @@
     exit($PS_EXIT_CONFIG_ERROR); 
 }
-$ppImage .= " -dbname $dbname" if defined $dbname;
-
-&my_die("Couldn't find input file: $input\n", $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input);
+
+&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, $exp_id ) if defined $workdir;
 
-my $outputRoot = $ipprc->file_prepare( "$exp_id.chp$chip_id", $workdir, $input );
-my $outputImage = $ipprc->filename("PPIMAGE.CHIP", $outputRoot, $class_id);
-my $outputMask = $ipprc->filename("PPIMAGE.CHIP.MASK", $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 $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+my $outputRoot  = $ipprc->file_prepare( "$exp_id.chp$chip_id", $workdir, $input_uri );
+
+my $outputImage = $ipprc->filename("PPIMAGE.CHIP",      $outputRoot, $class_id);
+my $outputMask  = $ipprc->filename("PPIMAGE.CHIP.MASK", $outputRoot, $class_id);
+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
 unless ($no_op) {
-    # Run ppImage
-    print "outputImage: $outputImage\n";
-    print "outputBin1: $outputBin1\n";
-    print "outputStats: $outputStats\n";
-
-    my $command = "$ppImage -file $input $outputRoot";
+    my $command = "$ppImage -file $input_uri $outputRoot";
     $command .= " -recipe PPIMAGE $recipe";
     $command .= " -recipe PPSTATS CHIPSTATS";
-    $command .= " -stats $outputStats"; # Command to run ppImage
+    $command .= " -stats $outputStats";
+    $command .= " -dbname $dbname" if defined $dbname;
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -119,35 +129,36 @@
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
-    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @contents) or
+
+    # parse the statistics MDC file
+    my $mdcParser = PS::IPP::Metadata::Config->new();	# Parser for metadata config files
+    my $metadata = $mdcParser->parse(join "", @contents);
+    unless ($metadata) {
 	&my_die("Unable to parse metadata config doc", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
-    $stats->parse($metadata) or
+    }
+
+    # extract the stats from the metadata
+    my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
+    unless ($stats->parse($metadata)) {
 	&my_die("Unable to find all values in statistics output.\n", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
+    }
+}
+
+# command to update database
+my $command = "$chiptool -addprocessedimfile";
+$command .= " -chip_id $chip_id";
+$command .= " -class_id $class_id";
+$command .= " -uri $outputImage";
+$command .= " -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
-my $bg            = $stats->bg_mean();
-my $bg_stdev      = $stats->bg_stdev();
-my $bg_mean_stdev = $stats->bg_mean_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];
-
 unless ($no_update) {
-    # Command to run chiptool
-    my $command = "$chiptool -addprocessedimfile";
-    $command .= " -chip_id $chip_id";
-    $command .= " -class_id $class_id";
-    $command .= " -uri $outputImage";
-    $command .= " -path_base $outputRoot";
-    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_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";
-    # XXX add sigma_ra, etc here
-    $command .= " -dbname $dbname" if defined $dbname;
-
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -157,6 +168,7 @@
 	exit($error_code);
     }
-}
-
+} else {
+    print "skipping command: $command\n";
+}
 
 sub my_die
