Last active
January 3, 2016 05:49
-
-
Save hfs/8418139 to your computer and use it in GitHub Desktop.
Convert AMDAR messages in ASCII bulletin format into GeoJSON to get a quick preview.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
if ($#ARGV < 0) { | |
print("Usage: $0 AMDAR-file(s)"); | |
exit(1); | |
} | |
print(qq/{ | |
"type": "FeatureCollection", | |
"features": [/); | |
my $feature_count = 0; | |
foreach my $filename (@ARGV) { | |
if (! open(IN, "<", $filename)) { | |
warn("Failed to open '$filename' for reading: $!"); | |
next; | |
} | |
print(",\n") if $feature_count++; | |
print(qq/ | |
{ | |
"type": "Feature", | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [\n/); | |
my %attributes = (); | |
my $lon = 0; | |
my $lat = 0; | |
my $count = 0; | |
while(<IN>) { | |
if (m/^UD[A-Z][A-Z]\d\d\s+(\w+)\s+(\d\d)(\d\d)(\d\d)/) { | |
$attributes{sender} = $1; | |
$attributes{day} = $2; | |
$attributes{hour} = $3; | |
$attributes{minute} = $4; | |
} | |
if (m/^(ASC|DES|LVR)\s+(\w+)\s+(\d+)([NS])\s+(\d+)([WE])/) { | |
$lon = $3/100.0; | |
$lon = -$lon if (lc($4) eq "s"); | |
$lat = $5/100.0; | |
$lat = -$lat if (lc($6) eq "w"); | |
$attributes{aircraft} = $2; | |
print(",\n") if $count++; | |
print(" [$lat, $lon]"); | |
} | |
} | |
print(qq/ | |
] | |
}, | |
"properties": {\n/); | |
$count = 0; | |
foreach my $key (sort keys %attributes) { | |
print(",\n") if $count++; | |
print(" \"$key\": \"$attributes{$key}\""); | |
} | |
print(qq/ | |
} | |
}/); | |
} | |
print(qq/ | |
] | |
}/); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment