pisces.query.stations#
- pisces.query.stations.assign_unique_net(q, network_name, affiliation_name, pref_nets=None, two_char_code=True, first_available=True, default_net='__')[source]#
- pisces.query.stations.filter_networks(query, net=None, netname=None, auth=None, sta=None, times=None, **tables)[source]#
Filter a network query using Network, Affiliation tables.
These filters are primarily catagorical. For additional station filtering, queries can be passed to and from filter_stations() and filter_responses() to and from filter_networks().
- Parameters:
- query: sqlalchemy.Query instance
Includes the required tables for your query (e.g. query = session.query(Network, Affiliation)), otherwise they must be provided as keywords (see below).
- netstr
Network code from
NetworkorAffiliation. Filter Network table on net column. Wildcards accepted. Multiple nets can be included as a list, tuple, or comma separated string (no spaces). If Network and Affiliation tables are provided in query or**tables, the Affiliation table is automatically joined to the Network table on the net column.- netname: str
Network name from
Network. Filter Network table on netname column. Wildcards accepted. Multiple netnames can be included as a list, tuple, or comma separated string (no spaces).- auth: str
Network author from
Network. Filter Network table on auth column. Wildcards accepted. Multiple authors can be included as a list, tuple, or comma separated string (no spaces).- stastr
Station code from
Affiliation. Filter Affiliation table on sta column. Wildcards accepted. Multiple stations can be included as a list, tuple, or comma separated string (no spaces).- timestuple
Time range applied to
Affiliation. (starttime, endtime) inclusive range containing int/float/None Unix timestamps. Filters Affiliation table on the time and endtime columns. If starttime or endtime are None, that column is not filtered.- **tables
If a required table isn’t in the SELECT of your query, you can provide it here as a keyword argument (e.g. site=Site). 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(Site)- Joins:
- If Network and Affiliation:
Network.net == Affiliation.net
- If Site:
Affiliation.sta == Site.sta
- If tables other than Network, Affiliation, or Site are present in the query, they will not
- be properly joined, resulting in improperly filtered output.
- Returns:
- query: sqlalchemy.Query instance
The returned variable is a sqlalchemy query with the filters corresponding to the provided inputs applied.
Examples
Forgot to include a required table when building query:
>>> q = session.query(Network) >>> try: ... q = filter_networks(q, sta = 'ANMO') ... except ValueError: ... # fails b/c Affiliation isn't in the query ... q = filter_networks(q, sta = 'ANMO', affiliation=Affiliation) >>> networks = q.all() # list of all networks affiliated with station ANMO
Get a cartesian join of both networks and stations
>>> q = session.query(Network, Affiliation) >>> q = session.query(Network).add_entity(Affiliation) # equivalent to above >>> nets_and_stas = filter_networks(q, sta = 'ANMO').all()
Get a cartesian join of networks to sites
>>> q = session.query(Network, Affiliation, Site) >>> q = session.query(Network, Affiliation).add_entity(Site) # equivalent to above >>> nets_and_sites = filter_networks(q, sta = 'ANMO').all()
String handling. The following queries will produce equivalent results
>>> q = session.query(Network) >>> q = filter_networks(q, net = 'SR,IU') >>> q = filter_networks(q, net = ('SR','IU')) >>> q = filter_networks(q, net = ['SR',IU'])
String handling. Wildcards
*and?are accepted as well as SQL wildcards%and_.>>> q = session.query(Network, Affiliation) >>> q = filter_networks(q, net = 'SR', sta = ['*'])
- pisces.query.stations.filter_responses(query, sta=None, chan=None, times=None, **tables)[source]#
Filter query for instrument response information using the Sensor and Instrument tables.
These filters are primarily catagorical. For additional station filtering, queries can be passed to and from filter_networks() and filter_stations to and from filter_responses().
filter_responses() only provides the response information in database tables. Actual response files will need to be read in separately from the dir and dfile columns returned in the Instrument table.
- Parameters:
- query: sqlalchemy.Query instance
Includes the required tables for your query (e.g. query = session.query(Sensor, Instrument)), otherwise they must be provided as keywords (see below). Both Sensor and Instrument are required and are joined on the inid column,
- stastr
Station code from
Sensor. Filter Sensor on sta column. Wildcards accepted. Multiple stations can be included as a list, tuple, or comma separated string (no spaces).- chanstr
Channel code from
Sensor. Filter Sensor table on chan column. Wildcards accepted. Multiple chans can be included as a list, tuple, or comma separated string (no spaces).- timestuple
Time range applied to
Sensor. (starttime, endtime) inclusive range containing int/float/None Unix timestamps. Filters Sensor table on the time and endtime columns. If starttime or endtime are None, that column is not filtered.- **tables
If a required table isn’t in the SELECT of your query, you can provide it here as a keyword argument (e.g. sitechan=Sitechan). 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(Sitechan)- Joins:
- Sensor.inid == Instrument.inid
- If Sitechan:
Sitechan.chanid == Sensor.chanid
- If tables other than Sensor, Instrument, or Sitechan are present in the query, they will not
- be properly joined, resulting in improperly filtered output.
- Returns:
- query: sqlalchemy.Query instance
The returned variable is a sqlalchemy query with the filters corresponding to the provided inputs applied.
Examples
See filter_networks() docstring for usage examples.
- pisces.query.stations.filter_stations(query, sta=None, chan=None, times=None, region=None, staname=None, refsta=None, **tables)[source]#
Filter a site query using Site, Sitechan tables.
These filters are primarily geograpchical and catagorical. For additional station filtering, queries can be passed to and from filter_networks() and filter_responses() to and from filter_stations().
- Parameters:
- query: sqlalchemy.Query instance
Includes the required tables for your query (e.g. query = session.query(Site, Sitechan)), otherwise they must be provided as keywords (see below).
- stastr
Station code from
SiteorSitechan. Filter Site or Sitechan table on sta column. Wildcards accepted. Multiple stations can be included as a list, tuple, or comma separated string (no spaces). If Site and Sitechan tables is provided in query or**tables, the Site table is automatically joined to the Sitechan table on the sta column.- chanstr
Channel code from
Sitechan. Filter Sitechan table on chan column. Wildcards accepted. Multiple chans can be included as a list, tuple, or comma separated string (no spaces).- timestuple
Time range applied to
SiteorSitechan. (starttime, endtime) inclusive range containing int following the format YYYYDDD for the 4 character year and the 3 character julian day. Filters Site or Sitechan table on the time and endtime columns. If starttime or endtime are None, that column is not filtered. If both Site and Sitechan are provided, Site will be further filtered by Sitechan.ondate.between(Site.ondate, Site.offdate)- regiontuple
Geographic bounds applied to
Site. (W, E, S, N) inclusive lat/lon box containing int/float/None(unbounded) degrees- staname: str
Station name from
Site. Filter Site table on staname column. Wildcards accepted. Multiple stanames can be included as a list, tuple, or comma separated string (no spaces).- refsta: str
Reference station from
Site. Filter Site table on refsta column. Wildcards accepted. Multiple refstas can be included as a list, tuple, or comma separated string (no spaces).- **tables
If a required table isn’t in the SELECT of your query, you can provide it here as a keyword argument (e.g. affiliation=Affiliation). 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(Affiliation)- Joins:
- If Site and Sitechan:
Site.sta == Sitechan.sta
- If times and Sitechan:
Sitechan.ondate.between(Site.ondate, Site.offdate)
- If Affiliation or Sensor are provided, filter_stations will attempt to join to site and
- sitechan as follows:
- If Sitechan and Sensor:
Sitechan.chanid == Sensor.chanid
- If Site and Affiliation:
Site.sta == Affiliation.sta
- If Affiliation is provided and not Site or if Sensor is provided and not Sitechan, the tables
- will not be properly joined resulting in an improperly filtered output. If any tables other
- than Affiliation, Site, Sitechan, or Sensor are provided they will not be joined at all
- resulting in an improperly filtered output.
- Returns:
- query: sqlalchemy.Query instance
The returned variable is a sqlalchemy query with the filters corresponding to the provided inputs applied.
Examples
Region filtering on Site table
>>> q = session.query(Site) >>> stations = filter_sites(q, region=(W, E, S, N)).all() >>> stations = filter_sites(q, region=(-107, -106, 34, 35)).all()
See filter_networks() docstring for additional usage examples.