#!/bin/perl

# Script to control decoding of buoy met reports.
# Outputs: for each buoy, a file $NAME.latest-met.html
#                       , a file $NAME.latest-met.text
#          a list of all the buoys (updates from previous list)

# Requires
use LWP::Simple;
require "decode_buoy.pl";
require "split_gts_synop.pl";

# Get parameters
# Base directory for output files (don't change this!)
# $BASE="/data/web/htdocs/icd/metlog/";
$BASE="/users/icd/wmc/np/metlog/";

# FILE - defaults to hour.raw
$FILE='hour.raw';
$FILEBASE='file:/users/icd/cmet/oper/data/';
eval "\$$1=\$2" while $ARGV[0] =~ /^(\w+)=(.*)/ && shift;

# Current date (with hour)
($DATENOW="(".`date +"%y/%h/%d:%H"`.")")=~s/\n//;

# Read in the old list-of-buoys-decoded file
open(LOSD,$BASE."buoys-met.html");
while ($text=<LOSD>) {
  $text=~s/\n$//;
  if ($text=~/^<li>/) {
    ($NAME)=($text=~/\[([^;]+);/);
    $NAMES{$NAME}=$text;
    if ($DEBUG) { print "Old $NAME $text\n" };
  };
};
close($LOSD);

# Get the latest hours reports, split into individual messages (but not decoded)
@text=split_gts_synop(get($FILEBASE.$FILE));

# Go through the messages and decode the BBXX ones.
# For each message, write $NAME.latest-met.html, and update
# the entry in $NAMES
foreach $synop (@text) {
  if ($synop=~/^ZZYY/) {
    ($NAME,$YEAR,$MON,$DAY,$HOUR,$LA,$LO,$T,$DIR,$SPD,$IW,$CLOUD,$ICE,$DEWT,$PSTA,$PRED,$PTND_code,$PTND_value,$PPN_code,$PPN_value)=decode_buoy($synop);

# Only interested in buoys south of 20 S
    if ($LA < -20) {

# Record the name and date
      $NAMES{$NAME}="<li>[$NAME; $LA oS, $LO oE] <a href=latest-met/$NAME.latest-met.html>$YEAR/$MON/$DAY:$HOUR</a> $DATENOW";

# The html output file
      open (OUT, "> ".$BASE."latest-met/".$NAME.".latest-met.html");
      if ($ICE ne "null") { $ICE="<li>Ice : $ICE" } else { undef $ICE };
      $CLOUD="<li>Cloud (oktas): $CLOUD";
      $WIND="<li>Wind Dir, Speed, Unit: $DIR $SPD $IW";
      $T="<li>T: $T ";
      $TD="<li>Dew T: $DEWT";
      $LOC="Lat: $LA Lon: $LO";
      $PRES="<li>Buoy pressure: $PSTA; MSLP: $PRED; PTND: $PTND_value ($PTND_code)";
      if ($PPN_code ne "null" ) { $PPN="<li>PPN: $PPN_value ($PPN_code)" };
      $TRAIL="Created by decode_buoys.pl at ".`date`;
   print OUT <<EOF;
<h1>Latest Meteorological report for buoy: $NAME</h1>
<p>Buoy: $NAME Date $YEAR/$MON/$DAY:$HOUR 
$LOC;
<p>
<ul>
  $PRES
  $T
  $TD
  $WIND
  $CLOUD
  $PPN
  $ICE
</ul>
<p><hr>
<p>
Actual Synoptic Report:

  <pre>$synop</pre>

<hr><p>
$TRAIL
EOF
      close (OUT);

# The text output file (used by map-drawing progs)
      open (OUT, "> ".$BASE."latest-met/".$NAME.".latest-met.buoy.text");
      print OUT "$NAME\n$YEAR $MON $DAY $HOUR\n$LA $LO\n$T $DEWT\n$PSTA $PRED\n$DIR $SPD\n$CLOUD\n";
      close (OUT);

    }
  }
};

# Re-write the list-of-buoys-decoded file
open(LOSD,"> ".$BASE."buoys-met.html");
print LOSD "<h1>List of buoys with Met.</h1><p>The following list is of buoys call signs. The last available met report is given (day of month:hour) which is a hyperlink. The date in brackets is the date when the decoding was done.<p>Note that the met reports are only very sketchily error-checked.<p><ul>\n";
# For buoys, don't reverse the sort order as we do with SYNOPs
foreach $K (sort keys(%NAMES)) {
  if ($DEBUG) { print "New: $NAMES{$K}\n" };
  print LOSD "$NAMES{$K}\n"
};
print LOSD "</ul><p><hr>Page by: <a href=/public/icd/wmc/>W. M. Connolley</a><br>Created at: ".`date`."by decode_buoys.pl";
close (LOSD);

# Construct (text) list of buoys decoded in last 2 days
open(LOSD2,"> ".$BASE."buoys-met.text");
@text=split("\n",`find $BASE -follow -name "*.latest-met.buoy.text" -mtime -1`);
while ($text=shift @text) {
  $text=~s/^.*\///;
  print LOSD2 $text,"\n"
};
close (LOSD2);

# Thats all folks!
exit;
