pysmo.tools.web
Tools for fetching seismological data from web services.
Thin wrappers around EarthScope's FDSN web services. fetch_stationxml,
fetch_sacpz, fetch_geocsvseismogram, and fetch_sac return raw,
unparsed responses — each a lower-level counterpart to a class's own
.fetch() classmethod (e.g. SAC.fetch),
useful on its own for saving a raw response to disk and deferring parsing
to later, without another network request. fetch_travel_times is the
exception: it has no class counterpart and returns an already-parsed
dict[str, float].
Type Aliases:
| Name | Description |
|---|---|
TravelTimeBackend |
Callable |
Functions:
| Name | Description |
|---|---|
fetch_geocsvseismogram |
Fetch raw GeoCSV waveform bytes for a station/channel and time window. |
fetch_sac |
Fetch a raw SAC zip archive for a station/channel and time window. |
fetch_sacpz |
Fetch raw SAC PZ response metadata text for a station/channel. |
fetch_stationxml |
Fetch raw StationXML response metadata bytes for a station/channel. |
fetch_travel_times |
Fetch seismic phase travel times for a given source–receiver geometry. |
TravelTimeBackend
Callable (depth_km, dist_deg, phases) -> dict[str, float] returning travel times.
Accepts source depth in kilometres, epicentral distance in degrees, and a list of
seismic phase names (e.g. ["P", "S"]). Returns a mapping of phase name to
travel time in seconds, omitting phases with no arrival at the given geometry.
fetch_geocsvseismogram
Fetch raw GeoCSV waveform bytes for a station/channel and time window.
A lower-level counterpart to
GeoCsvSeismogram.fetch:
returns the waveform unparsed and uninterpreted. Save it to disk to
defer parsing to later — offline, without another network request —
via GeoCsvSeismogram.from_text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
station
|
Station
|
Any object satisfying the |
required |
starttime
|
Timestamp
|
Start of the requested time window (UTC). |
required |
endtime
|
Timestamp
|
End of the requested time window (UTC). |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Raw GeoCSV document bytes. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If the dataselect web service returns an HTTP error. |
Examples:
>>> import pandas as pd
>>> from pathlib import Path
>>> from pysmo import MiniStation
>>> from pysmo.tools.web import fetch_geocsvseismogram
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="LHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> data = fetch_geocsvseismogram( # doctest: +SKIP
... station=station,
... starttime=pd.Timestamp("2010-02-27T06:44:00Z"),
... endtime=pd.Timestamp("2010-02-27T06:54:00Z"),
... )
>>> Path("ANMO.geocsv").write_bytes(data) # doctest: +SKIP
>>>
Source code in src/pysmo/tools/web.py
fetch_sac
Fetch a raw SAC zip archive for a station/channel and time window.
A lower-level counterpart to SAC.fetch:
returns the zip archive returned by the dataselect web service
unparsed and uninterpreted, without extracting or reading any of its
members. Save it to disk to defer parsing to later — offline, without
another network request — via
SAC.from_zip or
SAC.all_from_zip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
station
|
Station
|
Any object satisfying the |
required |
starttime
|
Timestamp
|
Start of the requested time window (UTC). |
required |
endtime
|
Timestamp
|
End of the requested time window (UTC). |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Raw zip archive bytes, as returned by the dataselect web service. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If the dataselect web service returns an HTTP error. |
Examples:
>>> import pandas as pd
>>> from pathlib import Path
>>> from pysmo import MiniStation
>>> from pysmo.tools.web import fetch_sac
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="LHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> data = fetch_sac( # doctest: +SKIP
... station=station,
... starttime=pd.Timestamp("2010-02-27T06:44:00Z"),
... endtime=pd.Timestamp("2010-02-27T06:54:00Z"),
... )
>>> Path("ANMO.sac.zip").write_bytes(data) # doctest: +SKIP
>>>
Source code in src/pysmo/tools/web.py
fetch_sacpz
Fetch raw SAC PZ response metadata text for a station/channel.
A lower-level counterpart to
SacPZ.fetch: returns the response
metadata unparsed and uninterpreted. Save it to disk to defer parsing
to later — offline, without another network request — via
SacPZ.from_text or
SacPZ.all_from_text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
station
|
Station
|
Any object satisfying the |
required |
time
|
Timestamp | None
|
Timestamp used to select the response epoch server-side, so
exactly one epoch is returned. If |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Raw SAC PZ text. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If the web service returns an HTTP error. |
Examples:
>>> from pathlib import Path
>>> from pysmo import MiniStation
>>> from pysmo.tools.web import fetch_sacpz
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="BHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> text = fetch_sacpz(station=station) # doctest: +SKIP
>>> Path("ANMO.pz").write_text(text) # doctest: +SKIP
>>>
Source code in src/pysmo/tools/web.py
fetch_stationxml
Fetch raw StationXML response metadata bytes for a station/channel.
A lower-level counterpart to
StationXML.fetch: returns the
StationXML document unparsed and uninterpreted, covering every response
epoch on record for the requested channel. Save it to disk to defer
parsing to later — offline, without another network request — via
StationXML.from_bytes or
StationXML.all_from_bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
station
|
Station
|
Any object satisfying the |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Raw StationXML document bytes. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If the station web service returns an HTTP error. |
Examples:
>>> from pathlib import Path
>>> from pysmo import MiniStation
>>> from pysmo.tools.web import fetch_stationxml
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="BHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> xml = fetch_stationxml(station=station) # doctest: +SKIP
>>> Path("ANMO.xml").write_bytes(xml) # doctest: +SKIP
>>>
Source code in src/pysmo/tools/web.py
fetch_travel_times
fetch_travel_times(
depth_km: float,
dist_deg: float,
phases: list[str],
model: str = "iasp91",
travel_time_backend: TravelTimeBackend | None = None,
) -> dict[str, float]
Fetch seismic phase travel times for a given source–receiver geometry.
Uses the EarthScope traveltime web service by default, or a custom callable if travel_time_backend is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth_km
|
float
|
Source depth in kilometres. |
required |
dist_deg
|
float
|
Epicentral distance in degrees. |
required |
phases
|
list[str]
|
Seismic phase names to request (e.g. |
required |
model
|
str
|
Velocity model name. |
'iasp91'
|
travel_time_backend
|
TravelTimeBackend | None
|
Optional callable overriding the web service. Must
accept |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Mapping of phase name to travel time in seconds. Only phases with |
dict[str, float]
|
arrivals at the given distance and depth are included. |
Examples:
Using a custom travel_time_backend instead of the EarthScope web service.
The lambda below is a stand-in; replace it with a real travel-time
calculator:
>>> from pysmo.tools.web import fetch_travel_times
>>> backend = lambda depth, dist, phases: {"P": 480.2, "S": 900.1}
>>> fetch_travel_times(22.9, 60.0, ["P", "S"], travel_time_backend=backend)
{'P': 480.2, 'S': 900.1}
>>>
Fetching a seismogram windowed around a predicted arrival is a short
combination of this function with pysmo.tools.azdist.haversine
and a class's own .fetch() method (e.g.
GeoCsvSeismogram.fetch, or
SAC.fetch):
>>> import pandas as pd
>>> from pysmo import MiniEvent, MiniStation
>>> from pysmo.classes import GeoCsvSeismogram
>>> from pysmo.tools.azdist import haversine
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="LHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> event = MiniEvent(
... latitude=-36.122, longitude=-72.898, depth=22900.0,
... time=pd.Timestamp("2010-02-27T06:34:11.53Z"),
... )
>>> dist = haversine(event, station)
>>> backend = lambda depth, dist, phases: {"P": 604.654} # stand-in
>>> travel_times = fetch_travel_times(
... event.depth / 1000.0, dist, ["P"], travel_time_backend=backend
... )
>>> predicted_p = event.time + pd.Timedelta(seconds=travel_times["P"])
>>> seismogram = GeoCsvSeismogram.fetch( # doctest: +SKIP
... station=station,
... starttime=predicted_p - pd.Timedelta(minutes=2),
... endtime=predicted_p + pd.Timedelta(minutes=8),
... )
>>>
Source code in src/pysmo/tools/web.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |