squirrel.base
¶
-
class
Selection
(database, persistent=None)[source]¶ Bases:
object
Database backed file selection (base class for
Squirrel
).Parameters: In the Squirrel framework, a selection is conceptually a list of files to be made available in the application. Instead of using
Selection
directly, user applications should usually use its subclassSquirrel
which adds content indices to the selection and provides high level data querying.By default, a temporary table in the database is created to hold the names of the files in the selection. This table is only visible inside the application which created it. If a name is given to
persistent
, a named selection is created, which is visible also in other applications using the same database.Besides the filename references, desired content kind masks and file format indications are stored in the selection’s database table to make the user choice regarding these options persistent on a per-file basis. Book-keeping on whether files are unknown, known or if modification checks are forced is also handled in the selection’s file-state table.
Paths of files can be added to the selection using the
add()
method and removed withremove()
.undig_grouped()
can be used to iterate over all content known to the selection.-
add
(paths, kind_mask=255, format='detect')[source]¶ Add files to the selection.
Parameters: paths (iterator yielding str
objects) – Paths to files to be added to the selection.
-
remove
(paths)[source]¶ Remove files from the selection.
Parameters: paths ( list
ofstr
) – Paths to files to be removed from the selection.
-
iter_paths
()[source]¶ Iterate over all file paths currently belonging to the selection.
Returns: Iterator yielding file paths.
-
get_paths
()[source]¶ Get all file paths currently belonging to the selection.
Returns: List of file paths.
-
undig_grouped
(skip_unchanged=False)[source]¶ Get inventory of cached content for all files in the selection.
Param: skip_unchanged: if True
only inventory of modified files is yielded (flag_modified()
must be called beforehand).This generator yields tuples
((format, path), nuts)
wherepath
is the path to the file,format
is the format assignation or'detect'
andnuts
is a list ofNut
objects representing the contents of the file.
-
flag_modified
(check=True)[source]¶ Mark files which have been modified.
Parameters: check – If True
query modification times of known files on disk. IfFalse
, only flag unknown files.Assumes file state is 0 for newly added files, 1 for files added again to the selection (forces check), or 2 for all others (no checking is done for those).
Sets file state to 0 for unknown or modified files, 2 for known and not modified files.
-
-
class
SquirrelStats
(**kwargs)[source]¶ Bases:
pyrocko.guts.Object
Container to hold statistics about contents available from a Squirrel.
See also
Squirrel.get_stats()
.-
♦
nfiles
¶ int
Number of files in selection.
-
♦
nnuts
¶ int
Number of index nuts in selection.
-
♦
codes
¶ list
oftuple
ofstr
objects objects, default:[]
Available code sequences in selection, e.g. (agency, network, station, location) for stations nuts.
-
♦
kinds
¶ list
ofstr
objects, default:[]
Available content types in selection.
-
♦
total_size
¶ int
Aggregated file size of files is selection.
-
♦
counts
¶ dict
ofdict
ofint
objects objects, default:{}
Breakdown of how many nuts of any content type and code sequence are available in selection,
counts[kind][codes]
.
-
♦
tmin
¶ builtins.float
(pyrocko.guts.Timestamp
), optionalEarliest start time of all nuts in selection.
-
♦
tmax
¶ builtins.float
(pyrocko.guts.Timestamp
), optionalLatest end time of all nuts in selection.
-
♦
-
class
Squirrel
(env=None, database=None, cache_path=None, persistent=None)[source]¶ Bases:
pyrocko.squirrel.base.Selection
Prompt, lazy, indexing, caching, dynamic seismological dataset access.
Parameters: - env (
SquirrelEnvironment
orstr
) – Squirrel environment instance or directory path to use as starting point for its detection. By default, the current directory is used as starting point. When searching for a usable environment the directory'.squirrel'
or'squirrel'
in the current (or starting point) directory is used if it exists, otherwise the parent directories are search upwards for the existence of such a directory. If no such directory is found, the user’s global Squirrel environment'$HOME/.pyrocko/squirrel'
is used. - database (
Database
orstr
) – Database instance or path to database. By default the database found in the detected Squirrel environment is used. - cache_path (
str
) – Directory path to use for data caching. By default, the'cache'
directory in the detected Squirrel environment is used. - persistent (
str
) – If given a name, create a persistent selection.
Provides a unified interface to query seismic waveforms, station and sensor metadata, and event information from local file collections and remote data sources. Query results are promptly returned, even for very large collections, thanks to a highly optimized database setup working behind the scenes. Assemblage of a data selection is very fast for known files as all content indices are cached in a database. Unknown files are automatically indexed when added to the selection.
Features
- Efficient[1] lookup of data relevant for a selected time window.
- Metadata caching and indexing.
- Modified files are re-indexed as needed.
- SQL database (sqlite) is used behind the scenes.
- Can handle selections with millions of files.
- Data can be added and removed at run-time, efficiently[1].
- Just-in-time download of missing data.
- Disk-cache of meta-data query results with expiration time.
- Efficient event catalog synchronization.
- Always-up-to-date data coverage indices.
- Always-up-to-date indices of available station/channel codes.
[1] O log N performance, where N is the number of data entities (nuts).
Queries are restricted to the contents offered by the files which have been added to the Squirrel (which usually is a subset of the information collected in the attached global file meta-information database).
By default, temporary tables are created in the attached database to hold the names of the files in the selection as well as various indices and counters. These tables are only visible inside the application which created it. If a name is given to
persistent
, a named selection is created, which is visible also in other applications using the same database.Paths of files can be added to the selection using the
add()
method.-
add
(paths, kinds=None, format='detect', check=True, progress_viewer='terminal')[source]¶ Add files to the selection.
Parameters: - paths (
list
ofstr
) – Iterator yielding paths to files or directories to be added to the selection. Recurses into directories. If given astr
, it is treated as a single path to be added. - kinds (
list
ofstr
) – Content types to be made available through the Squirrel selection. By default, all known content types are accepted. - format (
str
) – File format identifier or'detect'
to enable auto-detection.
Complexity: O(log N)
- paths (
-
reload
()[source]¶ Check for modifications and reindex modified files.
Based on file modification times.
-
add_virtual
(nuts, virtual_paths=None)[source]¶ Add content which is not backed by files.
Parameters: Stores to the main database and the selection.
-
add_source
(source)[source]¶ Add remote resource.
Parameters: source (subclass of Source
) – Remote data access client instance.
-
add_fdsn
(*args, **kwargs)[source]¶ Add FDSN site for transparent remote data access.
Arguments are passed to
FDSNSource
.
-
add_catalog
(*args, **kwargs)[source]¶ Add online catalog for transparent event data access.
Arguments are passed to
CatalogSource
.
-
iter_nuts
(kind=None, tmin=None, tmax=None, codes=None, naiv=False, kind_codes_ids=None)[source]¶ Iterate content entities matching given constraints.
Parameters: - kind (
str
,list
ofstr
) – Content kind (or kinds) to extract. - tmin (timestamp) – Start time of query interval.
- tmax (timestamp) – End time of query interval.
- codes (
tuple
ofstr
) – Pattern of content codes to be matched. - naiv (
bool
) – Bypass time span lookup through indices (slow, for testing). - kind_codes_ids (
list
ofstr
) – Kind-codes IDs of contents to be retrieved (internal use).
Complexity: O(log N) for the time selection part due to heavy use of database indices.
Yields
Nut
objects representing the intersecting content.Query time span is treated as a half-open interval
[tmin, tmax)
. However, iftmin
equalstmax
, the edge logics are modified to closed-interval so that content intersecting with the time instantt = tmin = tmax
is returned (otherwise nothing would be returned as[t, t)
never matches anything).Time spans of content entities to be matched are also treated as half open intervals, e.g. content span
[0, 1)
is matched by query span[0, 1)
but not by[-1, 0)
or[1, 2)
. Also here, logics are modified to closed-interval when the content time span is an empty interval, i.e. to indicate a time instant. E.g. time instant 0 is matched by[0, 1)
but not by[-1, 0)
or[1, 2)
.- kind (
-
get_nuts
(*args, **kwargs)[source]¶ Get content entities matching given constraints.
Like
iter_nuts()
but returns results as a list.
-
get_time_span
()[source]¶ Get time interval over all content in selection.
Complexity O(1), independent of the number of nuts.
Returns: (tmin, tmax)
-
get_deltat_span
(kind)[source]¶ Get min and max sampling interval of all content of given kind.
Parameters: kind ( str
) – Content kindReturns: (deltat_min, deltat_max)
-
iter_kinds
(codes=None)[source]¶ Iterate over content types available in selection.
Parameters: codes – if given, get kinds only for selected codes identifier Complexity: O(1), independent of number of nuts
-
iter_deltats
(kind=None)[source]¶ Iterate over sampling intervals available in selection.
Parameters: kind ( str
) – if given, get sampling intervals only for a given content typeComplexity: O(1), independent of number of nuts
-
iter_codes
(kind=None)[source]¶ Iterate over content identifier code sequences available in selection.
Parameters: kind ( str
) – if given, get codes only for a given content typeComplexity: O(1), independent of number of nuts
-
iter_counts
(kind=None)[source]¶ Iterate over number of occurrences of any (kind, codes) combination.
Parameters: kind – if given, get counts only for selected content type Yields tuples
((kind, codes), count)
Complexity: O(1), independent of number of nuts
-
get_kinds
(codes=None)[source]¶ Get content types available in selection.
Parameters: codes – if given, get kinds only for selected codes identifier Complexity: O(1), independent of number of nuts
Returns: sorted list of available content types
-
get_deltats
(kind=None)[source]¶ Get sampling intervals available in selection.
Parameters: kind – if given, get codes only for selected content type Complexity: O(1), independent of number of nuts
Returns: sorted list of available sampling intervals
-
get_codes
(kind=None)[source]¶ Get identifier code sequences available in selection.
Parameters: kind – if given, get codes only for selected content type Complexity: O(1), independent of number of nuts
Returns: sorted list of available codes as tuples of strings
-
get_counts
(kind=None)[source]¶ Get number of occurrences of any (kind, codes) combination.
Parameters: kind – if given, get codes only for selected content type Complexity: O(1), independent of number of nuts
Returns: dict
withcounts[kind][codes] or ``counts[codes]
if kind is notNone
-
update
(constraint=None, **kwargs)[source]¶ Update inventory of remote content for a given selection.
This function triggers all attached remote sources, to check for updates in the metadata. The sources will only submit queries when their expiration date has passed, or if the selection spans into previously unseen times or areas.
-
get_content
(nut, cache='default', accessor='default')[source]¶ Get and possibly load full content for a given index entry from file.
Loads the actual content objects (channel, station, waveform, …) from file. For efficiency sibling content (all stuff in the same file segment) will also be loaded as a side effect. The loaded contents are cached in the Squirrel object.
- env (
-
class
DatabaseStats
(**kwargs)[source]¶ Bases:
pyrocko.guts.Object
Container to hold statistics about contents cached in meta-information db.
-
♦
nfiles
¶ int
number of files in database
-
♦
nnuts
¶ int
number of index nuts in database
-
♦
codes
¶ list
oftuple
ofstr
objects objects, default:[]
available code sequences in database, e.g. (agency, network, station, location) for stations nuts.
-
♦
kinds
¶ list
ofstr
objects, default:[]
available content types in database
-
♦
total_size
¶ int
aggregated file size of files referenced in database
-
♦
counts
¶ dict
ofdict
ofint
objects objects, default:{}
breakdown of how many nuts of any content type and code sequence are available in database,
counts[kind][codes]
-
♦
-
class
Database
(database_path=':memory:', log_statements=False)[source]¶ Bases:
object
Shared meta-information database used by Squirrel.
-
dig
(nuts)[source]¶ Store or update content meta-information.
Given
nuts
are assumed to represent an up-to-date and complete inventory of a set of files. Any old information about these files is first pruned from the database (via database triggers). If such content is part of a live selection, it is also removed there. Then the new content meta-information is inserted into the main database. The content is not automatically inserted into the live selections again. It is in the responsibility of the selection object to perform this step.
-
remove
(path)[source]¶ Prune content meta-inforamation about a given file.
All content pieces belonging to file
path
are removed from the main database and any attached live selections (via database triggers).
-
reset
(path)[source]¶ Prune information associated with a given file, but keep the file path.
This method is called when reading a file failed. File attributes, format, size and modification time are set to NULL. File content meta-information is removed from the database and any attached live selections (via database triggers).
-