Index: trunk/ippScripts/scripts/chip_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/chip_imfile.pl	(revision 19938)
+++ trunk/ippScripts/scripts/chip_imfile.pl	(revision 19942)
@@ -19,5 +19,4 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
-use PS::IPP::Metadata::Stats;
 use PS::IPP::Config 1.01 qw( :standard );
 
@@ -74,30 +73,4 @@
 }
 
-# values to extract from output metadata and the stats to calculate
-# XXX commented-out entries are not yet defined in the output files
-my $STATS =
-   [
-       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
-       { name => "ROBUST_MEDIAN",  type => "mean",  flag => "-bg",             dtype => "float" },
-       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev",  dtype => "float" },
-       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev",       dtype => "float" },
-       { name => "OVER_VAL",       type => "mean",  flag => "-bias",           dtype => "float" },
-       { name => "OVER_VAL",       type => "stdev", flag => "-bias_stdev",     dtype => "float" },
-       { name => "FRINGE_0",       type => "rms",   flag => "-fringe_0",       dtype => "float" },
-       { name => "FRINGE_RESID_0", type => "rms",   flag => "-fringe_1",       dtype => "float" },
-       { name => "FRINGE_ERR_0",   type => "rms",   flag => "-fringe_2",       dtype => "float" },
-       { name => "APMIFIT",        type => "mean",  flag => "-ap_resid",       dtype => "float" },
-       { name => "DAPMIFIT",       type => "rms",   flag => "-ap_resid_stdev", dtype => "float" },
-       { name => "FWHM_X",         type => "mean",  flag => "-fwhm_major",     dtype => "float" },
-       { name => "FWHM_Y",         type => "mean",  flag => "-fwhm_minor",     dtype => "float" },
-       { name => "DT_DET",         type => "sum",   flag => "-dtime_detrend",  dtype => "float" },
-       { name => "DT_PHOT",        type => "sum",   flag => "-dtime_photom",   dtype => "float" },
-       { name => "DT_TOTAL",       type => "sum",   flag => "-dtime_total",    dtype => "float" },
-       { name => "NSTARS",         type => "sum",   flag => "-n_stars",        dtype => "int"   },
-#      { name => "?",              type => "sum",   flag => "-n_extended",     dtype => "int"   },
-#      { name => "?",              type => "sum",   flag => "-n_cr",           dtype => "int"   },
-   ];
-my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
-
 # Look for programs we need
 my $missing_tools;
@@ -105,4 +78,5 @@
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
 my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" 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.");
@@ -137,8 +111,8 @@
 }
 
+my $cmdflags;
+
 # Run ppImage
 unless ($no_op) {
-    ## XXX can we convert ppImage log and trace to use the filerules to generate consistent names
-    ## XXX also stats: output should be implied by $outroot
     my $command;
     my $do_stats;
@@ -205,21 +179,19 @@
         &my_die("Couldn't find expected output file: $outputStats\n",  $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStats);
 
-        # Get the statistics on the processed image
-        my $statsFile;              # File handle
-        open $statsFile, $ipprc->file_resolve($outputStats) or &my_die("Can't open statistics file $outputStats: $!", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-        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);
-        unless ($metadata) {
-            &my_die("Unable to parse metadata config doc", $exp_id, $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
-        }
-
-        # extract the stats from the metadata
-        unless ($stats->parse($metadata)) {
-            &my_die("Failure extracting metadata from the statistics output file.\n", $exp_id, $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
-        }
+	my $outputStatsReal = $ipprc->file_resolve($outputStats);
+	&my_die("Couldn't find expected output file: $outputStats", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStatsReal;
+
+	# measure chip stats
+	$command = "$ppStatsFromMetadata $outputStatsReal - CHIP_IMFILE";
+	( $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", $exp_id, $chip_id, $class_id, $error_code);
+	}
+	foreach my $line (@$stdout_buf) {
+	    $cmdflags .= " $line";
+	}
+	chomp $cmdflags;
     }
 }
@@ -236,5 +208,5 @@
     $command .= " -hostname $host" if defined $host;
     $command .= " -dbname $dbname" if defined $dbname;
-    $command .= $stats->cmdflags();
+    $command .= " $cmdflags";
     $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
 } else {
