1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6from __future__ import absolute_import, print_function
8from .. import common
9from pyrocko import util
10from pyrocko.plot import terminal
11from pyrocko.get_terminal_size import get_terminal_size
12from pyrocko.squirrel.error import ToolError
15def setup_subcommand(subparsers):
16 return common.add_parser(
17 subparsers, 'coverage',
18 help='Report time spans covered.',
19 description='''Report time spans covered.
21Time spans covered by the given data selection are listed or plotted.
22''')
25def setup(parser):
26 common.add_selection_arguments(parser)
27 common.add_query_arguments(parser, without=['time'])
30def call(parser, args):
31 from pyrocko import squirrel as sq
33 squirrel = common.squirrel_from_selection_arguments(args)
34 tmin_g, tmax_g = squirrel.get_time_span()
35 sx, _ = get_terminal_size()
37 kwargs = common.squirrel_query_from_arguments(args)
38 kinds = kwargs.pop('kind', sq.supported_content_kinds())
39 codes = kwargs.pop('codes', None)
40 tmin = kwargs.pop('tmin', tmin_g)
41 tmax = kwargs.pop('tmax', tmax_g)
42 if tmin is not None and tmax is not None:
43 if not tmin < tmax:
44 raise ToolError(
45 'Invalid time span: %s - %s' % (
46 util.time_to_str(tmin), util.time_to_str(tmax)))
48 for kind in kinds:
49 coverage = squirrel.get_coverage(
50 kind,
51 codes_list=[codes] if codes else None,
52 tmin=tmin,
53 tmax=tmax,
54 **kwargs)
56 if coverage:
57 slabels = [entry.labels for entry in coverage]
59 scs = [max(len(s) for s in entries) for entries in zip(*slabels)]
61 label = 'kind: %s' % kind
62 sc = max(len(label), sum(scs)) + 1
63 si = (sx-sc) - 2
64 sl = si // 2
65 sr = si - sl
66 print(''.join((
67 label.ljust(sc),
68 terminal.ansi_dim,
69 terminal.bar_right,
70 util.time_to_str(tmin).ljust(sl),
71 util.time_to_str(tmax).rjust(sr),
72 terminal.bar_left,
73 terminal.ansi_dim_reset)))
75 for (scodes, srate), entry in zip(slabels, coverage):
76 line = \
77 (scodes.ljust(scs[0])
78 + ' ' + srate.rjust(scs[1])).ljust(sc) \
79 + terminal.bar(
80 tmin, tmax, entry.changes,
81 entry.tmin, entry.tmax,
82 sx-sc)
84 print(line)
86 for line in terminal.time_axis(tmin, tmax, sx-sc):
87 print(''.join((
88 ' '*sc,
89 terminal.ansi_dim,
90 line,
91 terminal.ansi_dim_reset)))