Index: trunk/ippScripts/scripts/register_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/register_imfile.pl	(revision 19733)
+++ trunk/ippScripts/scripts/register_imfile.pl	(revision 20101)
@@ -20,5 +20,4 @@
 use PS::IPP::Config 1.01 qw( :standard );
 use PS::IPP::Metadata::Config;
-use PS::IPP::Metadata::Stats;
 
 my $PI = 3.141592653589793238462643383279502;
@@ -57,64 +56,9 @@
 my $RECIPE = "REGISTER"; # Recipe to use for ppStats
 
-# values to extract from output metadata and the stats to calculate
-my $STATS =
-   [
-       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
-       { name => "FILE.LEVEL",     type => "constant", flag => "-filelevel",       dtype => "string" }, # File level
-       { name => "CLASS.ID",       type => "constant", flag => "-class_id",        dtype => "string" }, # Real Class ID
-       { name => "FPA.OBJECT",     type => "constant", flag => "-object",          dtype => "string" }, # Object
-       { name => "FPA.OBSTYPE",    type => "constant", flag => "-exp_type",        dtype => "string" }, # Exposure type
-       { name => "FPA.FILTER",     type => "constant", flag => "-filter",          dtype => "string" }, # Filter used
-       { name => "FPA.COMMENT",    type => "constant", flag => "-comment",         dtype => "string" }, # Obs Comment
-       { name => "FPA.AIRMASS",    type => "constant", flag => "-airmass",         dtype => "float"  }, # Airmass
-       { name => "FPA.RA",         type => "constant", flag => "-ra",              dtype => "float"  }, # Right ascension
-       { name => "FPA.DEC",        type => "constant", flag => "-decl",            dtype => "float"  }, # Declination
-       { name => "FPA.ALT",        type => "constant", flag => "-alt",             dtype => "float"  }, # Altitude
-       { name => "FPA.AZ",         type => "constant", flag => "-az",              dtype => "float"  }, # Azimuth
-       { name => "FPA.POSANGLE",   type => "constant", flag => "-posang",          dtype => "float"  }, # Position angle
-       { name => "FPA.TIME",       type => "constant", flag => "-dateobs",         dtype => "string" }, # Date of observation (UTC)
-       { name => "FPA.TELESCOPE",  type => "constant", flag => "-telescope",       dtype => "string" }, # Telescope
-       { name => "FPA.CAMERA",     type => "constant", flag => "-inst",            dtype => "string" }, # Instrument
-       { name => "FPA.LONGITUDE",  type => "constant", flag => "-longitude",       dtype => "float"  }, # Site longitude
-       { name => "FPA.LATITUDE",   type => "constant", flag => "-latitude",        dtype => "float"  }, # Site latitude
-       { name => "FPA.ELEVATION",  type => "constant", flag => "-elevation",       dtype => "float"  }, # Site elevation
-       { name => "FPA.M1X",        type => "constant", flag => "-m1_x",            dtype => "float"  }, # M1X
-       { name => "FPA.M1Y",        type => "constant", flag => "-m1_y",            dtype => "float"  }, # M1Y
-       { name => "FPA.M1Z",        type => "constant", flag => "-m1_z",            dtype => "float"  }, # M1Z
-       { name => "FPA.M1TIP",      type => "constant", flag => "-m1_tip",          dtype => "float"  }, # M1TIP
-       { name => "FPA.M1TILT",     type => "constant", flag => "-m1_tilt",         dtype => "float"  }, # M1TILT
-       { name => "FPA.M2X",        type => "constant", flag => "-m2_x",            dtype => "float"  }, # M2X
-       { name => "FPA.M2Y",        type => "constant", flag => "-m2_y",            dtype => "float"  }, # M2Y
-       { name => "FPA.M2Z",        type => "constant", flag => "-m2_z",            dtype => "float"  }, # M2Z
-       { name => "FPA.M2TIP",      type => "constant", flag => "-m2_tip",          dtype => "float"  }, # M2TIP
-       { name => "FPA.M2TILT",     type => "constant", flag => "-m2_tilt",         dtype => "float"  }, # M2TILT
-       { name => "FPA.ENV.TEMP",   type => "constant", flag => "-env_temperature", dtype => "float"  }, # external temp
-       { name => "FPA.ENV.HUMID",  type => "constant", flag => "-env_humidity",    dtype => "float"  }, # external humidity
-       { name => "FPA.ENV.WIND",   type => "constant", flag => "-env_wind_speed",  dtype => "float"  }, # external wind speed
-       { name => "FPA.ENV.DIR",    type => "constant", flag => "-env_wind_dir",    dtype => "float"  }, # external wind direction
-
-       { name => "FPA.TELTEMP.M1",     type => "constant", flag => "-teltemp_m1",     dtype => "float"  }, # Primary mirror temps (C)
-       { name => "FPA.TELTEMP.M1CELL", type => "constant", flag => "-teltemp_m1cell", dtype => "float"  }, # Primary mirror support temps (C)
-       { name => "FPA.TELTEMP.M2",     type => "constant", flag => "-teltemp_m2",     dtype => "float"  }, # Secondary mirror temps (C
-       { name => "FPA.TELTEMP.SPIDER", type => "constant", flag => "-teltemp_spider", dtype => "float"  }, # Spider temperatures (C)
-       { name => "FPA.TELTEMP.TRUSS",  type => "constant", flag => "-teltemp_truss",  dtype => "float"  }, # Mid truss temperatures (C
-       { name => "FPA.TELTEMP.EXTRA",  type => "constant", flag => "-teltemp_extra",  dtype => "float"  }, # Miscellaneous temperatures (C)
-
-       { name => "FPA.PON.TIME",   type => "constant", flag => "-pon_time",        dtype => "float"  }, # time since last power on
-       { name => "CHIP.TEMP",      type => "mean",     flag => "-ccd_temp",        dtype => "float"  }, # CCD temperature
-       { name => "CELL.EXPOSURE",  type => "mean",     flag => "-exp_time",        dtype => "float"  }, # Exposure time
-       { name => "SAT_PIXEL_FRAC", type => "mean",     flag => "-sat_pixel_frac",  dtype => "float"  }, # fraction of saturated pixels
-       { name => "ROBUST_MEDIAN",  type => "mean",     flag => "-bg",              dtype => "float"  },
-       { name => "ROBUST_MEDIAN",  type => "stdev",    flag => "-bg_mean_stdev",   dtype => "float"  },
-       { name => "ROBUST_STDEV",   type => "rms",      flag => "-bg_stdev",        dtype => "float"  },
-   ];
-my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
-
 # Look for programs we need
 my $missing_tools;
