ObsPy Tutorial
Downloading Data
</div> </div> </div> image: User:Abbaszade656 / Wikimedia Commons / CC-BY-SA-4.0

Workshop for the "Training in Network Management Systems and Analytical Tools for Seismic"

Baku, October 2018

Seismo-Live: http://seismo-live.org

Authors:

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = 12, 8

ObsPy has clients to directly fetch data via...

  • FDSN webservices (IRIS, Geofon/GFZ, USGS, NCEDC, SeisComp3 instances, ...)
  • ArcLink (EIDA, ...)
  • Earthworm
  • SeedLink (near-realtime servers)
  • NERIES/NERA/seismicportal.eu
  • NEIC
  • SeisHub (local seismological database)

This introduction shows how to use the FDSN webservice client. The FDSN webservice definition is by now the default web service implemented by many data centers world wide. Clients for other protocols work similar to the FDSN client.

Waveform Data

In [2]:
import obspy
from obspy.clients.fdsn import Client

client = Client("IRIS")
t = obspy.UTCDateTime("2014-08-24T10:20:44.0")  # South Napa Earthquake
st = client.get_waveforms("II", "PFO", "00", "LHZ",
                          t - 5 * 60, t + 20 * 60)
print(st)
st.plot()
1 Trace(s) in Stream:
II.PFO.00.LHZ | 2014-08-24T10:15:44.069500Z - 2014-08-24T10:40:43.069500Z | 1.0 Hz, 1500 samples
Out[2]:
  • again, waveform data is returned as a Stream object
  • for all custom processing workflows it does not matter if the data originates from a local file or from a web service

Event Metadata

The FDSN client can also be used to request event metadata:

In [3]:
t = obspy.UTCDateTime("2014-08-24T10:20:44.0")  # South Napa earthquake
catalog = client.get_events(starttime=t - 100, endtime=t + 3600,
                            minmagnitude=6)
print(catalog)
catalog.plot();
1 Event(s) in Catalog:
2014-08-24T10:20:45.600000Z | +38.201, -122.086 | 6.1 MW
/Users/lion/miniconda3/envs/seismo_live/lib/python3.7/site-packages/obspy/imaging/maps.py:45: UserWarning: basemap/pyproj with proj4 version >= 5 has a bug that results in inverted map axes. Your maps may be wrong. Please use another version of proj4, or use cartopy.
  warnings.warn(msg)

Requests can have a wide range of constraints (see ObsPy Documentation):

  • time range
  • geographical (lonlat-box, circular by distance)
  • depth range
  • magnitude range, type
  • contributing agency

Station Metadata

Finally, the FDSN client can be used to request station metadata. Stations can be looked up using a wide range of constraints (see ObsPy documentation):

  • network/station code
  • time range of operation
  • geographical (lonlat-box, circular by distance)
In [4]:
event = catalog[0]
origin = event.origins[0]

# Livermore
lon = -121.768005
lat = 37.681873

# Get currently active stations in 5 km radius around Livermore.
inventory = client.get_stations(longitude=lon, latitude=lat,
                                maxradius=0.05, level="station",
                                starttime=obspy.UTCDateTime())
print(inventory)
inventory.plot(projection="local", resolution="i");
Inventory created at 2019-11-04T08:57:47.000000Z
	Created by: IRIS WEB SERVICE: fdsnws-station | version: 1.1.37
		    http://service.iris.edu/fdsnws/station/1/query?starttime=2019-11-04...
	Sending institution: IRIS-DMC (IRIS-DMC)
	Contains:
		Networks (3):
			GS, NC, NP
		Stations (13):
			GS.LV1B (Tri-Valley urban array)
			GS.LV1C (Tri-Valley urban array)
			GS.LV1D (Tri-Valley urban array)
			GS.LV1E (Tri-Valley urban array)
			GS.LV1F (Tri-Valley urban array)
			GS.LV1G (Tri-Valley urban array)
			GS.LV1H (Tri-Valley urban array)
			GS.LV1I (Tri-Valley urban array)
			GS.TVK4 (Tri-Valley urban array)
			NC.CBZ (Buffer Zone)
			NP.1786 (Livermore FS 6 East Av)
			NP.1803 (Livermore FS 8 Scenic)
			NP.1842 (Livermore FS 7 Rincon)
		Channels (0):

