Index: trunk/ippScripts/scripts/night_report.pl
===================================================================
--- trunk/ippScripts/scripts/night_report.pl	(revision 40461)
+++ trunk/ippScripts/scripts/night_report.pl	(revision 40530)
@@ -12,7 +12,8 @@
 GetOptions(
     'date=s'            => \$date,
-    'dbname|d=s'        => \$dbname,    # Database name
-    'verbose'           => \$verbose,   # Print to stdout
-    'very-verbose'      => \$verbose2,   # Print to stdout
+    'dbname|d=s'        => \$dbname,   # Database name
+    'verbose'           => \$verbose,  # Print to stdout
+    'report'            => \$report,   # Formatted for ps-obs report
+    'very-verbose'      => \$verbose2, # Print to stdout
     ) or pod2usage( 2 );
 
@@ -45,5 +46,5 @@
 my $dbh = DBI->connect( $dsn, $dbuser, $dbpass ) or die "Unable to connect to database: $DBI::errstr";
 
-# find a useful exp_id
+# find a useful exp_id (ALWAYS do this)
 $refExpID = 0;
 if (1) { 
@@ -63,6 +64,71 @@
       print "*** WARNING: no data in period 10-20 days before requested date (query will be slower) **\n";
     }
-    # print "Reference exp_id: $refExpID\n";
-}
+    if ($verbose2) { print "Reference exp_id: $refExpID\n"; }
+}
+
+##################33
+### testing the diff grouping -- this does not work for the Euclid data taken 2018.07.29 because the OBJECT
+### field is missing
+if (0) {
+    print "Expected number of WWdiffs excluding camRuns with bad quality:\n";
+    print "NOTE: multiple exposures with same comment string (xyz visit a) are treated as 1 exposure\n";
+
+    # my $query = "SELECT Nvisit, count(*) FROM";
+    # $query .= " (";
+    # $query .= "   SELECT object, filter, chunk, count(*) as Nvisit FROM";
+    # $query .= "   (";
+    my $query = "SELECT object, filter, comment, 'S', substr(comment, 1, position(' ' in comment)) as chunk, 'E'";
+    $query .= "     FROM rawExp";
+    $query .= "     LEFT JOIN chipRun using (exp_id)";
+    $query .= "     LEFT JOIN camRun using (chip_id)";
+    $query .= "     LEFT JOIN camProcessedExp using (cam_id)";
+    $query .= "     WHERE dateobs LIKE '$date%'";
+    $query .= "     AND exp_id > $refExpID"; #REF
+    $query .= "     AND exp_type = 'OBJECT'";
+    $query .= "     AND obs_mode NOT LIKE 'ENGINEERING'";
+    $query .= "     AND (obs_mode like '%SS%' or obs_mode like 'BRIGHT')";
+    $query .= "     AND rawExp.comment like '%visit%'";
+    $query .= "     AND (camProcessedExp.quality IS NULL or camProcessedExp.quality = 0)";
+#   $query .= "     GROUP BY comment, filter, substr(comment, 1, position(' ' in comment))";
+#    $query .= "   ) AS tableChunks GROUP BY object, filter, chunk";
+#    $query .= " ) AS tableVisits GROUP BY Nvisit";
+
+    my $result = &mysql_select ($query);
+    &mysql_dump_result ($result);
+    die;
+
+    $diffExpect = 0;
+    while (@row = $result->fetchrow_array()) {
+	# printf "%8s %15s : ", $row[0], $row[1]; # c, count(*)
+	if ($row[0] == 1) {
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 1 visit, no WWdiffs\n", $row[1], $verb;
+	} elsif ($row[0] == 2) {
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 2 visits, expect $row[1] WWdiff(s)\n", $row[1], $verb;
+	    $diffExpect += $row[1];
+	} elsif ($row[0] == 3) {
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 3 visits, expect %d WWdiffs (2 per tuple)\n", $row[1], $verb,  2 * $row[1];
+	    $diffExpect += $row[1]*2;
+	} elsif ($row[0] == 4) {
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 4 visits, expect %d WWdiffs (2 per tuple)\n", $row[1], $verb,  2 * $row[1];
+	    $diffExpect += $row[1]*2;
+	} else {
+	    # print "I don't understand, there are more visits than I expect\n";
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    if ($row[0] % 2) { $Ndiffs = int($row[0] / 2) + 1; } else { $Ndiffs = int($row[0] / 2); }
+	    printf "%3d %-11s %d visits, expect %d WWdiffs (2 per quad)\n", $row[1], $verb,  $row[0], $Ndiffs; 
+	    $diffExpect += $Ndiffs;
+	}
+    }
+    print "\n";
+    print " * $diffExpect diffRuns expected *\n";
+    if ($verbose) { print "\n"; }
+
+    die;
+}
+######################
 
 # check the status of summit exposures:
