Index: /trunk/PS-IPP-Config/lib/PS/IPP/Metadata/Stats.pm
===================================================================
--- /trunk/PS-IPP-Config/lib/PS/IPP/Metadata/Stats.pm	(revision 17798)
+++ /trunk/PS-IPP-Config/lib/PS/IPP/Metadata/Stats.pm	(revision 17799)
@@ -1,5 +1,5 @@
 # Copyright (c) 2006  Paul Price, Joshua Hoblitt
 #
-# $Id: Stats.pm,v 1.28 2008-05-09 21:02:21 eugene Exp $
+# $Id: Stats.pm,v 1.29 2008-05-24 02:54:09 price Exp $
 
 package PS::IPP::Metadata::Stats;
@@ -12,4 +12,5 @@
 use Carp qw( carp );
 use Statistics::Descriptive;
+use Sys::Hostname;
 
 use PS::IPP::Config qw(
@@ -24,7 +25,7 @@
 
 use base qw( Class::Accessor::Fast );
-__PACKAGE__->mk_accessors( qw( 
-			       results 
-			       ) );
+__PACKAGE__->mk_accessors( qw(
+                               results
+                               ) );
 
 #$::RD_TRACE = 1;
@@ -33,14 +34,18 @@
 
 sub new {
-    my $class = shift;		# Class name
-    my $entries = shift;	# Array of values to extract
-
-    my $self = { 
-	entries => $entries, # Array of values that should be constant through the FPA
-	data => {}		# The data
+    my $class = shift;          # Class name
+    my $entries = shift;        # Array of values to extract
+
+    my $self = {
+        entries => $entries, # Array of values that should be constant through the FPA
+        data => {}              # The data
     };
-    
+
     # add a hash for storage
     foreach my $entry (@$entries) {
+        if ($entry->{type} eq "hostname") {
+            $entry->{value} = hostname();
+            next;
+        }
         $entry->{data} = [];
     }
@@ -56,18 +61,18 @@
 
     # apply the needed calculation to the data based on the type
-    
+
     my $cmdflags;
     my $entries = $self->{entries};
-    
+
     foreach my $entry (@$entries) {
-	my $value = $entry->{value};
-	my $flag = $entry->{flag};
-	next unless defined $value and defined $flag;
-	if ($value =~ m|\S+\s+\S+|) {
-	    # protect values with whitespace 
-	    $cmdflags .= " $flag '$value'";
-	} else {
-	    $cmdflags .= " $flag $value";
-	}
+        my $value = $entry->{value};
+        my $flag = $entry->{flag};
+        next unless defined $value and defined $flag;
+        if ($value =~ m|\S+\s+\S+|) {
+            # protect values with whitespace
+            $cmdflags .= " $flag '$value'";
+        } else {
+            $cmdflags .= " $flag $value";
+        }
     }
 
@@ -87,150 +92,155 @@
 
     foreach my $entry (@$entries) {
-	my $type = $entry->{type};
-	my $data = $entry->{data};
-
-	if ($type eq "constant") {
-	    if (not defined $entry->{value}) {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "mean") {
-            if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = $stats->mean();
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	# calculate a 3-sigma clipped mean (if Nvalues > 8, reject > 3sigma outliers)
-	if ($type eq "clipmean") {
-            if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		my $rawMean = $stats->mean();
-		my $rawStdev = $stats->standard_deviation();
-		my $rawLimit = 3.0*$rawStdev;
-
-		# clip, if we can
-		if (scalar @$data > 8) {
-		    my @clipdata = ();
-		    foreach my $value (@$data){ 
-			my $delta = abs($value - $rawMean);
-			if ($delta > $rawLimit) { next; }
-			push @clipdata, $value;
-		    }
-		    my $clipstats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		    $clipstats->add_data(@clipdata);
-		    $entry->{value} = $clipstats->mean();
-		} else {
-		    $entry->{value} = $stats->mean();
-		}
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "stdev") {
+        my $type = $entry->{type};
+        my $data = $entry->{data};
+
+        if ($entry->{type} eq "hostname") {
+            # Dealt with in new()
+            next;
+        }
+
+        if ($type eq "constant") {
+            if (not defined $entry->{value}) {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "mean") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = $stats->mean();
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        # calculate a 3-sigma clipped mean (if Nvalues > 8, reject > 3sigma outliers)
+        if ($type eq "clipmean") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                my $rawMean = $stats->mean();
+                my $rawStdev = $stats->standard_deviation();
+                my $rawLimit = 3.0*$rawStdev;
+
+                # clip, if we can
+                if (scalar @$data > 8) {
+                    my @clipdata = ();
+                    foreach my $value (@$data){
+                        my $delta = abs($value - $rawMean);
+                        if ($delta > $rawLimit) { next; }
+                        push @clipdata, $value;
+                    }
+                    my $clipstats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                    $clipstats->add_data(@clipdata);
+                    $entry->{value} = $clipstats->mean();
+                } else {
+                    $entry->{value} = $stats->mean();
+                }
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "stdev") {
             if (scalar @$data > 1) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = $stats->standard_deviation();
-		next;
-	    } 
-	    if (scalar @$data == 1) {
-		$entry->{value} = 0.0;
-		next;
-	    } 
-	    $self->_null_for_type ($entry);
-	    next;
-	}
-
-	# calculate a 3-sigma clipped stdev (if Nvalues > 8, reject > 3sigma outliers)
-	if ($type eq "clipstdev") {
-            if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		my $rawMean = $stats->mean();
-		my $rawStdev = $stats->standard_deviation();
-		my $rawLimit = 3.0*$rawStdev;
-
-		# clip, if we can
-		if (scalar @$data > 8) {
-		    my @clipdata = ();
-		    foreach my $value (@$data){ 
-			my $delta = abs($value - $rawMean);
-			if ($delta > $rawLimit) { next; }
-			push @clipdata, $value;
-		    }
-		    my $clipstats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		    $clipstats->add_data(@clipdata);
-		    $entry->{value} = $clipstats->standard_deviation();
-		} else {
-		    $entry->{value} = $stats->standard_deviation();
-		}
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "rms") {
-            if (scalar @$data > 0) {
-		# Get statistics on the value
-		# for rmsStats, we pushed the value^2 on the data array
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = sqrt($stats->mean());
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "sum") {
-            if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = $stats->sum();
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "max") {
-	    if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = $stats->max();
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
-
-	if ($type eq "min") {
-	    if (scalar @$data > 0) {
-		# Get statistics on the value
-		my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-		$stats->add_data(@$data);
-		$entry->{value} = $stats->min();
-	    } else {
-		$self->_null_for_type ($entry);
-	    }
-	    next;
-	}
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = $stats->standard_deviation();
+                next;
+            }
+            if (scalar @$data == 1) {
+                $entry->{value} = 0.0;
+                next;
+            }
+            $self->_null_for_type ($entry);
+            next;
+        }
+
+        # calculate a 3-sigma clipped stdev (if Nvalues > 8, reject > 3sigma outliers)
+        if ($type eq "clipstdev") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                my $rawMean = $stats->mean();
+                my $rawStdev = $stats->standard_deviation();
+                my $rawLimit = 3.0*$rawStdev;
+
+                # clip, if we can
+                if (scalar @$data > 8) {
+                    my @clipdata = ();
+                    foreach my $value (@$data){
+                        my $delta = abs($value - $rawMean);
+                        if ($delta > $rawLimit) { next; }
+                        push @clipdata, $value;
+                    }
+                    my $clipstats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                    $clipstats->add_data(@clipdata);
+                    $entry->{value} = $clipstats->standard_deviation();
+                } else {
+                    $entry->{value} = $stats->standard_deviation();
+                }
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "rms") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                # for rmsStats, we pushed the value^2 on the data array
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = sqrt($stats->mean());
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "sum") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = $stats->sum();
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "max") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = $stats->max();
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
+
+        if ($type eq "min") {
+            if (scalar @$data > 0) {
+                # Get statistics on the value
+                my $stats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+                $stats->add_data(@$data);
+                $entry->{value} = $stats->min();
+            } else {
+                $self->_null_for_type ($entry);
+            }
+            next;
+        }
 
     }
@@ -242,13 +252,13 @@
 sub _is_null_for_type
 {
-    my ($item) = @_;		# Metadata item
+    my ($item) = @_;            # Metadata item
 
     return 1 unless defined $item->{type} and defined $item->{value};
 
     if (($item->{type} eq "F32" or $item->{type} eq "F64") and lc($item->{value} eq 'nan')) {
-	return 1;
+        return 1;
     }
     if ($item->{type} eq "STR" and lc($item->{value}) eq 'null') {
-	return 1;
+        return 1;
     }
     return 0;
@@ -260,14 +270,14 @@
 
     if ($entry->{dtype} eq "float") {
-	$entry->{value} = 'NAN';
-	return;
+        $entry->{value} = 'NAN';
+        return;
     }
     if ($entry->{dtype} eq "int") {
-	$entry->{value} = '0';
-	return;
+        $entry->{value} = '0';
+        return;
     }
     if ($entry->{dtype} eq "string") {
-	$entry->{value} = 'NULL';
-	return;
+        $entry->{value} = 'NULL';
+        return;
     }
     $entry->{value} = 'NAN';
@@ -279,5 +289,5 @@
     my ($self, $md) = @_;
 
-    # descend through the fpa        
+    # descend through the fpa
     foreach my $entry (@$md) {
         # recurse on nested metadata
@@ -285,8 +295,8 @@
             $self->_parse_metadata_stats($entry->{value});
         }
-	#if (defined $entry->{value}) {
-	#    print STDERR "found $entry->{name} : $entry->{value}\n";
-	#}
-	$self->_check_values($entry);
+        #if (defined $entry->{value}) {
+        #    print STDERR "found $entry->{name} : $entry->{value}\n";
+        #}
+        $self->_check_values($entry);
    }
 
@@ -301,66 +311,66 @@
     my $name = $mdItem->{name};        # Name of the item
     my $value = $mdItem->{value}; # Value of the item
-    
+
     if ( _is_null_for_type($mdItem) and $mdItem->{class} eq 'scalar' ) {
-	# print "Stats: Ignoring NULL entry $name\n";
-	return;
-    }
-	
+        # print "Stats: Ignoring NULL entry $name\n";
+        return;
+    }
+
     my $entries = $self->{entries};        # The data
 
     # we may request the value more than once (different stats on the same value)
     foreach my $entry (@$entries) {
-	if ($entry->{name} eq $name) {
-
-	    my $type = $entry->{type}; # Type of the item: constant or variable
-	    my $data = $entry->{data}; # Array containing all the values
-
-	    if ($type eq 'constant') {
-		if (defined $entry->{value} && ($entry->{value} ne $value)) {
-		    carp "Warning: different value for ", $name, " found: ", $entry->{value}, " (old) vs ", $value, " (new).  Ignoring new value.\n";
-		    next;
-		}
-		$entry->{value} = $value;
-		next;
-	    }
-
-	    if ($type eq 'mean') {
-		push @$data, $value;
-		next;
-	    }
-
-	    if ($type eq 'clipmean') {
-		push @$data, $value;
-		next;
-	    }
-
-	    if ($type eq 'stdev') {
-		push @$data, $value;
-		next;
-	    }
-
-	    if ($type eq 'clipstdev') {
-		push @$data, $value;
-		next;
-	    }
-
-	    if ($type eq 'rms') {
-		push @$data, ($value**2);
-		next;
-	    }
-
-	    if ($type eq 'sum') {
-		push @$data, $value;
-		next;
-	    }
-
-	    if ($type eq 'max' or $type eq 'min') {
-		push @$data, $value;
-		next;
-	    }
-
-	    carp "Unrecognised type (", $type, ") for value of ", $name, "\n";
-	    exit($PS_EXIT_PROG_ERROR);
-	}
+        if ($entry->{name} eq $name) {
+
+            my $type = $entry->{type}; # Type of the item: constant or variable
+            my $data = $entry->{data}; # Array containing all the values
+
+            if ($type eq 'constant') {
+                if (defined $entry->{value} && ($entry->{value} ne $value)) {
+                    carp "Warning: different value for ", $name, " found: ", $entry->{value}, " (old) vs ", $value, " (new).  Ignoring new value.\n";
+                    next;
+                }
+                $entry->{value} = $value;
+                next;
+            }
+
+            if ($type eq 'mean') {
+                push @$data, $value;
+                next;
+            }
+
+            if ($type eq 'clipmean') {
+                push @$data, $value;
+                next;
+            }
+
+            if ($type eq 'stdev') {
+                push @$data, $value;
+                next;
+            }
+
+            if ($type eq 'clipstdev') {
+                push @$data, $value;
+                next;
+            }
+
+            if ($type eq 'rms') {
+                push @$data, ($value**2);
+                next;
+            }
+
+            if ($type eq 'sum') {
+                push @$data, $value;
+                next;
+            }
+
+            if ($type eq 'max' or $type eq 'min') {
+                push @$data, $value;
+                next;
+            }
+
+            carp "Unrecognised type (", $type, ") for value of ", $name, "\n";
+            exit($PS_EXIT_PROG_ERROR);
+        }
     }
 
@@ -376,9 +386,9 @@
 
     foreach my $entry (@$entries) {
-	if ($flag eq $entry->{flag}) {
-	    return $entry->{value};
-	}
-    }
-    return 'NULL'; # 
+        if ($flag eq $entry->{flag}) {
+            return $entry->{value};
+        }
+    }
+    return 'NULL'; #
 }
 
