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