#!/usr/local/bin/perl -w

# summarize_cruise_info.pl
#Reads the inventory file and generates an html formatted summary of
#the U.S. GLOBEC Georges Bank and related cruises
# Valid command line arguments include:
#	all - do all options (same as no argument)
#	summary - do only summaries, no ship data details
#	by_cruise - show cruise details sorted by cruise
#	by_date - show cruise sorted by date
#

$version="July 25, 2005/V1.32";

# R. Groman

# Edited July 25, 2005. V1.32 Add check for undefined passed
#	parameter, ARGV[0].  rcg
# Edited June 9, 2005. V1.31  Add check for underscore to skip over
#	non-cruise entries.  rcg
# Edited June 7, 2005. V1.30  Skip over non-cruises by assuming they
#	are not all capital letters for the project.  Add STDOUT to
#	print here lines.  rcg
# Edited June 26, 2001. V1.20 Fix leap year computation.  RCG
# Edited June 23, 1999.  V1.1 Replace listgb with list.  RCG
# Edited March 23, 1998.  Initialize values; change /data to /data5.

$option=$ARGV[0];
if (defined $option and $option eq "all") {undef $option;}
@month=("nd", "Jan", "Feb", "March", "April", "May", "June",
		"July", "Aug", "Sept", "Oct", "Nov", 
		"Dec");
		
open (ERRORS, ">summarize_cruise_info.errors");
print STDOUT ("Content-type: text/html\n\n",
	"<html>\n<title>U.S. GLOBEC Georges Bank Cruise Summary</title>",
	"<head><h2 align=center>U.S. GLOBEC Georges Bank Cruise Summary",
	"</h2></head><body>\n\n");

#$dataobject="//globec.whoi.edu/globec/gb/inventory_test";
#print STDOUT ("<h4>**debug summarize_cruise_info.pl, dataobject=$dataobject</h4>\n\n");

#$dataobject="//globec.whoi.edu/globec/gb/inventory";
$dataobject="/globec/gb/inventory";

$object=$dataobject . "\\(project,platform,year_p_start,month_p_start," .
	"day_p_start,year_p_end,month_p_end,day_p_end,name_prin,brief_desc\\)";
#print STDOUT ("\n<p>**debug, object=$object\n\n");

$read_data="/data5/globec/bin/list -f -n -t -c $object ";
#print STDOUT ("\n\n<p>**debug, read_data=$read_data\n\n");

$i=-1;

foreach $record (`$read_data` ) {
#	print STDOUT "\n\n<p>**debug, record=$record\n";
	if ( $i == -1) {	#skip past variable list
		$i++;
		next;
	}
	chomp($record);
	$record=~ s/\t/ /g;
	$record=~ s/^ +//;
	@fields = split (/ +/, $record);
#	print STDOUT "\n\n<p>**debug, fields=@fields";
	($project[$i],$platform[$i],$year_p_start[$i],$month_p_start[$i],
		$day_p_start[$i],$year_p_end[$i],$month_p_end[$i],
		$day_p_end[$i],$name_prin[$i],$brief_desc[$i]) = @fields;

# Check for non cruise entries
	unless ($project[$i] eq uc $project[$i]) {next}
	if ($project[$i] =~ m/_/ ) {next}
	
	$brief_desc[$i]=~s/(^.*?)_.*/$1/;
	if ($brief_desc[$i] eq "long") {$brief_desc[$i]="mooring"}

		
#	print STDOUT ("\n\n<p>**debug, \n\tproject[$i]=$project[$i]\n\t",
#		"platform=$platform[$i]\n\t",
#		"start date: $year_p_start[$i],$month_p_start[$i],",
#		"$day_p_start[$i]\n\tend date: $year_p_end[$i],$month_p_end[$i],",
#		"$day_p_end[$i]","\n\tbrief_desc[$i]=$brief_desc[$i]",
#		"\n\tname_prin[$i]=$name_prin[$i]\n\n");
	$cruise_length[$i]=&daysbetween($year_p_start[$i],$month_p_start[$i],
		$day_p_start[$i],$year_p_end[$i],$month_p_end[$i],
		$day_p_end[$i]);
#	print STDOUT ("<p>\n\n**debug, Number of days=$cruise_length[$i]\n\n");
	$i++;
}
$number_cruises=$i;
print STDOUT ("<p><h3>There are ", $number_cruises, 
	" cruises listed in the inventory.</h3>\n\n");

# Initialize array
for ($i=0; $i < $number_cruises; $i++) {
	$total_by_platform{$platform[$i]}=0;
	$total_by_brief_desc{$brief_desc[$i]}=0;
	$total_by_year{$year_p_start[$i]}=0;
	$total_ship_days=0;
}

for ($i=0; $i < $number_cruises; $i++) {
	$total_by_platform{$platform[$i]}=$total_by_platform{$platform[$i]} + 
						$cruise_length[$i];
	$total_by_brief_desc{$brief_desc[$i]}=$total_by_brief_desc{$brief_desc[$i]}
						+ $cruise_length[$i];
	$total_by_year{$year_p_start[$i]}=$total_by_year{$year_p_start[$i]} +
						$cruise_length[$i];
	$total_ship_days=$total_ship_days + $cruise_length[$i];
}

