Wednesday, August 27, 2008

Perl: Make KML from NOAA Forecast

I wanted a simple way to make GoogleMaps KML to show forecasted storm tracks. The forcasted storm tracks can be downloaded from the National Hurricane Center and generally look like this. This is today's forecast for TS Gustav:


INITIAL 27/1500Z 18.8N 74.0W 50 KT
12HR VT 28/0000Z 19.0N 74.8W 55 KT
24HR VT 28/1200Z 19.2N 76.0W 60 KT
36HR VT 29/0000Z 19.3N 77.5W 70 KT
48HR VT 29/1200Z 19.9N 79.4W 80 KT
72HR VT 30/1200Z 21.5N 83.0W 100 KT
96HR VT 31/1200Z 24.5N 86.0W 100 KT
120HR VT 01/1200Z 28.5N 88.5W 100 KT

So I wrote a Perl program to translate that into a KML file. The program looks like this:

use strict;
#Get the storm name from the user
print "Storm Name? ";
my $storm = <STDIN>;
chomp($storm);

#Print out the KML header
print << "STARTLINE";
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>$storm</name>
<open>1</open>
<description>NOAA $storm track</description>
<Folder>
<name>Placemarks</name>
<description>$storm</description>
<Placemark>
<name>$storm</name>
<LineString>
<tessellate>1</tessellate>
<coordinates>
STARTLINE

# Build kml stanzas for the line and forecast markers
my $line = "";
my $points = "";
while(<DATA>) {
chomp;
if (m/^ *(INITIAL|\d+HR VT) +(\d+\/\d{4}Z) (\d+\.\d+)N +(\d+.\d+)W +(.*)$/) {
$line .= "-$4, $3\n";
$points .= << "HERE"
<Placemark>
<name>$1, $2, $5</name>
<Point>
<coordinates>-$4, $3</coordinates>
</Point>
</Placemark>
HERE
}
}
# Print everything out, including the end of the kml
print $line;
print << "FINISHLINE";
</coordinates>
</LineString>
</Placemark>
FINISHLINE
print $points;
print << "FINISHPOINTS";
</Folder>
</Document>
</kml>
FINISHPOINTS
__DATA__
INITIAL 27/1500Z 18.8N 74.0W 50 KT
12HR VT 28/0000Z 19.0N 74.8W 55 KT
24HR VT 28/1200Z 19.2N 76.0W 60 KT
36HR VT 29/0000Z 19.3N 77.5W 70 KT
48HR VT 29/1200Z 19.9N 79.4W 80 KT
72HR VT 30/1200Z 21.5N 83.0W 100 KT
96HR VT 31/1200Z 24.5N 86.0W 100 KT
120HR VT 01/1200Z 28.5N 88.5W 100 KT


As you can see, I copy and paste the NOAA storm forecast into the program. The resulting GoogleMap looks like this:

View Hurricane Gustav Forecast in a larger map
Oh my, that looks uncomfortable for New Orleans. Keep your fingers crossed.

No comments:

Post a Comment

I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.