Index: /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl
===================================================================
--- /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31416)
+++ /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31417)
@@ -6,5 +6,5 @@
 use Carp;
 use IPC::Cmd 0.36 qw( can_run run);
-use PS::IPP:Metadata::List qw( parse_md_list );
+use PS::IPP::Metadata::List qw( parse_md_list );
 use PS::IPP::Config 1.01 qw( :standard );
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
@@ -25,15 +25,79 @@
 my $laptool  = can_run('laptool') or (warn "Can't find laptool" and $missing_tools = 1);
 
-my ( $camera, $dbname, $logfile, $verbose, $debug);
-my ( $proj_cell, $filter);
-my ( $chip_mode, $diff_mode, $stack_mode, $qstack_mode, $magic_mode, $clean_mode);
-
-
-
+my ( $help, $verbose, $debug);
+my ( $camera, $dbname);
+my ( $lap_id );
+my ( $chip_mode, $monitor_mode, $cleanup_mode);
+
+GetOptions(
+    'help|h'       => \$help,
+    'verbose'      => \$verbose,
+    'debug'        => \$debug,
+
+    'camera=s'     => \$camera,
+    'dbname=s'     => \$dbname,
+    
+    'lap_id=s'     => \$lap_id,
+
+    'chip_mode'    => \$chip_mode,
+    'monitor_mode' => \$monitor_mode,
+    'cleanup_mode' => \$cleanup_mode,
+    ) or pod2usage ( 2 );
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Choose a mode: --chip_mode --monitor_mode --cleanup_mode",
+    -exitval => 3,
+    ) unless
+    defined $chip_mode or defined $monitor_mode or defined $cleanup_mode;
+pod2usage(
+    -msg => "--lap_id is required",
+    -exitval => 3,
+    ) unless
+    defined $lap_id;
+
+unless (defined $dbname) {
+    $dbname = 'gpc1';
+}
+
+my $mdcParser = PS::IPP::Metadata::Config->new;
+
+# Fetch all the information about this run
+my %lapRunInfo = get_lapRun_info($lap_id);
+
+# Run the appropriate mode
+
+if (defined($chip_mode)) {
+    unless ($lapRunInfo{state} eq 'new') {
+	&my_die("Cannot run chip_mode if lapRun.state != new!",$lap_id);
+    }
+    my $status = chip_mode($lap_id);
+    exit $status;
+}
+if (defined($monitor_mode)) {
+    unless ($lapRunInfo{state} eq 'run') {
+	&my_die("Cannot run monitor_mode if lapRun != run!", $lap_id);
+    }
+    my $status = monitor_mode($lap_id);
+    exit $status;
+}
+if (defined($cleanup_mode)) {
+    unless (($lapRunInfo{state} eq 'done')||
+	    ($lapRunInfo{state} eq 'drop')) {
+	&my_die("Cannot run cleanup_mode if lapRun != done!", $lap_id);
+    }
+    my $status = cleanup_mode($lap_id);
+    exit $status;
+}
 
 #
 # Chip
 ################################################################################
-# This queues a chipRun for this exposure, and records the chip_id in the exposure structure and in the database
+
+sub chip_mode {
+    my $lap_id = shift;
+    return(queue_chips($lap_id));
+}
+
+# This creates the chiptool command to use for the specified parameters.
 sub construct_chiptool_command {
     my $lap_id = shift;
@@ -45,5 +109,6 @@
     my $exp_id = shift;
 
-    my $cmd = "$chiptool ";
+    my $cmd = "$chiptool -pretend";
+    $cmd .= " -pretend " if defined $debug;
     $cmd .= " -dbname $dbname " if defined $dbname;
 
@@ -53,4 +118,6 @@
     return($cmd);
 }
+
+# retrieve the chip_id for the exposure inserted
 sub get_chip_id_from_metadata {
     my $exp_id = shift;
@@ -72,8 +139,32 @@
     # There should be only one.
     my $chip = ${ $chips }[0];
-    my $chip_id = $chip->{chip_id};
+    my $chip_id = 0;
+    if ($chip) {
+	$chip_id = $chip->{chip_id};
+    }
     
     return($chip_id);
 }
