Index: trunk/ippScripts/scripts/automate_stacks.pl
===================================================================
--- trunk/ippScripts/scripts/automate_stacks.pl	(revision 28886)
+++ trunk/ippScripts/scripts/automate_stacks.pl	(revision 28887)
@@ -25,4 +25,5 @@
 my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
 my $mkBTpcontrol = can_run('make_burntool_pcontrol.pl') or (warn "Can't find make_burntool_pcontrol.pl" and $missing_tools = 1);
+my $moondata = can_run('moondata') or (warn "Can't find moondata" and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -43,5 +44,5 @@
 my ( $help, $isburning, $force_stack_count, $test_mode, $this_target_only, $this_filter_only, $this_mode_only, $check_mode, $burntool_stats);
 my ( $check_registration, $define_burntool, $queue_burntool, $check_chips, $queue_chips);
-my ( $check_stacks, $queue_stacks, $check_diffs, $queue_diffs, $clean_old);
+my ( $check_stacks, $queue_stacks, $check_sweetspot, $queue_sweetspot, $check_diffs, $queue_diffs, $clean_old);
 my ( $check_detrends, $queue_detrends, $check_dqstats, $queue_dqstats);
 
@@ -69,4 +70,6 @@
     'check_stacks'         => \$check_stacks,
     'queue_stacks'         => \$queue_stacks,
+    'check_sweetspot'      => \$check_sweetspot,
+    'queue_sweetspot'      => \$queue_sweetspot,
     'check_detrends'       => \$check_detrends,
     'queue_detrends'       => \$queue_detrends,
@@ -99,4 +102,6 @@
            --check_stacks         Confirm that stacks can be built.
            --queue_stacks         Issue stacktool commands to queue stacks.
+           --check_sweetspot      See if we should queue SweetSpot stacks.
+           --queue_sweetspot      Issue stacktool commands to queue SweetSpot stacks.
            --check_detrends       Confirm that detrend verify runs can be built.
            --queue_detrends       Issue dettool commands to queue detrend verify runs.
@@ -113,6 +118,6 @@
           ) unless
     defined $check_registration or defined $define_burntool or defined $queue_burntool or
-    defined $queue_chips or defined $queue_stacks or $queue_detrends or $queue_dqstats or
-    defined $check_chips or defined $check_stacks or $check_detrends or $check_dqstats or
+    defined $queue_chips or defined $queue_stacks or $queue_sweetspot or $queue_detrends or $queue_dqstats or 
+    defined $check_chips or defined $check_stacks or $check_sweetspot or $check_detrends or $check_dqstats or
     defined $test_mode or defined $clean_old or defined $check_mode;
 
@@ -306,4 +311,5 @@
     unless (defined($test_mode) || defined($check_mode)) { exit(0); }
 }
+
 if (defined($check_dqstats) || defined($test_mode)) {
     $metadata_out{nsState} = 'CHECKDQSTATS';
@@ -318,4 +324,5 @@
     unless (defined($test_mode)) { exit(0); }
 }
+
 if (defined($check_detrends) || defined($test_mode) || defined($check_mode)) {
     $metadata_out{nsState} = 'CHECKDETRENDS';
@@ -330,4 +337,5 @@
     exit(0);
 }
+
 if (defined($define_burntool) || defined($test_mode)) {
     $metadata_out{nsState} = 'QUEUEBURNING';
@@ -341,4 +349,5 @@
     unless (defined($test_mode)) { exit(0); }
 }
+
 if (defined($check_chips) || defined($test_mode) || defined($check_mode)) {
     $metadata_out{nsState} = 'QUEUECHIPS';
@@ -356,4 +365,5 @@
     exit(0);
 }
+
 if (defined($check_stacks) || defined($test_mode) || defined($check_mode)) {
     $metadata_out{nsState} = 'TOWARP';
@@ -374,7 +384,22 @@
     exit(0);
 }
+
+if (defined($check_sweetspot) || defined($test_mode) || defined($check_mode)) {
+    $metadata_out{nsState} = 'CHECKSWEETSPOT';
+    &execute_sweetspot($date,"pretend");
+    return_metadata($date);
+    unless (defined($test_mode) || defined($check_mode)) { exit(0); }
+}
+if (defined($queue_sweetspot)) {
+    $metadata_out{nsState} = 'CHECKSWEETSPOT';
+    &execute_sweetspot($date);
+    return_metadata($date);
+    exit(0);
+}
+
 if (defined($check_diffs) || defined($queue_diffs)) {
     die("Diffs are currently not implemented.");
 }
