Index: trunk/ippScripts/scripts/make_burntool_pcontrol.pl
===================================================================
--- trunk/ippScripts/scripts/make_burntool_pcontrol.pl	(revision 27033)
+++ trunk/ippScripts/scripts/make_burntool_pcontrol.pl	(revision 27865)
@@ -1,8 +1,12 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl 
 # script to generate a pcontrol.pro file to run burntool on a set of data.
 
 use DBI;
+use warnings;
 use DateTime;
 use Getopt::Std;
+use PS::IPP::Metadata::List qw( parse_md_list );
+use PS::IPP::Config 1.01 qw( :standard );
+use IPC::Cmd 0.36 qw( can_run run);
 
 use constant DB_SOCKET => '/var/run/mysqld/mysqld.sock';
@@ -35,7 +39,11 @@
 		   ) or die "Unable to connect to database $DBI::errstr\n";
 
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+if ($missing_tools) {
+    die "Cannot find required tools.";
+}
 # 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";
+open(LAZY,"$ppConfigDump -camera GPC1 -dump-camera - |") || die "Can't run ppConfigDump\n";
 while(<LAZY>) {
     chomp;
@@ -46,14 +54,54 @@
 }
 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,
-	    'ENGINEERING' => 0, 'NULL' => 0, 'Unknown' => 1, ' ' => 0);
+
+# Read the nightly science config file and use that to determine which data is science
+my $verbose = 0;
+my $conf_cmd = "$ppConfigDump -dump-recipe NIGHTLY_SCIENCE -";
+my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    run(command => $conf_cmd, verbose => $verbose);
+unless ($success) {
+    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+    &my_die("Unable to perform ppConfigDUmp: $error_code", $date, $PS_EXIT_SYS_ERROR);
+}
+
+my @target_list;
+my %object_list;
+my $comment_list;
+
+my $mdcParser = PS::IPP::Metadata::Config->new;
+my $metadata = $mdcParser->parse(join "", @$stdout_buf);
+foreach my $entry (@{ $metadata }) {
+    if (${ $entry }{name} eq 'TARGETS') {
+        my @target_data = @{ ${ $entry }{value} };
+        my $this_target = '';
+	
+        foreach my $tentry (@target_data) {
+            if (${ $tentry }{name} eq 'NAME') {
+                $this_target = ${ $tentry }{value};
+                push @target_list, $this_target;
+		$obsmode_list{$this_target} = '.*';
+		$object_list{$this_target} = '.*';
+		$comment_list{$this_target} = '.*';
+            }
+            elsif (${ $tentry }{name} eq 'OBSMODE') {
+                $obsmode_list{$this_target} = ${ $tentry }{value};
+		$obsmode_list{$this_target} =~ s/%//;
+            }
+            elsif (${ $tentry }{name} eq 'OBJECT') {
+                $object_list{$this_target} = ${ $tentry }{value};
+		$object_list{$this_target} =~ s/%//;
+            }
+            elsif (${ $tentry }{name} eq 'COMMENT') {
+                $comment_list{$this_target} = ${ $tentry }{value};
+		$comment_list{$this_target} =~ s/%//;
+            }
+	}
+    }
+}
 if (exists($opt{P})) {
-    $science{Unknown} = 1;
+    push @target_list, 'PR';
+    $obsmode_list{'PR'} = 'Unknown';
+    $object_list{'PR'} = '.*';
+    $comment_list{'PR'} = '.*';
 }
 
@@ -119,5 +167,5 @@
     
     # Find _ALL_ observations within the date range we care about
-    $sth = "SELECT exp_id,dateobs,pon_time,exp_type,obs_mode,comment FROM rawExp WHERE " .
+    $sth = "SELECT exp_id,dateobs,pon_time,exp_type,obs_mode,comment,object FROM rawExp WHERE " .
 	"dateobs >= '${date_min}' AND dateobs <= '${date_max}' order by dateobs";
 
