1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6from __future__ import absolute_import, print_function 

7 

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 

13 

14 

15def setup(subparsers): 

16 p = common.add_parser( 

17 subparsers, 'coverage', 

18 help='Report time spans covered.', 

19 description='''Report time spans covered. 

20 

21Time spans covered by the given data selection are listed or plotted. 

22''') 

23 

24 common.add_selection_arguments(p) 

25 common.add_query_arguments(p, without=['time']) 

26 return p 

27 

28 

29def call(parser, args): 

30 from pyrocko import squirrel as sq 

31 

32 squirrel = common.squirrel_from_selection_arguments(args) 

33 tmin_g, tmax_g = squirrel.get_time_span() 

34 sx, _ = get_terminal_size() 

35 

36 kwargs = common.squirrel_query_from_arguments(args) 

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))) 

46 

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 return_raw=False, **kwargs) 

54 

55 if coverage: 

56 slabels = [entry.labels for entry in coverage] 

57 

58 scs = [max(len(s) for s in entries) for entries in zip(*slabels)] 

59 

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))) 

73 

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) 

82 

83 print(line) 

84 

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)))