Index: trunk/ippScripts/scripts/automate_stacks.pl
===================================================================
--- trunk/ippScripts/scripts/automate_stacks.pl	(revision 26986)
+++ trunk/ippScripts/scripts/automate_stacks.pl	(revision 26988)
@@ -30,17 +30,17 @@
 # my @filter_list = ('g.00000','r.00000','i.00000','z.00000','y.00000');
 # my @target_list = ('CMB','M31','MD01','MD02','MD03','MD04','MD05','MD06','MD07','MD08','MD09','MD10',
-# 		   'STS','SVS','SweetSpot','ThreePi');
+#                  'STS','SVS','SweetSpot','ThreePi');
 # my %tessID_list = ('CMB' => 'RINGS.V0', 'M31' => 'M31', 'MD01' => 'MD01', 'MD02' => 'MD02',
-# 		   'MD03' => 'MD03', 'MD04' => 'MD04', 'MD05' => 'MD05', 'MD06' => 'MD06',
-# 		   'MD07' => 'MD07', 'MD08' => 'MD08', 'MD09' => 'MD09', 'MD10' => 'MD10',
-# 		   'STS' => 'STS', 'SVS' => 'RINGS.V0', 'SweetSpot' => 'RINGS.V0', 'ThreePi' => 'RINGS.V0');
+#                  'MD03' => 'MD03', 'MD04' => 'MD04', 'MD05' => 'MD05', 'MD06' => 'MD06',
+#                  'MD07' => 'MD07', 'MD08' => 'MD08', 'MD09' => 'MD09', 'MD10' => 'MD10',
+#                  'STS' => 'STS', 'SVS' => 'RINGS.V0', 'SweetSpot' => 'RINGS.V0', 'ThreePi' => 'RINGS.V0');
 # my %comment_list = ('CMB' => 'CMB_Cold%', 'M31' => 'M31%', 'MD01' => 'MD01%', 'MD02' => 'MD02%',
-# 		    'MD03' => 'MD03%', 'MD04' => 'MD04%', 'MD05' => 'MD05%', 'MD06' => 'MD06%',
-# 		    'MD07' => 'MD07%', 'MD08' => 'MD08%', 'MD09' => 'MD09%', 'MD10' => 'MD10%',
-# 		    'STS' => 'Stellar Transit%', 'SVS' => 'SVS%', 'SweetSpot' => 'Sweetspot%', 'ThreePi' => 'ThreePi%');
+#                   'MD03' => 'MD03%', 'MD04' => 'MD04%', 'MD05' => 'MD05%', 'MD06' => 'MD06%',
+#                   'MD07' => 'MD07%', 'MD08' => 'MD08%', 'MD09' => 'MD09%', 'MD10' => 'MD10%',
+#                   'STS' => 'Stellar Transit%', 'SVS' => 'SVS%', 'SweetSpot' => 'Sweetspot%', 'ThreePi' => 'ThreePi%');
 # my %stackable_list = ('CMB' => 0, 'M31' => 1, 'MD01' => 1, 'MD02' => 1,
-# 		    'MD03' => 1, 'MD04' => 1, 'MD05' => 1, 'MD06' => 1,
-# 		    'MD07' => 1, 'MD08' => 1, 'MD09' => 1, 'MD10' => 1,
-# 		    'STS' => 1, 'SVS' => 0, 'SweetSpot' => 0, 'ThreePi' => 0);
+#                   'MD03' => 1, 'MD04' => 1, 'MD05' => 1, 'MD06' => 1,
+#                   'MD07' => 1, 'MD08' => 1, 'MD09' => 1, 'MD10' => 1,
+#                   'STS' => 1, 'SVS' => 0, 'SweetSpot' => 0, 'ThreePi' => 0);
 # my $retention_time = 9000;  # days.
 
@@ -63,7 +63,7 @@
     'help|h'               => \$help,
     'date=s'               => \$date, # night to make stacks for
-    'camera=s'             => \$camera, # camera 
+    'camera=s'             => \$camera, # camera
     'dbname=s'             => \$dbname, # Database name
-    'logfile=s'            => \$logfile, 
+    'logfile=s'            => \$logfile,
     'verbose'              => \$verbose,
     'isburning'            => \$isburning,
@@ -83,5 +83,5 @@
     'clean_old'            => \$clean_old,
     ) or pod2usage ( 2 );