+
+sub get_lapRun_info {
+    my $lap_id = shift;
+    my $command = "$laptool -pendingrun -lap_id $lap_id";
+    $command .= " -dbname $dbname " if defined $dbname;
+    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform laptool: $error_code", $lap_id);
+    }
+    my $Runs = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from laptool", $lap_id);
+    # There should be only one.
+    my $Run = ${ $Runs }[0];
+    my %info = %{ $Run };
+    return(%info);
+}
+
+
+# Queue a chipRun for this exposure with the appropriate parameters.
 sub remake_this_exposure {
     my $exposure = shift;
@@ -82,4 +173,7 @@
     $utctime[5] += 1900;
     $utctime[4] += 1;
+
+    my $label = $exposure->{label};
+
     my $date = sprintf("%4d%02d%02d",$utctime[5],$utctime[4],$utctime[3]);
     my $workdir_date = sprintf("%4d/%02d/%02d",$utctime[5],$utctime[4],$utctime[3]);
@@ -92,5 +186,5 @@
 					      $exposure->{exp_id});
     my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+	run(command => $chip_cmd, verbose => $verbose);
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
@@ -98,5 +192,5 @@
     }
     
-    $exposure->{chip_id} = get_chip_id_from_metadata($exposure->{exp_id},$exposure->{label},$data_group); #join "", @$stdout_buf);
+    $exposure->{chip_id} = get_chip_id_from_metadata($exposure->{exp_id},$exposure->{label},$data_group); 
     $exposure->{data_state} = 'new';
     update_this_exposure($exposure);
@@ -106,7 +200,5 @@
 # This is the "user level" subroutine.
 sub queue_chips {
-    my $proj_cell = shift;
-
-    my $lap_id = get_lap_id($proj_cell);
+    my $lap_id = shift;
     
     my $command = "$laptool -pendingexp -lap_id $lap_id";
@@ -116,21 +208,21 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform laptool: $error_code", $re_id, $proj_cell);
-    }
-    
-    if ($@stdout_buf == 0) {
+	&my_die("Unable to perform laptool: $error_code", $lap_id);
+    }
+    
+    if (@$stdout_buf == 0) {
 	# Nothing to do.
 	return(0);
     }
     
-    $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata from laptool", $re_id, $proj_cell);
-
-    # Preliminary scan of exposures to set up diff structures.
+    my $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from laptool", $lap_id);
+
     my $counter = 0;
     my %matching = ();
     my %indexing = ();
