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 

8import math 

9import logging 

10 

11from .. import common 

12from pyrocko.squirrel.error import SquirrelError 

13from pyrocko.progress import progress 

14 

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

16 

17 

18def setup_subcommand(subparsers): 

19 return common.add_parser( 

20 subparsers, 'summon', 

21 help='Fill local cache.') 

22 

23 

24def setup(parser): 

25 common.add_selection_arguments(parser) 

26 common.add_query_arguments(parser) 

27 

28 

29def call(parser, args): 

30 d = common.squirrel_query_from_arguments(args) 

31 squirrel = common.squirrel_from_selection_arguments(args) 

32 

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

34 raise SquirrelError('Time span required.') 

35 

36 tinc = 3600. 

37 

38 with progress.view(): 

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

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

41 iwindow = 0 

42 for trs in squirrel.chopper_waveforms( 

43 tinc=tinc, load_data=False, **d): 

44 

45 iwindow += 1 

46 task.update(iwindow) 

47 

48 task.done() 

49 

50 stats = str(squirrel) 

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

52 

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