Index: branches/pap/tools/ipp_apply_burntool.pl
===================================================================
--- branches/pap/tools/ipp_apply_burntool.pl	(revision 23948)
+++ branches/pap/tools/ipp_apply_burntool.pl	(revision 25027)
@@ -20,5 +20,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ( $class_id, $dateobs_begin, $dateobs_end, $skip_burned, $dbname, $verbose, $save_temps);
+my ( $class_id, $dateobs_begin, $dateobs_end, $skip_burned, $rerun_from_first, $dbname, $logfile, $verbose, $save_temps);
 GetOptions(
     'class_id=s'        => \$class_id, # chip identifier
@@ -26,5 +26,7 @@
     'dateobs_end=s'     => \$dateobs_end, # exposure date/time range stop
     'dbname|d=s'        => \$dbname, # Database name
+    'logfile=s'         => \$logfile,
     'skip_burned'       => \$skip_burned,   # Print to stdout
+    'rerun_from_first'  => \$rerun_from_first,   # Print to stdout
     'verbose'           => \$verbose,   # Print to stdout
     'save-temps'        => \$save_temps, # Save temporary files?
@@ -41,4 +43,6 @@
     defined $dbname;
 
+if ($skip_burned and $rerun_from_first) { &my_die("-rerun_from_first and -skip_burned are incompatible"); }
+
 my $missing_tools;
 my $regtool  = can_run('regtool')  or (warn "Can't find regtool" and $missing_tools = 1);
@@ -57,5 +61,5 @@
 $command .= " -dateobs_begin $dateobs_begin";
 $command .= " -dateobs_end $dateobs_end";
-# $command .= " -limit 20";
+$command .= " -ordered_by_date";
 $command .= " -dbname $dbname" if defined $dbname;
 
@@ -87,13 +91,39 @@
 my $ipprc = PS::IPP::Config->new() or my_die("Unable to set up");
 
+$ipprc->redirect_output($logfile) if $logfile;
+
 my $Nfiles = @files;
 print "files: $Nfiles\n";
 
 my $REALRUN = 1;
+my $RAWTABLES = 1;
+# XXX if we want to avoid using the raw tables in this script, we need to delay deletion of the tempfile until the next pass.
+
+# first pass: mark the db entries to catch failures
+# these will not be identified as burned by chip processing (user_1 > 0.5)
+if (! $skip_burned)  {
+    foreach my $file (@files) {
+	
+	# rerun_from_first treats the first image as already burned (if it is already burned)
+	# the artifact table from the first file is used for the rest of the sequence.
+	if ($rerun_from_first) {
+	    $skip_burned = 1;
+	    $rerun_from_first = 0;
+	    next;
+	}
+	my $exp_id = $file->{exp_id};
+
+	my $status;
+	$status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -user_1 0.1", 1);
+	if ($status) {
+	    &my_die("failed to update imfile");
+	}
+	$file->{user_1} = 0.1;
+    }
+}
 
 my $prevFileOpt = "";
 foreach my $file (@files) {
     my $exp_id = $file->{exp_id};
-    if ($skip_burned and ($file->{user_1} > 0.5)) { next; }
 
     my $rawImfile = $file->{uri};
@@ -104,5 +134,5 @@
 	($rawImfileReal) = $rawImfile =~ m|^neb:/(.*)|;
     }
-    # print "rawImfile: $rawImfile -> $rawImfileReal\n";
+    print "rawImfile: $rawImfile -> $rawImfileReal\n";
 
     # mangle name, create tmp file (always a UNIX file)
@@ -120,35 +150,52 @@
     # print "outImfile: $outImfile -> $outImfileReal\n";
 
-    # destination for the burntool artifacts
-    my $artImfile = $rawImfile;
-    $artImfile =~ s/fits$/burn.tbl/;
-    my $artImfileReal = $ipprc->file_resolve($artImfile, 1);
-    # print "artImfile: $artImfile -> $artImfileReal\n";
-
-    # uncompress the image (do we need to check if it is compressed?)
-    my $status = vsystem ("$funpack -S $rawImfileReal > $tmpImfileReal", $REALRUN);
-    if ($status) {
-	&my_die("failed on funpack");
-    }
-
-    $status = vsystem ("$burntool $tmpImfileReal out=$artImfileReal $prevFileOpt", $REALRUN);
-    if ($status) {
-	&my_die("failed on burntool");
-    }
-
-    # compress the image (do we need to check if it is compressed?)
-    $status = vsystem ("$fpack -S $tmpImfileReal > $outImfileReal", $REALRUN);
-    if ($status) {
-	&my_die("failed on fpack");
-    }
-
-    $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -user_1 1.0", 1);
-    if ($status) {
-	&my_die("failed to update imfile");
-    }
-
+    # burntool now can write the artifacts to the fits file.
+    # destination for the burntool artifacts.  use this if RAWTABLES is set.
+    my $artImfile;
+    my $artImfileReal;
+    if ($RAWTABLES) {
+	$artImfile = $rawImfile;
+	$artImfile =~ s/fits$/burn.tbl/;
+	$artImfileReal = $ipprc->file_resolve($artImfile, 1);
+	# print "artImfile: $artImfile -> $artImfileReal\n";
+    }
+
+    print "$rawImfile : $skip_burned, $file->{user_1}\n";
+
+    if (! ($skip_burned and ($file->{user_1} > 0.5))) {
+	print "running on: $rawImfile\n";
+        # uncompress the image (do we need to check if it is compressed?)
+        my $status = vsystem ("$funpack -S $rawImfileReal > $tmpImfileReal", $REALRUN);
+        if ($status) {
+            &my_die("failed on funpack");
+        }
+
+        if ($RAWTABLES) {
+            $status = vsystem ("$burntool $tmpImfileReal out=$artImfileReal $prevFileOpt", $REALRUN);
+        } else {
+            $status = vsystem ("$burntool $tmpImfileReal $prevFileOpt", $REALRUN);
+        }
+        if ($status) {
+            &my_die("failed on burntool");
+        }
+
+        # compress the image (do we need to check if it is compressed?)
+        $status = vsystem ("$fpack -S $tmpImfileReal > $outImfileReal", $REALRUN);
+        if ($status) {
+            &my_die("failed on fpack");
+        }
+
+        $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -user_1 1.0", 1);
+        if ($status) {
+            &my_die("failed to update imfile");
+        }
+        print "\n";
+    }
     # save the artifact file for the next image
-    $prevFileOpt = "in=$artImfileReal";
-    print "\n";
+    if ($RAWTABLES) {
+	$prevFileOpt = "in=$artImfileReal";
+    } else {
+	$prevFileOpt = "infits=$artImfileReal";
+    }
 }
 