-    foreach my $exposure (@$exposures) {
-	my $lap_id = $exposure->{lap_id};
+    # Determine which exposures need a chipRun queued.
+    foreach my $exposure (@$exposures) {  
+	# $lap_id = $exposure->{lap_id};  # This should be already known.
 	my $tess_id = $exposure->{tess_id};
 	my $exp_id = $exposure->{exp_id};
@@ -145,33 +237,25 @@
 	my $comment = $exposure->{comment};
 
-	if ($chip_id) { # We already have a defined chip_id
+	if (S64_IS_NOT_NULL($chip_id)) { # We already have a defined chip_id
 	    if (($pairwise) && !($pair_id)) {
 		# This is an error.
-		&my_die("Exposure $exp_id for $lap_id is declared pairwise without a defined pair", $lap_id, $proj_cell);
+		&my_die("Exposure $exp_id for $lap_id is declared pairwise without a defined pair", $lap_id);
 	    }
+	    $counter++; 
+	    next;
+	}
+	else { # We do not already have a chip_id.  
+	    # Make a chipRun, and update the exposure.
+	    $exposure = remake_this_exposure($exposure); 
+	    
+	    # Save our information for diff pairing.
+	    $matching{$object}{$comment} = $exp_id;
+	    $indexing{$exp_id} = $counter;
 	    $counter++;
-	    next;
-	}
-
-	$exposure = remake_this_exposure($exposure);
-# 	my $chip_cmd = construct_chiptool_command($lap_id,$proj_cell,$exp_id);
-# 	my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 	    run(command => $command, verbose => $verbose);
-# 	unless ($success) {
-# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-# 	    &my_die("Unable to perform chiptool: $error_code", $lap_id, $proj_cell);
-# 	}
-
-# 	$exposure->{chip_id} = get_chip_id_from_metadata(join "", @$stdout_buf);
-
-	# We will need to define a chipRun for this exposure.  However, let's do diff matching before we do so.
-	
-	$matching{$object}{$comment} = $exp_id;
-	$indexing{$exp_id} = $counter;
-	$counter++;
-    }
-    
-#    Pair up exposures to diff.
-    foreach my $object (keys %matching) {
+	}
+    }
+
+    # Determine which exposures have a pair for diffing.
+    foreach my $object (keys %matching) { 
 	my @exp_ids_to_diff = ();
 	foreach my $comment (keys %{ $matching{$object} }) {
@@ -184,5 +268,5 @@
 	}
 
-	while ($#{ $exp_ids_to_diff } > -1) {  # Save the pair information to the opposite exposure.
+	while ($#exp_ids_to_diff > -1) {  # Save the pair information to the opposite exposure.
 	    my $exp_id_A = shift(@exp_ids_to_diff);
 	    my $exp_id_B = shift(@exp_ids_to_diff);
@@ -198,7 +282,7 @@
 	}
     }
-	
-#    Queue chipRuns and update lapExp table.
-    foreach my $exposure (@$exposures) {
+
+    # Scan all exposures, and ensure that pairwise and private are set correctly
+    foreach my $exposure (@$exposures) { 
 	if ($exposure->{pairwise} && !($exposure->{pair_id})) {
 	    $exposure->{pairwise} = 0; # We marked it for pairwise diffs, but didn't match it. Probably an error.
@@ -208,17 +292,5 @@
 	}
 
-	update_this_exposure($exposure);
-# 	my $lapE_cmd = construct_laptool_updateExp_command($lap_id,
-# 							   $exposure->{exp_id},
-# 							   $exposure->{chip_id},
-# 							   $exposure->{private},
-# 							   $exposure->{pairwise},
-# 							   $exposure->{pair_id});
-# 	my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 	    run(command => $command, verbose => $verbose);
-# 	unless ($success) {
-# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-# 	    &my_die_with_fault("Unable to perform chiptool: $error_code", $lap_id, $proj_cell);
-# 	}
+	$exposure = update_this_exposure($exposure);
     }
 }
@@ -245,10 +317,10 @@
 
     ($defined_qstack,$have_qstack,$defined_fstack,$have_fstack) = (0,0,0,0);
-    if ($@stdout_buf == 0) {
+    if (@$stdout_buf == 0) {
 	# Nothing to do.  Possibly an error, but for now, just accept it
 	return($defined_qstack,$have_qstack,$defined_fstack,$have_fstack);
     }
 
-    $stacks = $mdcParser->parse_list(join "", @$stdout_buf) or
+    my $stacks = $mdcParser->parse_list(join "", @$stdout_buf) or
 	&my_die("Unable to parse metadata from laptool", $lap_id, "");
 
@@ -263,5 +335,5 @@
 	}
 	if ($stack->{final_stack_id}) {
-	    $defiend_fstack = 1;
+	    $defined_fstack = 1;
 	    $total_fstacks++;
 	}
@@ -296,17 +368,17 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform laptool: $error_code", $re_id, $proj_cell);
-    }
-    
-    if ($@stdout_buf == 0) {
+	&my_die("Unable to perform laptool: $error_code", $lap_id, $proj_cell);
+    }
+    
+    if (@$stdout_buf == 0) {
 	# Nothing to do. However, this is likely an error.
 	return(0);
     }
     
-    $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata from laptool", $re_id, $proj_cell);
+    my $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from laptool", $lap_id, $proj_cell);
 
     # Need to prescan to see who matches whom.
-    %match_hash = ();
+    my %match_hash = ();
     my $index = 0;
     foreach my $exposure (@$exposures) {
@@ -392,5 +464,5 @@
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform laptool: $error_code", $re_id, $proj_cell);
+	    &my_die("Unable to perform laptool: $error_code", $lap_id, $proj_cell);
 	}
     }
@@ -421,5 +493,5 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform chiptool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
+	&my_die("Unable to perform chiptool: $error_code", $lap_id);
     }
     
