1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import math
7import logging
9from pyrocko.squirrel.error import SquirrelError
10from pyrocko.progress import progress
12logger = logging.getLogger('psq.cli.summon')
14headline = 'Fill local cache.'
17def make_subparser(subparsers):
18 return subparsers.add_parser(
19 'summon',
20 help=headline,
21 description=headline)
24def setup(parser):
25 parser.add_squirrel_selection_arguments()
26 parser.add_squirrel_query_arguments()
27 parser.add_argument(
28 '--channel-priorities',
29 dest='channel_priorities',
30 metavar='CHA',
31 help='''
32List of 2-character band/instrument code combinations to try. For example,
33giving ```HH,BH``` would first try to get ```HH?``` channels and then fallback
34to ```BH?``` if these are not available. The first matching waveforms are
35returned. Use in combination with ``--sample-rate-min`` and
36``--sample-rate-max`` to constrain the sample rate.
37'''.strip())
39 parser.add_argument(
40 '--sample-rate-min',
41 dest='sample_rate_min',
42 metavar='FLOAT',
43 type=float,
44 help='Minimum sample rate [Hz] to consider.')
46 parser.add_argument(
47 '--sample-rate-max',
48 dest='sample_rate_max',
49 metavar='FLOAT',
50 type=float,
51 help='Maximum sample rate [Hz] to consider.')
54def run(parser, args):
55 d = args.squirrel_query
56 squirrel = args.make_squirrel()
58 if 'tmin' not in d or 'tmax' not in d:
59 raise SquirrelError('Time span required.')
61 tinc = 3600.
63 channel_priorities = None
64 if args.channel_priorities:
65 channel_priorities = [
66 cha.strip() for cha in args.channel_priorities.split(',')]
68 with progress.view():
69 nwindows = int(math.ceil((d['tmax'] - d['tmin']) / tinc))
70 task = progress.task('Summoning', nwindows)
71 iwindow = 0
72 for trs in squirrel.chopper_waveforms(
73 tinc=tinc,
74 load_data=False,
75 channel_priorities=channel_priorities,
76 sample_rate_min=args.sample_rate_min,
77 sample_rate_max=args.sample_rate_max,
78 **d):
80 iwindow += 1
81 task.update(iwindow)
83 task.done()
85 stats = str(squirrel)
86 stats = '\n'.join(' ' + s for s in stats.splitlines())
88 logger.info('Squirrel stats:\n%s' % stats)