pisces.query.events#

A series of query-building functions

These functions simply add WHERE clauses to an _existing_ query using more convenient syntax. The incoming query should contain the tables your using in your WHERE clause, or the required tables should be provided using keywords. If supplied in this way, they’ll be used to filter the result set, but won’t be added to the query SELECT statement.

pisces.query.events.filter_arrivals(query, sta=None, auth=None, times=None, orid=None, phase=None, **tables)[source]#

Filter a query for phase arrival information using Arrival, Assoc

Parameters:
querySQLAlchemy query object

Includes any of Arrival, Assoc tables

stastr or list

Station code from Assoc or Arrival.

authstr or list

Arrival author.

timestuple of (starttime, endtime)

Time range applied to Arrival. Anything that obspy.UTCDateTime can consume is accepted.

oridint or list of int

Origin IDs from Assoc.

phasestr or list of str

Phase names matched between Assoc.phase and Arrival.iphase.

Joins:
Origin.orid == Assoc.orid
Assoc.arid == Arrival.arid
pisces.query.events.filter_events(query, region=None, times=None, depth=None, evid=None, orid=None, prefor=False, auth=None, evname=None, etype=None, **tables)[source]#

Filter an event query using Event, Origin tables.

These filters are primarily geographic and catagorical. For magnitude filtering, Use the filter_magnitudes function for magnitude filtering, and filter_arrivals for arrivals and amplitudes.

Parameters:
query: sqlalchemy.Query instance

Includes the required tables for your query (e.g. query = session.query(Event, Origin)), otherwise they must be provided as keywords (see below).

regiontuple

Geographic bounds applied to Origin rows. (W, E, S, N) inclusive lat/lon box containing int/float/None(unbounded) degrees

timestuple

Time range applied to Origin rows. (starttime, endtime) inclusive range containing int/float/None Unix timestamps

depthtuple

Depth range applied to Origin rows. (mindepth, maxdepth) inclusive range, in float/int kilometers

evidlist or tuple of int

Event IDs from Event. Event ID number.

oridlist or tuple of int

Origin IDs from Origin. Origin ID number.

preforbool

Use the preferred origin from Event and Origin. Use only the preferred origin.

authstr

Origin author. Produces an equality clause.

evnamestr

Event name from Event. Produces a CONTAINS clause (looks for input as a substring).

etypestr or iterable of str

Event type from Origin. Two-character KBCore event type(s). Produces an == clause if it’s a string, an “IN” clause if it’s a list or tuple.

**tables

If a required table isn’t in the SELECT of your query, you can provide it here as a keyword argument (e.g. event=Event). It gets used in the filter, but isn’t included in the final result set (unless it was already in the SELECT). If you wish the table included in the result set, use the sqlalchemy.orm.Query.add_entity method prior to calling this function. E.g. q = q.add_entity(Event)

Joins:
Event.evid == Origin.evid
Event.prefor == Origin.orid

Examples

Forgot to include a required table when building query:

>>> q = session.query(Origin)
>>> try:
... q = filter_events(q, region=(W, E, S, N), prefor=True)
... except ValueError:
...     # fails b/c Event isn't in the query
...     q = filter_events(q, region=(W, E, S, N), prefor=True, event=Event)
>>> origins = q.all() # a list of preferred Origin instances

Get a cartesian join of both origins and events

>>> q = session.query(Origin, Event)
>>> q = session.query(Origin).add_entity(Event) # equivalent to above
>>> origins_and_events = filter_events(q, region=(W, E, S, N), prefor=True).all()
pisces.query.events.filter_magnitudes(query, net=None, sta=None, auth=None, snr=None, amptype=None, *, netmag=None, stamag=None, amplitude=None, **magnitudes)[source]#

Filter an event query by magnitude range using Origin, Netmag, and/or Stamag tables.

Supplied tables are joined, and filters are applied to the highest-granularity table they can be: Stamag, then Netmag, then Origin. Each magnitude keyword produces an OR clause.

Parameters:
querysqlalchemy.orm.Query

Query to filter.

netstr or iterable of str

Network code filter; requires Netmag.

stastr or iterable of str

Station code filter; requires Stamag.

authstr or iterable of str

Author filter, applied to Stamag, Netmag, or Origin in that priority order.

snrtuple

(min|None, max|None) signal-to-noise range; requires Amplitude.

amptypestr or iterable of str

Amplitude type filter, wildcards allowed; requires Amplitude.

netmag, stamagORM table class, optional

Magnitude-domain table to use for filtering when it is not selected by query. Event and Origin come from the preceding filter_events query.

amplitudeORM Amplitude table class, optional

Amplitude table to include or join for result rows. It requires Stamag and joins on Stamag.ampid == Amplitude.ampid.

**magnitudes

magtype=(min|None, max|None) filters, for example mb=(3.5, 5.5). all applies the range to every row-oriented magnitude type. Origin supports only mb, ml, and ms columns; wildcard magnitude types require Netmag or Stamag.

Joins:
Origin.orid == Netmag.orid
Netmag.evid == Event.evid
Netmag.magid == Stamag.magid
Origin.orid == Stamag.orid
Stamag.ampid == Amplitude.ampid