-pod2usage( -msg => 
+pod2usage( -msg =>
 "USAGE: automate_stacks.pl <options...> <mode>
         Options:
@@ -90,5 +90,5 @@
            --camera <camera>      Default GPC1.
            --dbname <db>          Default gpc1.
-           --verbose              
+           --verbose
            --isburning            Signal that we are currently burntooling.
            --force_stack_count    Force the chip/warp counts.
@@ -106,5 +106,5 @@
            --queue_diffs          Issue difftool commands to queue diffs.
            --clean_old            Mark old data 'goto_cleanup'.\n",
-	   -exitval => 2, ) if (defined($help));
+           -exitval => 2, ) if (defined($help));
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
@@ -114,5 +114,5 @@
     defined $check_registration or defined $define_burntool or defined $queue_burntool or
     defined $queue_chips or defined $queue_stacks or
-    defined $check_chips or defined $check_stacks or 
+    defined $check_chips or defined $check_stacks or
     defined $test_mode or defined $clean_old;
 
@@ -129,5 +129,5 @@
 # Grab the configuration data.
 my $conf_cmd = "$ppConfigDump -dump-recipe NIGHTLY_SCIENCE -";
-my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 
+my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     run(command => $conf_cmd, verbose => $verbose);
 unless ($success) {
@@ -140,33 +140,33 @@
 foreach my $entry (@{ $metadata }) {
     if (${ $entry }{name} eq 'RETENTION_TIME') {
-	$retention_time = ${ $entry }{value};
+        $retention_time = ${ $entry }{value};
     }
     elsif (${ $entry }{name} eq 'FILTERS') {
-	push @filter_list, ${ $entry }{value};
+        push @filter_list, ${ $entry }{value};
     }
     elsif (${ $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;
-	    }
-	    elsif (${ $tentry }{name} eq 'TESS') {
-		$tessID_list{$this_target} = ${ $tentry }{value};
-	    }
-	    elsif (${ $tentry }{name} eq 'OBSMODE') {
-		$obsmode_list{$this_target} = ${ $tentry }{value};
-	    }
-	    elsif (${ $tentry }{name} eq 'OBJECT') {
-		$object_list{$this_target} = ${ $tentry }{value};
-	    }
-	    elsif (${ $tentry }{name} eq 'COMMENT') {
-		$comment_list{$this_target} = ${ $tentry }{value};
-	    }
-	    elsif (${ $tentry }{name} eq 'STACKABLE') {
-		$stackable_list{$this_target} = ${ $tentry }{value};
-	    }
-	}
+        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;
+            }
+            elsif (${ $tentry }{name} eq 'TESS') {
+                $tessID_list{$this_target} = ${ $tentry }{value};
+            }
+            elsif (${ $tentry }{name} eq 'OBSMODE') {
+                $obsmode_list{$this_target} = ${ $tentry }{value};
+            }
+            elsif (${ $tentry }{name} eq 'OBJECT') {
+                $object_list{$this_target} = ${ $tentry }{value};
+            }
+            elsif (${ $tentry }{name} eq 'COMMENT') {
+                $comment_list{$this_target} = ${ $tentry }{value};
+            }
+            elsif (${ $tentry }{name} eq 'STACKABLE') {
+                $stackable_list{$this_target} = ${ $tentry }{value};
+            }
+        }
     }
 }
