Index: /branches/eam_branch_00/ippTasks/phase0.pro
===================================================================
--- /branches/eam_branch_00/ippTasks/phase0.pro	(revision 11109)
+++ /branches/eam_branch_00/ippTasks/phase0.pro	(revision 11110)
@@ -37,4 +37,7 @@
   periods      -exec $loadexec
   periods      -timeout 30
+
+  # XXX test that this option works
+  npending     1
 
   # silently drop stdout
@@ -100,9 +103,4 @@
     end
 
-    # XXX this is still a lame rule
-    $word = `basename $EXP_TAG | tr '.' ' '`
-    list word -split $word
-    $base = $word:0
-
     ## generate output log based on filerule
     $outroot = $OUTPATH/$EXP_TAG
Index: /trunk/ippScripts/scripts/Makefile.am
===================================================================
--- /trunk/ippScripts/scripts/Makefile.am	(revision 11109)
+++ /trunk/ippScripts/scripts/Makefile.am	(revision 11110)
@@ -16,3 +16,4 @@
 	phase2.pl \
 	phase3.pl \
+	ipp_filename.pl \
 	ipp_datapath.pl
Index: /trunk/ippScripts/scripts/inject.pl
===================================================================
--- /trunk/ippScripts/scripts/inject.pl	(revision 11109)
+++ /trunk/ippScripts/scripts/inject.pl	(revision 11110)
@@ -1,3 +1,6 @@
 #!/usr/bin/env perl
+
+# this program injects a list of single-file exposures into the db, taking the
+# filename (without .fits) as the exp_tag.
 
 use warnings;
@@ -7,134 +10,80 @@
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::List qw( parse_md_list );
-use Data::Dumper;
+use File::Spec;
+use File::Find::Rule;
+use PS::IPP::Config;
+my $ipprc = PS::IPP::Config->new();
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
-use Pod::Usage qw( pod2usage );
 
-my ($camera,			# Camera used
-    $telescope,			# Telescope used
-    $exp_type,			# Type of exposure
-    $path,			# Path to use
-    $inject_only		# Only inject (i.e., no phase 0)?
-    );
+# Parse the command-line arguments
+my ($outroot);
 GetOptions(
-	   'camera|c=s' => \$camera,
-	   'telescope|t=s' => \$telescope,
-	   'exp_type|e=s' => \$exp_type,
-	   'path=s' => \$path,
-	   'inject-only' => \$inject_only,
+    'outroot'     => \$outroot
 ) or pod2usage( 2 );
 
-pod2usage(
-	  -msg => "Required options: --camera --telescope --exp_type --path",
-	  -exitval => 3,
-	  ) unless scalar @ARGV == 0 or (defined $camera
-					 and defined $telescope
-					 and defined $exp_type
-					 and defined $path);
+pod2usage( -msg => "Usage: $0 [-outroot path] (files)", -exitval => 2 ) if scalar @ARGV == 0;
 
-my $mdcParser = PS::IPP::Metadata::Config->new;	# Metadata config parser
+my $pxinject = can_run('pxinject') or die "Can't find pxinject\n";
+my $ppStats = can_run('ppStats') or die "Can't find ppStats\n";
 
-# Look for programs we need
-my $missing_tools;
-my $pxinject = can_run('pxinject')  or (warn "Can't find pxinject" and $missing_tools = 1);
-my $p0tool = can_run('p0tool') or (warn "Can't find p0tool" and $missing_tools = 1);
-my $phase0_imfile = can_run('phase0_imfile.pl') or (warn "Can't find phase0_imfile.pl" and $missing_tools = 1);
-my $phase0_exp = can_run('phase0_exp.pl') or (warn "Can't find phase0_exp.pl" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
+# if outroot is not defined, assign the current path
+if (! $outroot) {
+    $outroot = File::Spec->rel2abs( "." );
+}
+my $relroot = $ipprc->convert_filename_relative( $outroot );
 
-# Inject new data into the database
-if (scalar @ARGV != 0) {
-    my @classes;
-    my $imfiles;
-    if ($camera eq "MEGACAM") {
-	for (my $i = 0; $i < 36; $i++) {
-	    push @classes, sprintf("ccd%02d", $i);
-	}
-    } elsif ($camera eq "MCSHORT") {
-	@classes = ( 'ccd12', 'ccd13', 'ccd14', 'ccd21', 'ccd22', 'ccd23' );
-    } elsif ($camera eq "CTIO_MOSAIC2") {
-	@classes = ();
-    } else {
-	die "Unrecognised camera name: $camera.\n";
-    }
-
-    foreach my $exp ( @ARGV ) {
-	my $command = "$pxinject -newExp -exp_id $exp -inst $camera -telescope $telescope -exp_type $exp_type -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;
-	
-	my @line = split(/\s+/, $$stdout_buf[0]); # The output line, containing the exposure tag
-	my $exp_tag = $line[2];	# The exposure tag
-	foreach my $class_id (@classes) {
-	    my $command = "$pxinject -newImfile -exp_tag $exp_tag -class chip -class_id $class_id -uri $path/$exp.$class_id.fits"; # Command to run
-	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-		run( command => $command, verbose => 1 );
-	    die "Unable to inject $exp $class_id: $error_code\n" if not $success;
-	}
-
-	if (scalar @classes == 0) {
-	    my $command = "$pxinject -newImfile -exp_tag $exp_tag -class fpa -class_id fpa -uri $path/$exp.fits"; # Command to run
-	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-		run( command => $command, verbose => 1 );
-	    die "Unable to inject $exp imfile: $error_code\n" if not $success;
-	}
-
-    }
+my $num = 0;
+foreach my $file ( @ARGV ) {
+    my $absfile = File::Spec->rel2abs( $file );
+    inject($absfile, $relroot);
+    $num ++;
 }
 
-exit 0 if $inject_only;
+print "$num files injected.\n";
 
-# Phase 0 imfile processing
+sub inject
 {
-    my $command = "$p0tool -pendingimfile"; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run( command => $command, verbose => 1 );
-    die "Unable to get phase 0 imfile list: $error_code\n" if not $success;
-    my $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
-	die "Unable to parse output from p0tool.\n";
+    my $absfile = shift;		# absolute path for this file
+    my $outroot  = shift;		# absolute path for output directory
 
-    foreach my $item (@$list) {
-	my $exp_tag = $item->{exp_tag};
-	my $class = $item->{class};
-	my $class_id = $item->{class_id};
-	my $uri = $item->{uri};
+    my ( $vol, $path, $name ) = File::Spec->splitpath( $absfile );
+    my ( $exp ) = $name =~ /(.*)\.fits/;
 
-	my $command = "$phase0_imfile --exp_tag $exp_tag --class $class --class_id $class_id --uri $uri";
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run( command => $command, verbose => 1 );
-	die "Unable to do phase 0 imfile processing on $exp_tag $class_id: $error_code\n" if not $success;
+    my $relfile = $ipprc->convert_filename_relative( $absfile );
+
+    my $command_type = "$ppStats -concept FPA.OBSTYPE -concept FPA.TELESCOPE -concept FPA.INSTRUMENT $absfile";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command_type, verbose => 1);
+    die "Unable to perform ppStats: $error_code\n" if not $success;
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) 
+        or die "unable to parse metadata config doc";
+
+    my $type;
+    my $telescope;
+    my $instrument;
+    foreach my $row (@$metadata) {
+	if ($row->{name} eq "FPA.TELESCOPE")  { $telescope = $row->{value}; }
+	if ($row->{name} eq "FPA.INSTRUMENT") { $instrument = $row->{value}; } # this entry must return the camera in the ipp/config system which we load
+	if ($row->{name} eq "FPA.OBSTYPE")    { $type = $row->{value}; }
     }
-}
+    
+    my $command_exp = "$pxinject -newExp -exp_id $exp -inst $instrument -telescope $telescope -exp_type $type -imfiles 1 -outroot $outroot" ; # Command to run
 
-# Phase 0 exposure processing
-{
-    my $command = "$p0tool -pendingexp"; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run( command => $command, verbose => 1 );
-    die "Unable to get phase 0 exposure list: $error_code\n" if not $success;
-    my $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
-	die "Unable to parse output from p0tool.\n";
-  
-    foreach my $item (@$list) {
-	my $exp_tag = $item->{exp_tag};
+    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: $error_code_exp\n" if not $success_exp;
+    
+    my @line = split(/\s+/, $$stdout_buf_exp[0]); # The output line, containing the exposure tag
+    my $exp_tag = $line[2];	# The exposure tag
+    
+    my $command_imfile = "$pxinject -newImfile -exp_tag $exp_tag -class fpa -class_id fpa -uri $relfile"; # Command to run
+    
+    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 imfile: $error_code_imfile\n" if not $success_imfile;
 
-	my $command = "$phase0_exp --exp_tag $exp_tag";
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run( command => $command, verbose => 1 );
-	die "Unable to do phase 0 exposure processing on $exp_tag: $error_code\n" if not $success;
-    }
-}
-
-END {
-    my $status = $?;
-    system("sync") == 0
-        or die "failed to execute sync: $!" ;
-    $? = $status;
+    return 1;
 }
 
 
 __END__
-
-
Index: /trunk/ippScripts/scripts/inject_phase0.pl
===================================================================
--- /trunk/ippScripts/scripts/inject_phase0.pl	(revision 11110)
+++ /trunk/ippScripts/scripts/inject_phase0.pl	(revision 11110)
@@ -0,0 +1,140 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use IPC::Cmd qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+use Data::Dumper;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($camera,			# Camera used
+    $telescope,			# Telescope used
+    $exp_type,			# Type of exposure
+    $path,			# Path to use
+    $inject_only		# Only inject (i.e., no phase 0)?
+    );
+GetOptions(
+	   'camera|c=s' => \$camera,
+	   'telescope|t=s' => \$telescope,
+	   'exp_type|e=s' => \$exp_type,
+	   'path=s' => \$path,
+	   'inject-only' => \$inject_only,
+) or pod2usage( 2 );
+
+pod2usage(
+	  -msg => "Required options: --camera --telescope --exp_type --path",
+	  -exitval => 3,
+	  ) unless scalar @ARGV == 0 or (defined $camera
+					 and defined $telescope
+					 and defined $exp_type
+					 and defined $path);
+
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Metadata config parser
+
+# Look for programs we need
+my $missing_tools;
+my $pxinject = can_run('pxinject')  or (warn "Can't find pxinject" and $missing_tools = 1);
+my $p0tool = can_run('p0tool') or (warn "Can't find p0tool" and $missing_tools = 1);
+my $phase0_imfile = can_run('phase0_imfile.pl') or (warn "Can't find phase0_imfile.pl" and $missing_tools = 1);
+my $phase0_exp = can_run('phase0_exp.pl') or (warn "Can't find phase0_exp.pl" and $missing_tools = 1);
+die "Can't find required tools.\n" if $missing_tools;
+
+# Inject new data into the database
+if (scalar @ARGV != 0) {
+    my @classes;
+    my $imfiles;
+    if ($camera eq "MEGACAM") {
+	for (my $i = 0; $i < 36; $i++) {
+	    push @classes, sprintf("ccd%02d", $i);
+	}
+    } elsif ($camera eq "MCSHORT") {
+	@classes = ( 'ccd12', 'ccd13', 'ccd14', 'ccd21', 'ccd22', 'ccd23' );
+    } elsif ($camera eq "CTIO_MOSAIC2") {
+	@classes = ();
+    } else {
+	die "Unrecognised camera name: $camera.\n";
+    }
+
+    foreach my $exp ( @ARGV ) {
+	my $command = "$pxinject -newExp -exp_id $exp -inst $camera -telescope $telescope -exp_type $exp_type -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;
+	
+	my @line = split(/\s+/, $$stdout_buf[0]); # The output line, containing the exposure tag
+	my $exp_tag = $line[2];	# The exposure tag
+	foreach my $class_id (@classes) {
+	    my $command = "$pxinject -newImfile -exp_tag $exp_tag -class chip -class_id $class_id -uri $path/$exp.$class_id.fits"; # Command to run
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run( command => $command, verbose => 1 );
+	    die "Unable to inject $exp $class_id: $error_code\n" if not $success;
+	}
+
+	if (scalar @classes == 0) {
+	    my $command = "$pxinject -newImfile -exp_tag $exp_tag -class fpa -class_id fpa -uri $path/$exp.fits"; # Command to run
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run( command => $command, verbose => 1 );
+	    die "Unable to inject $exp imfile: $error_code\n" if not $success;
+	}
+
+    }
+}
+
+exit 0 if $inject_only;
+
+# Phase 0 imfile processing
+{
+    my $command = "$p0tool -pendingimfile"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run( command => $command, verbose => 1 );
+    die "Unable to get phase 0 imfile list: $error_code\n" if not $success;
+    my $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
+	die "Unable to parse output from p0tool.\n";
+
+    foreach my $item (@$list) {
+	my $exp_tag = $item->{exp_tag};
+	my $class = $item->{class};
+	my $class_id = $item->{class_id};
+	my $uri = $item->{uri};
+
+	my $command = "$phase0_imfile --exp_tag $exp_tag --class $class --class_id $class_id --uri $uri";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run( command => $command, verbose => 1 );
+	die "Unable to do phase 0 imfile processing on $exp_tag $class_id: $error_code\n" if not $success;
+    }
+}
+
+# Phase 0 exposure processing
+{
+    my $command = "$p0tool -pendingexp"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run( command => $command, verbose => 1 );
+    die "Unable to get phase 0 exposure list: $error_code\n" if not $success;
+    my $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
+	die "Unable to parse output from p0tool.\n";
+  
+    foreach my $item (@$list) {
+	my $exp_tag = $item->{exp_tag};
+
+	my $command = "$phase0_exp --exp_tag $exp_tag";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run( command => $command, verbose => 1 );
+	die "Unable to do phase 0 exposure processing on $exp_tag: $error_code\n" if not $success;
+    }
+}
+
+END {
+    my $status = $?;
+    system("sync") == 0
+        or die "failed to execute sync: $!" ;
+    $? = $status;
+}
+
+
+__END__
+
+
Index: /trunk/ippScripts/scripts/ipp_filename.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_filename.pl	(revision 11110)
+++ /trunk/ippScripts/scripts/ipp_filename.pl	(revision 11110)
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use PS::IPP::Config;
+my $ipprc = PS::IPP::Config->new();
+
+die "USAGE: ipp_filename.pl (filerule) (output) (classID)\n" if scalar @ARGV != 3;
+
+my $filerule = $ARGV[0];
+my $output = $ARGV[1];
+my $classID = $ARGV[2];
+
+print $ipprc->convert_filename_absolute($filerule, $output, $classID) . "\n";
+
+1;
+
+__END__
Index: /trunk/ippScripts/scripts/phase2.pl
===================================================================
--- /trunk/ippScripts/scripts/phase2.pl	(revision 11109)
+++ /trunk/ippScripts/scripts/phase2.pl	(revision 11110)
@@ -22,4 +22,5 @@
     $input,			# Input FITS file
     $camera,			# Camera
+    $outpath,			# Outpath
     $no_update			# Don't update the database?
     );
