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

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

from __future__ import print_function 

 

import sys 

import logging 

import os.path as op 

from optparse import OptionParser 

 

from pyrocko import util, scenario, guts, gf 

from pyrocko import __version__ 

 

 

logger = logging.getLogger('pyrocko.apps.colosseo') 

km = 1000. 

 

 

def d2u(d): 

return dict((k.replace('-', '_'), v) for (k, v) in d.items()) 

 

 

description = '''This is Colosseo, an earthquake scenario generator. 

 

Create seismic waveforms, InSAR and GNSS offsets for a simulated earthquake 

scenario. 

 

Colosseo is part of Pyrocko. Version %s. 

''' % __version__ 

 

subcommand_descriptions = { 

'init': 'initialize a new, blank scenario', 

'fill': 'fill the scenario with modelled data', 

'snuffle': 'open Snuffler to inspect the waveform data', 

'map': 'map the scenario arena' 

} 

 

subcommand_usages = { 

'init': 'init <scenario_dir>', 

'fill': 'fill <scenario_dir>', 

'snuffle': 'snuffle <scenario_dir>', 

'map': '<scenario_dir>', 

} 

 

subcommands = subcommand_descriptions.keys() 

 

program_name = 'colosseo' 

 

usage_tdata = d2u(subcommand_descriptions) 

usage_tdata['program_name'] = program_name 

usage_tdata['description'] = description 

 

usage = '''%(program_name)s <subcommand> [options] [--] <arguments> ... 

 

%(description)s 

 

Subcommands: 

 

init %(init)s 

fill %(fill)s 

snuffle %(snuffle)s 

map %(map)s 

 

To get further help and a list of available options for any subcommand run: 

 

%(program_name)s <subcommand> --help 

 

''' % usage_tdata 

 

 

def die(message, err='', prelude=''): 

if prelude: 

prelude = prelude + '\n' 

 

if err: 

err = '\n' + err 

 

sys.exit('%s%s failed: %s%s' % (prelude, program_name, message, err)) 

 

 

def none_or_float(x): 

if x == 'none': 

return None 

else: 

return float(x) 

 

 

def add_common_options(parser): 

parser.add_option( 

'--loglevel', 

action='store', 

dest='loglevel', 

type='choice', 

choices=('critical', 'error', 'warning', 'info', 'debug'), 

default='info', 

help='set logger level to ' 

'"critical", "error", "warning", "info", or "debug". ' 

'Default is "%default".') 

 

 

def process_common_options(options): 

util.setup_logging(program_name, options.loglevel) 

 

 

def cl_parse(command, args, setup=None, details=None): 

usage = subcommand_usages[command] 

descr = subcommand_descriptions[command] 

 

if isinstance(usage, str): 

usage = [usage] 

 

susage = '%s %s' % (program_name, usage[0]) 

for s in usage[1:]: 

susage += '\n%s%s %s' % (' '*7, program_name, s) 

 

description = descr[0].upper() + descr[1:] + '.' 

 

if details: 

description = description + ' %s' % details 

 

parser = OptionParser(usage=susage, description=description) 

 

if setup: 

setup(parser) 

 

add_common_options(parser) 

(options, args) = parser.parse_args(args) 

process_common_options(options) 

return parser, options, args 

 

 

def get_scenario_yml(path): 

fn = op.join(path, 'scenario.yml') 

if op.exists(fn): 

return fn 

return False 

 

 

def command_init(args): 

 

def setup(parser): 

parser.add_option( 

'--force', dest='force', action='store_true', 

help='overwrite existing files') 

parser.add_option( 

'--location', dest='location', metavar='LAT,LON', 

help='set scenario center location [deg]') 

parser.add_option( 

'--radius', dest='radius', metavar='RADIUS', type=float, 

help='set scenario center location [km]') 

 

parser, options, args = cl_parse('init', args, setup=setup) 

 

if len(args) != 1: 

parser.print_help() 

sys.exit(1) 

 

if options.location: 

try: 

