#!/usr/bin/perl
#
# Gathers information for an HTML surf report.

use strict;
use warnings;
use DateTime;
use LWP;
use LWP::UserAgent;
use LWP::Protocol::https;

##############################
# Configuration
#

my $DEBUG = 0;
my $convert = "/usr/bin/convert";

##############################
# Data Source URLs
#

my $NWS_Coastal_Primary_URL = 
   "https://www.wrh.noaa.gov/pqr";
my $NWS_Coastal_Data_URL =
    "https://tgftp.nws.noaa.gov/data/forecasts/marine/coastal/pz/pzz253.txt";
my $WAM_Index_URL =
     "http://polar.ncep.noaa.gov/waves/viewer.shtml?-multi_1-5_back-NE_pacific-hs-";
my $WAM_Current_URL =
    "https://polar.ncep.noaa.gov/waves/WEB/multi_1.cycle.5_back/plots/NE_pacific.hs.f006h.png";
my $WAM_24hr_URL =
   "https://polar.ncep.noaa.gov/waves/WEB/multi_1.cycle.5_back/plots/NE_pacific.hs.f024h.png";
my $Tide_URL = 
  "http://tbone.biol.sc.edu/tide/sitesel.html";
my $Tide_Data_URL = 
  "http://tbone.biol.sc.edu/tide/tideshow.cgi?type=table;tplotdir=horiz;gx=640;gy=240;caltype=ndp;interval=00%3A01;glen=2;fontsize=%2B0;units=feet;killsun=1;cleanout=1;tzone=local;d_year=;d_month=01;d_day=01;d_hour=00;d_min=00;ampm24=ampm;weekday=1;colortext=black;colordatum=white;colormsl=yellow;colortics=red;colorday=skyblue;colornight=deep-%3Cbr%20%2F%3Eskyblue;colorebb=seagreen;colorflood=blue;site=Southbeach%2C%20Yaquina%20Bay%20and%20River%2C%20Oregon";
my @Buoys_List = ( 46005, 46002, 46050, 46089, 46015 );
my %Buoys_Names = 
  ( 46005, "Offshore N", 46002, "Offshore S", 46050, "Newport", 46089, "Tillamook", 46015, "Port Orford" );
my $Buoys_Data_URL = 
  "https://www.ndbc.noaa.gov/data/realtime2/";
my $Buoys_Index_URL = 
  "https://seaboard.ndbc.noaa.gov/maps/WestCoast_inset.shtml";
my @Wind_Buoys_List = ( "ASTO3", "SBEO3", "NWPO3", "CHAO3", "PORO3" );
my %Wind_Buoys_Names = 
  ( "ASTO3", "Astoria", "NWPO3", "Newport", "SBEO3", "South Beach" , "CHAO3", "Charleston", "PORO3", "Port Orford" );
my $Wind_Buoys_Data_URL = 
  "https://www.ndbc.noaa.gov/data/realtime2/";
my $HMSC_Data_URL =
  "http://cil-www.oce.orst.edu/wavereport";
my $HMSC_Index_URL = 
  "http://cil-www.oce.orst.edu/agate.html";


##############################
# HTTP/HTTPS Utilities
#

# https_save(url, filename)
sub https_save {
    my $url = $_[0];
    my $file_out = $_[1];

    my $ua = LWP::UserAgent->new(); 
    $ua->timeout(600);
    my $response = $ua->get($url);
     
    if ($DEBUG) { print "https_save($url $file_out)\n"; }

    if ($response->is_success) {
	if (!open (HTTPS_OUTFILE, ">$file_out")) {
            if ($DEBUG) {
	        print "Failed to write output file $file_out for https content\n";
            }
	    return 0;
	}
	print (HTTPS_OUTFILE $response->content);
	close HTTPS_OUTFILE;
	return 1;
    } else {
        if ($DEBUG) {
            printf("Failed to retrieve $url - %s\n", $response->status_line);
        }
	return 0;
    }
}


##############################
# Time Management
#

sub getDate {
    my $dt1 = DateTime->now(time_zone=>'America/Los_Angeles');
    my $result1 = sprintf("%s, %s %s, %s", $dt1->day_name, $dt1->month_name, $dt1->day, $dt1->year);
}