@@ -29,4 +30,5 @@
     'uri|u=s'       => \$input,
     'camera|c=s'    => \$camera,
+    'outpath|o=s'   => \$outpath,
     'no-update'     => \$no_update
 ) or pod2usage( 2 );
@@ -34,9 +36,10 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --exp_tag --class_id --uri --camera",
+    -msg => "Required options: --exp_tag --class_id --uri --camera --outpath",
     -exitval => 3,
 ) unless defined $expTag 
     and defined $classId 
     and defined $input
+    and defined $outpath
     and defined $camera;
 
@@ -49,8 +52,10 @@
 die "Can't find required tools.\n" if $missing_tools;
 
+# XXX old verion Output file name
+# XXX old version (drop) : my ($vol, $dir, $file) = File::Spec->splitpath( $input );
+
 ### Output file name --- must match camera configuration!
-my ($vol, $dir, $file) = File::Spec->splitpath( $input );
 my $outputRoot =  $expTag . '.p2';
-$outputRoot = File::Spec->catpath( $vol, $dir, $outputRoot );
+$outputRoot = File::Spec->catpath( $outpath, $outputRoot );
 $outputRoot = $ipprc->convert_filename_absolute($outputRoot);
 $input = $ipprc->convert_filename_absolute ( $input );
Index: /trunk/ippScripts/scripts/phase3.pl
===================================================================
--- /trunk/ippScripts/scripts/phase3.pl	(revision 11109)
+++ /trunk/ippScripts/scripts/phase3.pl	(revision 11110)
@@ -19,8 +19,9 @@
 use Pod::Usage qw( pod2usage );
 
