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='TODO') 

32 

33 

34def run(parser, args): 

35 d = args.squirrel_query 

36 squirrel = args.make_squirrel() 

37 

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

39 raise SquirrelError('Time span required.') 

40 

41 tinc = 3600. 

42 

43 channel_priorities = None 

44 if args.channel_priorities: 

45 channel_priorities = [ 

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

47 

48 with progress.view(): 

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

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

51 iwindow = 0 

52 for trs in squirrel.chopper_waveforms( 

53 tinc=tinc, 

54 load_data=False, 

55 channel_priorities=channel_priorities, 

56 **d): 

57 

58 iwindow += 1 

59 task.update(iwindow) 

60 

61 task.done() 

62 

63 stats = str(squirrel) 

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

65 

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