@@ -133,9 +181,12 @@
     foreach $row_ref (@{ $data_ref }) {
 
-	($exp_id,$dateobs,$pontime,$exp_type,$obs_mode,$comment) = @{ $row_ref };
+	($exp_id,$dateobs,$pontime,$exp_type,$obs_mode,$comment,$object) = @{ $row_ref };
 	# Fix nulls in the database
 	if (!defined($obs_mode)) {
 	    $obs_mode = ' ';
 	}
+	if (!defined($object)) {
+	    $object = ' ';
+	}
 	if (!defined($comment)) {
 	    $comment = ' ';
@@ -144,5 +195,5 @@
 	($date,$time) = split / /, $dateobs;
 	($year,$month,$day) = split /-/, $date;
-	($hour,$minute,$second) = split /:/, $time;
+	($hour,$minute,$second) = split /:/, $time; # / # This stops emacs run-on syntax highlights.
 	
 	$dt = DateTime->new( year   => $year, month  => $month, day    => $day,
@@ -155,16 +206,5 @@
 	$et = $dt->epoch();
 
-
-	if (!exists($science{$obs_mode})) {
-	    $science{$obs_mode} = 0;
-	}
-	if ((($science{$obs_mode} == 1)&&($comment !~ /Daytime/))||
-	    (($science{$obs_mode} == 0)&&(
-					  ($comment =~ /MD/)||($comment =~ /ThreePi/)||
-					  ($comment =~ /STS/i)||($comment =~ /M31/)||($comment =~ /Stellar Transit/)||
-					  ($comment =~ /CAL/i)||($comment =~ /SS/)||($comment =~ /SVS/)||
-					  ($comment =~ /Sweetspot/)||($comment =~ /virgo/i)||($comment =~ /kepler/)||
-					  ($comment =~ /sdss/))))					  
-	{
+	if (is_science($exp_type,$obs_mode,$object,$comment)) {
 	    if ($start_s[-1] == 0) {
 		$start_s[-1] = $et - 1800;
@@ -194,5 +234,5 @@
     # Scan again and count how many exposures are between windows
     foreach $row_ref (@{ $data_ref }) {
-	($exp_id,$dateobs,$pontime,$exp_type,$obs_mode,$comment) = @{ $row_ref };
+	($exp_id,$dateobs,$pontime,$exp_type,$obs_mode,$comment,$object) = @{ $row_ref };
 	if (!defined($obs_mode)) {
 	    $obs_mode = ' ';
@@ -204,5 +244,5 @@
 	($date,$time) = split / /, $dateobs;
 	($year,$month,$day) = split /-/, $date;
-	($hour,$minute,$second) = split /:/, $time;
+	($hour,$minute,$second) = split /:/, $time; # / # another emacs syntax highlight bug.
 	
 	$dt = DateTime->new( year   => $year, month  => $month, day    => $day,
@@ -215,10 +255,6 @@
 	$et = $dt->epoch();
 
-	if (!exists($science{$obs_mode})) {
-	    die "No mode >>$obs_mode<<\n";
-	}
-	
 	for ($i = 0; $i <= $#start_s; $i++) {
-	    if (($et < $start_s[$i])&&($science{$obs_mode} == 0)) {
+	    if (($et < $start_s[$i])&&(is_science($exp_type,$obs_mode,$object,$comment) == 0)) {
 		if ($i == 0) {
 		    $skips[$i]++;
@@ -278,4 +314,26 @@
 unless(exists($opt{b}) || ($N_ranges == 0)) {
     print_epilogue();
+}
+
+
+# Subroutine to use the configuration file data and the exposure data to determine whether or not something is "science"
+sub is_science {
+#    if (is_science($exp_type,$obs_mode,$object,$comment)) {
+    my ($exp_type,$obs_mode,$object,$comment) = @_;
+    my $return_value = 0;
+    if ($exp_type eq 'OBJECT') {
+	foreach my $target (@target_list) {
+	    if (($obs_mode =~ /$obsmode_list{$target}/)&&
+		($object   =~ /$object_list{$target}/)&&
+		($comment  =~ /$comment_list{$target}/)) {
+		$return_value = 1;
+	    }
+#	    print ">$return_value $exp_type $obs_mode $object $comment $target $obsmode_list{$target} $object_list{$target} $comment_list{$target}\n";
+	    if ($return_value) {
+		return($return_value);
+	    }
+	}
+    }
+    return(0);
 }
 