@@ -433,9 +505,9 @@
     $command .= " -dbname $dbname " if defined $dbname;
 
-    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform chiptool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
+    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform chiptool: $error_code", $lap_id);
     }
     
@@ -466,5 +538,5 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform chiptool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
+	&my_die("Unable to perform chiptool: $error_code", $lap_id);
     }
     
@@ -478,9 +550,9 @@
     $command .= " -dbname $dbname " if defined $dbname;
 
-    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform chiptool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
+    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform chiptool: $error_code", $lap_id);
     }
 
@@ -490,5 +562,5 @@
     
     # Need to prescan to see who matches whom.
-    %match_hash = ();
+    my %match_hash = ();
     my $index = 0;
     foreach my $exposure (@$exposures) {
@@ -508,11 +580,22 @@
 	}
 
-	my $command = "$difftoool ";
-	my $command .= " -dbname $dbname " if defined $dbname;
-	my $command .= " -set_label $label -set_workdir $workdir -set_data_group $data_group ";
-	my $command .= " -set_dist_group $dist_group ";
+	my @utctime = gmtime();
+	$utctime[5] += 1900;
+	$utctime[4] += 1;
+
+	my $label = $exposure->{label};
+	my $dist_group = $exposure->{dist_group};
+	my $date = sprintf("%4d%02d%02d",$utctime[5],$utctime[4],$utctime[3]);
+	my $workdir_date = sprintf("%4d/%02d/%02d",$utctime[5],$utctime[4],$utctime[3]);
+	my $workdir = "neb://\@HOST\@.0/${dbname}/${label}/${workdir_date}";
+	my $data_group = "${label}.${date}";
+
+	my $command = "$difftool ";
+	$command .= " -dbname $dbname " if defined $dbname;
+	$command .= " -set_label $label -set_workdir $workdir -set_data_group $data_group ";
+	$command .= " -set_dist_group $dist_group ";
 	
 	if ($exposure->{pairwise}) { # warpwarp
-	    $companion = ${ $exposures }[$match_hash{$exposure->{chip_id}}];
+	    my $companion = ${ $exposures }[$match_hash{$exposure->{chip_id}}];
 	    $command .= " -definewarpwarp ";
 	    $command .= "-input_label $label -template_label $label -backwards ";
@@ -539,5 +622,96 @@
     foreach my $exposure (@$exposures) {
 	$exposure->{active} = 0;
-	$exposure = update_this_exposure($exposure);
+	update_this_exposure($exposure);
+    }
+}
+
+
+#
+# Cleanup
+################################################################################
+
+# sub cleanup_exposures {
+#     my $command = "$laptool -inactive ";
+#     $command .= " -dbname $dbname " if defined $dbname;
+
+#     my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+# 	run(command => $command, verbose => $verbose);
+#     unless ($success) {
+# 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+# 	&my_die("Unable to perform laptool: $error_code", "none", "none");
+#     }
+#     if (@$stdout_buf == 0) {
+# 	# Nothing to do.
+# 	return(0);
+#     }
+    
+#     my $exposures = $mdcParser->parse_list(join "", @$stdout_buf) or
+# 	&my_die("Unable to parse metadata from laptool", $lap_id);
+
+#     foreach my $exposure (@$exposures) {
+# 	foreach my $clean_mode (@clean_modes) {
+# 	    $command = $clean_mode->{COMMAND};
+# 	    $clean_id = $clean_mode->{CLEAN_ID};
+# 	    $clean_state = $clean_mode->{CLEAN_STATE};
+# 	    if (($exposure->{$clean_id} != 0)&&
+# 		(($exposure->{$clean_state} eq 'full')||
+# 		 ($exposure->{$clean_state} eq 'drop'))) {
+
+# 		$command =~ s/\@DBNAME\@/$dbname/g;
+# 		$command =~ s/\@CHIP_ID\@/$exposure->{chip_id}/;
+# 		$command =~ s/\@CHIP_STATE\@/$exposure->{chip_state}/;
+# 		$command =~ s/\@CHIP_LABEL\@/$exposure->{chip_label}/;
+		
+# 		$command =~ s/\@WARP_ID\@/$exposure->{chip_id}/;
+# 		$command =~ s/\@WARP_STATE\@/$exposure->{chip_state}/;
+# 		$command =~ s/\@WARP_LABEL\@/$exposure->{chip_label}/;
+		
+# 		$command =~ s/\@DIFF_ID\@/$exposure->{diff_id}/;
+# 		$command =~ s/\@DIFF_STATE\@/$exposure->{diff_state}/;
+# 		$command =~ s/\@DIFF_LABEL\@/$exposure->{diff_label}/;
+		
+# 		$command =~ s/\@MAGICDS_ID\@/$exposure->{magicDS_id}/;
+# 		$command =~ s/\@MAGICDS_STATE\@/$exposure->{magicDS_state}/;
+# 		$command =~ s/\@MAGICDS_LABEL\@/$exposure->{magicDS_label}/;
+		
+# 		($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+# 		    run(command => $command, verbose => $verbose);
+# 		unless ($success) {
+# 		    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+# 		    &my_die("Unable to perform laptool: $error_code", "none", "none");
+# 		}
+# 	    }
+# 	}
+# 	$exposure->{data_state} = 'cleaned';
+# 	update_this_exposure($exposure);
+#     }
+# }
+    
+	    
+
+
+#
+# Utilities
+################################################################################
+
+sub my_die {
+    my $msg = shift; # Warning message on die
+    my $lap_id = shift; # identifier
+    my $optional = shift;
+    my $exit_code = shift;
+    unless (defined $exit_code) {
+	$exit_code = $PS_EXIT_PROG_ERROR;
+    }
+    carp($msg);
+    exit $exit_code;
+}
+
+# Check to see if a 64bit integer is NULL or not.  This is probably fragile, but I don't know how to get a S64 NULL out.
+sub S64_IS_NOT_NULL {
+    if ($_[0] == 9223372036854775807) {
+	return(0);
+    }
+    else {
+	return(1);
     }
 }