@@ -190,8 +190,8 @@
 if (defined($this_target_only)) {
     foreach my $t (@target_list) {
-	if ($t eq $this_target_only) {
-	    @target_list = ($this_target_only);
-	    last;
-	}
+        if ($t eq $this_target_only) {
+            @target_list = ($this_target_only);
+            last;
+        }
     }
     die("$this_target_only is invalid.") if ($#target_list != 0);
@@ -200,8 +200,8 @@
 if (defined($this_filter_only)) {
     foreach my $t (@filter_list) {
-	if ($t eq $this_filter_only) {
-	    @filter_list = ($this_filter_only);
-	    last;
-	}
+        if ($t eq $this_filter_only) {
+            @filter_list = ($this_filter_only);
+            last;
+        }
     }
     die("$this_filter_only is invalid.") if ($#filter_list != 0);
@@ -209,5 +209,5 @@
 
 #
-# Mode selection 
+# Mode selection
 ################################################################################
 if (defined($test_mode)) {
@@ -236,5 +236,5 @@
     &execute_chips($date,"pretend");
     if (($metadata_out{nsState} eq 'NEEDSBURNING')&&(defined($isburning))) {
-	$metadata_out{nsState} = 'BURNING';
+        $metadata_out{nsState} = 'BURNING';
     }
     return_metadata($date);
@@ -251,5 +251,5 @@
     &execute_stacks($date,"pretend");
     if ($metadata_out{nsState} eq 'FORCETOWARP') {
-	$metadata_out{nsState} = 'TOWARP';
+        $metadata_out{nsState} = 'TOWARP';
     }
     return_metadata($date);
@@ -267,8 +267,8 @@
 if (defined($clean_old) || defined($test_mode)) {
     if (defined($test_mode)) {
-	&execute_cleans($date,"pretend");
+        &execute_cleans($date,"pretend");
     }
     else {
-	&execute_cleans($date);
+        &execute_cleans($date);
     }
     unless (defined($test_mode)) { exit(0); }
@@ -276,5 +276,5 @@
 exit(0);
 #
-# Registration 
+# Registration
 ################################################################################
 
@@ -284,5 +284,5 @@
 
     # largely stolen from Bill's checkexp program.
-    
+
     my $sth = " SELECT exp_name, summitExp.dateobs AS registered, rawExp.dateobs, summitExp.imfiles, ";
     $sth .= " summitExp.fault AS summit_fault, pzDownloadExp.state AS download_state, ";
@@ -294,5 +294,5 @@
 
     my $data_ref = $db->selectall_arrayref( $sth );
-    
+
     my $Nsummit_exps = 0;
     my $Nsummit_faults = 0;
@@ -303,55 +303,55 @@
 
     foreach my $row_ref (@{ $data_ref }) {
-	my ($exp_name,$registered,$dateobs,$imfiles,$summit_fault,
-	    $download_state,$download_count,$new_state,$exp_id,$exp_type) = @{ $row_ref };
-	$Nsummit_exps++;
-	if ($summit_fault) {
-	    print STDERR "check_summit_copy: $date $exp_name has summit_fault $summit_fault";
-	    if ($exp_type ne 'OBJECT') {
-		print STDERR " (but I don't care).\n";
-	    }
-	    else {
-		print STDERR "\n";
-		$Nsummit_faults++;
-	    }
-	} 
-	elsif (!$download_state or $download_state eq 'run') {
-	    print STDERR "check_summit_copy: $date $exp_name has download_state $download_state";
-	    if ($exp_type ne 'OBJECT') {
-		print STDERR " (but I don't care).\n";
-	    }
-	    else {
-		print STDERR "\n";
-		$Ndownload_faults++;
-	    }
-	}
-	elsif (!$new_state or $new_state eq 'run' ) {
-	    print STDERR "check_summit_copy: $date $exp_name has new_state $new_state";
-	    if ($exp_type ne 'OBJECT') {
-		print STDERR " (but I don't care).\n";
-	    }
-	    else {
-		print STDERR "\n";
-		$Nregister_faults++;
-	    }
-	}
+        my ($exp_name,$registered,$dateobs,$imfiles,$summit_fault,
+            $download_state,$download_count,$new_state,$exp_id,$exp_type) = @{ $row_ref };
+        $Nsummit_exps++;
+        if ($summit_fault) {
+            print STDERR "check_summit_copy: $date $exp_name has summit_fault $summit_fault";
+            if ($exp_type ne 'OBJECT') {
+                print STDERR " (but I don't care).\n";
+            }
+            else {
+                print STDERR "\n";
+                $Nsummit_faults++;
+            }
+        }
+        elsif (!$download_state or $download_state eq 'run') {
+            print STDERR "check_summit_copy: $date $exp_name has download_state $download_state";
+            if ($exp_type ne 'OBJECT') {
+                print STDERR " (but I don't care).\n";
+            }
+            else {
+                print STDERR "\n";
+                $Ndownload_faults++;
+            }
+        }
+        elsif (!$new_state or $new_state eq 'run' ) {
+            print STDERR "check_summit_copy: $date $exp_name has new_state $new_state";
+            if ($exp_type ne 'OBJECT') {
+                print STDERR " (but I don't care).\n";
+            }
+            else {
+                print STDERR "\n";
+                $Nregister_faults++;
+            }
+        }
     }
 
     my $Nfaults = $Nsummit_faults + $Ndownload_faults + $Nregister_faults;
     if ($Nsummit_exps == 0) {
-	print STDERR "No exposures were found on the summit for $date.\n";
-	$metadata_out{nsState} = 'DROP';
+        print STDERR "No exposures were found on the summit for $date.\n";
+        $metadata_out{nsState} = 'DROP';
     }
     elsif ($Nfaults != 0) {
-	print STDERR "There were faults while downloading the exposures for $date.\n";
-	$metadata_out{nsState} = 'NEW';
+        print STDERR "There were faults while downloading the exposures for $date.\n";
+        $metadata_out{nsState} = 'NEW';
     }
     else {
-	print STDERR "Summit copy and Registration have succeeded for $date.\n";
-	$metadata_out{nsState} = 'REGISTERED';
-    }
-    
+        print STDERR "Summit copy and Registration have succeeded for $date.\n";
+        $metadata_out{nsState} = 'REGISTERED';
+    }
+
     return($Nsummit_exps,$Nfaults);
-}	 
+}
 
 #
@@ -364,19 +364,19 @@
     my $config_cmd = "$ppConfigDump -camera $camera -dump-camera - | grep BURNTOOL.STATE.GOOD | uniq";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run ( command => $config_cmd, verbose => $verbose);
+        run ( command => $config_cmd, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppConfigDump: $error_code", 0, 0, 0, $PS_EXIT_SYS_ERROR);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform ppConfigDump: $error_code", 0, 0, 0, $PS_EXIT_SYS_ERROR);
     }
     my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", 0, 0, 0, $PS_EXIT_SYS_ERROR);
+        &my_die("Unable to parse metadata config doc", 0, 0, 0, $PS_EXIT_SYS_ERROR);
     my $burntoolStateGood = 999;
     foreach my $cfg (@$recipeData) {
-	if ($cfg->{name} eq 'BURNTOOL.STATE.GOOD') {
-	    $burntoolStateGood = $cfg->{value};
-	}
+        if ($cfg->{name} eq 'BURNTOOL.STATE.GOOD') {
+            $burntoolStateGood = $cfg->{value};
+        }
     }
     if ($burntoolStateGood == 999) {
-	&my_die("Failed to determine BURNTOOL.STATE.GOOD", $burntoolStateGood, 0, 0, $PS_EXIT_SYS_ERROR);
+        &my_die("Failed to determine BURNTOOL.STATE.GOOD", $burntoolStateGood, 0, 0, $PS_EXIT_SYS_ERROR);
     }
     return($burntoolStateGood);
@@ -388,5 +388,5 @@
 
     my $db = init_gpc_db();
-    
+
     my $sth = "SELECT exp_id,exp_name,obs_mode,dateobs,class_id,burntool_state,comment FROM rawImfile WHERE exp_id = $exp_id";
     my $data_ref = $db->selectall_arrayref( $sth );
@@ -397,11 +397,11 @@
     foreach my $row_ref (@{ $data_ref }) {
         my ($exp_id,$exp_name, $obs_mode,$dateobs,$class_id,$burntool_state,$comment) = @{ $row_ref };
-	$Nimfiles++;
-	if (abs($burntool_state) == $burntoolStateGood) {
-	    $Nburntooled++;
-	}
+        $Nimfiles++;
+        if (abs($burntool_state) == $burntoolStateGood) {
+            $Nburntooled++;
+        }
     }
     return($Nimfiles,$Nburntooled);
-}    
+}
 
 sub find_burntool_ranges {
@@ -410,13 +410,13 @@
     my $command = "$mkBTpcontrol -d $date -b";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run ( command => $command, verbose => $verbose );
+        run ( command => $command, verbose => $verbose );
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform $command: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform $command: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     }
 
     if ($#{ $stderr_buf } > -1) {
-	$metadata_out{nsState} = 'ERROR';	
-	return(1);
+        $metadata_out{nsState} = 'ERROR';
+        return(1);
     }
 