+
 if (defined($clean_old) || defined($test_mode)) {
     if (defined($test_mode)) {
@@ -864,4 +889,134 @@
     if (($metadata_out{nsState} eq 'CHECKDETRENDS') && ($exposures > 0)) {
 	$metadata_out{nsState} eq 'QUEUEDETRENDS';
+    }
+}
+
+#
+# SweetSpot Stacking
+################################################################################
+sub construct_sweetspot_cmd {
+    my $date = shift;
+    my $target = 'SweetSpot';
+    my $filter = 'w.00000';
+
+    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
+
+    # Dateobs begin end?
+    my ($dateobs_begin,$dateobs_end) = get_lunation_extent($date);
+    my $select = "-select_dateobs_begin ${dateobs_begin}T00:00:00 -select_dateobs_end ${dateobs_end}T00:00:00";
+    my $cmd = "$stacktool";
+    $cmd .= " -simple -dbname $dbname -definebyquery ";
+    $cmd .= " -set_label SweetSpot.refstack -select_label $label ";
+    $cmd .= " -set_workdir $workdir -set_dist_group $dist_group ";
+    $cmd .= " -select_filter $filter -set_data_group $data_group ";
+    $cmd .= " -select_good_frac_min 0.1 -select_fwhm_major_max 8.0 ";
+    $cmd .= " -min_num 7 -min_new 4";
+    $cmd .= " $select ";
+    if ($debug == 1) {
+	$cmd .= ' -pretend ';
+    }
+    print STDERR "$cmd\n";
+    return($cmd);
+}
+
+sub get_lunation_extent {
+    my $date = shift;
+    my ($year,$month,$day) = split /-/,$date;
+    my $dateobs_begin;
+    my $dateobs_end;
+
+    my $dt = DateTime->new(year => $year, month => $month, day => $day,
+			   hour => 0, minute => 0, second => 0, nanosecond => 0,
+			   time_zone => 'Pacific/Honolulu');
+    do {
+	$dt->subtract(days => 1);
+	my $ymd = $dt->ymd;
+	my $md_cmd = "moondata $ymd 0 0";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run ( command => $md_cmd, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
+	}
+	my @result = split /\s+/,(join "\n", @$stdout_buf);
+	if (abs($result[6]) <= 0.5) {
+	    $dateobs_end = $ymd;
+	}
+    } while (!defined($dateobs_end));
+    
+    do {
+	$dt->subtract(days => 1);
+	my $ymd = $dt->ymd;
+	my $md_cmd = "moondata $ymd 0 0";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run ( command => $md_cmd, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform moondata: $error_code", 0,0, $date, $PS_EXIT_SYS_ERROR);
+	}
+	my @result = split /\s+/,(join "", @$stdout_buf);
+	if (abs($result[6]) <= 0.5) {
+	    $dateobs_begin = $ymd;
+	}
+    } while (!defined($dateobs_begin));
+	
+    return($dateobs_begin,$dateobs_end);
+}
+
+sub pre_sweetspot_queue { 
+    my $date = shift;
+    my $command = construct_sweetspot_cmd($date) . ' -pretend ';
+    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 sweetspot stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+    }
+    my @stacks = split /\n/, (join '', @$stdout_buf);
+    my $Nstacks = $#stacks + 1;
+        
+    return(1,$Nstacks);
+}
+
+sub sweetspot_queue {
+    my $date = shift;
+    my $command = construct_sweetspot_cmd($date);
+    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 sweetspot stacktool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+    }
+    my @stacks = split /\n/, (join '', @$stdout_buf);
+    my $Nstacks = $#stacks + 1;
+    
+    return(1,$Nstacks);
+}
+
+sub execute_sweetspot {    
+    my $date = shift;
+    my $pretend = shift;
+    
+    my ($is_lunation_date,$Nstacks) = pre_sweetspot_queue($date);
+    if ($Nstacks == 0) {
+	print STDERR "execute_sweetspot: No new stacks to make ($Nstacks)\n";
+	$metadata_out{nsState} = 'SS_EMPTY';
+	return();
+    }
+    if ($Nstacks < 10) {
+	print STDERR "execute_sweetspot: Too few new stacks to make ($Nstacks)\n";
+	$metadata_out{nsState} = 'SS_FEW';
+	return();
+    }
+    if ($is_lunation_date == 0) {
+	print STDERR "execute_sweetspot: Invalid lunation date. ($date $is_lunation_date)\n";
+	$metadata_out{nsState} = 'SS_ERROR';
+	return();
+    }
+    $metadata_out{nsState} = 'SS_QUEUE';
+    $metadata_out{nsSweetSpot} = $Nstacks;
+    unless(defined($pretend)) {
+	$metadata_out{nsState} = 'SS_DONE';
+	sweetspot_queue($date);
     }
 }
Index: trunk/ippTasks/nightly_stacks.pro
===================================================================
--- trunk/ippTasks/nightly_stacks.pro	(revision 28886)
+++ trunk/ippTasks/nightly_stacks.pro	(revision 28887)
@@ -7,4 +7,5 @@
     ns.detrends.off
     ns.dqstats.on
+    ns.sweetspot.on
     ns.registration.on
     ns.burntool.on
@@ -17,4 +18,5 @@
     ns.detrends.off
     ns.dqstats.off
+    ns.sweetspot.off
     ns.registration.off
     ns.burntool.off
@@ -49,4 +51,10 @@
 end
 
+macro ns.sweetspot.on
+  task ns.sweetspot.load
+    active true
+  end
+end
+
 macro ns.registration.on
   task ns.registration.load
@@ -96,4 +104,10 @@
 macro ns.dqstats.off
   task ns.dqstats.load
+    active false
+  end
+end
+
+macro ns.sweetspot.off
+  task ns.sweetspot.load
     active false
   end
@@ -263,4 +277,39 @@
 
     command automate_stacks.pl --queue_dqstats --date $today
+  end
+
+  task.exit       0
+    # nothing to do here
+  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
+
+#
+# Queue sweetspot runs
+#
+task              ns.sweetspot.load
+  host            local
+  periods         -poll 3600
+  periods         -exec $LOADEXEC
+  periods         -timeout 30
+  trange          02:00:00 02:59:59 -nmax 1
+  npending        1
+
+  task.exec
+    stdout $LOGDIR/ns.sweetspot.log
+    stderr $LOGDIR/ns.sweetspot.log
+    $today = `date +%Y-%m-%d`
+
+    command automate_stacks.pl --queue_sweetspot --date $today
   end
 
