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