#!/bin/perl

# Simple wrapper for decode_synop.pl
#
# Use: decode_synops.pl FILE=filename
#
# filename defaults to hour.raw
#
#   Copyright W. M. Connolley July 2003. wmc@bas.ac.uk
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details, available from:
#
#   http://www.gnu.org/licenses/gpl.txt.

# Requires
require "decode_synop.pl";
require "split_gts_synop.pl";

# Options
$FILE="hour.raw";
eval "\$$1=\$2" while $ARGV[0] =~ /^(\w+)=(.*)/ && shift;
if ($DEBUG) { $|=1 };

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

# Get the latest hours reports, split into individual messages (but not decoded)
if ($DEBUG) { print "Reading in: $FILE" };
$TEXT=`cat $FILE` or die "Failed to get $FILE";
if ($DEBUG) { print " (file length: ".length($TEXT).")\n" };
@text=split_gts_synop($TEXT);

# Go through the messages and decode the AAXX ones.
# For each message, write $NAME.latest-met.html, and update
# the entry in $NAMES
loop: foreach $synop (@text) {
  if ($DEBUG > 1) { print "Message: $synop\n" };
  if ($synop=~/^AAXX/) {
    ($NAME,$YEAR,$MON,$DAY,$HOUR,$LA,$LO,$T,$DIR,$SPD,$IW,$CLOUD,$ICE,$DEWT,$PSTA,$PRED,$PTND_code,$PTND_value,$PPN_code,$PPN_value,$ST_TYPE,$WW,$W1,$W2,$NH,$CL,$CM,$CH,$CLOUD_H)=decode_synop($synop);
# Check: have we decoded this already? If so, only use if its later
    if ($DONE{$NAME}) {
      $YMDH="$YEAR$MON$DAY$HOUR";
      if ($DONE{$NAME} > $YMDH) {
        if ($DEBUG) { print "Not replacing $NAME $DONE{$NAME} since $YEAR$MON$DAY$HOUR is earlier\n" };
        next loop;
      };
    };
    $DONE{$NAME}="$YEAR$MON$DAY$HOUR";

# Debug?
    if ($DEBUG) { print "$NAME,$YEAR,$MON,$DAY,$HOUR\n" };

# Reject of ID NAME isn't 5-digits
    next unless $NAME=~/^\d{5}$/;

# Weather
#  Past
    @Wx=("Cloud le 1/2 sky throughout", "Cloud partly gt 1/2 sky and partly le 1/2 sky",
         "Cloud gt 1/2 sky throughout", "Blowing snow (or sand or dust)",
         "Ice fog, fog or thick haze",  "Drizzle",
         "Rain",                        "Snow, or rain/snow",
         "Shower(s)",                   "Thunderstorm(s) w or w/o ppn");
    if ($W1 ne "null") { $W1=$W1." (".$Wx[$W1].")" };
    if ($W2 ne "null") { $W2=$W2." (".$Wx[$W2].")" };
#  Present
    @Wy=("No ppn, etc (on the whole)",  "No ppn, etc (on the whole)",
         "Ppn etc during prev hour",    "Blowing snow (or sand or dust)",
         "Ice fog, fog",                "Drizzle",
         "Rain",                        "Solid ppn not in showers",
         "Showery/Thunderstorm",        "Showery/Thunderstorm");
    ($WW1)=($WW=~/(\d)./);
    if ($WW1) { $WW1=$WW." (".@Wy[$WW1].")" } else { $WW1="null" };

# The html output file
    if ($ICE ne "null") { $ICE="Ice : $ICE" } else { undef $ICE };
    if ($CLOUD ne "null") {
      $Cloud="Cloud (oktas): $CLOUD";
      if ($CL ne "null" or $CM ne "null" or $CH ne "null") { $Cloud.= ", $CL (Low Type), $CM (Mid Type), $CH (High Type), (Base (m): $CLOUD_H)" };
    } else {
      $Cloud="No cloud info" 
    };
    $WIND="Wind Dir, Speed, Unit: $DIR $SPD $IW";
    $T1="T: $T ";
    $TD="Dew T: $DEWT";
    $PRES="Station pressure: $PSTA; MSLP: $PRED; PTND: $PTND_value ($PTND_code)";
    if ($PPN_code ne "null" ) { $PPN="Precipitation: $PPN_value ($PPN_code)" } else { undef $PPN } ;
    if ($W1.$W1.$WW eq "nullnullnull") {
      $WEATHER="No weather info";
    } else {
      $WEATHER="Weather: Present: $WW1 / Past: $W1, $W2"
    };

    print "$NAME Date $YEAR/$MON/$DAY:$HOUR $PRES $T1 $TD $WIND $Cloud $PPN $WEATHER\n";

  }
};

# Thats all folks!
exit;
