Index: trunk/ippScripts/scripts/phase0exp.pl
===================================================================
--- trunk/ippScripts/scripts/phase0exp.pl	(revision 8196)
+++ trunk/ippScripts/scripts/phase0exp.pl	(revision 8196)
@@ -0,0 +1,106 @@
+#!/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"; # Recipe to use for ppStats
+
+# These values should be constant for all components
+use constant CONSTANTS => (
+			   "OBSTYPE", # Observation type
+			   "DETTEM", # Detector temperature
+			   "FPA.FILTER", # Filter used
+			   "FPA.RA", # Right ascension
+			   "FPA.DEC", # Declination
+			   "FPA.AIRMASS", # Airmass
+			   "TELALT", # Altitude
+			   "TELAZ", # Azimuth
+			   "FPA.POSANGLE" # Position angle
+    );
+
+# These values may vary across components; we will take the average
+use constant VARIABLES => (
+			   "CELL.EXPOSURE", # Exposure time
+###			   "CELL.TIME" # Time of exposure --- not yet implemented
+			   );
+
+
+# Look for commands we need
+my $missing_tools;
+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 @filenames = @ARGV;		# Input files, all for one FPA
+my %values;			# Values to return
+
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+
+foreach my $fileName (@filenames) {
+    my $command = "$ppStats $fileName -recipe PPSTATS " . RECIPE; # Command to run
+    print "Executing: $command\n";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 0);
+    die "Unable to perform ppStats on $fileName: $error_code\n" if not $success;
+    my $parsed = $mdcParser->parse(join "", @$stdout_buf); # Parsed metadata
+
+    mdParse($parsed, \%values);
+}
+
+foreach my $constant (CONSTANTS) {
+    if (not defined $values{$constant}) {
+	die "Couldn't find value for $constant.\n";
+    }
+    my $value = $values{$constant}; # The value of interest
+    print "$constant: $value\n";
+}
+
+foreach my $variable (VARIABLES) {
+    if (not defined $values{$variable}) {
+	die "Couldn't find value for $variable.\n";
+    }
+    # Take the mean
+    my $value = $values{$variable}->{value} / $values{$variable}->{num}; # Value of interest
+    print "$variable: $value\n";
+}
+
+### Pau.
+
+# Parse a metadata, looking for the appropriate values
+sub mdParse
+{
+    my $mdItemArray = shift;	# Array of metadata items
+    my $values = shift;		# The values
+
+  MDITEM:    foreach my $mdItem (@$mdItemArray) {
+      # Recurse if required
+      if ($mdItem->{class} eq "metadata") {
+	  mdParse($mdItem->{value}, $values);
+	  return;
+      }
+      
+      return if not defined $mdItem->{name};
+      
+      foreach my $constant (CONSTANTS) {
+	  if ($mdItem->{name} eq $constant) {
+	      if (defined $values->{$constant} and $mdItem->{value} ne $values->{$constant}) {
+		  die "Differing values found for $constant\n";
+	      }
+	      $values->{$constant} = $mdItem->{value};
+	      next MDITEM;
+	  }
+      }
+	
+      foreach my $variable (VARIABLES) {
+	  if ($mdItem->{name} eq $variable) {
+	      $values->{$variable}->{value} += $mdItem->{value};
+	      $values->{$variable}->{num}++;
+	      next MDITEM;
+	  }
+      }
+  }
+}
