#!/usr/bin/perl

# PROGRAMED MY JOSHUA PATTERSON
# SPRING OF 2005.

use CGI qw(param);
use LWP::Simple;

&init_vars;
&decoder;

sub decoder {
$data = get("http://asp1.sbs.ohio-state.edu/text/tropical/atlantic/URNT12.KNHC") if($ENV{'REQUEST_METHOD'} eq "GET");
$data = param('data') if($ENV{'REQUEST_METHOD'} eq "POST");
@LINES = split("\n",$data);

print "Content-Type: text/html\n\n";
print <<HTML;
<html>
<head><title>Hurricane Data</title></head>
<body>
<script language="JavaScript">
function data_clear() {
document.recon_data.data.value = "";
}
</script>
<center>
<font face="verdana">
<h2>Vortex Recon Decoder</h2>
</font>
<hr>
</center>
<font face="Verdana" size="2">
HTML

foreach(@LINES){
$line++;

if(/^[A][.]/){
if(/\:/){$new = 1;}
s/[A]. //;
($DATE,$TIME) = split("\/");
if(!$new){
@DIGITS = split(//, $TIME);
$TIME = "$DIGITS[0]$DIGITS[1]:$DIGITS[2]$DIGITS[3]$DIGITS[4]";
}
$TIME =~ s/[Z]/ Zulu/;

$DECODE{'A'} = "<b>Date/Time of Recon Report:</b> $MONTHS[$mon] $DATE, $year $TIME";
}

if(/^[B][.]/){
s/[B]. //;
if($_ !~ /[w]/){$_ = "$_ $LINES[$line]";}
tr/[A-Z]/[a-z]/;

m/(\d+) deg (\d+) min (\D+) (\d+) deg (\d+) min (\D+)/;

if(defined($1)){
$deg1 = $1;
$min1 = $2;
$deg2 = $4;
$min2 = $5;
$D1 = $3;
$D2 = $6;
$D1 =~ tr/[a-z]/[A-Z]/;
$D2 =~ tr/[a-z]/[A-Z]/;
$North = sprintf("%.2f",$deg1+$min1/60);
$West = sprintf("%.2f",$deg2+$min2/60);
}
else {print "NA";}
s/min/'/g;
tr/[a-z]/[A-Z]/;
s/DEG/&deg;/g;
$DECODE{'B'} = "<font color=\"\#669900\"><b>Position Of The Center:</b> $_ ($North&deg; $D1 &nbsp;$West&deg; $D2) </font>";
}

if(/^[C][.]/){
tr/[A-Z]/[a-z]/;
s/[c]. //;
s/ //g;

m/(\D+)mb(\d+)m/;
m/(\d+)mb(\d+)m/;
@DATA = split(/\|/, $BARS{$1});
$level = $1;
$normal = $DATA[1];
if($level eq "na"){$level = "N/A";$normal="N/A";}
$DECODE{'C'} = "<b>Minimum Height Measured At Standard Level Of $level Millibars:</b> $2 Meters (Normal: $normal Meters)\n";
}

if(/^[D][.]/){
s/[D]. //;
tr/[A-Z]/[a-z]/;
s/ //g;
m/(\D+)kt/;
m/(\d+)kt/;
$speed = $1;
if($speed eq "na"){$speed = 0;}
$DECODE{'D'} =  "<font color=\"\#FF6600\"><b>Maximum Surface Winds Were Estimated At:</b> $speed Knots (" .$speed*1.15." MPH)</font>";
}

if(/^[E][.]/){
s/[E]. //;
s/ //g;
tr/[A-Z]/[a-z]/;
m/(\d+)deg(\d+)nm/;
$est = $2;
$est_miles = $est *1.15;
if($est eq ""){$est = "N/A"; $est_miles = "N/A";}

$DECODE{'E'} = "<font color=\"\#FF6600\"><b>Estimated Surface Winds Were Measured At: </b> $est Nautical Miles ($est_miles miles)<b> From Center At Bearing</b> $1&deg;</font>";
}

if(/^[F][.]/){
s/[F]. //;
s/ //g;
tr/[A-Z]/[a-z]/;
m/(\d+)deg(\d+)kt/;
$DECODE{'F'} = "<b>Maximum Flight Level Winds Near Center Were</b> $2 Knots (".$2*1.15." MPH) <b>From</b> $1&deg;";
}

if(/^[G][.]/){
s/[G]. //;
s/ //g;
tr/[A-Z]/[a-z]/;
m/(\d+)deg(\d+)nm/;
$DECODE{'G'} = "<b>Maximum Flight Level Winds Were Measured</b> $2 Nautical Miles (".$2*1.15." Miles) <b>From Center At Bearing</b> $1&deg;";
}

if(/^[H][.]/){
s/[H]. //;
tr/[A-Z]/[a-z]/;
s/ //g;

m/(\d+)mb/;
m/(\D+)(\d+)mb/;
if($2 eq ""){$MSG = "$1 Millibars";$pressure=$1;}
if($2 ne ""){$MSG = "$1 $2 Millibars";$pressure=$2;}

$inches = sprintf("%.3f",$pressure/33.8653);

$DECODE{'H'} = "<font color=\"#3333FF\"><b>Minimum Pressure</b>: $MSG ($inches Inches)</font>";
}

if(/^[I][.]/){
s/[I]. //;
s/\///;
tr/[A-Z]/[a-z]/;
s/ //g;
m/(\d+)c(\d+)m/;
$far = $1 * 9 / 5 + 32;
$DECODE{'I'} = "<b>Maxium Flight Level Temperature / Pressure Altitude Outside The Eye:</b> $1&deg;C ($far&deg;F) <b>/</b> $2 Meters";
}

if(/^[J][.]/){
s/[J]. //;
s/\///;
tr/[A-Z]/[a-z]/;
s/ //g;
m/(\d+)c(\d+)m/;
$far = $1 * 9 / 5 + 32;
$DECODE{'J'} = "<b>Maximum Flight Level Temperature / Pressure Altitude Inside The Eye:</b> $1&deg;C ($far&deg;F) <b>/</b> $2 Meters";
}

if(/^[K][.]/){
s/[K]. //;
s/[C]//;
@COND = split(/\//);
$COND[0] =~ s/[ ]$//;
$COND[1] =~ s/[ ]$//;
$far = $COND[0] * 9 / 5 + 32;
$f2 = $COND[1] * 9 / 5 + 32 if($COND[1] =~ m/[0-9]/);
if($COND[1] !~ /[0-9]/){$COND[1] = "NA";$f2 = "NA";}

$DECODE{'K'} = "<b>Dewpoint Temperature / Sea Surface Temperature Inside The Eye:</b>  $COND[0]&deg;C ($far&deg;F) <b>/</b> $COND[1]&deg;C ($f2&deg;F)";
}

if(/^[L][.]/){
s/[L]. //;
s/\n//;
$DECODE{'L'} = "<b>Eye Wall Was Characterized As Being:</b> $_";
}

if(/^[M][.]/){
s/[M]. //;
s/\n//;
$DECODE{'M'} = "<b>Eye Form Was Characterized As Being:</b> $_" ;
}

if(/^[N][.]/){
s/[N]. //;
($OBV,$LEV) = split(/\//);
@OBVS = split(//, $OBV);
foreach $observ(@OBVS){
$OBVString = "$OBVString $OBSERVES[$observ]" if($observ ne "");
}

@LEVES = split(/ /, $LEV);
foreach $LEVE(@LEVES){
chomp($LEVE);
$LEVString = "$LEVString $LEVELS[$LEVE]" if($LEVE ne "");
}

$DECODE{'N'} = "<b>Center Fix Established Using:</b> $OBVString";
$DECODE{'N2'} = "<b>Center Fix Established At Level(s):</b> $LEVString";
}

if(/^[O][.]/){
s/[O]. //;

s/ //g;
s/nm//;
@ACCURATE = split(/\//);
$NAV = $ACCURATE[0];
$METEO = $ACCURATE[1];
$METEO =~ s/[M-Nm-n]//g;

$DECODE{'O'} = "<b>Navigational Accuracy Measured At:</b> $NAV Nautical Miles";
$DECODE{'O2'} = "<b>Meteorological Accuracy Measured At:</b> $METEO Nautical Miles";
}

if(/^[P][.]/){
s/[P]. //;
@sets = split(/ /);
$sets[0] =~ m/(\D+)(\d+)/;
$flight_info = "$1 \#$2";

$sets[1] =~ m/(\d+)(\d+)(\d+)(\d+)(\D+)/;
$flights = "$1$2";
$storm = "$3$4";
$basin = $5;
$DECODE{'P'} = "<b>Storm $sets[2]: Observed by $flight_info</b>";
$DECODE{'P2'} = "<b>Storm \#$storm In $BASINS{$basin} Ocean</b>";
$DECODE{'P3'} = "<b>Total Flights For Storm \#$storm: $flights</b>";
$comment = $line;
}}




$extLine = "<p> <b>Other Information:</b>";
for($comment...$#LINES){
$comment_count++;
$extLine = "$extLine<br>$comment_count: $LINES[$_]" if($LINES[$_] =~ /[A-Za-z]/);
}
$extLine =~ s/MAX/Maximum/g;
$extLine =~ s/FL/Flight Level/g;
$extLine =~ s/WIND/Winds Were/;
$extLine =~ s/QUAD/Quadrant at/;
$extLine =~ s/TEMP/Temp/;
$DECODE{'PILOT'} = $extLine;


foreach $key(sort(keys(%DECODE))){
print "$DECODE{$ORDER{$key}}<br>\n";
}
chomp($data);
print <<END_OF_HTML;
</font>
<center>
<hr>
<font face="Tahoma" face="3">Enter Your Own Data: (<a 
href="http://asp1.sbs.ohio-state.edu/text/tropical/atlantic/URNT12.KNHC">Vortex Recon</a>)</font>
<br>
<form name="recon_data" action="$ENV{'SCRIPT_NAME'}" method="POST">
<textarea name="data" rows="14" cols="43">$data</textarea>
<p>
<input type="submit" value=" D E C O D E ">
<input type="button" value=" C L E A R" onMouseDown="data_clear();">
</form>
</font>
</center>
</body>
</html>
END_OF_HTML
}

sub init_vars {
%BARS = ();
%LEVELS = ();
%OBSERVES = ();
%BASINS = ();
%ORDER = ();
%DECODE = ();
@MONTHS = ();
$new = 0;
$comment = 0;
@MONTHS = 
('January','February','March','April','May','June','July','August','September','October','November','December');
($s,$m,$h,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime( time );
$year = $year + 1900;

$/ = "\n";
$BARS{'925'} = "2500|762";
$BARS{'850'} = "4780|1457";
$BARS{'700'} = "9880|3011";

$LEVELS[0] = "Surface";
$LEVELS[1] = "1500 Feet";
$LEVELS[2] = "200 Millibars";
$LEVELS[3] = "300 Millibars";
$LEVELS[4] = "400 Millibars";
$LEVELS[5] = "500 Millibars";
$LEVELS[6] = "700 Millibars";#IGNORE
$LEVELS[7] = "700 Millibars";
$LEVELS[8] = "850 Millibars";
$LEVELS[9] = "925 Millibars";

$OBSERVES[1] = "Penetration";
$OBSERVES[2] = "Radar";
$OBSERVES[3] = "Wind";
$OBSERVES[4] = "Pressure";
$OBSERVES[5] = "Temperature";

$BASINS{'A'} = "Atlantic";
$BASINS{'C'} = "Central Pacific";
$BASINS{'E'} = "Eastern Pacific";

$ORDER{'A'} = "P";
$ORDER{'B'} = "P2";
$ORDER{'C'} = "P3";
$ORDER{'D'} = "A";
$ORDER{'E'} = "B";
$ORDER{'F'} = "C";
$ORDER{'G'} = "D";
$ORDER{'H'} = "E";
$ORDER{'I'} = "F";
$ORDER{'J'} = "G";
$ORDER{'K'} = "H";
$ORDER{'L'} = "I";
$ORDER{'M'} = "J";
$ORDER{'N'} = "K";
$ORDER{'N2'} = "L";
$ORDER{'O'} = "M";
$ORDER{'O2'} = "N";
$ORDER{'P'} = "N2";
$ORDER{'P2'} = "O";
$ORDER{'P3'} = "O2";
$ORDER{'PILOT'} = "PILOT";
}

# PROGRAMED MY JOSHUA PATTERSON
# SPRING OF 2005. 
