1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5from __future__ import absolute_import, division 

6 

7import logging 

8import os 

9import signal 

10 

11import numpy as num 

12 

13from pyrocko import trace, gf 

14 

15guts_prefix = 'pf' 

16 

17logger = logging.getLogger('pyrocko.fomosto.dummy') 

18 

19 

20class Interrupted(gf.store.StoreError): 

21 def __str__(self): 

22 return 'Interrupted.' 

23 

24 

25class DummyGFBuilder(gf.builder.Builder): 

26 def __init__(self, store_dir, step, shared, force=False): 

27 self.store = gf.store.Store(store_dir, 'w') 

28 gf.builder.Builder.__init__( 

29 self, self.store.config, step, block_size=(1, 51), force=force) 

30 

31 def cleanup(self): 

32 self.store.close() 

33 

34 def work_block(self, index): 

35 (sz, firstx), (sz, lastx), (ns, nx) = \ 

36 self.get_block_extents(index) 

37 

38 logger.info( 

39 'Starting block %i / %i' % (index+1, self.nblocks)) 

40 

41 interrupted = [] 

42 

43 def signal_handler(signum, frame): 

44 interrupted.append(True) 

45 

46 original = signal.signal(signal.SIGINT, signal_handler) 

47 self.store.lock() 

48 duplicate_inserts = 0 

49 try: 

50 for x in num.linspace(firstx, lastx, nx): 

51 for ig in range(self.store.config.ncomponents): 

52 args = (sz, x, ig) 

53 irec = self.store.config.irecord(*args) 

54 tr = trace.Trace( 

55 deltat=self.store.config.deltat, 

56 ydata=num.zeros(10000)+float(irec)) 

57 

58 gf_tr = gf.store.GFTrace.from_trace(tr) 

59 

60 try: 

61 self.store.put(args, gf_tr) 

62 except gf.store.DuplicateInsert: 

63 duplicate_inserts += 1 

64 

65 finally: 

66 if duplicate_inserts: 

67 logger.warn( 

68 '%i insertions skipped (duplicates)' % duplicate_inserts) 

69 

70 self.store.unlock() 

71 signal.signal(signal.SIGINT, original) 

72 

73 if interrupted: 

74 raise KeyboardInterrupt() 

75 

76 logger.info('Done with block %i / %i' % (index+1, self.nblocks)) 

77 

78 

79km = 1000. 

80 

81 

82def init(store_dir, variant): 

83 if variant is not None: 

84 raise gf.store.StoreError('unsupported variant: %s' % variant) 

85 

86 store_id = os.path.basename(os.path.realpath(store_dir)) 

87 

88 config = gf.meta.ConfigTypeA( 

89 id=store_id, 

90 ncomponents=2, 

91 sample_rate=1.0, 

92 receiver_depth=0*km, 

93 source_depth_min=0*km, 

94 source_depth_max=400*km, 

95 source_depth_delta=4*km, 

96 distance_min=4*km, 

97 distance_max=400*km, 

98 distance_delta=4*km, 

99 modelling_code_id='dummy') 

100 

101 config.validate() 

102 return gf.store.Store.create_editables(store_dir, config=config) 

103 

104 

105def build( 

106 store_dir, 

107 force=False, 

108 nworkers=None, 

109 continue_=False, 

110 step=None, 

111 iblock=None): 

112 

113 return DummyGFBuilder.build( 

114 store_dir, force=force, nworkers=nworkers, continue_=continue_, 

115 step=step, iblock=iblock)