Index: trunk/ippScripts/scripts/phase0imfile.pl
===================================================================
--- trunk/ippScripts/scripts/phase0imfile.pl	(revision 8715)
+++ trunk/ippScripts/scripts/phase0imfile.pl	(revision 8763)
@@ -4,8 +4,36 @@
 use strict;
 
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
 use IPC::Cmd qw( can_run run );
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::Stats;
+use PS::IPP::Metadata::StatsFilter;
 use Data::Dumper;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($exp_id, $class, $class_id, $uri);
+
+GetOptions(
+    'exp_id|e=s'    => \$exp_id,
+    'class|c=s'     => \$class,
+    'class_id|i=s'  => \$class_id,
+    'uri|u=s'       => \$uri,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --exp_id --class --class_id --uri",
+    -exitval => 3,
+) unless defined $exp_id 
+    and defined $class 
+    and defined $class_id 
+    and defined $uri;
+
+# XXX the uri should be processed either here or by ppImage
+my $file = $uri;
 
 use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
@@ -40,15 +68,4 @@
 use constant P0TOOL_BG_MEAN_STDEV => '-bg_mean_stdev'; # Switch to add the bg mean stdev
 
-# Parse command-line arguments
-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 programs we need
 my $missing_tools;
@@ -63,5 +80,5 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => 1);
-    die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success;
+    die "Unable to perform ppStats on exposure id $exp_id: $error_code\n" if not $success;
     
     # Parse the output
@@ -69,4 +86,7 @@
     my $metadata = $mdcParser->parse(join "", @$stdout_buf)
             or die "unable to parse metadata config doc";
+    my $statsfilter = PS::IPP::Metadata::StatsFilter->new;
+    $metadata = $statsfilter->filter($metadata)
+        or die "failed to filter stats document";
     my @constants = keys %{CONSTANTS()}; # List of constants to parse out
     my @variables = keys %{VARIABLES()}; # List of variables to parse out
@@ -77,6 +97,6 @@
 # Push the results into the database
 {
-    my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $expid . " " .
-        P0TOOL_CLASSID() . " " . $classid; # Command to run p0tool
+    my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $exp_id . " " .
+        P0TOOL_CLASSID() . " " . $class_id; # Command to run p0tool
     
     foreach my $constant (keys %{CONSTANTS()}) {