@@ -587,70 +761,4 @@
 	&my_die("Unable to perform laptool: $error_code", $exposure->{lap_id}, $exposure->{proj_cell});
     }
-    
-}
-
-#
-# Cleanup
-################################################################################
-
-sub cleanup_exposures {
-    my $command = "$laptool -inactive ";
-    $command .= " -dbname $dbname " if defined $dbname;
-
-    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform laptool: $error_code", "none", "none");
-    }
-    if ($@stdout_buf == 0) {
-	# Nothing to do.
-	return(0);
-    }
-
-    foreach my $exposure (@$exposures) {
-	foreach my $clean_mode (@clean_modes) {
-	    $command = $clean_mode->{COMMAND};
-	    $clean_id = $clean_mode->{CLEAN_ID};
-	    $clean_state = $clean_mode->{CLEAN_STATE};
-	    if (($exposure->{$clean_id} != 0)&&
-		(($exposure->{$clean_state} eq 'full')||
-		 ($exposure->{$clean_state} eq 'drop'))) {
-
-		$command =~ s/@DBNAME@/$dbname/g;
-		$command =~ s/@CHIP_ID@/$exposure->{chip_id}/;
-		$command =~ s/@CHIP_STATE@/$exposure->{chip_state}/;
-		$command =~ s/@CHIP_LABEL@/$exposure->{chip_label}/;
-		
-		$command =~ s/@WARP_ID@/$exposure->{chip_id}/;
-		$command =~ s/@WARP_STATE@/$exposure->{chip_state}/;
-		$command =~ s/@WARP_LABEL@/$exposure->{chip_label}/;
-		
-		$command =~ s/@DIFF_ID@/$exposure->{diff_id}/;
-		$command =~ s/@DIFF_STATE@/$exposure->{diff_state}/;
-		$command =~ s/@DIFF_LABEL@/$exposure->{diff_label}/;
-		
-		$command =~ s/@MAGICDS_ID@/$exposure->{magicDS_id}/;
-		$command =~ s/@MAGICDS_STATE@/$exposure->{magicDS_state}/;
-		$command =~ s/@MAGICDS_LABEL@/$exposure->{magicDS_label}/;
-		
-		($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-		    run(command => $command, verbose => $verbose);
-		unless ($success) {
-		    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-		    &my_die("Unable to perform laptool: $error_code", "none", "none");
-		}
-	    }
-	}
-	$exposure->{data_state} = 'cleaned';
-	update_this_exposure($exposure);
-    }
-}
-    
-	    
-
-
-#
-# Utilities
-################################################################################
-
+}    
+
Index: /branches/czw_branch/20110406/ippTasks/lap.pro
===================================================================
--- /branches/czw_branch/20110406/ippTasks/lap.pro	(revision 31416)
+++ /branches/czw_branch/20110406/ippTasks/lap.pro	(revision 31417)
@@ -1,93 +1,49 @@
-## reprocessing.pro : -*- sh -*-
+## lap.pro : -*- sh -*-
 
 check.globals
 