@@ -424,11 +424,11 @@
     my @burntool_entries = split /\n/, (join '', @$stdout_buf);
     foreach my $row (@burntool_entries) {
-	my ($trash,$start,$end);
-	($trash,$trash,$start,$end) = (split /\s+/,$row);
-	$metadata_out{"btBegin${N}"} = $start;
-	$metadata_out{"btEnd${N}"} = $end;
-	$metadata_out{"btCounter${N}"} = 0;
-	$N++;
-	print STDERR "define_burntool: $row\n";
+        my ($trash,$start,$end);
+        ($trash,$trash,$start,$end) = (split /\s+/,$row);
+        $metadata_out{"btBegin${N}"} = $start;
+        $metadata_out{"btEnd${N}"} = $end;
+        $metadata_out{"btCounter${N}"} = 0;
+        $N++;
+        print STDERR "define_burntool: $row\n";
     }
     $metadata_out{btN} = $N - 1;
@@ -437,5 +437,5 @@
     return(0);
 }
-    
+
 #
 # Chips
@@ -447,5 +447,5 @@
 
     my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group) = get_tool_parameters($date,$target);
-    
+
     my $select =  "-dateobs_begin ${date}T00:00:00 -dateobs_end ${date}T23:59:59 ";
     $date =~ s/-//g;
