Coverage for /usr/local/lib/python3.13/dist-packages/pyrocko/squirrel/tool/commands/catalog.py: 23%
47 statements
« prev ^ index » next coverage.py v7.6.0, created at 2025-12-04 10:41 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2025-12-04 10:41 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Implementation of :app:`squirrel catalog`.
8'''
10import logging
11import time
13from pyrocko import util
14from pyrocko.model.event import EventFilter
16from ..common import ldq
18km = 1000.
20logger = logging.getLogger('psq.cli.catalog')
22headline = 'Query and list catalog events.'
25def make_subparser(subparsers):
26 return subparsers.add_parser(
27 'catalog',
28 help=headline,
29 description=headline + '''
31Update earthquake catalog information and output summary information about
32matching events to the terminal. It reads event information from catalog files
33(see ``--add``) or from configured data sources (see ``--dataset``). If the
34data sources include online catalogs, the local information is updated for the
35given time span. If no time span is specified, the latest events available are
36queried and shown. The available information can be filtered by time, magnitude
37and event depth.
39Examples: ```squirrel catalog -d :events-gcmt-m6``` -- show latest events in
40the Global-CMT catalog. ```squirrel catalog -d :``` -- shortcut to see list of
41preconfigured datasets. ```squirrel catalog --add events.yaml --tmin 2024
42--tmax 2024-02``` -- show all events defined in the file ```events.yaml```
43which occurred in January 2024.
44''')
47def setup(parser):
49 parser.add_squirrel_selection_arguments()
50 parser.add_squirrel_query_arguments(without=['time', 'codes', 'kind'])
51 EventFilter.setup_argparse(parser)
53 style_choices = ['table', 'summary', 'yaml']
55 parser.add_argument(
56 '--style',
57 dest='style',
58 choices=style_choices,
59 default='table',
60 help='Set style of presentation. Choices: %s' % ldq(style_choices))
63def run(parser, args):
64 sq = args.make_squirrel()
65 efilter = EventFilter.from_argparse(args).get_filter()
67 if 'tmin' in args.squirrel_query and 'tmax' in args.squirrel_query:
68 tmin = args.squirrel_query['tmin']
69 tmax = args.squirrel_query['tmax']
70 sq.update(tmin=tmin, tmax=tmax, inventory='event')
72 else:
73 logger.info('No time range specified, showing latest events.')
75 tmax = util.hour_start(time.time())
76 tduration = 3600.*24.
78 while True:
79 tmin = tmax - tduration
80 sq.update(tmin=tmin, tmax=tmax, inventory='event')
81 events = list(filter(
82 efilter, sq.get_events(tmin=tmin, tmax=tmax)))
84 if len(events) > 10 or tmin < util.str_to_time(
85 '1900-01-01 00:00:00'):
86 break
88 tduration *= 2
90 events = list(filter(efilter, sq.get_events(tmin=tmin, tmax=tmax)))
91 if not events:
92 logger.info('No events found in the time range %s - %s.' % (
93 util.time_to_str(tmin),
94 util.time_to_str(tmax)))
96 return
98 logger.info('Showing events for the time range %s - %s.' % (
99 util.time_to_str(tmin),
100 util.time_to_str(tmax)))
102 for ev in events:
103 if args.style == 'yaml':
104 print('# %s' % ev.summary)
105 print(ev.dump())
106 elif args.style == 'summary':
107 print(ev.summary)
108 elif args.style == 'table':
109 print('%-20s %-30s %3.1f %3.0f %s' % (
110 ev.name,
111 util.time_to_str(ev.time),
112 ev.magnitude,
113 ev.depth / km,
114 ev.region))
115 else:
116 assert False