Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 19938)
+++ trunk/ippScripts/scripts/camera_exp.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::Metadata::List qw( parse_md_list );
 use PS::IPP::Config 1.01 qw( :standard );
@@ -79,40 +78,4 @@
 &my_die("Unrecognised ADDSTAR recipe", $cam_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe1;
 
-# values to extract from output metadata and the stats to calculate
-# these should be coming from the psastro results
-my $CHIPSTATS =
-    [
-        #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
-        { name => "bg",             type => "mean",  flag => "-bg",             dtype => "float" },
-        { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev",       dtype => "float" },
-        { name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev",  dtype => "float" },
-        { name => "bias",           type => "mean",  flag => "-bias",           dtype => "float" },
-        { name => "bias_stdev",     type => "rms",   flag => "-bias_stdev",     dtype => "float" },
-        { name => "fringe_0",       type => "mean",  flag => "-fringe_0",       dtype => "float" },
-        { name => "fringe_1",       type => "rms",   flag => "-fringe_1",       dtype => "float" },
-        { name => "fringe_2",       type => "stdev", flag => "-fringe_2",       dtype => "float" },
-        { name => "ap_resid",       type => "mean",  flag => "-ap_resid",       dtype => "float" },
-        { name => "ap_resid_stdev", type => "rms",   flag => "-ap_resid_stdev", dtype => "float" },
-        { name => "fwhm_major",     type => "mean",  flag => "-fwhm_major",     dtype => "float" },
-        { name => "fwhm_minor",     type => "mean",  flag => "-fwhm_minor",     dtype => "float" },
-        { name => "n_stars",        type => "sum",   flag => "-n_stars",        dtype => "int"   },
-        { name => "n_extended",     type => "sum",   flag => "-n_extended",     dtype => "int"   },
-        { name => "n_cr",           type => "sum",   flag => "-n_cr",           dtype => "int"   },
-        ];
-my $chipStats = PS::IPP::Metadata::Stats->new($CHIPSTATS); # Stats parser
-
-# values to extract from camera-level output metadata and the stats to calculate
-my $CAMSTATS =
-   [
-       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
-      { name => "CERROR",         type => "rms",   flag => "-sigma_ra",       dtype => "float" },
-      { name => "CERROR",         type => "rms",   flag => "-sigma_dec",      dtype => "float" },
-#      { name => "DT_ASTR",        type => "sum",   flag => "-dtime_astrom",   dtype => "float" },
-      { name => "NASTRO",         type => "sum",   flag => "-n_astrom",       dtype => "int"   },
-#     { name => "ZP??",           type => "mean",  flag => "-zp_mean",        dtype => "float" },
-#     { name => "ZP??",           type => "rms",   flag => "-zp_stdev",       dtype => "float" },
-   ];
-my $camStats = PS::IPP::Metadata::Stats->new($CAMSTATS); # Stats parser
-
 # Look for programs we need
 my $missing_tools;
@@ -120,4 +83,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);
 
 # test for addstar and psastro:
@@ -131,4 +95,6 @@
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+my $cmdflags;
 
 # Get list of component files
@@ -150,7 +116,24 @@
         &my_die("Unable to parse metadata list", $cam_id, $PS_EXIT_PROG_ERROR);
 
-    # extract the stats from the metadata
-    unless ($chipStats->parse($metadata)) {
-        &my_die("Unable to find all values in statistics output.\n", $cam_id, $PS_EXIT_PROG_ERROR);
+    # since I can't figure out how to do input and output within PERL, I'm writing to a temp file
+    my ($statFile, $statName) = tempfile( "/tmp/$exp_tag.cm.$cam_id.stats.XXXX", UNLINK => !$save_temps );
+    print "saving stats to $statName\n";
+    foreach my $line (@$stdout_buf) {
+        print $statFile $line;
+    }
+    close $statFile;
+
+    # parse the stats in the metadata file
+    $command = "$ppStatsFromMetadata $statName - CAMERA_EXP_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);
+        warn("Unable to perform ppStatsFromMetadata: $error_code\n");
+        exit($error_code);
+    }
+
+    foreach my $line (@$stdout_buf) {
+        $cmdflags .= " $line";
     }
 }
@@ -248,5 +231,4 @@
     if ($chipObjectsExist) {
         # run psastro on the chipObjects, producing fpaObjects
-        # XXX add a ppStats call which will collect the astrometry stats
         my $command;
         $command  = "$psastro -list $list3Name $outroot";
@@ -256,5 +238,5 @@
         my $do_stats;
         if ($run_state eq 'new') {
-            $command .= " -stats $fpaStats -recipe PPSTATS CHIPSTATS";
+            $command .= " -stats $fpaStats -recipe PPSTATS CAMSTATS";
             $command .= " -dumpconfig $configuration";
             $do_stats = 1;
@@ -275,23 +257,19 @@
 
         if ($do_stats) {
-            &my_die("Unable to find expected output file: $fpaStats",   $cam_id, $PS_EXIT_PROG_ERROR) unless -f $ipprc->file_resolve($fpaObjects);
-
-            # Get the statistics on the processed image
-            my $statsFile;              # File handle
-            open $statsFile, $ipprc->file_resolve($fpaStats) or &my_die("Can't open statistics file $fpaStats: $!", $cam_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", $cam_id, $PS_EXIT_PROG_ERROR);
-            }
-
-            # extract the stats from the metadata
-            unless ($camStats->parse($metadata)) {
-                &my_die("Failure extracting metadata from the statistics output file.\n", $cam_id, $PS_EXIT_PROG_ERROR);
-            }
+	    my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
+	    &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless -f $fpaStatsReal;
+
+	    # parse stats from metadata
+	    $command = "$ppStatsFromMetadata $fpaStatsReal - CAMERA_EXP_FPA";
+	    ( $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", $cam_id, $error_code);
+	    }
+	    foreach my $line (@$stdout_buf) {
+		$cmdflags .= " $line";
+	    }
+	    chomp $cmdflags;
         }
 
@@ -340,5 +318,4 @@
 }
 
-
 my $fpaCommand = "$camtool -cam_id $cam_id";
 if ($run_state eq 'new') {
@@ -346,6 +323,5 @@
     $fpaCommand .= " -uri UNKNOWN";
     $fpaCommand .= " -path_base $outroot";
-    $fpaCommand .= $chipStats->cmdflags();
-    $fpaCommand .= $camStats->cmdflags();
+    $fpaCommand .= " $cmdflags";
     $fpaCommand .= " -hostname $host" if defined $host;
     $fpaCommand .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