@@ -457,15 +457,15 @@
     $cmd .= " -set_tess_id $tess_id -set_data_group $data_group ";
     if (defined($obs_mode)) {
-	$cmd .= " -obs_mode '$obs_mode' ";
+        $cmd .= " -obs_mode '$obs_mode' ";
     }
     if (defined($object)) {
-	$cmd .= " -object '$object' ";
+        $cmd .= " -object '$object' ";
     }
     if (defined($comment)) {
-	$cmd .= " -comment '$comment' ";
+        $cmd .= " -comment '$comment' ";
     }
     $cmd .= " $select ";
     if ($debug == 1) {
-	$cmd .= " -pretend ";
+        $cmd .= " -pretend ";
     }
     print STDERR "$cmd\n";
@@ -477,5 +477,5 @@
     my $date = shift;
     my $target = shift;
-    
+
     my $db = init_gpc_db();
     $date =~ s/-//g;
@@ -491,11 +491,11 @@
     my $date = shift;
     my $target = shift;
-    
+
     my $command = construct_chiptool_cmd($date,$target) . ' -pretend ';
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run ( command => $command, verbose => $verbose );
+        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", 0,0,$date, $PS_EXIT_SYS_ERROR);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     }
 
@@ -506,11 +506,11 @@
     my @input_exposures = split /\n/, (join '', @$stdout_buf);
     foreach my $entry (@input_exposures) {
-	my ($exp_id, @trash) = split /\s+/, $entry;
-	@trash = verify_burntool($exp_id,$burntoolStateGood);
-	$Nimfiles += $trash[0];
-	$Nburntooled += $trash[1];
-
-	@trash = verify_uniqueness_chip($exp_id,$date,$target);
-	$Nalready += $trash[0];
+        my ($exp_id, @trash) = split /\s+/, $entry;
+        @trash = verify_burntool($exp_id,$burntoolStateGood);
+        $Nimfiles += $trash[0];
+        $Nburntooled += $trash[1];
+
+        @trash = verify_uniqueness_chip($exp_id,$date,$target);
+        $Nalready += $trash[0];
     }
     return($#input_exposures + 1,$Nimfiles,$Nburntooled,$Nalready);
@@ -523,8 +523,8 @@
     my $command = construct_chiptool_cmd($date,$target);
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run ( command => $command, verbose => $verbose );
+        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", 0,0,$date, $PS_EXIT_SYS_ERROR);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform chiptool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     }
     $metadata_out{nsState} = 'TOWARP';
@@ -536,21 +536,21 @@
     my $pretend = shift;
     foreach my $target (@target_list) {
-	my ($Nexposures,$Nimfiles,$Nburntooled,$Nalready) = pre_chip_queue($date,$target);
-	if ($Nexposures == 0) {
-	    print STDERR "execute_chips: Target $target on $date had no exposures.\n";
-	    next;
-	}
-	if ($Nalready != 0) {
-	    print STDERR "execute_chips: Not queueing $target on $date due to already existing exposures.\n";
-	    next;
-	}
-	if ($Nimfiles != $Nburntooled) {
-	    print STDERR "execute_chips: Target $target on $date is not fully burntooled.\n";
-	    $metadata_out{nsState} = 'NEEDSBURNING';
-	    next;
-	}
-	unless(defined($pretend)) {
-	    chip_queue($date,$target);
-	}
+        my ($Nexposures,$Nimfiles,$Nburntooled,$Nalready) = pre_chip_queue($date,$target);
+        if ($Nexposures == 0) {
+            print STDERR "execute_chips: Target $target on $date had no exposures.\n";
+            next;
+        }
+        if ($Nalready != 0) {
+            print STDERR "execute_chips: Not queueing $target on $date due to already existing exposures.\n";
+            next;
+        }
+        if ($Nimfiles != $Nburntooled) {
+            print STDERR "execute_chips: Target $target on $date is not fully burntooled.\n";
+            $metadata_out{nsState} = 'NEEDSBURNING';
+            next;
+        }
+        unless(defined($pretend)) {
+            chip_queue($date,$target);
+        }
     }
 }
