Index: /trunk/ippScripts/scripts/phase0imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/phase0imfile.pl	(revision 8240)
+++ /trunk/ippScripts/scripts/phase0imfile.pl	(revision 8240)
@@ -0,0 +1,83 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use IPC::Cmd qw( can_run run );
+use PS::IPP::Metadata::Config;
+use Data::Dumper;
+
+use constant RECIPE => "PPSTATS_PHASE0_IMFILE"; # Recipe to use for ppStats
+
+use constant IMFILE_BG => 'background';	# Key for the background out of p0search -pendingimfile
+use constant IMFILE_BGSD => 'background_stdev';	# Key for the background standard deviation
+
+if (scalar @ARGV == 0 || scalar @ARGV > 3) {
+    die "Perform phase 0 processing at the imfile level.\n\n" .
+	"Usage: $0 EXP_ID CLASS_ID FILE.fits\n\n";
+}
+
+my $expid = shift @ARGV;	# Exposure identifier
+my $classid = shift @ARGV;	# Class identifier
+my $file = shift @ARGV;		# Input filename
+
+# Look for commands we need
+my $missing_tools;
+my $p0search = can_run('p0search')
+    or (warn "can't find p0search" and $missing_tools = 1);
+my $ppStats = can_run('ppStats')
+    or (warn "can't find ppStats" and $missing_tools = 1);
+die "Can't find required tools.\n" if $missing_tools;
+
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+
+# Run ppStats on the input file
+my $parsed;			# Parsed metadata
+{
+    my $command = "$ppStats $file"; # Command to run ppStats
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 0);
+    die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success;
+    $parsed = $mdcParser->parse(join "", @$stdout_buf);
+}
+
+my $bg = 0;			# Mean background level
+my $sd = 0;			# Mean standard deviation
+my $bgNum = 0;			# Number of background entries
+my $sdNum = 0;			# Number of standard deviation entries
+foreach my $fpaItem (@$parsed) {
+    if ($fpaItem->{class} eq "metadata") {
+	my $chipName = $fpaItem->{name}; # Name of chip
+	my $chipData = $fpaItem->{value}; # Chip-level data
+	foreach my $chipItem (@$chipData) {
+	    if ($chipItem->{class} eq "metadata") {
+		my $cellName = $chipItem->{name}; # Name of cell
+		my $cellData = $chipItem->{value}; # Cell-level data
+		foreach my $cellItem (@$cellData) {
+		    if ($cellItem->{name} =~ /^(SAMPLE|ROBUST|FITTED|CLIPPED)/) {
+			# It's a statistic of some sort
+			if ($cellItem->{name} =~ /STDEV$/) {
+			    $sd += $cellItem->{value};
+			    $sdNum++;
+			} else {
+			    $bg += $cellItem->{value};
+			    $bgNum++;
+			}
+		    }
+		}
+	    }
+	}
+    }
+}
+$bg /= $bgNum;
+$sd /= $sdNum;
+
+# Push the results into the database
+{
+    my $command = "$p0search -updateimfile -exp_id $expid -class_id $classid -background $bg " .
+	"-background_stdev $sd"; # Command to run p0search
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 0);
+    die "Unable to perform p0search -updateimfile: $error_code\n" if not $success;
+}
+