if (defined $option and ($option eq 'summary' or
	$option eq 'by_cruise' or $option eq 'by_date') ) {
		goto $option;
}
else {
	goto 'by_date';
}

summary:					#summary loop
$program_total_days=0;
print STDOUT ("<p><table border  \n");
print STDOUT ("<caption align=top><h2>Summary by Platform</h2></caption>\n");
print STDOUT "<tr><th>Platform</th> <th>Days at Sea</th> </tr>\n" ;
foreach $value (sort keys %total_by_platform) {
	print STDOUT "<tr><td>$value</td> <td>$total_by_platform{$value}</td></tr>\n";
	$program_total_days=$program_total_days + $total_by_platform{$value};
}
print STDOUT "<tr><td>Total days</td> <td>$program_total_days</td></tr>";
print STDOUT "</table><p><hr>\n\n";

$program_total_days=0;
print STDOUT ("<p><table border \n");
print STDOUT ("<caption align=top><h2>Summary by Brief Description</h2></caption>\n");
print STDOUT "<tr> <th>Brief description</th> <th>Days at Sea</th></tr>\n" ;
foreach $value (sort keys %total_by_brief_desc) {
	print STDOUT "<tr><td>$value</td> <td>$total_by_brief_desc{$value}</td></tr>\n";
	$program_total_days=$program_total_days + $total_by_brief_desc{$value};
}
print STDOUT "<tr><td>Total days</td> <td>$program_total_days</td></tr>";
print STDOUT "</table><p><hr>\n\n";

$program_total_days=0;
print STDOUT ("<p><table border \n");
print STDOUT ("<caption align=top><h2>Summary by Year</h2></caption>\n");
print STDOUT "<tr> <th>Year</th> <th>Days at Sea</th></tr>\n" ;
foreach $value (sort keys %total_by_year) {
	print STDOUT "<tr><td>$value</td> <td>$total_by_year{$value}</td></tr>\n";
	$program_total_days=$program_total_days + $total_by_year{$value};
}
print STDOUT "<tr><td>Total days</td> <td>$program_total_days</td></tr>";
print STDOUT "</table><p><hr>\n\n";

if (defined $option) {goto DONE;}

by_cruise:
print STDOUT ("<hr><p><h2>Details by year, sorted by cruise</h2>");
foreach $year (sort keys %total_by_year) {
	$program_total_days=0;
	print STDOUT ("<p><table border width=95% \n");
#	print STDOUT ("<caption align=top><h2>Details for $year</h2></caption>\n");
	print STDOUT "<tr> <th>Cruise</th> <th>Start Date</th> <th>End Date</th> " ;
	print STDOUT "<th>PI Name</th> <th>Brief Description</th> <th>Days at Sea</th></tr>";
	for ($i=0; $i < $number_cruises; $i++) { 
		if ($year == $year_p_start[$i]) {
			$day_p_start[$i]=~s/^0+//;	#remove leading zero
			$day_p_end[$i]=~s/^0+//;
			$month_p_start[$i]=~s/^0+//;
			$month_p_end[$i]=~s/^0+//;
			print <<"EOF";
			<tr><td>$project[$i]</td>
			<td>$month[$month_p_start[$i]] $day_p_start[$i], 
				$year_p_start[$i]</td>
			<td>$month[$month_p_end[$i]] $day_p_end[$i], 
				$year_p_end[$i]</td>
			<td>$name_prin[$i]</td><td>$brief_desc[$i]
			<td>$cruise_length[$i]</td></td>
EOF
		}
	}
	print STDOUT<<"EOF1";
	<tr><td></td><td></td><td></td><td></td>
	<td>Total days</td> <td>$total_by_year{$year}</td></tr>
	</table><p><hr>
EOF1
}

if (defined $option) {goto DONE;}