-macro reprocessing.on
-  re.chip.on
-  re.diff.on
-  re.stack.on
-  re.magic.on
-  re.clean.on
-end
-
-macro reprocessing.off
-  re.chip.off
-  re.diff.off
-  re.stack.off
-  re.magic.off
-  re.clean.off
-end
-
-macro re.on
-  reprocessing.on
-end
-
-macro re.off
-  reprocessing.off
-end
-
-macro re.chip.on
-  task re.chip.load
-    active true
-  end
-end
-
-macro re.diff.on
-  task re.diff.load
-    active true
-  end
-end
-
-macro re.stack.on
-  task re.stack.load
-    active true
-  end
-end
-
-macro re.magic.on
-  task re.magic.load
-    active true
-  end
-end
-
-macro re.clean.on
-  task re.clean.load
-    active true
-  end
-end
-
-macro re.chip.off
-  task re.chip.load
-    active false
-  end
-end
-
-macro re.diff.off
-  task re.diff.load
-    active false
-  end
-end
-
-macro re.stack.off
-  task re.stack.load
-    active false
-  end
-end
-
-macro re.magic.off
-  task re.magic.load
-    active false
-  end
-end
-
-macro re.clean.off
-  task re.clean.load
-    active false
-  end
-end
-
-book init reData
-
-task           re.chip.load
+$lapSeq:n = 0
+
+book init lapRuns
+
+macro lap.add.sequence
+  if ($0 != 2) 
+    echo "USAGE: lap.add.sequence (seq_id)"
+    break
+  end
+  if ($?lapSeq:n == 0) 
+    list lapSeq -add $1
+    return
+  end
+
+  local found
+  $found = 0
+  for i 0 $lapSeq:n
+    if ($lapSeq:$i == $1)
+      $found = 1
+      echo "$lapSeq:$i set"
+      last
+    end
+  end
+
+  if ($found == 0)
+    list lapSeq -add $1
+  end
+end
+
+macro lap.del.sequence
+  if ($0 != 2)
+    echo "USAGE: lap.del.sequence (seq_id)"
+    break
+  end
+  if ($?lapSeq:n == 0)
+    return
+  end
+
+  list lapSeq -del $1
+end
+ 
+
+task           lap.initial.load
   host         local
   periods      -poll $LOADPOLL
@@ -98,33 +54,105 @@
   task.exec
     stdout NULL
