Quick Lists of Recent Large Earthquakes with Python

Introduction

I am often interested in a quick summary of recent global seismic activity. A number of good web sites and apps can provide such information, but I often want a simple, quick summary. I wrote the following python script to execute from a command line (as in the OS X Terminal App) and to provide basic information on recent large earthuake activity. I place the script in ~/Dropbox/bin and can run it from all my Macs. I have also installed it on my iPads using the Pythonista App.

The script does nothing fancy, it simply performs a few searches on the U.S. Geological Survey’s earthquake feeds and summarizes information in three categories – great (M ≥ 8) earthquake occurrence in the last three years, major and great (M ≥ 7) earthquake activity in the last year, and strong, major, and great (M ≥ 6) earthquakes in the last 30 days.

Python Code

I left the code in one notebook block because it is designed to use in a standalone script (installed somewhere in your default path). Note the python package dependencies in the first lines.

#!/usr/local/anaconda/bin/python
#
import requests
import json
import time
import datetime
#
#----------------------------------------------------------------
def subtract_years(dt, years):
    try:
        dt = dt.replace(year=dt.year-years)
    except ValueError:
        dt = dt.replace(year=dt.year-years, day=dt.day-1)
    return dt
#
#----------------------------------------------------------------
def listEvents(events):
    now = datetime.datetime.utcnow()
    for ev in events:
        evg = ev['geometry']
        coords = evg['coordinates']
        evp = ev['properties']
        mag = evp['mag']
        magtype = evp['magType']
        epochTime = evp['time']
        originTime = datetime.datetime.utcfromtimestamp(epochTime/1000)
        #
        dt = now - originTime
        dtstring = '{:>4}'.format(dt.days)+' days, '+'{:>2d}'.format(dt.seconds/3600)+' hrs ago'
        #
        print '{:.1f}'.format(mag), '{:<3}'.format(magtype),originTime,'/', \
                dtstring, ' - ',evp['place'],'(',coords[2],'km)'
#
#====================================================================
#   MAIN PROGRAM
#====================================================================
if __name__ == '__main__':
    #----------------------------------------------------------------
    # search for the great earthquakes from the three years
    #----------------------------------------------------------------
    #
    now = datetime.datetime.utcnow()
    yr_ago = subtract_years(now,3)
    #
    s = 'starttime='+ yr_ago.isoformat() +'&endtime=' + now.isoformat()
    url = 'http://earthquake.usgs.gov/fdsnws/event/1/query?'
    url += s
    url += '&minmagnitude=7.99&maxmagnitude=100&format=geojson&orderby=time'
    r = requests.get(url);
    #
    data   = r.json();
    events = data['features'];
    #
    print '\nGreat Earthquakes (last three years)'
    listEvents(events)

    #----------------------------------------------------------------
    # search for the major and great earthquakes from the last year
    #----------------------------------------------------------------
    #
    yr_ago = subtract_years(now,1)
    #
    s = 'starttime='+ yr_ago.isoformat() +'&endtime=' + now.isoformat()
    url = 'http://earthquake.usgs.gov/fdsnws/event/1/query?'
    url += s
    url += '&minmagnitude=6.99&maxmagnitude=100&format=geojson&orderby=time'
    r = requests.get(url);
    #
    data   = r.json();
    events = data['features'];
    #
    print '\nMajor and Great Earthquakes (last year)'
    listEvents(events)

    #----------------------------------------------------------------
    # recent activity
    #----------------------------------------------------------------
    #
    r = requests.get('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson');
    data   = r.json();
    events = data['features'];
    print '\nRecent Earthquakes (last 30 days)'
    levents = []
    for ev in events:
        evp = ev['properties']
        mag = evp['mag']
        if(mag >= 6.0):
            levents.append(ev)
    #
    listEvents(levents)
    #
#

Sample Output

