1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import math 

7import logging 

8 

9from pyrocko.squirrel.error import SquirrelError 

10from pyrocko.progress import progress 

11 

12logger = logging.getLogger('psq.cli.summon') 

13 

14headline = 'Fill local cache.' 

15 

16 

17def make_subparser(subparsers): 

18 return subparsers.add_parser( 

19 'summon', 

20 help=headline, 

21 description=headline) 

22 

23 

24def setup(parser): 

25 parser.add_squirrel_selection_arguments() 

26 parser.add_squirrel_query_arguments() 

27 parser.add_argument( 

28 '--channel-priorities', 

29 dest='channel_priorities', 

30 metavar='CHA', 

31 help=''' 

32List of 2-character band/instrument code combinations to try. For example, 

33giving ```HH,BH``` would first try to get ```HH?``` channels and then fallback 

34to ```BH?``` if these are not available. The first matching waveforms are 

35returned. Use in combination with ``--sample-rate-min`` and 

36``--sample-rate-max`` to constrain the sample rate. 

37'''.strip()) 

38 

39 parser.add_argument( 

40 '--sample-rate-min', 

41 dest='sample_rate_min', 

42 metavar='FLOAT', 

43 type=float, 

44 help='Minimum sample rate [Hz] to consider.') 

45 

46 parser.add_argument( 

47 '--sample-rate-max', 

48 dest='sample_rate_max', 

49 metavar='FLOAT', 

50 type=float, 

51 help='Maximum sample rate [Hz] to consider.') 

52 

53 

54def run(parser, args): 

55 d = args.squirrel_query 

56 squirrel = args.make_squirrel() 

57 

58 if 'tmin' not in d or 'tmax' not in d: 

59 raise SquirrelError('Time span required.') 

60 

61 tinc = 3600. 

62 

63 channel_priorities = None 

64 if args.channel_priorities: 

65 channel_priorities = [ 

66 cha.strip() for cha in args.channel_priorities.split(',')] 

67 

68 with progress.view(): 

69 nwindows = int(math.ceil((d['tmax'] - d['tmin']) / tinc)) 

70 task = progress.task('Summoning', nwindows) 

71 iwindow = 0 

72 for trs in squirrel.chopper_waveforms( 

73 tinc=tinc, 

74 load_data=False, 

75 channel_priorities=channel_priorities, 

76 sample_rate_min=args.sample_rate_min, 

77 sample_rate_max=args.sample_rate_max, 

78 **d): 

79 

80 iwindow += 1 

81 task.update(iwindow) 

82 

83 task.done() 

84 

85 stats = str(squirrel) 

86 stats = '\n'.join(' ' + s for s in stats.splitlines()) 

87 

88 logger.info('Squirrel stats:\n%s' % stats)