pysmo.tools.archive
Local archives for raw FDSN fetch responses.
An archive fetcher wraps a raw-bytes fetch function (e.g.
fetch_sac,
fetch_geocsvseismogram) with
persistent storage, so a station/time-window combination already fetched
once is read back locally rather than re-fetched.
Particularly useful as
PysmoProject.fetch_seismogram,
so a project's entries are only ever fetched once across however many times
the project is used.
Examples:
SqliteArchiveFetcher stores
responses in a local SQLite database. Paired here with
fetch_sac and
SAC.from_zip:
>>> import pandas as pd
>>> from pysmo import MiniStation, Seismogram
>>> from pysmo.classes import SAC
>>> from pysmo.tools.archive import SqliteArchiveFetcher
>>> from pysmo.tools.web import fetch_sac
>>>
>>> def parse_sac_zip(raw: bytes) -> Seismogram:
... return SAC.from_zip(raw).seismogram
...
>>> station = MiniStation(
... name="ANMO", network="IU", location="00", channel="LHZ",
... latitude=34.945981, longitude=-106.457133,
... )
>>> starttime = pd.Timestamp("2010-02-27T06:44:00Z")
>>> endtime = pd.Timestamp("2010-02-27T06:54:00Z")
>>>
>>> archive = SqliteArchiveFetcher(
... path="project_cache.sqlite3", fetch_raw=fetch_sac, parse=parse_sac_zip
... )
>>> seismogram = archive(station, starttime, endtime) # miss: fetches and stores
>>> seismogram_again = archive(station, starttime, endtime) # hit: no fetch
>>> isinstance(seismogram_again, Seismogram)
True
>>>
Type Aliases:
| Name | Description |
|---|---|
RawParser |
Callable |
Classes:
| Name | Description |
|---|---|
RawFetcher |
Callable |
SqliteArchiveFetcher |
Caches raw fetch responses in one local SQLite database. |
RawParser
RawParser = Callable[[bytes], Seismogram]
Callable (raw) -> Seismogram parsing a raw fetch response.
E.g. a wrapper around SAC.from_zip or
GeoCsvSeismogram.from_text.
Must agree with whichever RawFetcher it
is paired with — nothing enforces this pairing statically, the same as
fetch_sac/SAC.from_zip are already paired by convention today.
RawFetcher
Bases: Protocol
Callable (*, station, starttime, endtime) -> bytes returning a raw, unparsed fetch response.
A Protocol with a keyword-only __call__, not a plain Callable[...]
type alias, specifically because the functions this slot is meant to be
filled with directly — fetch_sac and
fetch_geocsvseismogram — are
themselves keyword-only. A plain positional Callable type cannot
express that, and calling one positionally raises TypeError regardless
of what a type checker allows.
Methods:
| Name | Description |
|---|---|
__call__ |
Fetch raw bytes for a station and absolute time window. |
Source code in src/pysmo/tools/archive.py
__call__
SqliteArchiveFetcher
Caches raw fetch responses in one local SQLite database.
Format-agnostic: stores whatever bytes fetch_raw returns
(zlib-compressed), keyed by station identity and time window, and hands
the decompressed bytes to parse on both a cache hit and a miss. A hit
never calls fetch_raw again — a side effect of this is bit-identical
replay of a previously fetched window, rather than only detecting drift
after the fact.
Local disk only
SQLite's own documentation states that WAL mode does not work over a network filesystem, and recommends against concurrent multi-process access to a SQLite database over NFS at all. This class assumes the database file lives on local disk with correctly functioning file locking; it is not a safe choice for a cache shared over a network filesystem by more than one process at a time.
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Fail fast if |
__call__ |
Return a |
__getstate__ |
Drop the live connection; the lock is excluded entirely below (no |
__setstate__ |
Restore state without triggering any |
close |
Close the underlying connection, if one is open. |
Attributes:
| Name | Type | Description |
|---|---|---|
fetch_raw |
RawFetcher
|
Fetches a raw response for a station and absolute time window. |
parse |
RawParser
|
Parses a raw response (freshly fetched, or read back from cache) into a |
path |
Path
|
Location of the SQLite database file. |
wal |
bool
|
Enable WAL mode. Only for a database confirmed to be on local disk — see the class docstring. |
Source code in src/pysmo/tools/archive.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
fetch_raw
instance-attribute
fetch_raw: RawFetcher
Fetches a raw response for a station and absolute time window.
parse
instance-attribute
parse: RawParser
Parses a raw response (freshly fetched, or read back from cache) into a Seismogram.
path
class-attribute
instance-attribute
Location of the SQLite database file.
The file itself is created on first use if it doesn't exist; its parent directory must already exist, checked at construction time.
wal
class-attribute
instance-attribute
wal: bool = False
Enable WAL mode. Only for a database confirmed to be on local disk — see the class docstring.
__attrs_post_init__
Fail fast if path's parent directory doesn't exist.
__call__
__call__(
station: Station,
starttime: Timestamp,
endtime: Timestamp,
) -> Seismogram
Return a Seismogram for station and window, from cache if already fetched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
station
|
Station
|
Station to fetch data for. |
required |
starttime
|
Timestamp
|
Start of the requested window (UTC). |
required |
endtime
|
Timestamp
|
End of the requested window (UTC). |
required |
Returns:
| Type | Description |
|---|---|
Seismogram
|
Parsed result — from the cache database on a hit, freshly |
Seismogram
|
fetched (and then stored) on a miss. |
Source code in src/pysmo/tools/archive.py
__getstate__
__getstate__() -> dict
Drop the live connection; the lock is excluded entirely below (no threading.Lock is picklable, not even a fresh one).
__setstate__
__setstate__(state: dict) -> None
Restore state without triggering any on_setattr hooks, then create a fresh lock.
close
Close the underlying connection, if one is open.
Not required before the object is garbage-collected or the process exits — normal teardown closes the file descriptor regardless — but call it explicitly to release the connection sooner in a long-running process holding many such fetchers.