Major and Great Earthquakes (last year)
7.3 mww 2017-01-10 06:13:47 /    5 days, 11 hrs ago  -  188km SSE of Tabiauan, Philippines ( 612.71 km)
7.6 mww 2016-12-25 14:22:26 /   21 days,  3 hrs ago  -  42km SW of Puerto Quellon, Chile ( 35.15 km)
7.9 mww 2016-12-17 10:51:10 /   29 days,  6 hrs ago  -  54km E of Taron, Papua New Guinea ( 94.54 km)
7.8 mww 2016-12-08 17:38:46 /   37 days, 23 hrs ago  -  70km WSW of Kirakira, Solomon Islands ( 41 km)
7.8 mww 2016-11-13 11:02:56 /   63 days,  6 hrs ago  -  54km NNE of Amberley, New Zealand ( 15.22 km)
7.0 mww 2016-09-01 16:37:57 /  136 days,  0 hrs ago  -  175km NE of Gisborne, New Zealand ( 19 km)
7.1 mww 2016-08-29 04:29:57 /  139 days, 13 hrs ago  -  North of Ascension Island ( 10 km)
7.4 mww 2016-08-19 07:32:22 /  149 days, 10 hrs ago  -  South Georgia Island region ( 10 km)
7.2 mww 2016-08-12 01:26:36 /  156 days, 16 hrs ago  -  110km E of Ile Hunter, New Caledonia ( 16.37 km)
7.7 mww 2016-07-29 21:18:24 /  169 days, 20 hrs ago  -  29km SW of Agrihan, Northern Mariana Islands ( 196 km)
7.2 mww 2016-05-28 09:46:59 /  232 days,  7 hrs ago  -  53km NNE of Visokoi Island, South Georgia and the South Sandwich Islands ( 78 km)
7.0 mww 2016-04-28 19:33:24 /  261 days, 21 hrs ago  -  2km N of Norsup, Vanuatu ( 24 km)
7.8 mww 2016-04-16 23:58:36 /  273 days, 17 hrs ago  -  27km SSE of Muisne, Ecuador ( 20.59 km)
7.0 mww 2016-04-15 16:25:06 /  275 days,  1 hrs ago  -  1km E of Kumamoto-shi, Japan ( 10 km)
7.8 mww 2016-03-02 12:49:48 /  319 days,  4 hrs ago  -  Southwest of Sumatra, Indonesia ( 24 km)
7.2 mww 2016-01-30 03:25:12 /  351 days, 14 hrs ago  -  88km N of Yelizovo, Russia ( 177 km)
7.1 mww 2016-01-24 10:30:30 /  357 days,  7 hrs ago  -  86km E of Old Iliamna, Alaska ( 129 km)

Recent Earthquakes (last 30 days)
6.1 mwp 2017-01-14 06:11:41 /    1 days, 11 hrs ago  -  152km SW of Nadi, Fiji ( 10 km)
6.3 mww 2017-01-10 15:27:15 /    5 days,  2 hrs ago  -  104km WNW of Kirakira, Solomon Islands ( 28.47 km)
7.3 mww 2017-01-10 06:13:47 /    5 days, 11 hrs ago  -  188km SSE of Tabiauan, Philippines ( 612.71 km)
6.9 mww 2017-01-03 21:52:31 /   11 days, 19 hrs ago  -  221km SW of Nadi, Fiji ( 17.1 km)
6.3 mwb 2017-01-02 13:14:02 /   13 days,  4 hrs ago  -  South of the Fiji Islands ( 555.12 km)
6.2 mwp 2016-12-29 22:30:18 /   16 days, 19 hrs ago  -  33km S of Tolotangga, Indonesia ( 72.27 km)
7.6 mww 2016-12-25 14:22:26 /   21 days,  3 hrs ago  -  42km SW of Puerto Quellon, Chile ( 35.15 km)
6.7 mww 2016-12-21 00:17:14 /   25 days, 17 hrs ago  -  283km ENE of Dili, East Timor ( 152 km)
6.4 mww 2016-12-20 04:21:28 /   26 days, 13 hrs ago  -  80km WNW of Kirakira, Solomon Islands ( 11.33 km)
6.4 mww 2016-12-18 13:30:11 /   28 days,  4 hrs ago  -  201km S of Tarauaca, Brazil ( 619.4 km)
6.2 mww 2016-12-18 09:47:04 /   28 days,  7 hrs ago  -  23km ESE of Ngulu, Micronesia ( 12.43 km)
6.0 mwp 2016-12-18 05:46:25 /   28 days, 11 hrs ago  -  83km WNW of Kirakira, Solomon Islands ( 39.11 km)
6.3 mww 2016-12-17 11:27:38 /   29 days,  6 hrs ago  -  168km SE of Taron, Papua New Guinea ( 26.5 km)
7.9 mww 2016-12-17 10:51:10 /   29 days,  6 hrs ago  -  54km E of Taron, Papua New Guinea ( 94.54 km)

I suppose I could reformat the information to create a display that worked better on a mobile device, but it’s really a simple unix-like uitlity for terminal-based interaction.

Summary

Nothing fancy, just simple and straight forward summary of earthquake activity. For some unknown and strange reason, this takes forever on my office iMac, but takes only a few seconds to complete on my MacBook Pro and my iPads.