FDSN / Python Tutorials (OLD- move to FDSN Guide)

Last modified by robert on 2025/03/24 10:36

How to connect to AusPass with & without authenticated access

 

import obspy
from obspy.clients.fdsn import Client

# Initialize FDSN client for AusPass

# For open access data, no username or password is required.
client = Client('AUSPASS')

# To access restricted data, supply your username and password
# Replace 'Z1' and '12345' with your actual credentials
client = Client('AUSPASS', user='Z1', password='12345')

How to download waveform data

 

from obspy import UTCDateTime
from obspy.clients.fdsn import Client

# Initialize the FDSN client (you can also specify other data centers)
client = Client("AUSPASS")

# Event information
network = "S1"
station = "AUGRF"
starttime = UTCDateTime("2021-09-21T23:15:53")  # The time of the earthquake
endtime = starttime + 360  # One hour of data after the earthquake

# Download the MiniSEED data
st = client.get_waveforms(network=network, station=station, location="*", channel="BHZ",
                        starttime=starttime, endtime=endtime)
# Save the stream to a MiniSEED file
st.write("Woodspoint_2021.mseed", format="MSEED")
print("Downloaded and saved the MiniSEED file.")

How to remove instrument response

 

from obspy import read
from obspy.core.util import AttribDict

# Load the MiniSEED file
st = read("Woodspoint_2021.mseed")

# Download the instrument response
inv = client.get_stations(network=network, station=station, location="*",
                          channel="*", starttime=starttime, endtime=endtime,
                          level="response")
                         
# Remove the instrument response
output = 'VEL'  # Output unit ('VEL' = velocity (default), 'DISP' = displacement, 'ACC' = acceleration)

for tr in st:
    tr.remove_response(inventory=inv, output=output, plot=True)

# Save the corrected MiniSEED file
st.write("Woodspoint_2021_corrected.mseed", format="MSEED")

How to apply a bandpass filter

 

from obspy import read

# Load the MiniSEED file
st = read("Woodspoint_2021.mseed")

# Define the frequency band
freq_min = 0.1  # Minimum frequency in Hz
freq_max = 1.0  # Maximum frequency in Hz

# Apply the bandpass filter
for tr in st:
    tr.filter(type='bandpass', freqmin=freq_min, freqmax=freq_max)

# Save the filtered MiniSEED file
st.write("Woodspoint_2021_filtered.mseed", format="MSEED")

How to slice a waveform

 

from obspy import read, UTCDateTime, Stream  # Importing Stream here

# Load the filtered MiniSEED file
st = read("Woodspoint_2021_filtered.mseed")

# Define the time window for slicing
slice_start = UTCDateTime("2021-09-21T23:20:00")
slice_end = slice_start +10

# Slice the waveform for each Trace in the Stream
sliced_st = Stream()  # Now Stream is defined
for tr in st:
    sliced_tr = tr.slice(starttime=slice_start, endtime=slice_end)
    sliced_st.append(sliced_tr)

# Save the sliced MiniSEED file
sliced_st.write("Woodspoint_2021_filtered_sliced.mseed", format="MSEED")

How to save a waveform

 

# Save the sliced file as MiniSEED
sliced_st.write("Woodspoint_2021_filtered_sliced.mseed", format="MSEED")

# Or, save the sliced SAC file
sliced_st.write("Woodspoint_2021_filtered_sliced.sac", format="SAC")

How to convert miniseed to sac

 

from obspy import read

# Read the MiniSEED file
st = read("Woodspoint_2021.mseed")

# Take the first Trace from the Stream
tr = st[0]

# Save that Trace as a SAC file
tr.write("Woodspoint_2021.sac", format="SAC")

 

How to download an Earthquake Catalog

 

from obspy.clients.fdsn import Client
from obspy import UTCDateTime

# Initialize the AusPass FDSN client
client = Client("AUSPASS")

# Define the time range for the earthquake catalog
start_time = UTCDateTime("2021-08-01")
end_time = UTCDateTime("2022-01-01")  # End of year

# Define the geographic region (latitude and longitude for Woodspoint, Victoria, Australia)
latitude = -37.47
longitude = 146.10
max_radius = 5  # in degrees

# Download the earthquake catalog
catalog = client.get_events(starttime=start_time, endtime=end_time,
                            minmagnitude=2, latitude=latitude, longitude=longitude,
                            maxradius=max_radius)

# Save the catalog to a file (e.g., QuakeML format)
catalog.write("Woodspoint_earthquakes.xml", format="QUAKEML")

How to make an inventory

 

from obspy.clients.fdsn import Client
from obspy import UTCDateTime

# Initialize the AusPass FDSN client
client = Client("AUSPASS")

# Define the network and time range (adjust as needed)
network = "S1"
start_time = UTCDateTime("2021-01-01")
end_time = UTCDateTime()  # Setting end time to the current date and time

inventory = client.get_stations(network=network, starttime=start_time)