lat, lon = map(float, options.location.split(',')) 

except Exception: 

die('expected --location=LAT,LON') 

else: 

lat = lon = None 

 

if options.radius is not None: 

radius = options.radius * km 

else: 

radius = None 

 

project_dir = args[0] 

try: 

scenario.ScenarioGenerator.initialize( 

project_dir, lat, lon, radius, force=options.force) 

 

gf_stores_path = op.join(project_dir, 'gf_stores') 

util.ensuredir(gf_stores_path) 

 

except scenario.CannotCreatePath as e: 

die(str(e) + ' Use --force to override.') 

 

except scenario.ScenarioError as e: 

die(str(e)) 

 

 

def command_fill(args): 

 

def setup(parser): 

parser.add_option( 

'--force', dest='force', action='store_true', 

help='overwrite existing files') 

 

parser, options, args = cl_parse('fill', args, setup=setup) 

 

if len(args) == 0: 

args.append('.') 

 

fn = get_scenario_yml(args[0]) 

 

if not fn: 

parser.print_help() 

sys.exit(1) 

 

project_dir = args[0] 

 

gf_stores_path = op.join(project_dir, 'gf_stores') 

 

try: 

engine = get_engine([gf_stores_path]) 

 

sc = guts.load(filename=fn) 

sc.init_modelling(engine) 

sc.ensure_gfstores(interactive=True) 

sc.prepare_data(path=project_dir, overwrite=options.force) 

sc.ensure_data(path=project_dir) 

sc.make_map(op.join(project_dir, 'map.pdf')) 

 

except scenario.CannotCreatePath as e: 

die(str(e) + ' Use --force to override.') 

 

except scenario.ScenarioError as e: 

die(str(e)) 

 

 

def command_map(args): 

parser, options, args = cl_parse('map', args) 

 

if len(args) == 0: 

args.append('.') 

 

fn = get_scenario_yml(args[0]) 

 

if not fn: 

parser.print_help() 

sys.exit(1) 

 

project_dir = args[0] 

 

gf_stores_path = op.join(project_dir, 'gf_stores') 

engine = get_engine([gf_stores_path]) 

 

try: 

sc = guts.load(filename=fn) 

sc.init_modelling(engine) 

sc.make_map(op.join(project_dir, 'map.pdf')) 

 

except scenario.ScenarioError as e: 

die(str(e)) 

 

 

def command_snuffle(args): 

from pyrocko.gui import snuffler 

parser, options, args = cl_parse('map', args) 

 

if len(args) == 0: 

args.append('.') 

 

fn = get_scenario_yml(args[0]) 

 

if not fn: 

parser.print_help() 

sys.exit(1) 

 

project_dir = args[0] 

gf_stores_path = op.join(project_dir, 'gf_stores') 

 

engine = get_engine([gf_stores_path]) 

sc = guts.load(filename=fn) 

sc.init_modelling(engine) 

 

return snuffler.snuffle( 

sc.get_pile(), 

stations=sc.get_stations(), 

events=sc.get_events()) 

 

 

def main(args=None): 

if args is None: 

args = sys.argv[1:] 

 

if len(args) < 1: 

sys.exit('Usage: %s' % usage) 

 

command = args.pop(0) 

 

if command in subcommands: 

globals()['command_' + command](args) 

 

elif command in ('--help', '-h', 'help'): 

if command == 'help' and args: 

acommand = args[0] 

if acommand in subcommands: 

globals()['command_' + acommand](['--help']) 

 

sys.exit('Usage: %s' % usage) 

 

else: 

sys.exit('%s: error: no such subcommand: %s' % (program_name, command)) 

 

 

def get_engine(gf_store_superdirs): 

engine = gf.LocalEngine( 

store_superdirs=gf_store_superdirs, use_config=True) 

 

logger.info( 

'Directories to be searched for GF stores:\n%s' 

% '\n'.join(' ' + s for s in engine.store_superdirs)) 

 

return engine 

 

 

if __name__ == '__main__': 

main()