Index: trunk/ippScripts/scripts/phase0imfile.pl
===================================================================
--- trunk/ippScripts/scripts/phase0imfile.pl	(revision 8256)
+++ trunk/ippScripts/scripts/phase0imfile.pl	(revision 8309)
@@ -6,16 +6,38 @@
 use IPC::Cmd qw( can_run run );
 use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
 use Data::Dumper;
 
-use constant RECIPE => "PPSTATS_PHASE0_IMFILE"; # Recipe to use for ppStats
+use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
 
-# Switches for p0search
-use constant P0SEARCH_MODE => '-updateimfile'; # Mode for p0search
-use constant P0SEARCH_EXPID => '-exp_id'; # Switch to specify the exposure id
-use constant P0SEARCH_CLASSID => '-class_id'; # Switch to specify the class id
-use constant P0SEARCH_BG => '-bg'; # Switch to add the background
-use constant P0SEARCH_BGSD => '-background_stdev'; # Switch to add the bg stdev
-use constant P0SEARCH_EXPTIME => '-exp_time'; # Switch to add the exposure time
+# These values should be constant for all components
+# The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp"
+use constant CONSTANTS => {
+    "FPA.FILTER"   => "-filter", # Filter used
+    "FPA.AIRMASS"  => "-airmass", # Airmass
+    "FPA.RA"       => "-ra",	# Right ascension
+    "FPA.DEC"      => "-decl",	# Declination
+    "TELALT"       => "-alt",	# Altitude
+    "TELAZ"        => "-az",	# Azimuth
+    "DETTEM"       => "-ccd_temp", # CCD temperature
+    "FPA.POSANGLE" => "-posang", # Position angle
+    };
 
+# These values may vary across components; we will take the average
+# The key is the name in the ppStats output; the value is the switch for "phase0 -updateexp"
+use constant VARIABLES => {
+    "CELL.EXPOSURE" => "-exp_time" # Exposure time
+    };
+
+
+# Switches for p0tool
+use constant P0TOOL_MODE => '-updateimfile'; # Mode for p0tool
+use constant P0TOOL_EXPID => '-exp_id'; # Switch to specify the exposure id
+use constant P0TOOL_CLASSID => '-class_id'; # Switch to specify the class id
+use constant P0TOOL_BG_MEAN => '-bg'; # Switch to add the background
+use constant P0TOOL_BG_STDEV => '-bg_stdev'; # Switch to add the bg stdev
+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" .
@@ -27,16 +49,13 @@
 my $file = shift @ARGV;		# Input filename
 
-# Look for commands we need
+
+# Look for programs 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);
+my $p0tool = can_run('p0tool') or (warn "Can't find p0tool" 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 $stats;
 {
     my $command = "$ppStats $file -recipe PPSTATS " . RECIPE; # Command to run ppStats
@@ -44,53 +63,37 @@
 	run(command => $command, verbose => 1);
     die "Unable to perform ppStats on exposure id $expid: $error_code\n" if not $success;
-    $parsed = $mdcParser->parse(join "", @$stdout_buf);
+    
+    # Parse the output
+    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf);
+    my @constants = keys %{CONSTANTS()}; # List of constants to parse out
+    my @variables = keys %{VARIABLES()}; # List of variables to parse out
+    $stats = PS::IPP::Metadata::Stats->new(\@constants, \@variables); # Stats parser
+    $stats->parse($metadata) or die "Unable to find all values.\n";
 }
-
-my $bg = 0;			# Mean background level
-my $sd = 0;			# Mean standard deviation
-my $expTime = 0;		# Mean exposure time
-my $bgNum = 0;			# Number of background entries
-my $sdNum = 0;			# Number of standard deviation entries
-my $expTimeNum = 0;		# Number of exposure time 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++;
-			}
-		    } elsif ($cellItem->{name} =~ /CELL\.EXPOSURE/) {
-			$expTime += $cellItem->{value};
-			$expTimeNum++;
-		    }
-		}
-	    }
-	}
-    }
-}
-die "Unable to find statistics.\n" if ($bgNum == 0 or $sdNum == 0);
-$bg /= $bgNum;
-$sd /= $sdNum;
-die "Unable to find exposure times.\n" if ($expTimeNum == 0);
-$expTime /= $expTimeNum;
 
 # Push the results into the database
 {
-    my $command = "$p0search " . P0SEARCH_MODE . " " . P0SEARCH_EXPID() . " $expid " .
-	P0SEARCH_CLASSID() . " $classid " . P0SEARCH_BG() ." $bg " . P0SEARCH_BGSD() ." $sd " .
-	P0SEARCH_EXPTIME() . " $expTime"; # Command to run p0search
+    my $command = $p0tool . " " . P0TOOL_MODE() . " " . P0TOOL_EXPID() . " " . $expid . " " .
+	P0TOOL_CLASSID() . " " . $classid; # Command to run p0tool
+    
+    foreach my $constant (keys %{CONSTANTS()}) {
+	$command .= " " . CONSTANTS->{$constant} . " " . ($stats->data($constant))->{value};
+    }
+    foreach my $variable (keys %{VARIABLES()}) {
+	# Just use the mean value
+	$command .= " " . VARIABLES->{$variable} . " " . ($stats->data($variable))->{mean};
+    }
+    
+    $command .= " " . P0TOOL_BG_MEAN() . " " . $stats->bg_mean();
+    $command .= " " . P0TOOL_BG_STDEV() . " " . $stats->bg_stdev();
+    $command .= " " . P0TOOL_BG_MEAN_STDEV() . " " . $stats->bg_mean_stdev();
+ 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform p0search -updateimfile: $error_code\n" if not $success;
+    die "Unable to perform p0tool -updateimfile: $error_code\n" if not $success;
 }
+
+# Pau.
+
+__END__
