Index: trunk/ippScripts/scripts/chip_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/chip_imfile.pl	(revision 35682)
+++ trunk/ippScripts/scripts/chip_imfile.pl	(revision 35685)
@@ -228,4 +228,6 @@
     ## XXX make the feature more general?
     my $useDeburnedImage = metadataLookupBool($recipeData, 'USE.DEBURNED.IMAGE');
+    my $ppImageApplyBurntool = metadataLookupBool($recipeData, 'APPLY.BURNTOOL');
+    my $burntoolArgument;
 
     if ($useDeburnedImage) {
@@ -328,84 +330,107 @@
         }
 
-        ## We now know that we have an image that has been burntooled.
-        my ($tempFile, $tempName) = tempfile( "/tmp/chip.$exp_id.$class_id.deburned.XXXX",
+        # if recipe option to have ppImage apply burntool is set and the burntool information is store in
+        # an external table, check the existence of that table file and set $burntoolArgument.
+        # If burntool state does not match fall through to the code that uses the burntool program
+        if ($ppImageApplyBurntool && ($burntoolState == -1 * $burntoolStateGood)) {
+            my $burntoolTable_uri = $uri;
+            $burntoolTable_uri =~ s/fits$/burn.tbl/;
+            my $burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
+            if ((!$burntoolTable_uriReal)||(!($ipprc->file_exists($burntoolTable_uri)))) {
+                my $repair_cmd = "$nebrepair $burntoolTable_uri";
+                my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
+                unless ($repair_success) {
+                    &my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+                }
+                $burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
+            }
+            unless ($ipprc->file_exists($burntoolTable_uri)) {
+                # Catch errors here
+                &my_die("Couldn't find burntool table: $burntoolTable_uri",$exp_id,$chip_id,$class_id, $PS_EXIT_SYS_ERROR);
+            }
+            $burntoolArgument = " -burntool $burntoolTable_uri";
+        } else {
+
+            ## We now know that we have an image that has been burntooled.
+            my ($tempFile, $tempName) = tempfile( "/tmp/chip.$exp_id.$class_id.deburned.XXXX",
                                               UNLINK => !$save_temps, SUFFIX => '.fits' );
 
-        # get the UNIX version of the (possible) neb: or path: filename
-        my $uriReal = $ipprc->file_resolve( $uri );
-	# Catch errors here
-	if (!$uriReal) {
-	    my $repair_cmd = "$nebrepair $uri";
-	    my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
-	    unless ($repair_success) {
-		&my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-	    }
-	    $uriReal = $ipprc->file_resolve( $uri );
-	}
-	    
-        &my_die("Unable to resolve $uri on $host", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR) if !$uriReal;
-
-        # funpack into the temp file.
-        my $funpack_cmd = "$funpack -S $uriReal > $tempName";
-        ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $funpack_cmd, verbose => $verbose);
-        unless ($success) {
-	    # Catch errors here
-	    my $repair_cmd = "$nebrepair $uri";
-	    my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
-	    unless ($repair_success) {
-		&my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-	    }
-	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $funpack_cmd, verbose => $verbose);
-	}
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform funpack: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-        }
-
-        ## Construct commands to apply the pre-calculated burntool trailfits to the pixel data.
-        my ($burntoolTable_uri, $burntoolTable_uriReal);
-        my $burntool_cmd = "$burntool ";
-
-        if ($burntoolState == -1 * $burntoolStateGood) {   # Burntool information stored in an external table.
-            $burntoolTable_uri = $uri;
-            $burntoolTable_uri =~ s/fits$/burn.tbl/;
-            $burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
-	    if ((!$burntoolTable_uriReal)||(!($ipprc->file_exists($burntoolTable_uri)))) {
-		my $repair_cmd = "$nebrepair $burntoolTable_uri";
-		my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
-		unless ($repair_success) {
-		    &my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-		}
-		$burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
-	    }
-            unless ($ipprc->file_exists($burntoolTable_uri)) {
-		# Catch errors here
-                &my_die("Couldn't find burntool table: $burntoolTable_uri",$exp_id,$chip_id,$class_id, $PS_EXIT_SYS_ERROR);
-            }
-
-            $burntool_cmd .= "$tempName in=${burntoolTable_uriReal} persist=t apply=t";
-        }
-        elsif ($burntoolState == $burntoolStateGood) { # Burntool information stored in a header table.
-            $burntool_cmd .= "$tempName persist=t apply=t";
-        }
-        else {
-            &my_die("Image data not properly burntooled, impossible state", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-        }
-
-        ## Run burntool to change the pixels of tempfile, and repoint $uri to that file.
-        ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
-            run(command => $burntool_cmd, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform burntool: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-        }
-
-        $uri = $tempName;
-        unless ($ipprc->file_exists($uri)) {
-            &my_die("Couldn't find deburned input file: $uri\n", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
-        }
-
-#       print STDERR "$uri $uriReal $tempName $burntoolTable_uri $burntoolTable_uriReal $burntool_cmd $save_temps\n";
-#       exit(100);
+            # get the UNIX version of the (possible) neb: or path: filename
+            my $uriReal = $ipprc->file_resolve( $uri );
+                # Catch errors here
+            if (!$uriReal) {
+                my $repair_cmd = "$nebrepair $uri";
+                my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
+                unless ($repair_success) {
+                    &my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+                }
+                $uriReal = $ipprc->file_resolve( $uri );
+            }
+                
+            &my_die("Unable to resolve $uri on $host", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR) if !$uriReal;
+
+            # funpack into the temp file.
+            my $funpack_cmd = "$funpack -S $uriReal > $tempName";
+            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $funpack_cmd, verbose => $verbose);
+            unless ($success) {
+                # Catch errors here
+                my $repair_cmd = "$nebrepair $uri";
+                my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
+                unless ($repair_success) {
+                    &my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+                }
+                ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $funpack_cmd, verbose => $verbose);
+            }
+            unless ($success) {
+                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform funpack: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+            }
+
+            ## Construct commands to apply the pre-calculated burntool trailfits to the pixel data.
+            my ($burntoolTable_uri, $burntoolTable_uriReal);
+            my $burntool_cmd = "$burntool ";
+
+            if ($burntoolState == -1 * $burntoolStateGood) {   # Burntool information stored in an external table.
+                $burntoolTable_uri = $uri;
+                $burntoolTable_uri =~ s/fits$/burn.tbl/;
+                $burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
+                if ((!$burntoolTable_uriReal)||(!($ipprc->file_exists($burntoolTable_uri)))) {
+                    my $repair_cmd = "$nebrepair $burntoolTable_uri";
+                    my ($repair_success, $repair_error_code, $repair_full_buf, $repair_stdout_buf, $repair_stderr_buf ) = run(command => $repair_cmd, verbose => $verbose);
+                    unless ($repair_success) {
+                        &my_die("Unable to attempt repair: $uri $repair_error_code", $exp_id,$chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+                    }
+                    $burntoolTable_uriReal = $ipprc->file_resolve( $burntoolTable_uri );
+                }
+                unless ($ipprc->file_exists($burntoolTable_uri)) {
+                    # Catch errors here
+                    &my_die("Couldn't find burntool table: $burntoolTable_uri",$exp_id,$chip_id,$class_id, $PS_EXIT_SYS_ERROR);
+                }
+
+                $burntool_cmd .= "$tempName in=${burntoolTable_uriReal} persist=t apply=t";
+            }
+            elsif ($burntoolState == $burntoolStateGood) { # Burntool information stored in a header table.
+                $burntool_cmd .= "$tempName persist=t apply=t";
+            }
+            else {
+                &my_die("Image data not properly burntooled, impossible state", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+            }
+
+            ## Run burntool to change the pixels of tempfile, and repoint $uri to that file.
+            ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
+                run(command => $burntool_cmd, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform burntool: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+            }
+
+            $uri = $tempName;
+            unless ($ipprc->file_exists($uri)) {
+                &my_die("Couldn't find deburned input file: $uri\n", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+            }
+
+    #       print STDERR "$uri $uriReal $tempName $burntoolTable_uri $burntoolTable_uriReal $burntool_cmd $save_temps\n";
+    #       exit(100);
+        }
     }
 
@@ -483,4 +508,5 @@
         $command .= " -R PPIMAGE.CHIP FITS.TYPE NONE";
     }
+    $command .= $burntoolArgument if $burntoolArgument;
     $command .= " -threads $threads" if defined $threads;
     $command .= " -image_id $chip_imfile_id" if defined $chip_imfile_id;