sub getTime {
    my $dt1 = DateTime->now(time_zone=>'America/Los_Angeles');
    my$result2 = sprintf("%s, US/Pacific", $dt1->time);
}


##############################
# Data Unit Conversion Utilities
#

sub convertDir {
  my $dir = $_[0];

  if(($dir > 338 && $dir <= 360) || ($dir >= 0 && $dir <= 22)) { return("N"); }
  if($dir > 22  && $dir <= 67 ) { return("NE"); }
  if($dir > 67  && $dir <= 112) { return("E"); }
  if($dir > 112 && $dir <= 157) { return("SE"); }
  if($dir > 157 && $dir <= 202) { return("S"); }
  if($dir > 202 && $dir <= 247) { return("SW"); }
  if($dir > 247 && $dir <= 292) { return("W"); }
  if($dir > 292 && $dir <= 338) { return("NW"); }
  return("--");
}  

sub meters2feet {
  my $m = $_[0];
  
  return($m / .3048);
}

sub mps2knots {
  my $m = $_[0];

  return($m * 1.9438445);
}


##############################
# Data Collection
#

# Arg 1 is the file handler to output to
sub getHMSC {
  my $OUTFILE = $_[0];
  my $in = "";
  my $out = "";
  my ($i, $j, $hgt, $per);

  if ($DEBUG) { print "Getting HMSC\n"; }

  if (!https_save($HMSC_Data_URL, "/tmp/wavereport")) {
     system "rm -f /tmp/wavereport";
     if ($DEBUG) {
         print($OUTFILE "<I>Couldn't retrieve HMSC Data<\/I>\n");
     }
     return;
  }

  open(WRFILE,"/tmp/wavereport") || return;
  while (<WRFILE>) {
    chop($_);
    $out = join(" ", $out, $_);
  }
  close(WRFILE);

  if ($out =~ /No data found for this report/) {
      system "rm -f /tmp/wavereport";
      if ($DEBUG) {
          print($OUTFILE "<I>Couldn't retrieve HMSC Data<\/I>\n");
      }
      return;
  }
  $i = index($out, "Prepared at");
  if ($i < 0) {
      system "rm -f /tmp/wavereport";
      if ($DEBUG) {
          print($OUTFILE "<I>Couldn't retrieve HMSC Data<\/I>\n");
      }
      return;
  }

  #XXX LINUX remove time checking for now

  $i = index($out, "height:");
  $j = index($out, "period:");
  $hgt = sprintf("%.1f", meters2feet(substr($out, $i+7, 6)));
  $per = sprintf("%.1f", substr($out, $j+7, 7));

  print($OUTFILE "<TABLE BORDER>\n");
  print($OUTFILE "<TR ALIGN=LEFT><TD><\/TD><TD><B><FONT SIZE=-1>Height<\/FONT><\/B><\/TD><TD><B><FONT SIZE=-1>Period<\/FONT><\/B><\/TD><\/TR>\n");

  $out = sprintf("<TR ALIGN=LEFT><TD><B><FONT SIZE=-1>Newport<\/FONT><\/B><\/TD><TD><FONT SIZE=-1>%s ft<\/FONT><\/TD><TD><FONT SIZE=-1>%s s<\/FONT><\/TD><\/TR>\n", $hgt, $per);
  print($OUTFILE $out);
  print($OUTFILE "<\/TABLE>\n");

  system "rm -f /tmp/wavereport";
}  