@@ -572,15 +572,16 @@
     my $cmd = "$stacktool";
 #    $cmd .= ' -pretend -simple -dbname gpc1 -definebyquery -min_new 4 ';  # Probably silly, but I want to be safe and not overwrite
-    $cmd .= ' -simple -dbname gpc1 -definebyquery ';  
+    $cmd .= ' -simple -dbname gpc1 -definebyquery ';
     $cmd .= " -set_label $label -select_label $label ";
     $cmd .= " -set_workdir $workdir -set_dist_group $dist_group ";
     $cmd .= " -select_filter $filter -set_data_group $data_group ";
+    $cmd .= " -min_num 4";
     $cmd .= " $select ";
     if ($debug == 1) {
-	$cmd .= ' -pretend ';
+        $cmd .= ' -pretend ';
     }
     print STDERR "$cmd\n";
     return($cmd);
-}    
+}
 
 sub verify_uniqueness_stack {
@@ -596,5 +597,5 @@
     my $sth = "SELECT skycell_id from stackRun where data_group = '$data_group' AND skycell_id = '$skycell' AND filter = '$filter'";
     my $data_ref = $db->selectall_arrayref( $sth );
-    
+
     return($#{ $data_ref } + 1);
 }
@@ -620,18 +621,18 @@
     # check that we will be able to queue them up
     my $command = construct_stacktool_cmd($date,$target,$filter) . ' -pretend ';
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 
-	run ( command => $command, verbose => $verbose );
+    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 stacktool: $error_code", $#{ $chip_ref },$#{ $warp_ref },$date, $PS_EXIT_SYS_ERROR);
-    }
-    
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform stacktool: $error_code", $#{ $chip_ref },$#{ $warp_ref },$date, $PS_EXIT_SYS_ERROR);
+    }
+
     my $Nalready = 0;
 
-    my @input_exposures = split /\n/, (join '', @$stdout_buf);    
+    my @input_exposures = split /\n/, (join '', @$stdout_buf);
     foreach my $entry (@input_exposures) {
-	my ($skycell, @trash) = split /\s+/, $entry;
-	@trash = verify_uniqueness_stack($skycell,$date,$target,$filter);
-	$Nalready += $trash[0];
+        my ($skycell, @trash) = split /\s+/, $entry;
+        @trash = verify_uniqueness_stack($skycell,$date,$target,$filter);
+        $Nalready += $trash[0];
     }
 
@@ -645,9 +646,9 @@
 
     my $command = construct_stacktool_cmd($date,$target,$filter);
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 
-	run ( command => $command, verbose => $verbose );
+    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 stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
     }
     return(0);
@@ -658,34 +659,34 @@
     my $pretend = shift;
     foreach my $target (@target_list) {
-	if ($stackable_list{$target} == 1) {
-	    foreach my $filter (@filter_list) {
-		my ($Nexposures,$NprocChips,$NprocWarps,$Nalready) = pre_stack_queue($date,$target,$filter);
-		if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
-		    print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
-		    $metadata_out{nsState} = 'FORCETOWARP';
-		    next;
-		}
-		if ($Nexposures == 0) {
-		    print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
-		    next;
-		}
-		if ($Nalready != 0) {
-		    print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
-		    next;
-		}
-		unless ($metadata_out{nsState} eq 'FORCETOWARP') {
-		    $metadata_out{nsState} = 'QUEUESTACKS';
-		}
-		unless(defined($pretend)) {
-		    print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
-		    stack_queue($date,$target,$filter);
-		}
-	    }
-	}
-	else {
-	    print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
-	}
-    }
-    
+        if ($stackable_list{$target} == 1) {
+            foreach my $filter (@filter_list) {
+                my ($Nexposures,$NprocChips,$NprocWarps,$Nalready) = pre_stack_queue($date,$target,$filter);
+                if ((!defined($force_stack_count))&&($NprocChips != $NprocWarps)) { # This makes me sad. :(
+                    print STDERR "execute_stacks: Target $target on $date is not fully processed. ($NprocChips $NprocWarps)\n";
+                    $metadata_out{nsState} = 'FORCETOWARP';
+                    next;
+                }
+                if ($Nexposures == 0) {
+                    print STDERR "execute_stacks: Target $target in filter $filter on $date has no exposures.\n";
+                    next;
+                }
+                if ($Nalready != 0) {
+                    print STDERR "execute_stacks: Not queueing $target in filter $filter on $date due to already existing stacks.\n";
+                    next;
+                }
+                unless ($metadata_out{nsState} eq 'FORCETOWARP') {
+                    $metadata_out{nsState} = 'QUEUESTACKS';
+                }
+                unless(defined($pretend)) {
+                    print STDERR "execute_stacks: Target $target in filter $filter on $date has exposures and will be queued.\n";
+                    stack_queue($date,$target,$filter);
+                }
+            }
+        }
+        else {
+            print STDERR "execute_stacks: Target $target is not auto-stackable.\n";
+        }
+    }
+
 }
 
