#!/usr/bin/perl
{
   $flag = 0;   # set to 1 when file opened.
		# Converts Jan to 01, etc.
   @months = ("01", "02", "03", "04", "05", "06", "07", "08", "09",
	   	"10", "11", "12");
   $filename = "../../tides.shtml"; #name of master tide file
   $tidelist = "Tidelocations";	# list of tide locations and files to process
   $file = "";			# holds the name of the current location being processed
   $index = "";			# hold the lines for the tide index page

		# get date, will only print tide predictions from today
  $date = &get_date;
  $new_date = &new_date ($date);
  		# read in all the sunrise/sunset files and put them in a hash table
		# based on the filename
  @files = `ls ../sun/*.htm`;
  foreach $sfile (@files) {
	chomp ($sfile);
  	unless (open IN2, "<$sfile") {
	   	die "Cannot open the file $sfile\n";}
	$flag = 0;
	while ( <IN2> ) {
			# the year is on the line with Set and Sun, get it
		if (/Rise and Set/){
			s/^.*Sun for//;
			s/\s*U\..*//;
			s/\s//g;
			$year = $_;
		}
			# skip other lines that are before the first sun rise date.
		next if (!$flag && !/^01/);
		$flag = 1;
			# extra spaces on a sunrise line means that that end of month
			# doesn't have 31 days.  Fill with a '-'
		s/         / &nbsp;&nbsp;&nbsp;- &nbsp;&nbsp;&nbsp;-/g;
		s/\s+U\..*//;
		s/\s+/ /g;
		@line = split (" ");
			# now read the first line, goes day for each month
		$day = shift(@line);
		$j = 0;
		for ($i=0; $i<24; $i=$i+2) {
			$filen = $sfile;
			$filen =~ s,../sun/,,;
			$filen =~ s/\..*//;
			$key = "d" . $months[$j] . $day . $year;
			$value = "<td class='syell'>$line[$i]</td><td class='sblue'>$line[$i+1]</td>";
			$sunrise{$filen}{$key} = $value;
			$j = $j + 1;
		}
	}
  }


		# open file to read in list of tides locations to process
  unless (open INPUT, "<$tidelist") {
	   die "Cannot open the file $tidelist\n";}
  my $fl = 0;
  while ( <INPUT> ) {
	  chomp;
	  	# skip if blank line or a comment line
	  next if  (/^$/);
	  next if (/^#/);

		 	# open the file to process.  If it is a title line or
			# a data line (file name to process).
	  if (/.txt/) {
		  	# the first element is the tide file, the second is the
			# sunrise/sunset file to use.  Save that for later use
			# when printing the data
		($_, $sun) = split("\t");
		$sun =~ s/\..*//;
		$temp = "<../data/$_";
  		unless (open INF, "<../data/$_") {
	   		die "Cannot open the tide file $_\n";}
		s/txt/shtml/;
			# save the file name for use on index page
		$link = $_;
			# open the html file
  		unless (open OUT, ">../$_") {
	   		die "Cannot open the file $_\n";}
		&process_tides($_);
		&print_end;
		close OUT
	 	
	   }else{
		 # Add the current title to the index for later printing
		 # but first check for the ending </ul>.  Needed only if
		 # not the first time
		 if ($fl) {$index = $index . "</ul>\n";}
		 $fl = 1;
		 $index = $index . "<h3>$_</h3>\n <ul>\n";
	   }
  }

		# now create index.html page, this page has been created from the
		# titles from the "Tidelocation" file and the "StateName"
		# from the tide files.
	unless (open OUT1, ">$filename") {
	   die "Cannot open the file $$filename\n";}
	&print_head_index;
	print OUT1 $index;
	&print_end_index;
	close OUT1;

	
}
sub process_tides {
	# subroutine to read in and process the tide charts for a specific location
	local $month, $year, $title;
	my @line;			# holds the tide data line
	my $flag = 1;
	my $td = "<td>";		# html column start
	my $ths = '<td class="hblue">';	# html column start of high cell
	my $thhs= '<td class="hturq">';	# html column start of extra high cell
	my $tls = '<td class="lgold">';	# html column start of low cell
	my $tend = '</td>';		# html column end
	my $new = "\n";			# new line
			# skip through header infomation
	while ( <INF> ) {
		chomp;
		s/^\s//;
		next if (/^$/);
		if (/StationName:/ && !/Reference/) {
			(my $dum, $title) = split (":");
			$title =~ s/^\s+//;
			$index = $index . '<li><a href="' . "tides/$link" . '"' .
				"> $title</a></li>\n";
		}
		last if (/Date/);
	}
			# print beginning html page information
	&print_head;
			# now process the data
	my $last = 0;
	$num = 0;
	while ( <INF> ) {
		chomp;
			# want to get rid of space between time and AM/PM
		s/ AM/AM/;
		s/ PM/PM/;
			# convert spaces to a character for the split
		s/\s+/;/g;
		@line = split(";", $_);
			# Get rid of tide data that is before today's date
		my $dd = $line[0];
		$dd =~ s,/,,g;
		next if ($dd < $new_date);

			# tide data runs across multiple lines, if it is the
			# first time, need to print the <tr>, etc.
		if ($line[0] eq $last) {
				# process more data for the date
			if ($line[5] =~ /L/) {
				$tideline = $tideline .
					"$tls $line[2] $tend $tls $line[3] $tend $new";
				$num ++;
			}else{
					# I want extra high tides to be a different color
				$hightd = $ths;
				if ($line[3] > 7.) {$hightd=$thhs;}
				$tideline = $tideline .
					"$hightd $line[2] $tend $hightd $line[3] $tend $new";
				$num ++;
			}
				
		}else{
				# First tide line for a day and maybe the first
				# tide line of the chart. Print out the end of
				# row, if there was a previous day's tide
			if ($last != 0) {
					# check to see if I have filled the whole line
				&fill_line ($num);
					# not a new chart, just a new day
				print OUT $tideline;
				print OUT "</tr><tr>\n";
				$tideline = "";
				$num = 0;
			}else{
					# new chart
				$tideline = "<tr>\n";
			}
				# reverse date, want month first
			$last = $line[0];
			($y, $m, $d)  = split ("/", $line[0]);
			$line[0] = $m . "/". $d . "/" . $y;
			$key = $line[0];
			$key =~ s,/,,g;
				# put date and day of week out
			$tideline = $tideline .
				"$td $line[0] $tend $td $line[1] $tend $new";
				# My chart has an H first, if a low, blank out the 
				# first H field
			if ($line[5] =~ /L/) {
				$tideline = $tideline . 
					" $ths &nbsp;&nbsp;&nbsp;- $tend $ths &nbsp;&nbsp;&nbsp;- $tend\n";
				$num ++;
				$tideline = $tideline . 
					"$tls $line[2] $tend $tls $line[3] $tend $new";
				$num ++;
			}else{
				# add in the tide information
				$hightd = $ths;
				if ($line[3] > 7.) {$hightd=$thhs;}
				$tideline = $tideline . 
					"$hightd $line[2] $tend $hightd $line[3] $tend $new";
				$num ++;
			}
		}

	}
	&fill_line ($num);
	print OUT $tideline;
	print OUT "</tr>\n";
}
sub fill_line {
# I want to make sure all five tide table entries are filled because sunrise and
# sunset need to be in 6 and 7
#
	my $i;
	for ($i = $_[0]; $i < 5; ++$i) {
		$tideline = $tideline . 
			"<td> &nbsp;&nbsp;&nbsp;- </td> <td> &nbsp;&nbsp;&nbsp;- </td>\n";
	}

	$key = "d" . $key;
	my $value = $sunrise{$sun}{$key};
	$tideline = $tideline . $value . "\n";
}
sub get_date {
	my $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst;
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
	
		# want day to have a leading zero
	if ($mday < 10) {$mday = "0" . $mday;}
	$mon = $mon+1;
	if ($mon < 10) {$mon = "0" . $mon;}
	$year = $year+1900;
	$date = "$mon/$mday/$year";
}
sub new_date {
# reformats date for mm/dd/yyyy to yyyy/mm/dd for sorting and comparisons
  my @fields, $date_num;
  @fields = split/\//, $_[0];
  $date_num = $fields[2] . $fields[0] . $fields[1];
}

sub print_head {
# html header information
#
	print OUT <<HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name ="keywords" content="San Francisco Bay Tides">
<meta name ="description" content="Pictures of $_[0]">
<link rel='stylesheet' type='text/css' href='tides.css'>
<title>$title Tide Tables</title>
</head>
<body>
<h1>$title Tide Tables</h1>
<table>
<tr><td>Date</td><td>Day</td>
<td class="hblue">High Tide</td>
<td class="hblue">Level</td>
<td class="lgold">Low Tide</td>
<td class="lgold">Level</td>
<td class="hblue">High Tide</td>
<td class="hblue">Level</td>
<td class="lgold">Low Tide</td>
<td class="lgold">Level</td>
<td class="hblue">High Tide</td>
<td class="hblue">Level</td>
<td class="syell">Sunrise</td>
<td class="sblue">Sunset</td>
</tr>
HTML
}
sub print_end {
# html ending information
	print OUT <<HTML;
</table>
</body>
</html>
HTML
}
sub print_head_index {
	print OUT1 <<HTML;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name ="keywords" content="San Francisco Bay Tides">
<meta name ="description" content="Tide tables for San Francisco Bay Area">
<title>$place Tide Tables</title>
<link rel=stylesheet href="birds.css" type="text/css">
</head>
<body>
  <div id="wrapper"><!-- #wrapper -->
    <header id="header">
     <div id = "search">
           <!-- Google Search -->
       <form id="cse-search-box" action="http://google.com/cse">
          <input type="hidden" name="cx" value="012404667295668730505:u20imb620cg" />
          <input type="hidden" name="ie" value="UTF-8" />
          <input type="text" name="q" size="26" />
          <input type="submit" name="sa" value="Search" />
       </form>
     </div>
    </header>
	<nav><!-- top nav -->
	    <div class="menu">
	        <ul>
		<li><a href="index.shtml">Home</a></li>
		<li><a href="class_index.shtml">Bird Index</a></li>
		<li><a href="class_scien.shtml">Scientific Name Index</a></li>
		<li><a href="bird_Favorites.shtml">Favorites</a></li>
		<li><a href="places.shtml">Places to Go</a></li>
		<li><a href="bird_Latest.shtml">Recent</a></li>
		<li><a href="tides.shtml">Tides</a></li>
		<li><a href="tongues.shtml">Tongues</a></li>
        <!--
		<li><a href="videos.shtml">Videos</a></li>
        -->
		<li><a href="Bibliography.shtml">Bibliography</a></li>
		</ul>
	    </div>
	</nav><!-- end of top nav -->
	<!-- left menu lines -->
	<section id="main"><!-- #main content and sidebar areas -->
            <aside id="sidebar1"><!-- sidebar1 -->
             <!--#include virtual="birds_menu.html"-->
            </aside>
	
            <section id="content_main">
               <article>
                   <a name="atop" id="atop"></a>
                   <h1>Local Tide Tables</h1>
                   <p class="p_desc p_desc1">
My re-working of the NOAA tide tables are for my favorite areas
in the San Francisco Bay Area and other places I travel to.
My goal was to easily see when a high or low tide was happening,
especially on my iPhone.
I also wanted to know when extra high tides (>7.) was, so those entries are
a darker turquoise.
Since sunrise/sunset information is not available for all towns, I used a city
that was close for the sunrise and sunset information.
</p>

HTML
}
sub print_end_index {
# html ending information
	print OUT1 <<HTML;
</ul>
        </article>
        </section>
        <footer>
	    <section id="footer-area">
	        <aside class="footer-segment">
		    <h4>nej@vdn.com</h4>
		</aside><!-- end of #first footer segment -->

		<aside class="footer-right">
		    <h4>Copyright 2006, Nancy Johnston </h4>
	        </aside><!-- end of #second footer segment -->

    	    </section><!-- end of footer-area -->
	</footer>
	
</div><!-- #wrapper -->
</body>
</html>
HTML
}