# Arg 1 is the file handler to output to
sub getWind {
  my $OUTFILE = $_[0];
  my $buoynum = 0;
  my $ready = 0;
  my ($wdir,$wspd,$wgst);
  my $URL;

  if ($DEBUG) { print "Getting Wind Buoys\n"; }

  print($OUTFILE "<TABLE BORDER>\n");
  print($OUTFILE "<TR ALIGN=LEFT><TD><\/TD><TD><FONT SIZE=-1><B>Dir<\/B><\/FONT><\/TD><TD><FONT SIZE=-1><B>Speed<\/B><BR>(kts)<\/FONT><\/TD><TD><FONT SIZE=-1><B>Peak<\/B><BR>(kts)<\/FONT><\/TD><\/TR>\n");

  foreach $b (@Wind_Buoys_List)
    {
      $URL = sprintf("%s%s.txt", $Wind_Buoys_Data_URL, $b);
      if (!https_save($URL, "/tmp/$b.txt")) {
         if ($DEBUG) { print "Failed to get Wind Buoy: $URL\n"; }
	  system "rm -f /tmp/$b.txt";
	  next;
      }

      if ($DEBUG) { print "Reading Wind Buoy: $URL\n"; }

      open(BUOYFILE,"</tmp/$b.txt") || return;
      $ready = 0;
      while (<BUOYFILE>) {
	if($ready==1) {
	  my @buoy_data = split(" ", $_);

	  # XXX LINUX Removed time checks for now
	  $wdir = $buoy_data[5];
	  $wspd = $buoy_data[6];
	  $wgst = $buoy_data[7];
	  if ($DEBUG) { print "\t$wspd\t$wgst\t$wdir\n"; }
	  
	  if($wspd eq "MM" || $wspd eq "N/A" || $wspd eq "0") {
	      $wspd = "--";
	  } else {
	      $wspd = sprintf("%.1f", mps2knots($wspd));
	  }
	  if($wgst eq "MM" || $wgst eq "N/A" || $wgst eq "0")
	  { $wgst = "--"; }
	  else { 
	      $wgst = sprintf("%.1f", mps2knots($wgst));
	  }
	  
	  if($wdir eq "MM") { 
	      $wdir = "--";
	  }
	  else { 
	      $wdir = convertDir($wdir);
	  }

	  print($OUTFILE "<TR ALIGN=LEFT><TD><FONT SIZE=-1><B>$Wind_Buoys_Names{$b}<\/B><\/FONT><\/TD>\n");
	  print($OUTFILE "<TD><FONT SIZE=-1>$wdir<\/FONT><\/TD><TD><FONT SIZE=-1>$wspd<\/FONT><\/TD><TD><FONT SIZE=-1>$wgst<\/FONT><\/TD><\/TR>\n");

	  close(BUOYFILE);
	  last;
	}	  

	if(/#yr/) {
	  $ready = 1;
	}
      }

      system "rm -f /tmp/$b.txt";
    }
  
  print($OUTFILE "<\/TABLE>\n");
}

# Arg 1 is the file handler to output to
sub getBuoys {
  my $OUTFILE = $_[0];
  my $out;
  my $buoynum = 0;
  my $ready = 0;
  my ($wvht,$wvper,$wvdir);

  if ($DEBUG) { print "Getting Buoys\n"; }

  print($OUTFILE "<TABLE BORDER>\n");
  print($OUTFILE "<TR ALIGN=LEFT><TD><\/TD><TD><B><FONT SIZE=-1>Height<\/FONT><\/B><\/TD><TD><B><FONT SIZE=-1>Period<\/FONT><\/B><\/TD><\/TR>\n");

  foreach $b (@Buoys_List)
    {
      my $URL = sprintf("%s%s.spec", $Buoys_Data_URL, $b);
      if (!https_save($URL, "/tmp/$b.txt")) {
	  system "rm -f /tmp/$b.txt";
	  next;
      }

      open(BUOYFILE,"</tmp/$b.txt") || return;
      $ready = 0;
      while (<BUOYFILE>) {
	if($ready==1) {
	  my @buoy_data = split(" ", $_);

	  # Check data time
	  # XXX LINUX Removed time checks for now.
	  $wvht = $buoy_data[6];
	  $wvper = $buoy_data[7];
	  $wvdir = $buoy_data[10];

	  # Swell height
	  if(($wvht eq "0") || ($wvht eq "MM")) {
	      $wvht = "--";
	  } else {
	      $wvht = sprintf("%.1f", meters2feet($wvht));
	  }

	  # Swell period
	  if($wvper eq "MM" || $wvper eq "N/A" || $wvper eq "0") { 
	      $wvper = "--";
	  }
	  else {
	      $wvper = "$wvper"; 
	  }

	  # Swell direction
	  if($wvdir eq "MM") { 
	      $wvdir = "--";
	  }
	  else { 
	      if ($wvdir =~ /^\d*$/) {
		  $wvdir = convertDir($wvdir); 
	      }
	  }

	  $out = sprintf("<TR ALIGN=LEFT><TD><B><FONT SIZE=-1>$Buoys_Names{$b}<\/FONT><\/B><\/TD><TD><FONT SIZE=-1>$wvht ft<\/FONT><\/TD><TD><FONT SIZE=-1>$wvper s<\/FONT><\/TD><\/TR>\n");
	  print($OUTFILE $out);

	  close(BUOYFILE);
	  last;
	}	  

	if(/#yr/) {
	  $ready = 1;
	}
      }

      system "rm -f /tmp/$b.txt";
    }
  
  print($OUTFILE "<\/TABLE>\n");
}

