Skip to content

Instantly share code, notes, and snippets.

@privong
Last active August 29, 2015 14:06
Show Gist options
  • Save privong/63ac04c6287fb9612b50 to your computer and use it in GitHub Desktop.
Save privong/63ac04c6287fb9612b50 to your computer and use it in GitHub Desktop.
Earthquake checker
#!/usr/bin/env python
# Load the daily USGS atom feed and see if there are any earthquakes within
# a specified distance of a specified location
import feedparser
import argparse
import math
import sys
# compute the great circle distance using the haversine formula
def GreatCircDist(loc1, loc2):
dlat = loc2[0] - loc1[0]
dlong = loc2[1] - loc1[1]
rdlat = math.radians(dlat)
rdlong = math.radians(dlong)
ha = math.sin(rdlat/2) * math.sin(rdlat/2) + \
math.cos(math.radians(loc1[0])) * \
math.cos(math.radians(loc2[0]))*math.sin(rdlong/2) * \
math.sin(rdlong/2)
hc = 2 * math.atan2(math.sqrt(ha), math.sqrt(1-ha))
return 6378.1*hc
parser = argparse.ArgumentParser(description="Load the daily USGS atom feed \
and see if there are any earthquakes within a specified distance of \
a specified location.")
parser.add_argument('-l', type=float, default=False,
help='Latitude of interest.')
parser.add_argument('-g', type=float, default=False,
help='Longitude of interest.')
parser.add_argument('-d', type=float, default=False,
help='Radius of interest (in km).')
args = parser.parse_args()
if not(args.l and args.g and args.d):
sys.stderr.write("Error. Incorrect arguments. Exiting.\n")
sys.exit(1)
usgs = feedparser.parse('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom')
number = 0
for entry in usgs['entries']:
pos = [float(x) for x in entry['georss_point'].split()]
dist = GreatCircDist([args.l, args.g], pos)
if args.d > dist:
number += 1
sys.stdout.write(
entry['title'].split(' -')[0] + ' earthquake ' +
'{0:0.0f}'.format(dist) + ' km away,' +
entry['title'].split('-')[1] + ' (' +
str(pos[0]) + ', ' + str(pos[1]) + ') at an elevation of ' +
'{0:0.1f}'.format(float(entry['georss_elev'])/1000.) +
'km.\n')
if not(number):
sys.stdout.write('No earthquakes within ' + str(args.d) + ' km of (' +
str(args.l) + ', ' + str(args.g) + ') in the past 24h.\n')
else:
sys.stdout.write('\n' + str(number) + ' earthquake')
if number != 1:
sys.stdout.write('s')
sys.stdout.write(' within ' +
str(args.d) + ' km of (' + str(args.l) + ', ' +
str(args.g) + ') in the past 24h.\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment