<jupyter_start><jupyter_text>ObsPy Tutorial            Downloading Data            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 2018Seismo-Live: http://seismo-live.org Authors:* <NAME> ([@krischer](https://github.com/krischer))* <NAME> ([@megies](https://github.com/megies))--- ![](images/obspy_logo_full_524x179px.png)<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = 12, 8<jupyter_output><empty_output><jupyter_text>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<jupyter_code>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()<jupyter_output><empty_output><jupyter_text>- 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 MetadataThe FDSN client can also be used to request event metadata:<jupyter_code>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();<jupyter_output><empty_output><jupyter_text>Requests can have a wide range of constraints (see [ObsPy Documentation](http://docs.obspy.org/packages/autogen/obspy.clients.fdsn.client.Client.get_events.html)):- time range- geographical (lonlat-box, circular by distance)- depth range- magnitude range, type- contributing agency  Station MetadataFinally, the FDSN client can be used to request station metadata. Stations can be looked up using a wide range of constraints (see [ObsPy documentation](http://docs.obspy.org/packages/autogen/obspy.clients.fdsn.client.Client.get_stations.html)): * network/station code * time range of operation * geographical (lonlat-box, circular by distance)<jupyter_code>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");<jupyter_output><empty_output><jupyter_text>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!)<jupyter_code>inventory = client.get_stations(network="II", station="PFO",
                                level="station")
print(inventory)
inventory = client.get_stations(network="II", station="PFO",
                                level="channel")
print(inventory)<jupyter_output><empty_output><jupyter_text>For waveform requests that include instrument correction, the appropriate instrument response information also has to be downloaded.<jupyter_code>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()<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>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*<jupyter_output><empty_output><jupyter_text>FDSN Client ExerciseUse 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)<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>- 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.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>- 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<jupyter_code>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()<jupyter_output><empty_output><jupyter_text>- correct the instrument response for all stations and plot the corrected data<jupyter_code>st.remove_response(inventory=inventory, water_level=20)
st.plot()<jupyter_output><empty_output><jupyter_text>If you have time, assemble and plot another similar dataset (e.g. like before stations at a certain distance from a big event, or use Transportable Array data for a big event, etc.)  Routed/Federated ClientsFinally, all of the above queries can not only be directed at one specific data center, but instead can be sent to either .. * [IRIS Federator](https://service.iris.edu/irisws/fedcatalog/1/), or * [EIDA routing](http://www.orfeus-eu.org/data/eida/webservices/routing/) service.. to query data across all existing FDSN web service data centers (or at least those registered with IRIS/EIDA).These federation/routing services support station lookup and waveform request queries.This example queries the IRIS Federator which in turn combines data from all other FDSN web service data centers.<jupyter_code>from obspy.clients.fdsn import RoutingClient

client = RoutingClient('iris-federator')  # or use 'eida-routing' for the EIDA routing service

# Tohoku Earthquake
t = obspy.UTCDateTime('2011-03-11T14:46:24+09')

st = client.get_waveforms(
    channel="LHZ", starttime=t, endtime=t+20,
    latitude=50, longitude=14, maxradius=10)
print(st)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10450555880631948, 'fraction_numerical': 0.022820362785254535, 'mean_word_length': 4.04188790560472, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '424332', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2524, 'n_words': 818, 'path': 'notebooks/Workshops/2017_Baku_STCU_IRIS_ObsPy_course/06_FDSN_exercise.ipynb', 'repo_name': 'krischer/seismo_live_build', 'size': '8559'}