by_date:
print STDOUT ("<hr><p><h2>Details by year, sorted by date</h2>");
foreach $year (sort keys %total_by_year) {
	$program_total_days=0;
	print STDOUT ("<p><table border width=95% \n");
#	print STDOUT ("<caption align=top><h2>Details for $year</h2></caption>\n");
	print STDOUT "<tr> <th>Cruise</th> <th>Start Date</th> <th>End Date</th> " ;
	print STDOUT "<th>PI Name</th> <th>Brief Description</th> <th>Days at Sea</th></tr>";

	$j = -1;
	undef %length;
	for ($i=0; $i < $number_cruises; $i++) { 	#get info on cruise lengths
		if ($year == $year_p_start[$i]) {	#for this year
			$j++;
			$program_total_days=$program_total_days+$cruise_length[$i];
			$jday=&julian($year_p_start[$i],$month_p_start[$i],
					$day_p_start[$i]);
			if ( ! defined $length{$jday}) {
				$length{$jday}=$i;
			}
			else {				#can handle up to 2 dups
				if ( ! defined $length_over1{$jday}) {
					$length_over1{$jday}=$i;
				}
				else {
					if ( ! defined $length_over2{$jday}) {
						$length_over2{$jday}=$i;
					}
					else {
						print STDOUT ("%%Error in ",
						  "summarize_cruise_info.pl - ",
						  "too many cruises have the same",
						  " start date.");
						die "Cannot handle.";
					}
				}
			}
		}	
	}
	
	foreach $j ( sort numerically keys %length) { 	#get pointer indirectly
			$i=$length{$j};			#from the hash table
REPRINT:						#Loop if have dups
			print STDOUT<<"EOF3";
			<tr><td>$project[$i]</td>
			<td>$month[$month_p_start[$i]] $day_p_start[$i], 
				$year_p_start[$i]</td>
			<td>$month[$month_p_end[$i]] $day_p_end[$i], 
				$year_p_end[$i]</td>
			<td>$name_prin[$i]</td><td>$brief_desc[$i]
			<td>$cruise_length[$i]</td></td>
EOF3
			if (defined $length_over1{$j} ) {
				$i=$length_over1{$j};
				undef $length_over1{$j};
				goto REPRINT;
			}
			elsif (defined $length_over2{$j} ) {
				$i=$length_over2{$j};
				undef $length_over2{$j};
				goto REPRINT;
			}
	}
	print STDOUT<<"EOF4";
	<tr><td></td><td></td><td></td><td></td>
	<td>Total days</td> <td>$total_by_year{$year}</td></tr>
	</table><p><hr>
EOF4
}
DONE:
$date=`date`;
chomp($date);
print STDOUT "<p><i>Today's date: $date</i>\n";
print STDOUT ("<br>Program version: $version</i>\n");
print STDOUT "<p></html>\n";

#----------------------------------------------------------------------
sub daysbetween {

#Compute the number of days between the year1, month1, day1, year2, month2, 
#day2 values specied in the call.  Date 2 mudy be after (later)
#than date 1.

# Limitations: dates can be no more than one year appart

my ($jdate1,$jdate2,$diff);

#print STDOUT ("\n**debug, daysbetween, passed values=",$_[0],$_[1],$_[2],
#	$_[3],$_[4],$_[5]);
$jdate1=&julian($_[0],$_[1],$_[2]);
$jdate2=&julian($_[3],$_[4],$_[5]);
#print STDOUT ("\n\n**debug daysbetween, jdate1=$jdate1 and jdate2=$jdate2\n\n");

if ($_[0] > $_[3] || ($_[0] == $_[3] && $_[1] > $_[4]) ||
	($_[0] == $_[3] && $_[1] == $_[4] && $_[2] > $_[5]) ) {
	print ERRORS ("\n\n%%daysbetween error, date 2 before date 1\n\t",
		$_[0]," ", $_[1]," ", $_[2]," ", $_[3]," ", $_[4]," ", $_[5],
		"\n\tReturn zero value.\n\n");
	return 0;
}
#Check if year change
if ($_[0] == $_[3]) {
	return $jdate2 - $jdate1 + 1;
}
elsif ( abs($_[0] - $_[3]) > 1) {
	print ERRORS ("\n\n%%daysbetween limitation, cannot handle more than one year",
		" difference between dates \n\t", $_[0]," ", $_[1]," ", $_[2]," ",
		$_[3]," ", $_[4]," ", $_[5]), "\n\tReturn zero value.\n\n";
	return 0;
}
else {
	$diff=365 - $jdate1 + $jdate2 + 1;
	if (($_[0] % 4) == 0 && ($_[0] % 1000) != 0) {$diff++};
}
return $diff;
}


#-----------------------------------------------------------------------
sub julian {

#Computes pseudo julian day bassed for year, month, day passed parameters

#print STDOUT " \n**debug julian, passed values: $_[0], $_[1], $_[2]\n"; 
my ($i, $julian, @month );
unless ( $_[0] > 0 &&  $_[1] > 0 && $_[1] < 13 && 
	$_[2] > 0 && $_[2] < 32) {
	print ERRORS ("\n%%Error in julian, illegal date: $_[0], $_[1], $_[2]\n");
	print  ("\tReturning zero value");
	return 0;
}
@month=(31,28,31,30,31,30,31,31,30,31,30,31);

$julian=$_[2];
if ($_[1] == 1) {return $julian};
if( $_[0]/4 == int ($_[0]/4) and $_[0]/100 != int ($_[0]/100) ) {
#	print STDOUT " <br>\n**debug julian, passed values: ",
#		"$_[0], $_[1], $_[2]", "\n<br>";
	$month[1]=29;
}
if( $_[0]/4 == int ($_[0]/4) and $_[0]/100 == int ($_[0]/100)
		and $_[0]/400 == int ($_[0]/400) ) {
#	print STDOUT " <br>\n**debug julian, passed values: ",
#		"$_[0], $_[1], $_[2]", "\n<br>";
	$month[1]=29;
}

for ($i=0; $i < $_[1]-1; $i++) {
	$julian=$julian + $month[$i];
}
return $julian;
}

#-----------------------------------------------------------------------

sub numerically{
#Used by sort to sort numerically
$a <=> $b;
}

#-----------------------------------------------------------------------