@@ -697,9 +698,9 @@
     my $date = shift;
     my $target = shift;
-    
+
     my ($year,$month,$day) = split /-/,$date;
     my $dt = DateTime->new(year => $year, month => $month, day => $day,
-			       hour => 0, minute => 0, second => 0, nanosecond => 0,
-			       time_zone => 'Pacific/Honolulu');
+                               hour => 0, minute => 0, second => 0, nanosecond => 0,
+                               time_zone => 'Pacific/Honolulu');
     $dt->subtract(days => $retention_time);
     my $cleaning_date = $dt->ymd;
@@ -709,5 +710,5 @@
     my $args .= " -dbname gpc1 -updaterun -set_state goto_cleaned -state full -data_group $data_group ";
     if ($debug == 1) {
-	$args .= ' -pretend ';
+        $args .= ' -pretend ';
     }
     return($cleaning_date,$args);
@@ -719,34 +720,34 @@
 
     foreach my $target (@target_list) {
-	my ($cleaning_date,$args) = construct_cleantool_args($date,$target);
-
-	my $command = $chiptool . $args;
-	print STDERR "$command\n";
-	if (!(defined($pretend) || $debug == 1)) {
-#	    print STDERR "BEAR IS DRIVING!?\n";
-	    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 stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
-	    }
-	}
-	$command = $warptool . $args;
-	print STDERR "$command\n";
-	if (!(defined($pretend) || $debug == 1)) {
-#	    print STDERR "BEAR IS DRIVING $pretend $debug!?\n";
-	    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 stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
-	    }
-	}
+        my ($cleaning_date,$args) = construct_cleantool_args($date,$target);
+
+        my $command = $chiptool . $args;
+        print STDERR "$command\n";
+        if (!(defined($pretend) || $debug == 1)) {
+#           print STDERR "BEAR IS DRIVING!?\n";
+            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 stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+            }
+        }
+        $command = $warptool . $args;
+        print STDERR "$command\n";
+        if (!(defined($pretend) || $debug == 1)) {
+#           print STDERR "BEAR IS DRIVING $pretend $debug!?\n";
+            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 stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+            }
+        }
     }
     return(0);
 }
-	
-	
-	    
+
+
+
 
 #
@@ -779,8 +780,8 @@
     my $dbpass = 'ippuser';
     $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" .
-		       "mysql_socket=" . DB_SOCKET(),
-		       ${dbuser},${dbpass}, 
-		       { RaiseError => 1, AutoCommit => 1}
-	) or die "Unable to connect to database $DBI::errstr\n";
+                       "mysql_socket=" . DB_SOCKET(),
+                       ${dbuser},${dbpass},
+                       { RaiseError => 1, AutoCommit => 1}
+        ) or die "Unable to connect to database $DBI::errstr\n";
     return($db);
 }
@@ -792,5 +793,5 @@
     print STDOUT "   date                  STR          $date\n";
     foreach my $k (keys %metadata_out) {
-	print STDOUT "   $k                STR          $metadata_out{$k}\n";
+        print STDOUT "   $k                STR          $metadata_out{$k}\n";
     }
     print STDOUT "END\n";
@@ -810,7 +811,7 @@
 sub my_trace {
     if ($debug == 1) {
-	foreach my $thing (@_) {
-	    carp($thing);
-	}
-    }
-}
+        foreach my $thing (@_) {
+            carp($thing);
+        }
+    }
+}
