Index: trunk/tools/make_burntool_pcontrol.pl
===================================================================
--- trunk/tools/make_burntool_pcontrol.pl	(revision 25952)
+++ trunk/tools/make_burntool_pcontrol.pl	(revision 26334)
@@ -35,4 +35,20 @@
 		   ) or die "Unable to connect to database $DBI::errstr\n";
 
+# Determine what the value of "BURNTOOL.STATE.GOOD" currently is:
+$burntoolStateGood = 999;
+open(LAZY,"ppConfigDump -camera GPC1 -dump-camera - |") || die "Can't run ppConfigDump\n";
+while(<LAZY>) {
+    chomp;
+    if ($_ =~ /BURNTOOL.STATE.GOOD/) {
+        @line = split /\s+/;
+        $burntoolStateGood = $line[2];
+    }
+}
+close(LAZY);
+#unless( $burntoolStateGood == 999) {
+#    print STDERR "GOOD == $burntoolStateGood\n";
+#}
+
+
 # List of which obs_modes are "science" and which are something else.
 %science = ('MD' => 1, '3PI' => 1, 'STS' => 1, 'CAL' => 1, 'M31' => 1, 'SS' => 1,
@@ -41,8 +57,11 @@
     $science{Unknown} = 1;
 }
-# Zero the arrays.
+
+# Zero the arrays and counters
 @dates_min = ();
 @dates_max = ();
-
+$N_burntooled_images = 0;
+$N_total_images = 0;
+$N_ranges = 0;
 # Parse SQL query and fill any valid dates to the arrays
 if (exists($opt{Q})) {
@@ -66,4 +85,5 @@
     }
 }
+
 # Read a single date, and calculate the end and insert
 elsif (exists($opt{d})) {
@@ -73,4 +93,5 @@
     push @dates_max, sprintf("%04d-%02d-%02d",$year,$mon,$date);
 }
+
 # Just shift the supplied values into the arrays.
 else {
@@ -92,5 +113,5 @@
     print "##query: SELECT dateobs FROM rawExp WHERE $opt{Q}\n";
 }
-#for ($d = 0; $d <= $#dates_min; $d++) {
+
 while ($#dates_min > -1) {
     $date_min = shift(@dates_min);
@@ -138,14 +159,10 @@
 	    $science{$obs_mode} = 0;
 	}
-#%science = ('MD' => 1, '3PI' => 1, 'STS' => 1, 'CAL' => 1, 'M31' => 1, 'SS' => 1,
-#	    'ENGINEERING' => 0, 'NULL' => 0, 'Unknown' => 0, ' ' => 0);
-# I don't like this bit, but we need to work around the bad values of obs_mode that periodically crop up.
-# This isn't the most robust solution (which would of course be "having trustworthy obs_mode values"), but
-# it'll probably work for now.
 	if ((($science{$obs_mode} == 1)&&($comment !~ /Daytime/))||
 	    (($science{$obs_mode} == 0)&&(
 					  ($comment =~ /MD/)||($comment =~ /ThreePi/)||
 					  ($comment =~ /STS/)||($comment =~ /M31/)||
-					  ($comment =~ /CAL/)||($comment =~ /SS/)||($comment =~ /SVS/)|| ($comment =~ /Sweetspot/)|| ($comment =~ /cal/)|| ($comment =~ /virgo/)|| ($comment =~ /Virgo/)|| ($comment =~ /kepler/)||
+					  ($comment =~ /CAL/i)||($comment =~ /SS/)||($comment =~ /SVS/)||
+					  ($comment =~ /Sweetspot/)||($comment =~ /virgo/i)||($comment =~ /kepler/)||
 					  ($comment =~ /sdss/))))					  
 	{
@@ -174,4 +191,5 @@
 	next;
     }
+
     # Scan again and count how many exposures are between windows
     foreach $row_ref (@{ $data_ref }) {
@@ -212,7 +230,5 @@
 		}
 	    }
-#	    print ">>>$i $start_s[$i] $skips[$i]\n";
-	}
-#	print ">>> $dateobs\n";
+	}
     }
 
@@ -227,17 +243,36 @@
     }
     # Print out the end points in the format burntool wants.
+
     for ($i = 0; $i <= $#start_s; $i++) {
 	@start_t = localtime($start_s[$i]);
 	@end_t = localtime($end_s[$i]);
-	printf(" burntool %04d-%02d-%02dT%02d:%02d:%02d %04d-%02d-%02dT%02d:%02d:%02d\n",
-	       $start_t[5] + 1900,$start_t[4] + 1,$start_t[3],
-	       $start_t[2],$start_t[1],$start_t[0],
-	       $end_t[5] + 1900,$end_t[4] + 1,$end_t[3],
-	       $end_t[2],$end_t[1],$end_t[0]);
+	
+	my $date_min = sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
+			       $start_t[5] + 1900,$start_t[4] + 1,$start_t[3],
+			       $start_t[2],$start_t[1],$start_t[0]);
+	my $date_max = sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
+			       $end_t[5] + 1900,$end_t[4] + 1,$end_t[3],
+			       $end_t[2],$end_t[1],$end_t[0]);
+
+	my $total_images_sth = "SELECT count(exp_id) from rawImfile where dateobs >= '$date_min' AND dateobs <= '$date_max'";
+	my $im_ref = $db->selectall_arrayref( $total_images_sth );
+	$N_total_images += ${ $im_ref }[0][0];
+
+	my $burntooled_images_sth = "SELECT count(exp_id) from rawImfile where dateobs >= '$date_min' AND dateobs <= '$date_max' AND abs(burntool_state) = $burntoolStateGood";
+	$im_ref = $db->selectall_arrayref( $burntooled_images_sth );
+	$N_burntooled_images += ${ $im_ref }[0][0];
+	
+
+	printf(" burntool %s %s\n",$date_min,$date_max);
     }
     print "\n";
-}
-
-unless(exists($opt{b})) { 
+    $N_ranges += $#start_s;
+}
+
+if ($N_ranges == 0) {
+    print_short_epilogue();
+}
+
+unless(exists($opt{b}) || ($N_ranges == 0)) {
     print_epilogue();
 }
@@ -275,6 +310,16 @@
 return(0);
 }
+sub print_short_epilogue {
+    print << 'END_SHORT_EPILOGUE';
+
+    echo "There were no science exposures to process. Sorry, try again tomorrow."
+
+end
+END_SHORT_EPILOGUE
+}
 
 sub print_epilogue {
+    print " echo 'There were $N_total_images total images, of which $N_burntooled_images were already burntooled.'\n";
+    print "### BTSTAT $N_total_images $N_burntooled_images\n";
     print << 'END_EPILOGUE';
     
