Index: trunk/ippScripts/scripts/nightly_science.pl
===================================================================
--- trunk/ippScripts/scripts/nightly_science.pl	(revision 31603)
+++ trunk/ippScripts/scripts/nightly_science.pl	(revision 31649)
@@ -60,5 +60,5 @@
     'test_mode'            => \$test_mode,
     'force_stack_count'    => \$force_stack_count,
-    'force_diff_count'    => \$force_diff_count,
+    'force_diff_count'     => \$force_diff_count,
     'this_target_only=s'   => \$this_target_only,
     'this_filter_only=s'   => \$this_filter_only,
@@ -865,47 +865,47 @@
 # }
 
-# 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));
+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->add(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.6) {
+	    $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.6) {
+	    $dateobs_begin = $ymd;
+	}
+    } while (!defined($dateobs_begin));
 	
-#     return($dateobs_begin,$dateobs_end);
-# }
+    return($dateobs_begin,$dateobs_end);
+}
 
 # sub pre_sweetspot_queue { 
@@ -1405,6 +1405,12 @@
 		}
 	    }
-	}
-    }
+	    if ((defined($science_config{$target}{OFFNIGHT_DIFFS})) && ($science_config{$target}{OFFNIGHT_DIFFS} == 1)) {
+		foreach my $filter (@filter_list) {
+		    offnight_diff_queue($date,$target,$filter,$pretend);
+		}
+	    }
+	}
+    }
+
 #     if (($Npotential == $Nnoexp)&&($metadata_out{nsObservingState} eq 'END_OF_NIGHT')&&($is_processing == 0)) {
 # 	$metadata_out{nsDiffState} = 'FINISHED_DIFFS';
@@ -1682,4 +1688,43 @@
 
 }
+
+sub offnight_diff_queue {
+    my $date = shift;
+    my $target = shift;
+    my $filter = shift;
+    my $pretend = shift;
+    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
+
+    my $db = init_gpc_db();
+
+#    my $Npotential = 0;
+#    my $Nqueued = 0;
+
+    my ($lunation_start,$lunation_end) = get_lunation_extent($date);
+    
+    my $new_data_group = "${data_group}.offnight";
+    my $new_dist_group = "${dist_group}.offnight";
+    my $cmd = "$difftool -dbname $dbname  -definewarpwarp ";
+    $cmd .= "-input_label $label  -template_label $label ";
+    $cmd .= "-set_workdir $workdir  -set_dist_group $new_dist_group  -set_data_group $new_data_group ";
+    $cmd .= " -simple  -set_label $label -filter $filter ";
+    $cmd .= " -dateobs_begin $lunation_start -dateobs_end $lunation_end -distance 1.5 ";
+
+    if (defined($pretend)) {
+	$cmd .= ' -pretend ';
+    }
+    if (defined($debug)) {
+	print STDERR "ON_diffs wants to run this command: $cmd\n";
+    }
+    
+    if (($debug == 0)&&(!defined($pretend))) {
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run ( command => $cmd, verbose => $verbose );
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform difftool: $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
+	}
+    }
+}
 	    
 