The level=... keyword is used to specify the level of detail in the requested inventory

  • "network": only return information on networks matching the criteria
  • "station": return information on all matching stations
  • "channel": return information on available channels in all stations networks matching the criteria
  • "response": include instrument response for all matching channels (large result data size!)
In [5]:
inventory = client.get_stations(network="II", station="PFO",
                                level="station")
print(inventory)
Inventory created at 2019-11-04T08:57:50.000000Z
	Created by: IRIS WEB SERVICE: fdsnws-station | version: 1.1.37
		    http://service.iris.edu/fdsnws/station/1/query?network=II&station=P...
	Sending institution: IRIS-DMC (IRIS-DMC)
	Contains:
		Networks (1):
			II
		Stations (2):
			II.PFO (Pinon Flat, California, USA) (2x)
		Channels (0):

In [6]:
inventory = client.get_stations(network="II", station="PFO",
                                level="channel")
print(inventory)
Inventory created at 2019-11-04T08:57:50.000000Z
	Created by: IRIS WEB SERVICE: fdsnws-station | version: 1.1.37
		    http://service.iris.edu/fdsnws/station/1/query?network=II&station=P...
	Sending institution: IRIS-DMC (IRIS-DMC)
	Contains:
		Networks (1):
			II
		Stations (2):
			II.PFO (Pinon Flat, California, USA) (2x)
		Channels (579):
			II.PFO.00.BHZ (26x), II.PFO.00.BHN (18x), II.PFO.00.BHE (18x), 
			II.PFO.00.BH1 (10x), II.PFO.00.BH2 (8x), II.PFO.00.BLZ (8x), 
			II.PFO.00.BLN (8x), II.PFO.00.BLE (8x), II.PFO.00.ENZ (6x), 
			II.PFO.00.ENN (6x), II.PFO.00.ENE (6x), II.PFO.00.EN1, 
			II.PFO.00.EN2, II.PFO.00.LCE, II.PFO.00.LCQ, II.PFO.00.LDI (3x), 
			II.PFO.00.LHZ (25x), II.PFO.00.LHN (17x), II.PFO.00.LHE (17x), 
			II.PFO.00.LH1 (10x), II.PFO.00.LH2 (8x), II.PFO.00.LLZ (7x), 
			II.PFO.00.LLN (7x), II.PFO.00.LLE (7x), II.PFO.00.LNZ (6x), 
			II.PFO.00.LNN (6x), II.PFO.00.LNE (6x), II.PFO.00.LN1, 
			II.PFO.00.LN2, II.PFO.00.MHZ (3x), II.PFO.00.MHN (2x), 
			II.PFO.00.MHE (2x), II.PFO.00.MLZ (2x), II.PFO.00.MLN (2x), 
			II.PFO.00.MLE (2x), II.PFO.00.VHZ (26x), II.PFO.00.VHN (18x), 
			II.PFO.00.VHE (18x), II.PFO.00.VH1 (10x), II.PFO.00.VH2 (8x), 
			II.PFO.00.VLZ (2x), II.PFO.00.VLN (2x), II.PFO.00.VLE (2x), 
			II.PFO.00.VMZ (11x), II.PFO.00.VMN (11x), II.PFO.00.VME (11x), 
			II.PFO.00.VM1 (5x), II.PFO.00.VM2 (2x), II.PFO.00.WDI (3x), 
			II.PFO.00.WDO, II.PFO.10.BHZ (14x), II.PFO.10.BHN (10x), 
			II.PFO.10.BHE (10x), II.PFO.10.BH1 (6x), II.PFO.10.BH2 (6x), 
			II.PFO.10.BLZ (3x), II.PFO.10.BLN (3x), II.PFO.10.BLE (3x), 
			II.PFO.10.BL1, II.PFO.10.BL2, II.PFO.10.LCE (2x), 
			II.PFO.10.LCQ (2x), II.PFO.10.LDI, II.PFO.10.LHZ (11x), 
			II.PFO.10.LHN (7x), II.PFO.10.LHE (7x), II.PFO.10.LH1 (5x), 
			II.PFO.10.LH2 (5x), II.PFO.10.VHZ (11x), II.PFO.10.VHN (7x), 
			II.PFO.10.VHE (7x), II.PFO.10.VH1 (5x), II.PFO.10.VH2 (5x), 
			II.PFO.10.VMU (9x), II.PFO.10.VMV (9x), II.PFO.10.VMW (9x), 
			II.PFO.20.BFZ, II.PFO.20.BFN, II.PFO.20.BFE, II.PFO.20.BF1, 
			II.PFO.20.BF2, II.PFO.20.LCE, II.PFO.20.LCQ, II.PFO.20.LFZ, 
			II.PFO.20.LFN, II.PFO.20.LFE, II.PFO.20.LF1, II.PFO.20.LF2, 
			II.PFO.XX.BHZ (2x), II.PFO.XX.BHN (2x), II.PFO.XX.BHE (2x), 
			II.PFO.XX.LHZ (2x), II.PFO.XX.LHN (2x), II.PFO.XX.LHE (2x), 
			II.PFO.XX.VHZ (2x), II.PFO.XX.VHN (2x), II.PFO.XX.VHE (2x)