@@ -129,5 +195,5 @@
 # check the status of raw exposures:
 if (1) { 
-    if ($verbose) { print "Exposures in rawExp:\n"; }
+    if ($verbose or $report) { print "Exposures in rawExp:\n"; }
 
     $expSkipped = 0;
@@ -144,7 +210,7 @@
     my $result = &mysql_select ($query);
 
-    if ($verbose) { printf "%8s %15s %6s %4s\n", "exp_type", "obs_mode", "state", "N(exp)"; }
-    while (@row = $result->fetchrow_array()) {
-	if ($verbose) { printf "%8s %15s %6s %4d\n", $row[0], $row[1], $row[2], $row[3]; }
+    if ($verbose or $report) { printf "%8s %15s %6s %4s\n", "exp_type", "obs_mode", "state", "N(exp)"; }
+    while (@row = $result->fetchrow_array()) {
+	if ($verbose or $report) { printf "%8s %15s %6s %4d\n", $row[0], $row[1], $row[2], $row[3]; }
 	if ($row[2] eq "new") {
 	    $expPending += $row[3];
@@ -294,6 +360,10 @@
 # count the number of exposures per chunk, determine expected diffRuns:
 {
-    print "Expected number of WWdiffs excluding camRuns with bad quality:\n";
-    print "NOTE: multiple exposures with same comment string (xyz visit a) are treated as 1 exposure\n";
+    if ($report) { 
+	print "\n"; 
+    } else {
+	print "Expected number of WWdiffs excluding camRuns with bad quality:\n";
+	print "NOTE: multiple exposures with same comment string (xyz visit a) are treated as 1 exposure\n";
+    }
 
     # my $result = &mysql_select ("");
@@ -324,20 +394,24 @@
 	# printf "%8s %15s : ", $row[0], $row[1]; # c, count(*)
 	if ($row[0] == 1) {
-	    if ($row[1] == 1) { $verb = "quad has"; } else { $verb = "quads have"; }
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
 	    printf "%3d %-11s 1 visit, no WWdiffs\n", $row[1], $verb;
 	} elsif ($row[0] == 2) {
-	    if ($row[1] == 1) { $verb = "quad has"; } else { $verb = "quads have"; }
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
 	    printf "%3d %-11s 2 visits, expect $row[1] WWdiff(s)\n", $row[1], $verb;
 	    $diffExpect += $row[1];
 	} elsif ($row[0] == 3) {
-	    if ($row[1] == 1) { $verb = "quad has"; } else { $verb = "quads have"; }
-	    printf "%3d %-11s 3 visits, expect %d WWdiffs (2 per quad)\n", $row[1], $verb,  2 * $row[1];
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 3 visits, expect %d WWdiffs (2 per tuple)\n", $row[1], $verb,  2 * $row[1];
 	    $diffExpect += $row[1]*2;
 	} elsif ($row[0] == 4) {
-	    if ($row[1] == 1) { $verb = "quad has"; } else { $verb = "quads have"; }
-	    printf "%3d %-11s 4 visits, expect %d WWdiffs (2 per quad)\n", $row[1], $verb,  2 * $row[1];
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    printf "%3d %-11s 4 visits, expect %d WWdiffs (2 per tuple)\n", $row[1], $verb,  2 * $row[1];
 	    $diffExpect += $row[1]*2;
 	} else {
-	    print "I don't understand, there are more visits than I expect\n";
+	    # print "I don't understand, there are more visits than I expect\n";
+	    if ($row[1] == 1) { $verb = "tuple has"; } else { $verb = "tuples have"; }
+	    if ($row[0] % 2) { $Ndiffs = int($row[0] / 2) + 1; } else { $Ndiffs = int($row[0] / 2); }
+	    printf "%3d %-11s %d visits, expect %d WWdiffs (2 per quad)\n", $row[1], $verb,  $row[0], $Ndiffs; 
+	    $diffExpect += $Ndiffs;
 	}
     }
@@ -432,5 +506,5 @@
 	print "*** WARNING: Expect $diffExpect diffRuns, complete $diffDone diffRuns: inconsistent ***\n"; 
     } else {
-	print " * $diffDone of $diffExpect diffRuns completed *\n"; 
+	if (not $report) { print " * $diffDone of $diffExpect diffRuns completed *\n";  }
     }
 }
@@ -452,5 +526,5 @@
 
     @row = $result->fetchrow_array();
-    printf " * %d diffRuns can be published *   (has at least 1 skycell with quality = 0 or quality is NULL)\n", $row[0];
+    if (not $report) { printf " * %d diffRuns can be published *   (has at least 1 skycell with quality = 0 or quality is NULL)\n", $row[0]; }
     if ($verbose) { print "\n"; }
     $diffRunsToBePublished = $row[0];
@@ -555,17 +629,17 @@
 
     while (@row = $result->fetchrow_array()) {
-	if ($verbose && $doHeader) {
+	if (($verbose or $report) && $doHeader) {
 	    print "Bad quality camRun exposures:\n";
 	    printf "%11s %7s | %15s\n", "exp_name", "quality", "comment";
 	    $doHeader = 0;
 	}
-	if ($verbose) { printf "%11s %7d | %15s\n", $row[0], $row[1], $row[2]; } else { $NbadCam ++; }
-    }
-    if ($verbose && !$doHeader) { print "\n"; }
-    if (!$verbose) { print "$NbadCam bad quality camera exposures\n\n"; }
+	if ($verbose or $report) { printf "%11s %7d | %15s\n", $row[0], $row[1], $row[2]; } else { $NbadCam ++; }
+    }
+    if (($verbose or $report) && !$doHeader) { print "\n"; }
+    if (!($verbose or $report)) { print "$NbadCam bad quality camera exposures\n\n"; }
 }
 
 # check the status of diff runs NOT for Solar System:
-if ($verbose) {
+if ($verbose or $report) {
     # my $result = &mysql_select ("");
     my $query = "SELECT * FROM";
@@ -595,8 +669,8 @@
 	if ($doHeader) {
 	    print "Chunks which should not generate WWdiffs (not Solar System diffs)\n";
-	    printf "%8s %8s %8s %8s\n", "Object", "Filter", "Chunk", "N(visit)";
+	    printf "%16s %-8s %20s | %8s\n", "Object", "Filter", "Chunk", "N(visit)";
 	    $doHeader = 0;
 	}
-	printf "%8s %8s %8s %4d\n", $row[0], $row[1], $row[2], $row[3];
+	printf "%16s %-8s %20s | %4d\n", $row[0], $row[1], $row[2], $row[3];
     }
     if (!$doHeader) { print "\n"; }
