1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

from __future__ import absolute_import, division 

 

import numpy as num 

import logging 

import os 

import math 

import signal 

 

from pyrocko import trace, cake, gf 

from pyrocko.ahfullgreen import add_seismogram, Impulse 

from pyrocko.moment_tensor import MomentTensor, symmat6 

 

km = 1000. 

 

guts_prefix = 'pf' 

 

logger = logging.getLogger('pyrocko.fomosto.ahfullgreen') 

 

# how to call the programs 

program_bins = { 

'ahfullgreen': 'ahfullgreen', 

} 

 

components = 'r t z'.split() 

 

 

def example_model(): 

material = cake.Material(vp=3000., vs=1000., rho=3000., qp=200., qs=100.) 

layer = cake.HomogeneousLayer( 

ztop=0., zbot=30*km, m=material, name='fullspace') 

mod = cake.LayeredModel() 

mod.append(layer) 

return mod 

 

 

class AhfullgreenError(gf.store.StoreError): 

pass 

 

 

def make_traces(material, source_mech, deltat, norths, easts, 

source_depth, receiver_depth): 

 

if isinstance(source_mech, MomentTensor): 

m6 = source_mech.m6() 

f = (0., 0., 0.) 

elif isinstance(source_mech, tuple): 

m6 = (0., 0., 0., 0., 0., 0.) 

f = source_mech 

 

npad = 120 

 

traces = [] 

for i_distance, (north, east) in enumerate(zip(norths, easts)): 

d3d = math.sqrt( 

north**2 + east**2 + (receiver_depth - source_depth)**2) 

 

tmin = (math.floor(d3d / material.vp / deltat) - npad) * deltat 

tmax = (math.ceil(d3d / material.vs / deltat) + npad) * deltat 

ns = int(round((tmax - tmin) / deltat)) 

 

outx = num.zeros(ns) 

outy = num.zeros(ns) 

outz = num.zeros(ns) 

 

x = (north, east, receiver_depth-source_depth) 

 

add_seismogram( 

material.vp, material.vs, material.rho, material.qp, material.qs, 

x, f, m6, 'displacement', 

deltat, tmin, outx, outy, outz, 

stf=Impulse()) 

 

for i_comp, o in enumerate((outx, outy, outz)): 

comp = components[i_comp] 

tr = trace.Trace('', '%04i' % i_distance, '', comp, 

tmin=tmin, ydata=o, deltat=deltat, 

meta=dict( 

distance=math.sqrt(north**2 + east**2), 

azimuth=0.)) 

 

traces.append(tr) 

 

return traces 

 

 

class AhfullGFBuilder(gf.builder.Builder): 

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

 

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

 

if block_size is None: 

block_size = (1, 1, 2000) 

 

if len(self.store.config.ns) == 2: 

block_size = block_size[1:] 

 

gf.builder.Builder.__init__( 

self, self.store.config, step, block_size=block_size, force=force) 

 

def cleanup(self): 

self.store.close() 

 

def work_block(self, index): 

if len(self.store.config.ns) == 2: 

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

self.get_block_extents(index) 

 

rz = self.store.config.receiver_depth 

else: 

(rz, sz, firstx), (rz, sz, lastx), (nr, ns, nx) = \ 

self.get_block_extents(index) 

 

logger.info('Starting block %i / %i' % 

(index+1, self.nblocks)) 

 

dx = self.gf_config.distance_delta 

 

distances = num.linspace(firstx, firstx + (nx-1)*dx, nx).tolist() 

 

mmt1 = (MomentTensor(m=symmat6(1, 0, 0, 1, 0, 0)), 

{'r': (0, +1), 't': (3, +1), 'z': (5, +1)}) 

mmt2 = (MomentTensor(m=symmat6(0, 0, 0, 0, 1, 1)), 

{'r': (1, +1), 't': (4, +1), 'z': (6, +1)}) 

mmt3 = (MomentTensor(m=symmat6(0, 0, 1, 0, 0, 0)), 

{'r': (2, +1), 'z': (7, +1)}) 

mmt4 = (MomentTensor(m=symmat6(0, 1, 0, 0, 0, 0)), 

{'r': (8, +1), 'z': (9, +1)}) 

mmt0 = (MomentTensor(m=symmat6(1, 1, 1, 0, 0, 0)), 

{'r': (0, +1), 'z': (1, +1)}) 

 

component_scheme = self.store.config.component_scheme 

 

if component_scheme == 'elastic8': 

gfmapping = [mmt1, mmt2, mmt3] 

 

elif component_scheme == 'elastic10': 

gfmapping = [mmt1, mmt2, mmt3, mmt4] 

 

elif component_scheme == 'elastic2': 

gfmapping = [mmt0] 

 

elif component_scheme == 'elastic5': 

gfmapping = [ 

((1., 1., 0.), {'r': (1, +1), 'z': (4, +1), 't': (2, +1)}), 

((0., 0., 1.), {'r': (0, +1), 'z': (3, +1)})] 

 

else: 

raise gf.UnavailableScheme( 

'fomosto backend "ahfullgreen" cannot handle component scheme ' 

'"%s"' % component_scheme) 

 

for source_mech, gfmap in gfmapping: 

 

rawtraces = make_traces( 

self.store.config.earthmodel_1d.require_homogeneous(), 

source_mech, 1.0/self.store.config.sample_rate, 

distances, num.zeros_like(distances), sz, rz) 

 

interrupted = [] 

 

def signal_handler(signum, frame): 

interrupted.append(True) 

 

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

self.store.lock() 

duplicate_inserts = 0 

try: 

for itr, tr in enumerate(rawtraces): 

if tr.channel not in gfmap: 

logger.debug('%s not in gfmap' % tr.channel) 

continue 

 

x = tr.meta['distance'] 

if x > firstx + (nx-1)*dx: 

logger.error("x out of range") 

continue 

 

ig, factor = gfmap[tr.channel] 

 

if len(self.store.config.ns) == 2: 

args = (sz, x, ig) 

else: 

args = (rz, sz, x, ig) 

 

tr = tr.snap() 

 

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

gf_tr.data *= factor 

 

try: 

self.store.put(args, gf_tr) 

except gf.store.DuplicateInsert: 

duplicate_inserts += 1 

 

finally: 

if duplicate_inserts: 

logger.warn('%i insertions skipped (duplicates)' % 

duplicate_inserts) 

 

self.store.unlock() 

signal.signal(signal.SIGINT, original) 

 

if interrupted: 

raise KeyboardInterrupt() 

 

logger.info('Done with block %i / %i' % 

(index+1, self.nblocks)) 

 

 

def init(store_dir, variant): 

assert variant is None 

 

modelling_code_id = 'ahfullgreen' 

 

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

 

config = gf.meta.ConfigTypeA( 

id=store_id, 

ncomponents=10, 

sample_rate=20., 

receiver_depth=0*km, 

source_depth_min=1*km, 

source_depth_max=10*km, 

source_depth_delta=1*km, 

distance_min=1*km, 

distance_max=20*km, 

distance_delta=1*km, 

earthmodel_1d=example_model(), 

modelling_code_id=modelling_code_id, 

tabulated_phases=[ 

gf.meta.TPDef( 

id='anyP', 

definition='P,p,\\P,\\p'), 

gf.meta.TPDef( 

id='anyS', 

definition='S,s,\\S,\\s')]) 

 

config.validate() 

return gf.store.Store.create_editables( 

store_dir, config=config) 

 

 

def build(store_dir, force=False, nworkers=None, continue_=False, step=None, 

iblock=None): 

 

return AhfullGFBuilder.build( 

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

step=step, iblock=iblock)