#!/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="June 9, 2005/V1.31"; # R. Groman # 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 ($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", "\nU.S. GLOBEC Georges Bank Cruise Summary", "

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

\n\n"); #$dataobject="//globec.whoi.edu/globec/gb/inventory_test"; #print STDOUT ("

**debug summarize_cruise_info.pl, dataobject=$dataobject

\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

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

**debug, read_data=$read_data\n\n"); $i=-1; foreach $record (`$read_data` ) { # print STDOUT "\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 STDOUT "\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; # 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

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

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

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

\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) {goto $option;} summary: #summary loop $program_total_days=0; print STDOUT ("

Summary by Platform

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


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

Summary by Brief Description

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


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

Summary by Year

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


\n\n"; if (defined $option) {goto DONE;} by_cruise: print STDOUT ("

Details by year, sorted by cruise

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

Details for $year

\n"); print STDOUT " " ; print STDOUT ""; 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 STDOUT<<"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 } if (defined $option) {goto DONE;} by_date: print STDOUT ("

Details by year, sorted by date

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

Details for $year

\n"); print STDOUT " " ; print STDOUT ""; $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"; 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";
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 } DONE: $date=`date`; chomp($date); print STDOUT "

Today's date: $date\n"; print STDOUT ("
Program version: $version\n"); print STDOUT "

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