#!/usr/bin/perl -w ##################################################################### # # Program name: genData.pl # # Description : script to fetch earthquake bulletin # and convert to shapefile # # $Id: genData.pl,v 1.6 2004/01/02 21:20:30 tkralidi Exp $ # # Revisions: (see end of file for revision history) # # # Copyright (c) 2004 Athanasios Tom Kralidis # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies of this Software or works derived from this Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # # This definition is reproduced from the MIT License at: # # http://www.opensource.org/licenses/mit-license.html # ##################################################################### # enable strict mode use strict; # load URL fetching module use LWP::Simple; # set variables my @quakes; my $datecreated; my $month; my $i = 0; my $basedir = "/usr/local/wwwsites/apache/apps/mapserv/services/neis/"; my $filename = $basedir . "neis.gml"; # URL to raw data my $url = "http://neic.usgs.gov/neis/finger/quake.asc"; # fetch remote data and store in array my $content = get($url); my @arr = split /\n/, $content; # loop through file and pick up attributes # we have to put a few conditions here # to realize empty data foreach my $line(@arr) { my ($date, $time, $x, $y); if ($line =~ /^0/) { # pick up attribute info # if "Q" is empty if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s{5}(.*)$/) { $date = $1; $time = $2; $y = $3; $x = $4; $quakes[$i]{depthkm} = $5; $quakes[$i]{magnitude} = $6; $quakes[$i]{q} = "none"; $quakes[$i]{comment} = $7; } # if "MAG" and "Q" are empty elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s{10}(.*)$/) { $date = $1; $time = $2; $y = $3; $x = $4; $quakes[$i]{depthkm} = $5; $quakes[$i]{magnitude} = 9999; # dummy numerical value $quakes[$i]{q} = "none"; $quakes[$i]{comment} = $6; } # elsif "MAG" and "Q" are NOT empty elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/) { $date = $1; $time = $2; $y = $3; $x = $4; $quakes[$i]{depthkm} = $5; $quakes[$i]{magnitude} = $6; $quakes[$i]{q} = $7; $quakes[$i]{comment} = $8; } # alter coords to be expressed as +/- vals if ($y =~ /^(\d+\.\d+)N$/) { $y = $1; } if ($y =~ /^(\d+\.\d+)S$/) { $y = "-" . $1; } if ($x =~ /^(\d+\.\d+)E$/) { $x = $1; } if ($x =~ /^(\d+\.\d+)W$/) { $x = "-" . $1; } $quakes[$i]{y} = $y; $quakes[$i]{x} = $x; # format ISO8160 timestring like YYYY-MM-DDTHH:MM:SSZ to timestamp record $date =~ s/\//-/g; $quakes[$i]{datetime} = "20" . $date . "T" . $time . "Z"; # get rid of magnitude unit symbol 'M' so we can classify # with numerical comparisons $quakes[$i]{magnitude} =~ s/M//; # strip angle brackets from comment for XML well-formedness $quakes[$i]{comment} =~ s#<#<#; $quakes[$i]{comment} =~ s#>#>#; $i++; } # format ISO8160 timestring like YYYY-MM-DDTHH:MM:SSZ to timestamp document if ($line =~ /^Updated as of (\S{3}) (\S{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) GMT (\S{4})/) { $month = "01" if $2 eq "Jan"; $month = "02" if $2 eq "Feb"; $month = "03" if $2 eq "Mar"; $month = "04" if $2 eq "Apr"; $month = "05" if $2 eq "May"; $month = "06" if $2 eq "Jun"; $month = "07" if $2 eq "Jul"; $month = "08" if $2 eq "Aug"; $month = "09" if $2 eq "Sep"; $month = "10" if $2 eq "Oct"; $month = "11" if $2 eq "Nov"; $month = "12" if $2 eq "Dec"; $datecreated = $7 . "-" . $month . "-" . $3 . "T" . $4 . ":" . $5 . ":" . $6 . "Z"; } } $i = 0; # OK, now that we have data, remove previous copies of data unlink($filename); # create a new file open(FILE, ">$filename") or die "$!\n"; print FILE < This is a GML document which provides locations of USGS NEIS near real time earthquake data USGS NEIS Near Real Time Earthquake Bulletin -180 -90 180 90 END # populate feature members foreach my $k (@quakes) { print FILE < $k->{comment} $i $k->{x} $k->{y} $k->{datetime} $k->{depthkm} $k->{magnitude} $k->{q} END print "$k->{datetime} -- $k->{y} -- $k->{x} -- $k->{depthkm} -- $k->{magnitude} -- $k->{q} -- $k->{comment}\n"; $i++; } print FILE <$datecreated END close(FILE); ##################################################################### # # Revision History # # $Log: genData.pl,v $ # #####################################################################