How to download event, station, instrument response

 

import obspy
from obspy.clients.fdsn import Client

# Use AusPass client for event and waveform information
client = Client("AUSPASS")

# Event time for the Woodspoint earthquake 
event_time = obspy.UTCDateTime("2021-09-21T23:15:53")

# Get the event information. 
cat = client.get_events(starttime=event_time - 10, endtime=event_time + 10,
                         minmagnitude=2, latitude=-37.47, longitude=146.10)
print(cat)

# Download station information for AUMTC station in S1 network at the response level
inv = client.get_stations(network="S1", station="AUMTC", location="*",
                           channel="*", starttime=event_time - 60,
                           endtime=event_time + 1000, level="response")
print(inv)

# Download 3 component waveforms for AUMTC station in S1 network
st = client.get_waveforms(network="S1", station="AUMTC", location="*",
                           channel="*", starttime=event_time - 60,
                           endtime=event_time + 1000)
print(st)
inv.plot()
inv.plot_response(0.001)
cat.plot()
st.plot()

How to plot (Global) Earthquakes

 

from obspy import UTCDateTime
from obspy.clients.fdsn import Client

# Initialize FDSN client to connect to the IRIS data center
client = Client("IRIS")

# Set the time range for fetching earthquake data
# Start time: January 1, 2023
# End time: Current time
starttime = UTCDateTime("2023-01-01")
endtime = UTCDateTime()

# Fetch earthquake events with a minimum magnitude of 7
catalog = client.get_events(starttime=starttime, endtime=endtime, minmagnitude=7)
#client.get_events(). This function returns a Catalog object that contains a list of Event objects. 
#Each Event object, in turn, has an Origins attribute that contains the depth information

# Plot the fetched earthquake data using an orthographic projection
catalog.plot(projection="ortho", title="Global Earthquakes with Magnitude >= 7 since 2023")
#catalog.plot(), ObsPy automatically uses the depth information to color the events in the plot

How to plot (Local) Earthquakes

 

from obspy import UTCDateTime
from obspy.clients.fdsn import Client

# Initialize FDSN client
client = Client("AUSPASS")

# Define time range
starttime = UTCDateTime("2023-01-01")
endtime = UTCDateTime()

# Latitude and longitude bounds for Australia
minlatitude = -44.0
maxlatitude = -10.0
minlongitude = 113.0
maxlongitude = 154.0

# Fetch event data for Australia with a minimum magnitude 
catalog = client.get_events(starttime=starttime, endtime=endtime, minmagnitude=4,
                            minlatitude=minlatitude, maxlatitude=maxlatitude,
                            minlongitude=minlongitude, maxlongitude=maxlongitude)

# Plot the earthquakes
catalog.plot(projection="local", title="Australia Earthquakes", resolution="i")

How to download a lot of waveform data

One day at a time

 

from obspy import UTCDateTime
from obspy.clients.fdsn import Client
import datetime

# Initialize client and set parameters
client = Client("IRIS")
start_date = UTCDateTime("2023-07-01")
end_date = UTCDateTime("2023-07-02")
network = "S1"
station = "AUMTS"
channel = "BHZ"

# Loop to download data one day at a time
while start_date <= end_date:
    next_date = start_date + datetime.timedelta(days=1)
   try:
        st = client.get_waveforms(network, station, "*", channel, start_date, next_date)
        st.write(f"{start_date.date}.mseed", format="MSEED")
   except:
       print(f"Failed for {start_date} to {next_date}")
    start_date = next_date

How to store and archive waveform data in SeisComP Data Structure (SDS)

 

import os
from obspy import UTCDateTime, read
from obspy.clients.fdsn import Client

# Initialize the client
client = Client("AUSPASS")  # Replace with the correct client endpoint if different

# Define event time and time window
event_time = UTCDateTime("2021-09-21T23:15:53")
starttime = event_time - 600  # 10 minutes before the event
endtime = event_time + 1800  # 30 minutes after the event

# Download the waveform data
st = client.get_waveforms(network="S1", station="AUMTS", location="*", channel="*", starttime=starttime, endtime=endtime)

# Create SDS structure: ROOT/YEAR/NET/STA/CHAN.TYPE/NET.STA.LOC.CHAN.YEAR.DAY
sds_root = "."  # Replace with your desired directory

for tr in st:
    net = tr.stats.network
    sta = tr.stats.station
    loc = tr.stats.location
    chan = tr.stats.channel
    year = str(tr.stats.starttime.year)
    jday = str(tr.stats.starttime.julday).zfill(3)
   
    sds_path = os.path.join(sds_root, year, net, sta, f"{chan}.D", f"{net}.{sta}.{loc}.{chan}.{year}.{jday}")
   
   # Create directories if they don't exist
    os.makedirs(os.path.dirname(sds_path), exist_ok=True)
   
   # Save the trace as a MiniSEED file
    tr.write(sds_path, format="MSEED")