1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import time
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 now = time.time()
40 if tmax_g is None or tmax_g > now:
41 tmax_g = now
43 kwargs = args.squirrel_query
44 kinds = kwargs.pop('kind', sq.supported_content_kinds())
45 codes = kwargs.pop('codes', None)
46 tmin = kwargs.pop('tmin', tmin_g)
47 tmax = kwargs.pop('tmax', tmax_g)
48 if tmin is not None and tmax is not None:
49 if not tmin < tmax:
50 raise ToolError(
51 'Invalid time span: %s - %s' % (
52 util.time_to_str(tmin), util.time_to_str(tmax)))
54 for kind in kinds:
55 coverage = squirrel.get_coverage(
56 kind,
57 codes=codes if (codes is not None) else None,
58 tmin=tmin,
59 tmax=tmax,
60 **kwargs)
62 if coverage:
63 slabels = [entry.labels for entry in coverage]
65 scs = [max(len(s) for s in entries) for entries in zip(*slabels)]
67 label = 'kind: %s' % kind
68 sc = max(len(label), sum(scs)) + 1
69 si = (sx-sc) - 2
70 sl = si // 2
71 sr = si - sl
72 print(''.join((
73 label.ljust(sc),
74 terminal.ansi_dim,
75 terminal.bar_right,
76 util.time_to_str(tmin).ljust(sl),
77 util.time_to_str(tmax).rjust(sr),
78 terminal.bar_left,
79 terminal.ansi_dim_reset)))
81 for (scodes, srate), entry in zip(slabels, coverage):
82 line = \
83 (scodes.ljust(scs[0])
84 + ' ' + srate.rjust(scs[1])).ljust(sc) \
85 + terminal.bar(
86 tmin, tmax, entry.changes,
87 entry.tmin, entry.tmax,
88 sx-sc)
90 print(line)
92 for line in terminal.time_axis(tmin, tmax, sx-sc):
93 print(''.join((
94 ' '*sc,
95 terminal.ansi_dim,
96 line,
97 terminal.ansi_dim_reset)))