Index: trunk/ippScripts/scripts/chip_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/chip_imfile.pl	(revision 24549)
+++ trunk/ippScripts/scripts/chip_imfile.pl	(revision 25442)
@@ -19,4 +19,5 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
 use PS::IPP::Config 1.01 qw( :standard );
 use File::Temp qw( tempfile );
@@ -143,7 +144,115 @@
     ## XXX make the feature more general?
     my $useDeburnedImage = metadataLookupBool($recipeData, 'USE.DEBURNED.IMAGE');
-    if ($useDeburnedImage && $deburned) {
-        $uri =~ s/fits$/burn.fits/;
-        &my_die("Couldn't find deburned input file: $uri\n", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($uri);
+    if ($useDeburnedImage) {
+	## Check that we have required programs:
+#	print STDERR "Inside burntool loop!\n";
+	my $regtool  = can_run('regtool') or &my_die ("Can't find regtool", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	my $funpack  = can_run('funpack') or &my_die ("Can't find funpack", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	my $burntool = can_run('burntool') or &my_die ("Can't find burntool", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	
+	## Check the current burntool processing version:
+	my $regtool_state_cmd = "$regtool -processedimfile -exp_id $exp_id -class_id $class_id -limit 1";
+	if (defined($dbname)) {
+	    $regtool_state_cmd .= " -dbname $dbname";
+	}
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $regtool_state_cmd, verbose => $verbose);
+
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform regtool: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	}
+
+	my $regData = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse regtool metadata", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	my $regData2 = parse_md_list($regData);
+
+	my $burntoolState = 999;
+	foreach my $regEntry (@$regData2) {
+#	    print "$regEntry->{exp_id} $regEntry->{burntool_state}\n";
+
+	    if ($regEntry->{exp_id} == $exp_id) {
+		$burntoolState = $regEntry->{burntool_state};
+	    }
+	}
+	if ($burntoolState == 999) {
+	    &my_die("Unable to find burntool_state in metadata", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	}
+
+	## Read camera config to get the current good burntool state :
+	## XXX This is extremely slow. Any better way to do this?
+	my $camera_config_cmd = "$ppConfigDump -camera $camera -dump-camera -";
+	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $camera_config_cmd, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppConfigDump: $error_code", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	}
+
+	my $camData = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse ppConfigDump metadata", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+
+	my $burntoolStateGood;
+	foreach my $camEntry (@$camData) {
+	    if ($camEntry->{name} eq "BURNTOOL.STATE.GOOD") {
+		$burntoolStateGood = $camEntry->{value};
+	    }
+	}
+
+	if (abs($burntoolState) != $burntoolStateGood) {
+	    &my_die("Image burntool version does not match current accepted version.", $exp_id, $chip_id, $class_id, $PS_EXIT_SYS_ERROR);
+	}
+
+	## 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 );
+
+	# 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) {
+	    $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 );
+	    unless ($ipprc->file_exists($burntoolTable_uri)) {
+		&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} apply=t";
+	}
+	elsif ($burntoolState == $burntoolStateGood) { # Burntool information stored in a header table.
+	    $burntool_cmd .= "$tempName 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);	
     }
 
