1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import time 

7 

8from pyrocko import util 

9from pyrocko.plot import terminal 

10from pyrocko.get_terminal_size import get_terminal_size 

11from pyrocko.squirrel.error import ToolError 

12 

13from ..common import ldq 

14 

15 

16headline = 'Report time spans covered.' 

17 

18 

19def make_subparser(subparsers): 

20 return subparsers.add_parser( 

21 'coverage', 

22 help=headline, 

23 description=headline + ''' 

24 

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

26''') 

27 

28 

29def setup(parser): 

30 parser.add_squirrel_selection_arguments() 

31 parser.add_squirrel_query_arguments(without=['time']) 

32 

33 style_choices = ['visual', 'summary', 'yaml'] 

34 

35 parser.add_argument( 

36 '--style', 

37 dest='style', 

38 choices=style_choices, 

39 default='visual', 

40 help='Set style of presentation. Choices: %s' % ldq(style_choices)) 

41 

42 

43def run(parser, args): 

44 from pyrocko import squirrel as sq 

45 

46 squirrel = args.make_squirrel() 

47 tmin_g, tmax_g = squirrel.get_time_span() 

48 sx, _ = get_terminal_size() 

49 

50 now = time.time() 

51 if tmax_g is None or tmax_g > now: 

52 tmax_g = now 

53 

54 kwargs = args.squirrel_query 

55 kinds = kwargs.pop('kind', sq.supported_content_kinds()) 

56 codes = kwargs.pop('codes', None) 

57 tmin = kwargs.pop('tmin', tmin_g) 

58 tmax = kwargs.pop('tmax', tmax_g) 

59 if tmin is not None and tmax is not None: 

60 if not tmin < tmax: 

61 raise ToolError( 

62 'Invalid time span: %s - %s' % ( 

63 util.time_to_str(tmin), util.time_to_str(tmax))) 

64 

65 for kind in kinds: 

66 coverage = squirrel.get_coverage( 

67 kind, 

68 codes=codes if (codes is not None) else None, 

69 tmin=tmin, 

70 tmax=tmax, 

71 **kwargs) 

72 

73 if coverage: 

74 

75 if args.style == 'yaml': 

76 for entry in coverage: 

77 print(entry) 

78 elif args.style == 'summary': 

79 for entry in coverage: 

80 print(entry.summary) 

81 elif args.style == 'visual': 

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

83 

84 scs = [ 

85 max(len(s) for s in entries) 

86 for entries in zip(*slabels)] 

87 

88 label = 'kind: %s' % kind 

89 sc = max(len(label), sum(scs)) + 1 

90 si = (sx-sc) - 2 

91 sl = si // 2 

92 sr = si - sl 

93 print(''.join(( 

94 label.ljust(sc), 

95 terminal.ansi_dim, 

96 terminal.bar_right, 

97 util.time_to_str(tmin).ljust(sl), 

98 util.time_to_str(tmax).rjust(sr), 

99 terminal.bar_left, 

100 terminal.ansi_dim_reset))) 

101 

102 for (scodes, srate), entry in zip(slabels, coverage): 

103 line = \ 

104 (scodes.ljust(scs[0]) 

105 + ' ' + srate.rjust(scs[1])).ljust(sc) \ 

106 + terminal.bar( 

107 tmin, tmax, entry.changes, 

108 entry.tmin, entry.tmax, 

109 sx-sc) 

110 

111 print(line) 

112 

113 for line in terminal.time_axis(tmin, tmax, sx-sc): 

114 print(''.join(( 

115 ' '*sc, 

116 terminal.ansi_dim, 

117 line, 

118 terminal.ansi_dim_reset)))