-    stderr $LOGDEIR/re.chip.log
-
-    $run = echo no command yet
-    command $run
-  end
-  # success
-  task.exit  0
-    book delpage reData $options:0
-    ipptool2book stdout reData -uniq -key proj
-
-    if ($VERBOSE > 2)
-      book listbook reData
-    end
-  end
-  # locked list
-  task.exit    default
-    showcommand failure
-  end
-  task.exit    crash
-    showcommand crash
-  end
-  #operation times out?
-  task.exit    timeout
-    showcommand timeout
-  end
-end
-
-
-task           re.diff.load
+    stderr $LOGDIR/lap.load.log
+
+    $run = laptool -pendingrun -state new
+
+    if ($lapSeq:n == 0)
+      break
+    else 
+      option $lapSeq:$lap_N
+      $run = $run -seq_id $lapSeq:$lap_N
+      $lap_N ++
+      if ($lap_N >= $lapSeq:n) set lap_N = 0
+    end
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      option $DB:$lap_DB
+      $run = $run -dbname $DB:$lap_DB
+      $lap_DB ++
+      if ($lap_DB >= $DB:n) set lap_DB = 0
+    end
+
+    command $run
+  end
+  # success
+  task.exit  0
+    book delpage lapNewRuns $options:0
+    ipptool2book stdout lapNewRuns -uniq -key proj
+
+    if ($VERBOSE > 2)
+      book listbook lapNewRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task           lap.initial.run
+  host         local
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 600
+# This can probably be increased and spread over hosts in the future.
+  npending     1            
+
+  task.exec
+    stdout NULL
+    stderr $LOGDIR/lap.initial.log
+
+    book npages lapNewRuns -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+
+
+    book getpage lapNewRuns 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword lapNewRuns $pageName pantaskState RUN
+    book getword lapNewRuns $pageName lap_id -var LAP_ID
+    book getword lapNewRuns $pageName dbname -var DBNAME
+
+    $run = lap_science.pl --chip_mode --dbname $DBNAME --lap_id $LAP_ID
+
+    command $run
+
+  end
+
+  # success
+  task.exit  0
+    book delpage lapNewRuns $options:0
+    ipptool2book stdout lapNewRuns -uniq -key proj
+
+    if ($VERBOSE > 2)
+      book listbook lapNewRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+
+
+task           lap.monitor.load
   host         local
   periods      -poll $LOADPOLL
@@ -135,32 +163,105 @@
   task.exec
     stdout NULL
-    stderr $LOGDEIR/re.diff.log
-
-    $run = echo no command yet
-    command $run
-  end
-  # success
-  task.exit  0
-    book delpage reData $options:0
-    ipptool2book stdout reData -uniq -key proj
-
-    if ($VERBOSE > 2)
-      book listbook reData
-    end
-  end
-  # locked list
-  task.exit    default
-    showcommand failure
-  end
-  task.exit    crash
-    showcommand crash
-  end
-  #operation times out?
-  task.exit    timeout
-    showcommand timeout
-  end
-end
-
-task           re.magic.load
+    stderr $LOGDIR/lap.load.log
+
+    $run = laptool -pendingrun -state run
+
+    if ($lapSeq:n != 0)
+      option $lapSeq:$lap_N
+      $run = $run -seq_id $lapSeq:$lap_N
+      $lap_N ++
+      if ($lap_N >= $lapSeq:n) set lap_N = 0
+    end
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      option $DB:$lap_DB
+      $run = $run -dbname $DB:$lap_DB
+      $lap_DB ++
+      if ($lap_DB >= $DB:n) set lap_DB = 0
+    end
+
+    add_poll_labels run
+
+    command $run
+  end
+  # success
+  task.exit  0
+    book delpage lapRuRuns $options:0
+    ipptool2book stdout lapRunRuns -uniq -key lap_id
+
+    if ($VERBOSE > 2)
+      book listbook lapRunRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task           lap.monitor.run
+  host         local
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 600
+# This can probably be increased and spread over hosts in the future.
+  npending     1            
+
+  task.exec
+    stdout NULL
+    stderr $LOGDIR/lap.initial.log
+
+    book npages lapRunRuns -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+
+
+    book getpage lapNewRuns 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword lapNewRuns $pageName pantaskState RUN
+    book getword lapNewRuns $pageName lap_id -var LAP_ID
+    book getword lapNewRuns $pageName dbname -var DBNAME
+
+    $run = lap_science.pl --monitor_mode --dbname $DBNAME --lap_id $LAP_ID
+
+    command $run
+
+  end
+
+  # success
+  task.exit  0
+    book delpage lapRunRuns $options:0
+    ipptool2book stdout lapRunRuns -uniq -key proj
+
+    if ($VERBOSE > 2)
+      book listbook lapRunRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+
+
+task           lap.cleanup.load
   host         local
   periods      -poll $LOADPOLL
@@ -171,100 +272,99 @@
   task.exec
     stdout NULL
