Index: trunk/ippScripts/scripts/phase3.pl
===================================================================
--- trunk/ippScripts/scripts/phase3.pl	(revision 11492)
+++ trunk/ippScripts/scripts/phase3.pl	(revision 11575)
@@ -132,12 +132,18 @@
 
 # Generate the file list, and get the statistics
-my $outputRoot = $exp_tag . '.p3'; # Root output name
-$outputRoot = File::Spec->catfile( $workdir, $outputRoot );
+my $outputFile =  "$exp_tag.p3";
+my $outputRoot = File::Spec->catfile( $workdir, $exp_tag, $outputFile );
+
 my $list1Name = $outputRoot . '.b1.list'; # Name for the input file list for binning 1
 my $list2Name = $outputRoot . '.b2.list'; # Name for the input file list for binning 2
+my $list3Name = $outputRoot . '.b3.list'; # Name for the input file list for chip astrometry
+
 my @means;			# Array of means
 my @stdevs;			# Array of stdevs
 open my $list1File, '>' . $list1Name;
 open my $list2File, '>' . $list2Name;
+open my $list3File, '>' . $list3Name;
+
+my $chipObjects; 
 foreach my $file (@$files) {
     # use the b1_uri as OUTPUT root and convert the filenames
@@ -145,12 +151,10 @@
     my $class_id = $file->{class_id};
     my $origRoot = $ipprc->convert_filename_absolute( $file->{b1_uri} ); # Original root name
+
+    # if there is only one chip, we use this name for the input to addstar
+    $chipObjects = $ipprc->filename("PSASTRO.OUTPUT", $origRoot, $class_id);
     print $list1File $ipprc->filename("PPIMAGE.BIN1", $origRoot, $class_id);
     print $list2File $ipprc->filename("PPIMAGE.BIN2", $origRoot, $class_id);
-    print $list1File $ipprc->filename("PSPHOT.OUTPUT", $origRoot, $class_id);
-    print $list1File $ipprc->filename("PSASTRO.OUTPUT", $origRoot, $class_id);
-    # XXX PSASTRO.OUTPUT is explicitly a CHIP output file 
-    # XXX below the PSASTRO.OUTPUT must be an FPA output file
-    # print $list1File ($ipprc->convert_filename_absolute($file->{b1_uri}) . "\n");
-    # print $list2File ($ipprc->convert_filename_absolute($file->{b2_uri}) . "\n");
+    print $list3File $chipObjects;
     push @means, $file->{bg};
     push @stdevs, $file->{bg_stdev};
@@ -158,16 +162,38 @@
 close $list1File;
 close $list2File;
+close $list3File;
 
 # Output products --- need to synch with the camera configuration!
 my $jpeg1 = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
 my $jpeg2 = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
-my $objects = $ipprc->filename("PSASTRO.OUTPUT", $outputRoot); # MEF psastro output
-
-# if file == CHIP
-#    run psastro +mosastro on PSASTRO.OUTPUT.CHIP
-#    run addstar on PSASTRO.OUTPUT.FPA
-
-# if file == FPA
-#    run addstar on PSASTRO.OUTPUT.CHIP
+my $fpaObjects = $ipprc->filename("PSASTRO.OUTPUT", $outputRoot); # MEF psastro output
+
+# run psastro +mosastro on the set of chips
+# XXX note that this is wrong if imfiles are cells
+if (@$files > 1) {
+    my $command = "$psastro -list $list3Name $outputRoot +mosastro -chipastro";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform psastro: $error_code", $exp_tag, $error_code);
+    }
+    &my_die("Unable to find expected output file: $fpaObjects", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $fpaObjects;
+} else {
+    $fpaObjects = $chipObjects;
+}
+
+# run addstar on either the single chip output or the single fpa output
+# XXX this construct requires the user to have a valid .ptolemyrc 
+# XXX which in turn points at ippconfig/dvo.site
+{
+    my $command = "addstar -D CAMERA $camera $fpaObjects";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform addstar: $error_code", $exp_tag, $error_code);
+    }
+}
 
 # Make the jpeg for binning 1