-my $regtool = can_run('regtool')
-    or (warn "Can't find regtool" and $missing_tools = 1);
-my $ppStats = can_run('ppStats')
-    or (warn "Can't find ppStats" and $missing_tools = 1);
+my $regtool = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
+my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -135,6 +79,5 @@
 # Run ppStats on the input file
 {
-    # extract the data from the image header; -level is used to get FILE.LEVEL and CLASS.ID
-    my $command = "$ppStats $uri -recipe PPSTATS $RECIPE -level"; # Command to run ppStats
+    my $command = "$ppStats $uri -recipe PPSTATS $RECIPE -level | $ppStatsFromMetadata - - REGISTER_IMFILE"; # Command to run ppStats and ppStatsFromMetadata
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -144,16 +87,8 @@
         &my_die ("Unable to perform ppStats on exposure id $exp_id: $error_code", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $error_code);
     }
-
-    # Parse the output
-    my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # XXX is this join necessary?
-    unless ($metadata) {
-        &my_die ("Unable to parse metadata config doc", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_PROG_ERROR);
-    }
-
-    # extract the stats from the metadata
-    unless ($stats->parse($metadata)) {
-        &my_die ("Unable to find all values", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR);
-    }
+    foreach my $line (@$stdout_buf) {
+	$cmdflags .= " $line";
+    }
+    chomp $cmdflags;
 }
 
@@ -162,9 +97,9 @@
 
 # we require at a minimum: -telescope, -inst, -filelevel, -class_id, -exp_type
-if (uc($stats->value_for_flag ("-telescope")) eq "NULL") { &my_die ("telescope not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
-if (uc($stats->value_for_flag ("-inst"))      eq "NULL") { &my_die ("inst      not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
-if (uc($stats->value_for_flag ("-filelevel")) eq "NULL") { &my_die ("filelevel not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
-if (uc($stats->value_for_flag ("-class_id"))  eq "NULL") { &my_die ("class_id  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
-if (uc($stats->value_for_flag ("-exp_type"))  eq "NULL") { &my_die ("exp_type  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&value_for_flag ($cmdflags, "-telescope")) eq "NULL") { &my_die ("telescope not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&value_for_flag ($cmdflags, "-inst"))      eq "NULL") { &my_die ("inst      not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&value_for_flag ($cmdflags, "-filelevel")) eq "NULL") { &my_die ("filelevel not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&value_for_flag ($cmdflags, "-class_id"))  eq "NULL") { &my_die ("class_id  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&value_for_flag ($cmdflags, "-exp_type"))  eq "NULL") { &my_die ("exp_type  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $uri, $PS_EXIT_CONFIG_ERROR); }
 
 my $command = "$regtool -addprocessedimfile";
@@ -175,13 +110,13 @@
 $command .= " -hostname $host" if defined $host;
 $command .= " -dbname $dbname" if defined $dbname;
-$command .= $stats->cmdflags();
+$command .= " $cmdflags";
 
 # determine solar-system parameters
-my $longitude = $stats->value_for_flag ("-longitude");
-my $latitude  = $stats->value_for_flag ("-latitude");
-my $elevation = $stats->value_for_flag ("-elevation");
-my $ra        = $stats->value_for_flag ("-ra");
-my $dec       = $stats->value_for_flag ("-decl");
-my $dateobs   = $stats->value_for_flag ("-dateobs");
+my $longitude = &value_for_flag ($cmdflags, "-longitude");
+my $latitude  = &value_for_flag ($cmdflags, "-latitude");
+my $elevation = &value_for_flag ($cmdflags, "-elevation");
+my $ra        = &value_for_flag ($cmdflags, "-ra");
+my $dec       = &value_for_flag ($cmdflags, "-decl");
+my $dateobs   = &value_for_flag ($cmdflags, "-dateobs");
 
 # if the needed data is available, pass it to sunmoon:
@@ -238,4 +173,16 @@
         return @output;
     }
+}
+
+sub value_for_flag
+{
+    my $cmdflags = shift;
+    my $flag = shift;
+
+    my $value = 0.0;
+    if ($cmdflags =~ m|$flag|) {
+        ($value) = $cmdflags =~ m|$flag\s+(\S+)|;
+    }
+    $value;
 }
 