-    stderr $LOGDEIR/re.magic.log
-
-    $run = echo no command yet
-    command $run
-  end
-  # success
-  task.exit  0
-    book delpage reData $options:0
-    ipptool2book stdout reData -uniq -key proj
-
-    if ($VERBOSE > 2)
-      book listbook reData
-    end
-  end
-  # locked list
-  task.exit    default
-    showcommand failure
-  end
-  task.exit    crash
-    showcommand crash
-  end
-  #operation times out?
-  task.exit    timeout
-    showcommand timeout
-  end
-end
-
-task           re.stack.load
-  host         local
-  periods      -poll $LOADPOLL
-  periods      -exec $LOADEXEC
-  periods      -timeout 30
-  npending     1
-
-  task.exec
-    stdout NULL
-    stderr $LOGDEIR/re.stack.log
-
-    $run = echo no command yet
-    command $run
-  end
-  # success
-  task.exit  0
-    book delpage reData $options:0
-    ipptool2book stdout reData -uniq -key proj
-
-    if ($VERBOSE > 2)
-      book listbook reData
-    end
-  end
-  # locked list
-  task.exit    default
-    showcommand failure
-  end
-  task.exit    crash
-    showcommand crash
-  end
-  #operation times out?
-  task.exit    timeout
-    showcommand timeout
-  end
-end
-
-task           re.clean.load
-  host         local
-  periods      -poll $LOADPOLL
-  periods      -exec $LOADEXEC
-  periods      -timeout 30
-  npending     1
-
-  task.exec
-    stdout NULL
-    stderr $LOGDEIR/re.clean.log
-
-    $run = echo no command yet
-    command $run
-  end
-  # success
-  task.exit  0
-    book delpage reData $options:0
-    ipptool2book stdout reData -uniq -key proj
-
-    if ($VERBOSE > 2)
-      book listbook reData
-    end
-  end
-  # locked list
-  task.exit    default
-    showcommand failure
-  end
-  task.exit    crash
-    showcommand crash
-  end
-  #operation times out?
-  task.exit    timeout
-    showcommand timeout
-  end
-end
+    stderr $LOGDIR/lap.load.log
+
+    $run = laptool -pendingrun -state done
+
+    if ($lapSeq:n != 0)
+      option $lapSeq:$lap_N
+      $run = $run -seq_id $lapSeq:$lap_N
+      $lap_N ++
+      if ($lap_N >= $lapSeq:n) set lap_N = 0
+    end
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      option $DB:$lap_DB
+      $run = $run -dbname $DB:$lap_DB
+      $lap_DB ++
+      if ($lap_DB >= $DB:n) set lap_DB = 0
+    end
+
+    add_poll_labels run
+
+    command $run
+  end
+  # success
+  task.exit  0
+    book delpage lapDoneRuns $options:0
+    ipptool2book stdout lapDoneRuns -uniq -key lap_id
+
+    if ($VERBOSE > 2)
+      book listbook lapRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task           lap.cleanup.run
+  host         local
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 600
+# This can probably be increased and spread over hosts in the future.
+  npending     1            
+
+  task.exec
+    stdout NULL
+    stderr $LOGDIR/lap.cleanup.log
+
+    book npages lapNewRuns -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+
+
+    book getpage lapNewRuns 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword lapDoneRuns $pageName pantaskState RUN
+    book getword lapDoneRuns $pageName lap_id -var LAP_ID
+    book getword lapDoneRuns $pageName dbname -var DBNAME
+
+    $run = lap_science.pl --cleanup_mode --dbname $DBNAME --lap_id $LAP_ID
+
+    command $run
+
+  end
+
+  # success
+  task.exit  0
+    book delpage lapDoneRuns $options:0
+    ipptool2book stdout lapDoneRuns -uniq -key proj
+
+    if ($VERBOSE > 2)
+      book listbook lapDoneRuns
+    end
+  end
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+  task.exit    crash
+    showcommand crash
+  end
+  #operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
Index: /branches/czw_branch/20110406/ippTools/src/laptool.c
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31416)
+++ /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31417)
@@ -740,7 +740,8 @@
     return(true);
   }
-  else {
-    return(false);
-  }  
+/*   else { */
+/*     return(false); */
+/*   }   */
+  return(true);
 }
 
