Index: /trunk/ippScripts/scripts/ipp_serial_inject_split.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_serial_inject_split.pl	(revision 14558)
+++ /trunk/ippScripts/scripts/ipp_serial_inject_split.pl	(revision 14559)
@@ -16,72 +16,100 @@
 
 use IPC::Cmd 0.36 qw( can_run run );
-use PS::IPP::Config qw( caturi );
+use File::Spec;
+use PS::IPP::Config;
+
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
 
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
 
-my ($camera, $telescope, $dbname, $workdir, $path);
+# Parse the command-line arguments
+my ($workdir, $dbname, $telescope, $camera, $reduction, $help);
 GetOptions(
+    	   'workdir|w=s'   => \$workdir, # working directory for output files
+    	   'dbname|d=s'    => \$dbname, # Database name
+	   'telescope|t=s' => \$telescope, # Telescope used      
 	   'camera|c=s'    => \$camera,    # Camera used	      
-	   'telescope|t=s' => \$telescope, # Telescope used      
-	   'workdir=s'     => \$workdir,   # Database name	      
-	   'path=s'        => \$path,	   # Working directory   
-	   'dbname=s'      => \$dbname,	   # Path to data        
+           'reduction=s'   => \$reduction, # user-supplied camera name
+           'help'          => \$help # give help listing
 ) or pod2usage( 2 );
+
+pod2usage( -msg => "inject one or many image exposures (directories) into the IPP pipeline database", 
+	   -exitval => 2) if 
+    defined $help;
+
+pod2usage( -msg => "Usage: $0 --telescope (name) --camera (name) [--workdir path] [--dbname dbname] (files)", 
+	   -exitval => 2 ) if 
+    scalar @ARGV == 0;
 
 pod2usage(
 	  -msg => "Required options: --camera --telescope --workdir --path --dbname",
-	  -exitval => 3,
-	  ) unless defined $camera
-    and defined $telescope
-    and defined $workdir
-    and defined $path
-    and defined $dbname;
-
-my $ipprc = PS::IPP::Config->new(); # IPP configuration
+	  -exitval => 3) unless 
+    defined $telescope and
+    defined $camera;
 
 # Look for programs we need
-my $missing_tools;
-my $pxinject = can_run('pxinject')  or (warn "Can't find pxinject" and $missing_tools = 1);
+my $pxinject = can_run('pxinject') or die "Can't find pxinject\n";
 
-if (scalar @ARGV == 0) {
-    die "No exposures provided.\n";
+# if workdir is not defined, assign the current path
+# XXX we need to handle relative paths for workdir (not allowed)
+if (! $workdir) {
+    $workdir = File::Spec->rel2abs( "." );
 }
 
-# Inject new data into the database
-my @classes;			# Names of the classes
-my @files;			# What to add to the filename for each class
-my $add_dir;			# Add directory name to get file name?
+my $num = 0;
+foreach my $filedir ( @ARGV ) {
+    # check for filedir existence
+    if (! -e $filedir) { die "file dir $filedir not found\n"; }
+    my $absfiledir = File::Spec->rel2abs( $filedir );
+    inject($absfiledir, $workdir, $dbname, $telescope, $camera);
+    $num ++;
+}
 
-foreach my $exp_name ( @ARGV ) {
-    my @files = <"$exp_name/*.fits">;
+sub inject
+{
+    my $absfiledir = shift;	# absolute path for this file
+    my $workdir  = shift;	# absolute path for output directory
+    my $dbname = shift;		# IPP database to use
+    my $telescope = shift;	# user-specified telescope
+    my $camera = shift;	# user-specified camera
+
+    # XXX provide an option for an alternative extension
+    my @files = <$absfiledir/*.fits>;
     my $Nfiles = scalar @files;
 
-    my $command = "$pxinject -newExp";
-    $command .= " -exp_name $exp_name";
-    $command .= " -inst $camera";
-    $command .= " -telescope $telescope";
-    $command .= " -workdir $workdir";
-    $command .= " -dbname $dbname" if defined $dbname;
+    print "absfiledir: $absfiledir\n";
+    print "Nfiles: $Nfiles\n";
 
-    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;
+    # the absfiledir is of the form path://PATH/data/foo/expname
+    my ( $vol, $path, $exp_name ) = File::Spec->splitpath( $absfiledir );
+
+    # 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;
+    $command_exp .= " -reduction $reduction" if defined $reduction;
+
+    my ( $success_exp, $error_code_exp, $full_buf_exp, $stdout_buf_exp, $stderr_buf_exp ) =
+	run( command => $command_exp, verbose => 1 );
+    die "Unable to inject $exp_name: $error_code_exp\n" if not $success_exp;
     
-    my @line = split(/\s+/, $$stdout_buf[0]); # The output line, containing the exposure tag
+    my @line = split(/\s+/, $$stdout_buf_exp[0]); # The output line, containing the exposure tag
     my $exp_id = $line[2];	# The exposure tag
 
-    my $class_id = 0;
-    foreach my $filename (@files) {
+    foreach my $absfile (@files) {
 
-	my $absfile = File::Spec->rel2abs( $filename );
 	my $relfile = $ipprc->convert_filename_relative( $absfile );
 
-	# XXX the class_id used here should be temporary : register should replace it with the true class_id
+	my ( $tmpvol, $tmppath, $filename ) = File::Spec->splitpath( $absfile );
+	my ( $class_name ) = $filename =~ /(.*)\.fits/;
+
+	# 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 .= " -exp_name $exp_name";
-	$command_imfile .= " -class chip";
-	$command_imfile .= " -class_id $class_id";
+	$command_imfile .= " -tmp_class_id $class_name";
 	$command_imfile .= " -uri $relfile";
 	$command_imfile .= " -dbname $dbname" if defined ($dbname);
@@ -89,28 +117,17 @@
 	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;
-
-	$class_id ++;
     }
 
-    # finalize the exposure
-    $command  = "$pxinject -updatenewExp";
-    $command .= " -exp_id $exp_id";
-    $command .= " -state run";
-    $command .= " -dbname $dbname" if defined $dbname;
+    # 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;
 
-    ( $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;
+    return 1;
 }
 
-END {
-    my $status = $?;
-system("sync") == 0
-    or die "failed to execute sync: $!" ;
-$? = $status;
-}
-
-
 __END__
-
-