-my ($exp_tag, $camera, $no_update);
+my ($exp_tag, $camera, $outpath, $no_update);
 GetOptions(
     'exp_tag|e=s'       => \$exp_tag,
     'camera|c=s'        => \$camera,
+    'outpath|o=s'   => \$outpath,
     'no-update'         => \$no_update
 ) or pod2usage( 2 );
@@ -28,5 +29,5 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --exp_tag --camera",
+    -msg => "Required options: --exp_tag --camera --outpath",
     -exitval => 3,
 ) unless defined $exp_tag
@@ -95,10 +96,11 @@
 }
 
-my $example = ${$files}[0]->{b1_uri}; # Example filename
-my ($vol, $dir, $file) = File::Spec->splitpath( $example );
+# XXX old verison, drop
+#my $example = ${$files}[0]->{b1_uri}; # Example filename
+#my ($vol, $dir, $file) = File::Spec->splitpath( $example );
 
 # Generate the file list, and get the statistics
 my $outputRoot = $exp_tag . '.p3'; # Root output name
-$outputRoot = File::Spec->catpath( $vol, $dir, $outputRoot );
+$outputRoot = File::Spec->catpath( $outpath, $outputRoot );
 $outputRoot = $ipprc->convert_filename_absolute($outputRoot);
 my $list1Name = $outputRoot . '.b1.list'; # Name for the input file list for binning 1
Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 11109)
+++ /trunk/ippTools/src/dettool.c	(revision 11110)
@@ -5946,5 +5946,5 @@
 
     // XXX this is going in without checking that the det_id is valid.
-    // it seems like it be better to be using forgein key constraints rather
+    // it seems like it be better to be using foreign key constraints rather
     // then having to resort to a seperate query (and locking) to check that
     // the det_id is valid