For waveform requests that include instrument correction, the appropriate instrument response information also has to be downloaded.

In [7]:
t = obspy.UTCDateTime("2014-08-24T10:20:44.0")  # South Napa earthquake
st = client.get_waveforms("II", "PFO", "00", "LHZ",
                          t - 10 * 60, t + 20 * 60)
inv = client.get_stations(network="II", station="PFO", location="00", channel="LHZ",
                          level="response", starttime=t - 10, endtime=t + 10)
st.plot()

st.remove_response(inventory=inv)
st.plot()
Out[7]:

All data requested using the FDSN client can be directly saved to file using the filename="..." option. The data is then stored exactly as it is served by the data center, i.e. without first parsing by ObsPy and outputting by ObsPy.

In [8]:
client.get_events(starttime=t-100, endtime=t+24*3600, minmagnitude=5,
                  filename="/tmp/requested_events.xml")
client.get_stations(network="II", station="PFO", level="station",
                    filename="/tmp/requested_stations.xml")
client.get_waveforms("II", "PFO", "00", "LHZ", t - 10 * 60, t + 20 * 60,
                     filename="/tmp/requested_waveforms.mseed")
!ls -lrt /tmp/requested*
-rw-r--r--  1 lion  wheel   8685 Nov  4 09:57 /tmp/requested_events.xml
-rw-r--r--  1 lion  wheel  24838 Nov  4 09:57 /tmp/requested_stations.xml
-rw-r--r--  1 lion  wheel  12288 Nov  4 09:57 /tmp/requested_waveforms.mseed

FDSN Client Exercise

Use the FDSN client to assemble a small waveform dataset for on event.

  • search for a large earthquake (e.g. by depth or in a region of your choice, use option limit=5 to keep network traffic down)
In [9]:
from obspy.clients.fdsn import Client

client = Client("IRIS")
catalog = client.get_events(minmagnitude=8, limit=5)
print(catalog)
catalog.plot()
event = catalog[0]
print(event)
5 Event(s) in Catalog:
2019-05-26T07:41:15.058000Z |  -5.813,  -75.278 | 8.0 Mww
2018-08-19T00:19:40.670000Z | -18.113, -178.154 | 8.2 mww
2017-09-08T04:49:20.000000Z | +15.036,  -93.907 | 8.1 Mww
2015-09-16T22:54:32.860000Z | -31.573,  -71.674 | 8.3 mww
2014-04-01T23:46:47.380000Z | -19.619,  -70.788 | 8.1 MW
Event:	2019-05-26T07:41:15.058000Z |  -5.813,  -75.278 | 8.0 Mww

	            resource_id: ResourceIdentifier(id="smi:service.iris.edu/fdsnws/event/1/query?eventid=11041250")
	             event_type: 'earthquake'
	    preferred_origin_id: ResourceIdentifier(id="smi:service.iris.edu/fdsnws/event/1/query?originid=38683486")
	 preferred_magnitude_id: ResourceIdentifier(id="smi:service.iris.edu/fdsnws/event/1/query?magnitudeid=193627740")
	                   ---------
	     event_descriptions: 1 Elements
	                origins: 1 Elements
	             magnitudes: 1 Elements
  • search for stations to look at waveforms for the event. stations should..
    • be available at the time of the event
    • use a vertical 1 Hz stream ("LHZ", to not overpower our network..)
    • be in a narrow angular distance around the event (e.g. 90-91 degrees)
    • adjust your search so that only a small number of stations (e.g. 3-6) match your search criteria
    • Once you found a good set of stations, please use level="response" as you will need the response later on.
In [10]:
origin = event.origins[0]
t = origin.time

inventory = client.get_stations(longitude=origin.longitude, latitude=origin.latitude,
                                minradius=101, maxradius=103,
                                starttime=t, endtime =t+100,
                                channel="LHZ", level="response",
                                matchtimeseries=True)
print(inventory)
Inventory created at 2019-11-04T08:58:02.000000Z
	Created by: IRIS WEB SERVICE: fdsnws-station | version: 1.1.37
		    http://service.iris.edu/fdsnws/station/1/query?starttime=2019-05-26...
	Sending institution: IRIS-DMC (IRIS-DMC)
	Contains:
		Networks (4):
			GE, II, IU, NZ
		Stations (9):
			GE.VSU (GEOFON Station Vasula, Estonia)
			II.LVZ (Lovozero, Russia)
			IU.ADK (Adak, Aleutian Islands, Alaska)
			IU.LSZ (Lusaka, Zambia)
			NZ.HIZ (Hauiti)
			NZ.KHZ (Kahutara)
			NZ.ODZ (Otahua Downs)
			NZ.QRZ (Quartz Range)
			NZ.RPZ (Rata Peaks)
		Channels (13):
			GE.VSU..LHZ, II.LVZ.00.LHZ, II.LVZ.10.LHZ, IU.ADK.00.LHZ, 
			IU.ADK.10.LHZ, IU.ADK.60.LHZ, IU.LSZ.00.LHZ, IU.LSZ.10.LHZ, 
			NZ.HIZ.10.LHZ, NZ.KHZ.10.LHZ, NZ.ODZ.10.LHZ, NZ.QRZ.10.LHZ, 
			NZ.RPZ.10.LHZ
  • for each of these stations download data of the event, e.g. a couple of minutes before to half an hour after the event
  • put all data together in one stream (put the get_waveforms() call in a try/except/pass block to silently skip stations that actually have no data available)
  • print stream info, plot the raw data
In [11]:
from obspy import Stream
st = Stream()

for network in inventory:
    for station in network:
        try:
            st += client.get_waveforms(network.code, station.code, "*", "LHZ",
                                       t - 5 * 60, t + 30 * 60)
        except:
            pass

print(st)
st.plot()
13 Trace(s) in Stream:
GE.VSU..LHZ   | 2019-05-26T07:36:15.120000Z - 2019-05-26T08:11:14.120000Z | 1.0 Hz, 2100 samples
II.LVZ.00.LHZ | 2019-05-26T07:36:15.069539Z - 2019-05-26T08:11:14.069539Z | 1.0 Hz, 2100 samples
II.LVZ.10.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
IU.ADK.00.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
IU.ADK.10.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
IU.ADK.60.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
IU.LSZ.00.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
IU.LSZ.10.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
NZ.HIZ.10.LHZ | 2019-05-26T07:36:15.069541Z - 2019-05-26T08:11:14.069541Z | 1.0 Hz, 2100 samples
NZ.KHZ.10.LHZ | 2019-05-26T07:36:15.069534Z - 2019-05-26T08:11:14.069534Z | 1.0 Hz, 2100 samples
NZ.ODZ.10.LHZ | 2019-05-26T07:36:15.069541Z - 2019-05-26T08:11:14.069541Z | 1.0 Hz, 2100 samples
NZ.QRZ.10.LHZ | 2019-05-26T07:36:15.069538Z - 2019-05-26T08:11:14.069538Z | 1.0 Hz, 2100 samples
NZ.RPZ.10.LHZ | 2019-05-26T07:36:15.069533Z - 2019-05-26T08:11:14.069533Z | 1.0 Hz, 2100 samples