# Arg 1 is the file handler to output to
sub getNWSInfo {
  my $OUTFILE = $_[0];
  my $in = "";
  my $out = "";
  my ($i,$j,$k) = (0,0,0);
  my $tonight_first = 0;

  # Get the NWS file 

  if ($DEBUG) { print "Getting NWS Info\n"; }

  if (!https_save($NWS_Coastal_Data_URL, "/tmp/marine.PDX")) {
      system "rm -f /tmp/marine.PDX";
      if ($DEBUG) {
          print($OUTFILE "<I>Couldn't retrieve NWS Forecast<\/I><BR>\n");
      }
      return;
  } 

  open(NWSFILE,"/tmp/marine.PDX") || return;
  while (<NWSFILE>) {
    chop($_);
    $out = join(" ", $out, $_);
  }

  # Extract the Florence section 
  $i = index($out, "PZZ255");

  if($i == $[-1) {
    seek(NWSFILE, 0, 0);
    $i = index($out, "PZZ253");
  }
  if($i == $[-1) {
    seek(NWSFILE, 0, 0);
    $i = index($out, "PZZ275");
  }
  if($i == $[-1) {
    seek(NWSFILE, 0, 0);
    $i = index($out, "PZZ270");
  }
  if($i == $[-1) {
    if ($DEBUG) {
      print "Zone parsing FAILED\n";
    }
    $out = "<I>Couldn't retrieve NWS Forecast<\/I>";
    print($OUTFILE "$out\n");
    close(NWSFILE);
    system "rm -f /tmp/marine.PDX";
    return;
  }

  if ($DEBUG) {
    print "Start: $i\n";
  }

  $j = index($out, "\$\$", $i);
 
  if ($DEBUG) {
    print "End: $j\n";
  }

  $out = substr($out, $i, $j-$i);

  $k = index($out, "\.TODAY\.\.\.");
  if($k == -1) {
     $k = index($out, "\.TONIGHT\.\.\.");
     $tonight_first = 1;
  }
  if($k == -1) {
     $k = index($out, "  \.");
  }
  $out = substr($out, $k, $j-$i-$k);
  $out =~ y/A-Z/a-z/;

  $out =~ s/\.\.\.small craft advisory\.\.\.//g;

  # Convert the times
  $out =~ s/\.today\.\.\./<B>Today:<\/B> /g;
  if($tonight_first == 1) {
    $out =~ s/\.tonight\.\.\./<B>Tonight:<\/B> /g;
  } else {
    $out =~ s/\.tonight\.\.\./<BR><B>Tonight:<\/B> /g;
  }
  $out =~ s/\.sun\.\.\./<BR><B>Sunday:<\/B> /g;
  $out =~ s/\.mon\.\.\./<BR><B>Monday:<\/B> /g;
  $out =~ s/\.tue\.\.\./<BR><B>Tuesday:<\/B> /g;
  $out =~ s/\.wed\.\.\./<BR><B>Wednesday:<\/B> /g;
  $out =~ s/\.thu\.\.\./<BR><B>Thursday:<\/B> /g;
  $out =~ s/\.fri\.\.\./<BR><B>Friday:<\/B> /g;
  $out =~ s/\.sat\.\.\./<BR><B>Saturday:<\/B> /g;
  $out =~ s/\.sun night\.\.\./<BR><B>Sunday Night:<\/B> /g;
  $out =~ s/\.mon night\.\.\./<BR><B>Monday Night:<\/B> /g;
  $out =~ s/\.tue night\.\.\./<BR><B>Tuesday Night:<\/B> /g;
  $out =~ s/\.wed night\.\.\./<BR><B>Wednesday Night:<\/B> /g;
  $out =~ s/\.thu night\.\.\./<BR><B>Thursday Night:<\/B> /g;
  $out =~ s/\.fri night\.\.\./<BR><B>Friday Night:<\/B> /g;
  $out =~ s/\.sat night\.\.\./<BR><B>Saturday Night:<\/B> /g;
  $out =~ s/\.independence day\.\.\./<BR><B>Independence Day:<\/B> /g;
  #$out = join("", $out, "<BR>\n");

  # Convert the wind/swell directrions
  $out =~ s/\.\.\./ \.\.\. /g;
  $out =~ s/ n / N /g;
  $out =~ s/ nw / NW /g;
  $out =~ s/ w / W /g;
  $out =~ s/ sw / SW /g;
  $out =~ s/ s / S /g;
  $out =~ s/ se / SE /g;
  $out =~ s/ e / E /g;
  $out =~ s/ ne / NE /g;
  $out =~ s/ n\./ N\. /g;
  $out =~ s/ nw\. / NW\. /g;
  $out =~ s/ w\. / W\. /g;
  $out =~ s/ sw\. / SW\. /g;
  $out =~ s/ s\. / S\. /g;
  $out =~ s/ se\. / SE\. /g;
  $out =~ s/ e\. / E\. /g;
  $out =~ s/ ne\. / NE\. /g;

  # Capitalize commonly referenced names
  $out =~ s/florence/Florence/g;
  $out =~ s/newport/Newport/g;
  $out =~ s/cape lookout/Cape Lookout/g;
  $out =~ s/cape blanco/Cape Blanco/g;
  $out =~ s/cascade head/Cascade Head/g;
  $out =~ s/cape/Cape/g;
  $out =~ s/disappointment/Disappointment/g;
  $out =~ s/shoalwater/Shoalwater/g;
  $out =~ s/washington/Washington/g;
  $out =~ s/oregon/Oregon/g;
  $out =~ s/california/California/g;

  # Convert some abreviations
  $out =~ s/tstms/thunderstorms/g;

  print($OUTFILE "$out\n");

  close(NWSFILE);
  system "rm -f /tmp/marine.PDX";
}

# Arg 1 is the file handler to output to
sub getWAM {
  my $OUTFILE = $_[0];

  # Get the current and 24-out WAM
  if ($DEBUG) { print "Getting WAM\n"; }

  print($OUTFILE "<B>6 Hours from now:<\/B><BR>\n");
  if (!https_save($WAM_Current_URL, "/tmp/wam06.png")) {
      system "rm -f /tmp/wam06.png";
      system "cp shim.gif ww3-thumb.gif";
      if ($DEBUG) {
          print($OUTFILE "<I>Couldn't retrieve WW3 Image<\/I><BR>\n");
      }
  } 
  else {
      system "$convert -crop 400x200+35+120 +repage -border 1 -bordercolor black /tmp/wam06.png wam06-nwus.png";
      
      print($OUTFILE "<IMG SRC=\"wam06-nwus.png\" ALT=\"6 Hour Swell Model Image\"><BR>\n");
  }

  print($OUTFILE "<B>24 Hours from now:<\/B><BR>\n");
  if (!https_save($WAM_24hr_URL, "/tmp/wam24.png")) {
      system "rm -f /tmp/wam24.png";
      if ($DEBUG) {
          print($OUTFILE "<I>Couldn't retrieve WW3 Image<\/I><BR>\n");
      }
  } 
  else {
      system "$convert -crop 400x200+35+120 +repage -border 1 -bordercolor  black /tmp/wam24.png wam24-nwus.png";
      print($OUTFILE "<IMG SRC=\"wam24-nwus.png\" ALT=\"24-Hour Swell Model Image\"><BR>\n");
  }

  print($OUTFILE "<IMG SRC=wam_key.png ALT=\"WAM Key\"><P>\n");

  system "rm -f /tmp/wam06.png /tmp/wam24.png";
}

sub getTide {
  my $OUTFILE = $_[0];
  my ($dummy1,$date,$time,$ampm, $dummy2, $height, $dummy3, $tide);

  if ($DEBUG) { print "Getting Tide Data\n"; }

  if (!https_save($Tide_Data_URL, "/tmp/tidetable")) {
     system "rm -f /tmp/tidetable";
     if ($DEBUG) {
         print($OUTFILE "<I>Couldn't retrieve tide table<\/I>\n");
     }
     return;
  }

  open(TIDEFILE,"/tmp/tidetable") || return;

  print($OUTFILE "<TABLE BORDER>\n");
  print($OUTFILE "<TR ALIGN=LEFT><TD><\/TD><TD><FONT SIZE=-1><B>Date<\/B><\/FONT><\/TD><TD><FONT SIZE=-1><B>Time<\/B><\/FONT><\/TD><TD><FONT SIZE=-1><B>Height<\/B><BR>(ft)<\/FONT><\/TD><\/TR>\n");

  while(<TIDEFILE>) {
    if(/High Tide/ || /Low Tide/) {
      if ($DEBUG) { print $_; }
      ($dummy1,$date,$time,$ampm, $dummy2, $height, $dummy3, $tide) = 
	split(" ", $_);
      if($ampm eq "AM") {
        $ampm = "a";
      } else {
        $ampm = "p";
      }
      $time = join("", $time, $ampm);
      $time =~ y/A-Z/a-z/;

      print($OUTFILE "<TR ALIGH=LEFT><TD><FONT SIZE=-1><B>$tide<\/B><\/FONT><\/TD><TD><FONT SIZE=-1>$date<\/FONT><\/TD><TD><FONT SIZE=-1>$time<\/FONT><\/TD><TD><FONT SIZE=-1>$height<\/FONT><\/TD><\/TR>\n");
    }
  }
  print($OUTFILE "<\/TABLE><BR>\n");
  close(TIDEFILE);
  system "rm -f /tmp/tidetable";
}

#links{row,col}  from 1 to $links_maxrow and 1 to $links_maxcol
my %links_txt;
my %links_URL;
my $links_maxrow = 6;
my $links_maxcol = 2;

$links_txt{1,1} = "Oregon Surfrider";
$links_URL{1,1} = "http:\/\/www.surfrider.org\/oregon\/";
$links_txt{2,1} = "Oregon Surf Page";
$links_URL{2,1} = "http:\/\/www.oregonsurf.com\/";
$links_txt{3,1} = "Oregon Climate Service";
$links_URL{3,1} = "http:\/\/www.ocs.orst.edu\/";
$links_txt{4,1} = "National Weather Service";
$links_URL{4,1} = "http:\/\/www.wrh.noaa.gov\/pqr\/marine.php";
$links_txt{5,1} = "ORST Surf Page";
$links_URL{5,1} = "http:\/\/cil-www.oce.orst.edu\/agate.html";
$links_txt{6,1} = "buoyweather.com";
$links_URL{6,1} = "http:\/\/buoyweather.com";
$links_txt{1,2} = "Coastal Marine Forecast";
$links_URL{1,2} = $NWS_Coastal_Primary_URL;
$links_txt{2,2} = "Buoy Reports";
$links_URL{2,2} = $Buoys_Index_URL;
$links_txt{3,2} = "NOAA WW3 Swell Models";
$links_URL{3,2} = $WAM_Index_URL;
$links_txt{4,2} = "Wind Observations";
$links_URL{4,2} = $Buoys_Index_URL;
$links_txt{5,2} = "Tide Predictor";
$links_URL{5,2} = $Tide_URL;
$links_txt{6,2} = "Magic Seaweed"; 
$links_URL{6,2} = "http:\/\/magicseaweed.com";


sub getLinks {
  my $OUTFILE = $_[0];
  my ($i, $j);

  print($OUTFILE "<TABLE>\n");

  for($i=1;$i<=$links_maxrow;$i++) {
    print($OUTFILE "<TR>\n");
    for($j=1;$j<=$links_maxcol;$j++) {
      print($OUTFILE "<TD><A HREF=\"$links_URL{$i,$j}\"><FONT SIZE=-2>$links_txt{$i,$j}<\/FONT><\/A><\/TD>\n");
    }
    print($OUTFILE "<\/TR>\n");
  }
  print($OUTFILE "<\/TABLE>\n");

}


sub SurfCheck {
  my $SURFCHECKFILE;

  open($SURFCHECKFILE, ">surfcheck-tmp.html") || die;
  
  print($SURFCHECKFILE "<HTML>\n"); 
  print($SURFCHECKFILE "<HEAD>\n");
  print($SURFCHECKFILE "<TITLE>One-Stop Oregon Surf Check<\/TITLE>\n");
  print($SURFCHECKFILE "<META HTTP-EQUIV=\"Refresh\" CONTENT=3600>\n");
  print($SURFCHECKFILE "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n");
  print($SURFCHECKFILE "<\/HEAD>\n"); 
  print($SURFCHECKFILE "<BODY BGCOLOR=\"#FFFFFF\">\n");
  print($SURFCHECKFILE "<CENTER><TABLE WIDTH=640 BORDER=0><TR><TD>\n");
  print($SURFCHECKFILE "<TABLE WIDTH=640>\n<TR>");
  print($SURFCHECKFILE "<TD WIDTH=450><FONT SIZE=+3><IMG SRC=\"sctitle.gif\" ALT=\"Oregon Surf Check\"><\/FONT><\/TD>\n");
  print($SURFCHECKFILE "<TD WIDTH=190><FONT SIZE=+2>Data Updated:<\/FONT><BR>");
  print $SURFCHECKFILE getDate(), "<BR>";
  print $SURFCHECKFILE getTime();
  print($SURFCHECKFILE "\n");
  print($SURFCHECKFILE "<\/TD><\/TR><\/TABLE><HR>\n");
  #print($SURFCHECKFILE "<B><I><FONT COLOR=\"#FF0000\" SIZE=+1>Site is now updating...still rebuilding the rest.</FONT></I></B><HR>\n");

  print($SURFCHECKFILE "<TABLE><TR VALIGN=TOP><TD WIDTH=190>\n");

  print($SURFCHECKFILE "<B>Buoy Reports<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$Buoys_Index_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR>\n");
  getBuoys($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<B>ORST Microseismometer<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$HMSC_Data_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR>\n");
  getHMSC($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<B>Wind<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$Buoys_Index_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR>\n");
  getWind($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<B>Tides at Newport<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$Tide_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR>\n");
  getTide($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<\/TD><TD WIDTH=450>");

  print($SURFCHECKFILE "<B>National Weather Service Forecast<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$NWS_Coastal_Primary_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR><BR>\n"); 
  getNWSInfo($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<B>NOAA Wave Watch 3 Swell Models (WW3)<\/B>\n");
  print($SURFCHECKFILE "<A HREF=$WAM_Index_URL><IMG BORDER=0 SRC=\"linkarrow.gif\" ALT=\"[Source Data]\"><\/A><BR>\n");
  getWAM($SURFCHECKFILE);
  print($SURFCHECKFILE "<P>\n"); 

  print($SURFCHECKFILE "<CENTER><B>Links<\/B><BR>\n");
  getLinks($SURFCHECKFILE);
  print($SURFCHECKFILE "<\/CENTER><P>\n");

  print($SURFCHECKFILE "<\/TD><\/TR><\/TABLE>");
  print($SURFCHECKFILE "<HR><IMG SRC=\"linkarrow.gif\"> clickable links to source data<P><I><A HREF=\"mailto:kurtwindisch\@yahoo.com\">kurtwindisch\@yahoo.com<\/A><\/I>\n");
  print($SURFCHECKFILE "<\/CENTER><\/TD><\/TR><\/TABLE>\n");
  print($SURFCHECKFILE "<\/BODY><\/HTML>\n");
  close $SURFCHECKFILE;
}

# MAIN
SurfCheck();

