#!/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 # May 16, 1997 # R. Groman @month=("nd", "Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"); open (ERRORS, ">summarize_cruise_info.errors"); print ("Content-type: text/html\n\n", "\nU.S. GLOBEC Georges Bank Cruise Summary", "

U.S. GLOBEC Georges Bank Cruise Summary", "

\n\n"); #$dataobject="/globec/gb/inventory_test"; #print ("

**debug summarize_cruise_info.pl, dataobject=$dataobject

\n\n"); $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 ("\n

**debug, object=$object\n\n"); $read_data="/data/globec/bin/listgb -f -n -t -c $object "; #print ("\n\n

**debug, read_data=$read_data\n\n"); $i=-1; foreach $record (`$read_data` ) { # print "\n\n

**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 "\n\n

**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; $brief_desc[$i]=~s/(^.*?)_.*/$1/; if ($brief_desc[$i] eq "long") {$brief_desc[$i]="mooring"} # print ("\n\n

**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 ("

\n\n**debug, Number of days=$cruise_length[$i]\n\n"); $i++; } $number_cruises=$i; print ("

There are ", $number_cruises, " cruises listed in the inventory.

\n\n"); 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]; } $program_total_days=0; print ("

Summary by Platform

\n"); print "\n" ; foreach $value (sort keys %total_by_platform) { print "\n"; $program_total_days=$program_total_days + $total_by_platform{$value}; } print ""; print "
Platform Days at Sea
$value $total_by_platform{$value}
Total days $program_total_days


\n\n"; $program_total_days=0; print ("

Summary by Brief Description

\n"); print "\n" ; foreach $value (sort keys %total_by_brief_desc) { print "\n"; $program_total_days=$program_total_days + $total_by_brief_desc{$value}; } print ""; print "
Brief description Days at Sea
$value $total_by_brief_desc{$value}
Total days $program_total_days


\n\n"; $program_total_days=0; print ("

Summary by Year

\n"); print "\n" ; foreach $value (sort keys %total_by_year) { print "\n"; $program_total_days=$program_total_days + $total_by_year{$value}; } print ""; print "
Year Days at Sea
$value $total_by_year{$value}
Total days $program_total_days


\n\n"; print ("

Details by year, sorted by cruise

"); foreach $year (sort keys %total_by_year) { $program_total_days=0; print ("

Details for $year

\n"); print " " ; print ""; 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"; EOF } } print <<"EOF1";
Cruise Start Date End DatePI Name Brief Description Days at Sea
$project[$i] $month[$month_p_start[$i]] $day_p_start[$i], $year_p_start[$i] $month[$month_p_end[$i]] $day_p_end[$i], $year_p_end[$i] $name_prin[$i]$brief_desc[$i] $cruise_length[$i]
Total days $total_by_year{$year}


EOF1 } print ("

Details by year, sorted by date

"); foreach $year (sort keys %total_by_year) { $program_total_days=0; print ("

Details for $year

\n"); print " " ; print ""; $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 ("%%Error in ", "summarize_cruise_info.pl - ", "too many cruises have the same", " start date."); die "Cannot handle."; } } } } } $numb=$j; foreach $j ( sort numerically keys %length) { #get pointer indirectly $i=$length{$j}; #from the hash table REPRINT: #Loop if have dups print <<"EOF3"; 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 <<"EOF4";
Cruise Start Date End DatePI Name Brief Description Days at Sea
$project[$i] $month[$month_p_start[$i]] $day_p_start[$i], $year_p_start[$i] $month[$month_p_end[$i]] $day_p_end[$i], $year_p_end[$i] $name_prin[$i]$brief_desc[$i] $cruise_length[$i]
Total days $total_by_year{$year}


EOF4 } $date=`date`; chomp($date); print "

Today's date: $date\n"; print "

\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 ("\n**debug, daysbetween, passed values=",$_[0],$_[1],$_[2], # $_[3],$_[4],$_[5]); $jdate1=&julian($_[0],$_[1],$_[2]); $jdate2=&julian($_[3],$_[4],$_[5]); #print ("\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 " \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 && ($_[0] % 1000) != 0) { $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; } #-----------------------------------------------------------------------