Last active
February 22, 2023 17:22
-
-
Save mhearne-usgs/6b040c0b423b7d03f4b9 to your computer and use it in GitHub Desktop.
This code demonstrates the use of the the USGS M2.5+ 30 day feed, and downloading the most #recent version of the event entered by the user.
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/env python | |
#stdlib imports | |
import urllib2 | |
import json | |
import sys | |
import os.path | |
import zipfile | |
import StringIO | |
#This code demonstrates the use of the the USGS M2.5+ 30 day feed, and downloading the most | |
#recent version of the event entered by the user. If using this code snippet in another | |
#context (say ArcGIS), then the __main__ entry point,print statements, and output file location | |
#should probably be replaced with whatever is appropriate. | |
#These feeds are found on this page: | |
#http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php | |
FEEDURL = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson' #30 day M2.5+ | |
#FEEDURL = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson' #30 day M1.0+ | |
#Get the list of event IDs in the current feed | |
def geteEventList(): | |
fh = urllib2.urlopen(FEEDURL) #open a URL connection to the event feed. | |
data = fh.read() #read all of the data from that URL into a string | |
fh.close() | |
jdict = json.loads(data) #Parse that data using the stdlib json module. This turns into a Python dictionary. | |
eventlist = [] | |
for earthquake in jdict['features']: #jdict['features'] is the list of events | |
if earthquake['id'] not in eventlist: | |
eventlist.append(earthquake['id']) | |
return eventlist | |
#In ArcGIS, you would call this function with an event ID (presumably) chosen from a list, | |
#or entered into a text box. | |
def main(eventid): | |
fh = urllib2.urlopen(FEEDURL) #open a URL connection to the event feed. | |
data = fh.read() #read all of the data from that URL into a string | |
fh.close() | |
jdict = json.loads(data) #Parse that data using the stdlib json module. This turns into a Python dictionary. | |
for earthquake in jdict['features']: #jdict['features'] is the list of events | |
if earthquake['id'] == eventid: | |
eventurl = earthquake['properties']['detail'] #get the event-specific URL | |
fh = urllib2.urlopen(eventurl) | |
data = fh.read() #read event data into a string | |
fh.close() | |
jdict2 = json.loads(data) #and parse using json module as before | |
if 'shakemap' not in jdict2['properties']['products'].keys(): | |
print 'Event %s does not have a ShakeMap product associated with it. Exiting.' | |
sys.exit(1) | |
shakemap = jdict2['properties']['products']['shakemap'][0] #get the first shakemap associated with the event | |
shapezipurl = shakemap['contents']['download/shape.zip']['url'] #get the download url for the shape zipfile. | |
#Here, read the binary zipfile into a string | |
fh = urllib2.urlopen(shapezipurl) | |
data = fh.read() | |
fh.close() | |
#Create a StringIO object, which behaves like a file | |
stringbuf = StringIO.StringIO(data) | |
eventdir = os.path.join(os.getcwd(),eventid) | |
if not os.path.isdir(eventdir): | |
os.mkdir(eventdir) | |
#Create a ZipFile object, instantiated with our file-like StringIO object. | |
#Extract all of the data from that StringIO object into files in the provided output directory. | |
myzip = zipfile.ZipFile(stringbuf,'r',zipfile.ZIP_DEFLATED) | |
myzip.extractall(eventdir) | |
myzip.close() | |
stringbuf.close() | |
filelist = os.listdir(eventdir) | |
print 'Extracted %i ShakeMap files to %s' % (len(filelist),eventdir) | |
sys.exit(0) | |
#This section is the "entry point" of the script when called from the command line | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print 'Enter the id (i.e., nc72282711). Exiting.' | |
sys.exit(1) | |
eventid = sys.argv[1] #Grab the second item from the command line arguments (first is always name of script) | |
main(eventid) #call the main function defined above |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/mhearne-usgs/6b040c0b423b7d03f4b9#file-getevent-py-L43
Event detail URLs have changed, and the GeoJSON detail URL is now in the
detail
property:eventurl = earthquake['properties']['detail']