pisces.query.builder#

Fluent SQLAlchemy select() queries for Pisces tables.

QueryBuilder supplies the table setup and valid ordering around the lower-level filter_* functions. It is an internal convenience API; the filters remain available for unusual projections or filter-only tables.

Examples#

Build an event query without selecting or passing table classes yourself:

q = (
    QueryBuilder(session, event=Event, origin=Origin, netmag=Netmag)
    .events(region=(west, east, south, north), prefor=True)
    .magnitudes(mw=(4, 6))
)

# more readable?
q = (
    QueryBuilder(session,
        event=Event,
        origin=Origin,
        netmag=Netmag
        )
    .events(
        region=(west, east, south, north),
        prefor=True,
    )
    .magnitudes(
        mw=(4, 6),
    )
)

# Realize the chain with normal SQLAlchemy.
rows = session.execute(query.statement).all()

Build a station query from networks down through responses. Network and station filters belong on the initial stations call; each later fluent method adds the tables its filter needs:

responses = (
    QueryBuilder(session,
        network=Network,
        affiliation=Affiliation,
        site=Site,
        sitechan=Sitechan,
        sensor=Sensor,
        instrument=Instrument
        )
    .stations(
        net="IU",
        region=(west, east, south, north)
        )
    .channels(
        chan="BH?"
        )
    .responses()
)
rows = session.execute(responses.statement).all()

The underlying SQLAlchemy statement is available for inspection or further composition:

print(q.statement)
stmt = q.statement.order_by(Origin.time)
rows = session.execute(stmt).all()

The fluent API adds tables before applying their filters, so this is valid:

q.events().arrivals(phase="P", sta="ANMO")

while unsupported domain transitions are absent rather than becoming runtime query errors:

q.events().responses()  # no such method

Terminal catalog() and inventory() methods establish the intended domain API. Their converters are not implemented yet; until then, execute query.statement yourself.

class pisces.query.builder.EventQuery(client: QueryBuilder, statement: Select)[source]#

Bases: object

An immutable Event/Origin query chain.

Each filter method returns a new chain; branching does not mutate the original:

base = client.events(region=region)
magnitudes = base.magnitudes(mb=(3, 4))
arrivals = base.arrivals(phase="P")
arrivals(*, sta=None, auth=None, times=None, orid=None, phase=None) → Self[source]#

Include Assoc and Arrival rows and apply pisces.query.filter_arrivals().

catalog(**kwargs)[source]#

Build an ObsPy Catalog from this event query.

Requires pisces.catalog.catalog(session, stmt, **kwargs).

client: QueryBuilder#
magnitudes(*, net=None, auth=None, **magnitudes) → Self[source]#

Include Netmag rows and apply pisces.query.filter_magnitudes().

Keyword magnitude ranges match filter_magnitudes: for example, mw=(4, 6) or mb=(None, 5). Station magnitude filtering (sta=...) is not in this initial fluent API; use filter_magnitudes directly when Stamag is needed.

statement: Select#
class pisces.query.builder.QueryBuilder(session: Session, **tables)[source]#

Bases: object

Create fluent internal query chains backed by a SQLAlchemy session.

Parameters:
session

SQLAlchemy session associated with the fluent query chain. Realize a chain with session.execute(query.statement).

tables

Pisces ORM table classes keyed by lowercase table name, such as event=Event and origin=Origin. The event chain requires those two tables; magnitudes additionally requires netmag and arrivals requires assoc and arrival. The station chain starts with network and affiliation; its optional layers require site, sitechan, sensor, and instrument as applicable.

events(*, region=None, times=None, depth=None, evid=None, orid=None, prefor=False, auth=None, evname=None, etype=None) → EventQuery[source]#

Start an Event/Origin query and apply pisces.query.filter_events().

stations(*, net=None, netname=None, network_auth=None, affiliation_times=None, sta=None, times=None, region=None, staname=None, refsta=None) → StationQuery[source]#

Start a Network/Affiliation/Site query.

Network filters use the names from pisces.query.filter_networks(); the auth and times names are disambiguated here as network_auth and affiliation_times. Station filters otherwise match pisces.query.filter_stations().

class pisces.query.builder.StationQuery(client: QueryBuilder, statement: Select)[source]#

Bases: object

An immutable Network/Affiliation/Site query chain.

channels(*, sta=None, chan=None, times=None) → Self[source]#

Include Sitechan rows and apply pisces.query.filter_stations().

client: QueryBuilder#
inventory()[source]#

Build an ObsPy Inventory at the deepest layer included in this chain.

stations().inventory() is station-level, channels().inventory() is channel-level, and responses().inventory() is response-level.

responses(*, sta=None, chan=None, times=None) → Self[source]#

Include response rows and apply pisces.query.filter_responses().

Sitechan is added first because it links the Site chain to Sensor. The returned chain can be realized as a response-level inventory with inventory().

TODO: expose additional response filters here when filter_responses supports them.

statement: Select#