Index: /trunk/ippScripts/Build.PL
===================================================================
--- /trunk/ippScripts/Build.PL	(revision 14038)
+++ /trunk/ippScripts/Build.PL	(revision 14039)
@@ -42,4 +42,5 @@
 			       scripts/stack_skycell.pl
 			       scripts/ipp_serial_inject.pl
+			       scripts/ipp_serial_inject_split.pl
 			       scripts/ipp_serial_inject_mosaic.pl
 			       scripts/ipp_serial_register.pl
Index: /trunk/ippScripts/scripts/ipp_serial_inject.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_serial_inject.pl	(revision 14038)
+++ /trunk/ippScripts/scripts/ipp_serial_inject.pl	(revision 14039)
@@ -3,7 +3,7 @@
 # this program injects a list of single-file exposures into the db,
 # taking the filename (without .fits) as the exp_tag.  the user
-# supplies a temporary telescope and instrument name.  these are used
+# supplies a temporary telescope and camera name.  these are used
 # for informational purposes only until the registration step can
-# determine the true telescope and instrument name from the image
+# determine the true telescope and camera name from the image
 # headers.
 
@@ -24,23 +24,25 @@
 
 # Parse the command-line arguments
-my ($workdir, $dbname, $telescope, $instrument, $help);
+my ($workdir, $dbname, $telescope, $camera, $help);
 GetOptions(
     'workdir|w=s'    => \$workdir, # working directory for output files
     'dbname|d=s'     => \$dbname, # Database name
     'telescope|t=s'  => \$telescope, # user-supplied telescope name
-    'instrument|i=s' => \$instrument, # user-supplied instrument name
+    'camera|i=s'     => \$camera, # user-supplied camera name
     'help'           => \$help # give help listing
 ) or pod2usage( 2 );
 
-pod2usage( -msg => "inject one or many files into the IPP pipeline database", -exitval => 2
+pod2usage( -msg => "inject one or many files into the IPP pipeline database", 
+	   -exitval => 2) if 
+    defined $help;
 
-pod2usage( -msg => "Usage: $0 --telescope (name) --instrument (name) [--workdir path] [--dbname dbname] (files)", 
+pod2usage( -msg => "Usage: $0 --telescope (name) --camera (name) [--workdir path] [--dbname dbname] (files)", 
 	   -exitval => 2 ) if 
-	   scalar @ARGV == 0;
+    scalar @ARGV == 0;
 
-pod2usage( -msg => "Required options: --telescope (name) --instrument (name)",
+pod2usage( -msg => "Required options: --telescope (name) --camera (name)",
 	   -exitval => 3) unless
-	   defined $telescope and
-	   defined $instrument;
+    defined $telescope and
+    defined $camera;
 
 my $pxinject = can_run('pxinject') or die "Can't find pxinject\n";
@@ -54,6 +56,8 @@
 my $num = 0;
 foreach my $file ( @ARGV ) {
+    # check for file existence
+    if (! -e $file) { die "file $file not found\n"; }
     my $absfile = File::Spec->rel2abs( $file );
-    inject($absfile, $workdir, $dbname, $telescope, $instrument);
+    inject($absfile, $workdir, $dbname, $telescope, $camera);
     $num ++;
 }
@@ -67,5 +71,5 @@
     my $dbname = shift;		# IPP database to use
     my $telescope = shift;	# user-specified telescope
-    my $instrument = shift;	# user-specified instrument
+    my $camera = shift;	# user-specified camera
 
     my ( $vol, $path, $name ) = File::Spec->splitpath( $absfile );
@@ -74,8 +78,11 @@
     my $relfile = $ipprc->convert_filename_relative( $absfile );
 
-    my $command_exp = "$pxinject -newExp -exp_name $exp_name -inst $instrument -telescope $telescope -imfiles 1 -workdir $workdir" ; # Command to run
-    if ($dbname) {
-	$command_exp = "$command_exp -dbname $dbname";
-    }
+    # the telescope, instrument, and exp_name used here are temporary : register replaces them with the true values
+    my $command_exp = "$pxinject -newExp";
+    $command_exp .= " -tmp_exp_name $exp_name";
+    $command_exp .= " -tmp_inst $camera";
+    $command_exp .= " -tmp_telescope $telescope";
+    $command_exp .= " -workdir $workdir";
+    $command_exp .= " -dbname $dbname" if defined $dbname;
 
     my ( $success_exp, $error_code_exp, $full_buf_exp, $stdout_buf_exp, $stderr_buf_exp ) =
@@ -86,12 +93,22 @@
     my $exp_id = $line[2];	# The exposure tag
     
-    # XXX the class_id used here should be temporary : register should replace it with the true class_id
-    my $command_imfile = "$pxinject -newImfile -exp_id $exp_id -exp_name $exp_name -class fpa -class_id fpa -uri $relfile"; # Command to run
-    if ($dbname) {
-	$command_imfile = "$command_imfile -dbname $dbname";
-    }
+    # the class_id used here is temporary : register replaces it with the true class_id
+    my $command_imfile = "$pxinject -newImfile";
+    $command_imfile .= " -exp_id $exp_id";
+    $command_imfile .= " -tmp_class_id fpa";
+    $command_imfile .= " -uri $relfile";
+    $command_imfile .= " -dbname $dbname" if defined ($dbname);
     
     my ( $success_imfile, $error_code_imfile, $full_buf_imfile, $stdout_buf_imfile, $stderr_buf_imfile ) = run( command => $command_imfile, verbose => 1 );
     die "Unable to inject $exp_name imfile: $error_code_imfile\n" if not $success_imfile;
+
+    # the class_id used here is temporary : register replaces it with the true class_id
+    my $command_update = "$pxinject -updatenewExp";
+    $command_update .= " -exp_id $exp_id";
+    $command_update .= " -state run";
+    $command_update .= " -dbname $dbname" if defined ($dbname);
+    
+    my ( $success_update, $error_code_update, $full_buf_update, $stdout_buf_update, $stderr_buf_update ) = run( command => $command_update, verbose => 1 );
+    die "Unable to update $exp_name: $error_code_update\n" if not $success_update;
 
     return 1;
Index: /trunk/ippScripts/scripts/ipp_serial_inject_mosaic.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_serial_inject_mosaic.pl	(revision 14038)
+++ /trunk/ippScripts/scripts/ipp_serial_inject_mosaic.pl	(revision 14039)
@@ -1,3 +1,15 @@
 #!/usr/bin/env perl
+
+# this program injects a set of multi-file exposures into the db.  the
+# program takes a list of base exposure names and injects all files
+# associated with the exposure.  It constructs the expected filenames
+# from the exposure tag and rules for the camera.  This program is for
+# the test only since it requires too much information at the inject
+# stage.  use 'ipp_serial_inject.pl' for single-file images and
+# 'ipp_serial_inject_split.pl' for multiple file images in split
+# format
+
+# this program should not fail because of the data format or the
+# configuration, except for the very basic database setup.
 
 use warnings;
@@ -75,17 +87,24 @@
 }
 
-foreach my $exp ( @ARGV ) {
-    my $command = "$pxinject -newExp -exp_id $exp -inst $camera -telescope $telescope -workdir $workdir -dbname $dbname -imfiles " . (scalar @classes or 1) ; # Command to run
+foreach my $exp_name ( @ARGV ) {
+    my $command = "$pxinject -newExp";
+    " -exp_name $exp_name";
+    " -inst $camera";
+    " -telescope $telescope";
+    " -workdir $workdir";
+    " -dbname $dbname" if defined $dbname;
+    " -imfiles " . (scalar @classes or 1) ; # Command to run
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run( command => $command, verbose => 1 );
-    die "Unable to inject $exp: $error_code\n" if not $success;
+    die "Unable to inject $exp_name: $error_code\n" if not $success;
     
     my @line = split(/\s+/, $$stdout_buf[0]); # The output line, containing the exposure tag
-    my $exp_tag = $line[2];	# The exposure tag
+    my $exp_id = $line[2];	# The exposure tag
     for (my $i = 0; $i < scalar @classes; $i++) {
 	my $class_id = $classes[$i];
 	my $file_id = $files[$i];
-	my $filename = $exp . $file_id . '.fits';
-	$filename = caturi( $exp, $filename ) if defined $add_dir;
+	my $filename = $exp_name . $file_id . '.fits';
+	$filename = caturi( $exp_name, $filename ) if defined $add_dir;
 	$filename = caturi( $path, $filename );
 
@@ -93,5 +112,12 @@
 
 	$filename = $ipprc->convert_filename_relative( $filename );
-	my $command = "$pxinject -newImfile -exp_tag $exp_tag -class chip -class_id $class_id -uri $filename -dbname $dbname"; # Command to run
+	my $command = "$pxinject -newImfile";
+	" -exp_id $exp_id";
+	" -exp_name $exp_name";
+	" -class chip";
+	" -class_id $class_id";
+	" -uri $filename";
+	" -dbname $dbname" if defined ($dbname); # Command to run
+
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	    run( command => $command, verbose => 1 );
Index: /trunk/ippScripts/scripts/register_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/register_exp.pl	(revision 14038)
+++ /trunk/ippScripts/scripts/register_exp.pl	(revision 14039)
@@ -136,5 +136,5 @@
 }
 
-my $exp_type = &STATS_value_for_flag ("-exp_type");
+my $exp_type = &STATS_value_for_flag ($STATS, "-exp_type");
 
 # Add the detrend flag, if needed
Index: /trunk/ippScripts/scripts/register_imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/register_imfile.pl	(revision 14038)
+++ /trunk/ippScripts/scripts/register_imfile.pl	(revision 14039)
@@ -38,22 +38,22 @@
 use Pod::Usage qw( pod2usage );
 
-my ($cache, $exp_id, $class_id, $exp_name, $uri, $workdir, $dbname, $no_update, $no_op);
+my ($cache, $exp_id, $tmp_class_id, $exp_name, $uri, $workdir, $dbname, $no_update, $no_op);
 GetOptions(
-    'caches'        => \$cache,
-    'exp_id|e=s'    => \$exp_id,
-    'class_id|i=s'  => \$class_id,
-    'exp_name|n=s'  => \$exp_name,
-    'uri|u=s'       => \$uri,
-    'workdir|w=s'   => \$workdir, # Working directory for output files
-    'dbname|d=s'    => \$dbname,# Database name
-    'no-update'     => \$no_update,
-    'no-op'         => \$no_op,
+    'caches'           => \$cache,
+    'exp_id|e=s'       => \$exp_id,
+    'tmp_class_id|i=s' => \$tmp_class_id,
+    'exp_name|n=s'     => \$exp_name,
+    'uri|u=s'          => \$uri,
+    'workdir|w=s'      => \$workdir, # Working directory for output files
+    'dbname|d=s'       => \$dbname,# Database name
+    'no-update'        => \$no_update,
+    'no-op'            => \$no_op,
 ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --exp_id --class_id --exp_name --uri",
+pod2usage( -msg => "Required options: --exp_id --tmp_class_id --exp_name --uri",
 	   -exitval => 3) unless
     defined $exp_id and
-    defined $class_id and
+    defined $tmp_class_id and
     defined $exp_name and
     defined $uri;
@@ -79,7 +79,7 @@
        { name => "FPA.TELESCOPE",  type => "constant", flag => "-telescope" }, # Telescope
        { name => "FPA.INSTRUMENT", type => "constant", flag => "-inst" },      # Instrument 
-       { name => "CHIP.TEMP"       type => "mean",     flag => "-ccd_temp" }, # CCD temperature
-       { name => "CELL.EXPOSURE"   type => "mean",     flag => "-exp_time" }, # Exposure time
-       { name => "SAT_PIXEL_FRAC"  type => "mean",     flag => "-sat_pixel_frac" } # fraction of saturated pixels
+       { name => "CHIP.TEMP",      type => "mean",     flag => "-ccd_temp" }, # CCD temperature
+       { name => "CELL.EXPOSURE",  type => "mean",     flag => "-exp_time" }, # Exposure time
+       { name => "SAT_PIXEL_FRAC", type => "mean",     flag => "-sat_pixel_frac" }, # fraction of saturated pixels
        { name => "ROBUST_MEDIAN",  type => "mean",     flag => "-bg" },
        { name => "ROBUST_MEDIAN",  type => "stdev",    flag => "-bg_mean_stdev" },
@@ -114,5 +114,5 @@
     unless ($success) { 
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die ("Unable to perform ppStats on exposure id $exp_id: $error_code", $exp_id, $class_id, $error_code);
+        &my_die ("Unable to perform ppStats on exposure id $exp_id: $error_code", $exp_id, $exp_name, $tmp_class_id, $error_code);
     }
     
@@ -121,5 +121,5 @@
     my $metadata = $mdcParser->parse(join "", @$stdout_buf); # XXX is this join necessary?
     unless ($metadata) {
-        &my_die ("Unable to parse metadata config doc", $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
+        &my_die ("Unable to parse metadata config doc", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_PROG_ERROR);
     }
 
@@ -127,12 +127,19 @@
     my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     unless ($stats->parse($metadata)) {
-        &my_die ("Unable to find all values", $exp_id, $class_id, $PS_EXIT_CONFIG_ERROR);
-    }
-}
+        &my_die ("Unable to find all values", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR);
+    }
+}
+
+# we require at a minimum: -telescope, -inst, -filelevel, -class_id, -exp_type
+if (uc(&STATS_value_for_flag ($STATS, "-telescope")) eq "NAN") { &my_die ("telescope not found", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&STATS_value_for_flag ($STATS, "-inst"))      eq "NAN") { &my_die ("inst      not found", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&STATS_value_for_flag ($STATS, "-filelevel")) eq "NAN") { &my_die ("filelevel not found", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&STATS_value_for_flag ($STATS, "-class_id"))  eq "NAN") { &my_die ("class_id  not found", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
+if (uc(&STATS_value_for_flag ($STATS, "-exp_type"))  eq "NAN") { &my_die ("exp_type  not found", $exp_id, $exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
 
 my $command = "$regtool -addprocessedimfile";
 $command .= " -exp_id $exp_id";
-$command .= " -temp_class_id $class_id"; # the original class_id supplied by the user, replace by ppStats CLASS.ID
 $command .= " -exp_name $exp_name"; # keep the supplied exp_name (could be derived from the file)
+$command .= " -tmp_class_id $tmp_class_id"; # the original class_id supplied by the user, replace by ppStats CLASS.ID
 $command .= " -dbname $dbname" if defined $dbname;
 
@@ -153,4 +160,6 @@
         exit($error_code);
     }
+} else {
+    print "skipping command: $command\n";
 }
 
@@ -173,17 +182,37 @@
     my $msg = shift; # Warning message on die
     my $exp_id = shift;
-    my $class_id = shift;
+    my $exp_name = shift;
+    my $tmp_class_id = shift;
     my $exit_code = shift;
 
+    # for failed imfiles, we insert UNKNOWN for inst, telescope, class_id
+
     carp($msg);
-    if ($exp_id && $class_id and not $no_update) {
+    if ($exp_id && $tmp_class_id and not $no_update) {
 	my $command = "$regtool -addprocessedimfile";
 	$command .= " -exp_id $exp_id";
-	$command .= " -class_id $class_id";
+	$command .= " -exp_name $exp_name";
+	$command .= " -tmp_class_id $tmp_class_id";
+	$command .= " -telescope UNKNOWN";
+	$command .= " -inst UNKNOWN";
+	$command .= " -class_id $tmp_class_id";
 	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
-###        system ($command);
+        system ($command);
     }
     exit $exit_code;
+}
+
+sub STATS_value_for_flag 
+{
+    my $STATS = shift;
+    my $flag  = shift;
+
+    foreach my $entry (@$STATS) {
+	if ($flag eq $entry->{flag}) {
+	    return $entry->{value};
+	}
+    }
+    return 'NAN';
 }
 
