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
14headline = 'Report time spans covered.'
17def make_subparser(subparsers):
18 return subparsers.add_parser(
19 'coverage',
20 help=headline,
21 description=headline + '''
23Time spans covered by the given data selection are listed or plotted.
24''')
27def setup(parser):
28 parser.add_squirrel_selection_arguments()
29 parser.add_squirrel_query_arguments(without=['time'])
32def run(parser, args):
33 from pyrocko import squirrel as sq
35 squirrel = args.make_squirrel()
36 tmin_g, tmax_g = squirrel.get_time_span()
37 sx, _ = get_terminal_size()
39 kwargs = args.squirrel_query
40 kinds = kwargs.pop('kind', sq.supported_content_kinds())
41 codes = kwargs.pop('codes', None)
42 tmin = kwargs.pop('tmin', tmin_g)
43 tmax = kwargs.pop('tmax', tmax_g)
44 if tmin is not None and tmax is not None:
45 if not tmin < tmax:
46 raise ToolError(
47 'Invalid time span: %s - %s' % (
48 util.time_to_str(tmin), util.time_to_str(tmax)))
50 for kind in kinds:
51 coverage = squirrel.get_coverage(
52 kind,
53 codes=codes if (codes is not None) else None,
54 tmin=tmin,
55 tmax=tmax,
56 **kwargs)
58 if coverage:
59 slabels = [entry.labels for entry in coverage]
61 scs = [max(len(s) for s in entries) for entries in zip(*slabels)]
63 label = 'kind: %s' % kind
64 sc = max(len(label), sum(scs)) + 1
65 si = (sx-sc) - 2
66 sl = si // 2
67 sr = si - sl
68 print(''.join((
69 label.ljust(sc),
70 terminal.ansi_dim,
71 terminal.bar_right,
72 util.time_to_str(tmin).ljust(sl),
73 util.time_to_str(tmax).rjust(sr),
74 terminal.bar_left,
75 terminal.ansi_dim_reset)))
77 for (scodes, srate), entry in zip(slabels, coverage):
78 line = \
79 (scodes.ljust(scs[0])
80 + ' ' + srate.rjust(scs[1])).ljust(sc) \
81 + terminal.bar(
82 tmin, tmax, entry.changes,
83 entry.tmin, entry.tmax,
84 sx-sc)
86 print(line)
88 for line in terminal.time_axis(tmin, tmax, sx-sc):
89 print(''.join((
90 ' '*sc,
91 terminal.ansi_dim,
92 line,
93 terminal.ansi_dim_reset)))