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

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

from __future__ import print_function 

import logging 

import os.path as op 

import shutil 

import os 

import tarfile 

import threading 

import signal 

import time 

 

from http.server import HTTPServer, SimpleHTTPRequestHandler 

 

from pyrocko import guts, util 

from pyrocko.model import Event 

from pyrocko.guts import Object, String, Unicode, Bool 

 

from grond.meta import HasPaths, Path, expand_template, GrondError 

 

from grond import core, environment 

from grond.problems import ProblemInfoNotAvailable, ProblemDataNotAvailable 

from grond.version import __version__ 

from grond import info 

from grond.plot import PlotConfigCollection, get_all_plot_classes 

from grond.run_info import RunInfo 

 

guts_prefix = 'grond' 

logger = logging.getLogger('grond.report') 

 

 

class ReportIndexEntry(Object): 

path = String.T() 

problem_name = String.T() 

event_reference = Event.T(optional=True) 

event_best = Event.T(optional=True) 

grond_version = String.T(optional=True) 

run_info = RunInfo.T(optional=True) 

 

 

class ReportConfig(HasPaths): 

report_base_path = Path.T(default='report') 

entries_sub_path = String.T( 

default='${event_name}/${problem_name}') 

title = Unicode.T( 

default=u'Grond Report', 

help='Title shown on report overview page.') 

description = Unicode.T( 

default=u'This interactive document aggregates earthquake source ' 

u'inversion results from optimisations performed with Grond.', 

help='Description shown on report overview page.') 

plot_config_collection = PlotConfigCollection.T( 

help='Configurations for plots to be included in the report.') 

make_archive = Bool.T( 

default=True, 

help='Set to `false` to prevent creation of compressed archive.') 

 

 

class ReportInfo(Object): 

title = Unicode.T(optional=True) 

description = Unicode.T(optional=True) 

version_info = info.VersionInfo.T() 

have_archive = Bool.T(optional=True) 

 

 

def read_config(path): 

get_all_plot_classes() # make sure all plot modules are imported 

try: 

config = guts.load(filename=path) 

except OSError: 

raise GrondError( 

'Cannot read Grond report configuration file: %s' % path) 

 

if not isinstance(config, ReportConfig): 

raise GrondError( 

'Invalid Grond report configuration in file "%s".' % path) 

 

config.set_basepath(op.dirname(path) or '.') 

return config 

 

 

def write_config(config, path): 

try: 

basepath = config.get_basepath() 

dirname = op.dirname(path) or '.' 

config.change_basepath(dirname) 

guts.dump( 

config, 

filename=path, 

header='Grond report configuration file, version %s' % __version__) 

 

config.change_basepath(basepath) 

 

except OSError: 

raise GrondError( 

'Cannot write Grond report configuration file: %s' % path) 

 

 

def iter_report_entry_dirs(report_base_path): 

for path, dirnames, filenames in os.walk(report_base_path): 

for dirname in dirnames: 

dirpath = op.join(path, dirname) 

stats_path = op.join(dirpath, 'problem.yaml') 

if op.exists(stats_path): 

yield dirpath 

 

 

def copytree(src, dst): 

names = os.listdir(src) 

if not op.exists(dst): 

os.makedirs(dst) 

 

for name in names: 

srcname = op.join(src, name) 

dstname = op.join(dst, name) 

if op.isdir(srcname): 

copytree(srcname, dstname) 

else: 

shutil.copy(srcname, dstname) 

 

 

def report(env, report_config=None, update_without_plotting=False, 

make_index=True, make_archive=True, nthreads=0): 

 

if report_config is None: 

report_config = ReportConfig() 

report_config.set_basepath('.') 

 

event_name = env.get_current_event_name() 

problem = env.get_problem() 

logger.info('Creating report entry for run "%s"...' % problem.name) 

 

optimiser = env.get_optimiser() 

optimiser.set_nthreads(nthreads) 

 

fp = report_config.expand_path 

entry_path = expand_template( 

op.join( 

fp(report_config.report_base_path), 

report_config.entries_sub_path), 

dict( 

event_name=event_name, 

problem_name=problem.name)) 

 

if op.exists(entry_path) and not update_without_plotting: 

shutil.rmtree(entry_path) 

 

try: 

problem.dump_problem_info(entry_path) 

 

guts.dump(env.get_config(), 

filename=op.join(entry_path, 'config.yaml'), 

header=True) 

 

util.ensuredir(entry_path) 

plots_dir_out = op.join(entry_path, 'plots') 

util.ensuredir(plots_dir_out) 

 

event = env.get_dataset().get_event() 

guts.dump(event, filename=op.join(entry_path, 'event.reference.yaml')) 

 

try: 

rundir_path = env.get_rundir_path() 

 

core.export( 

'stats', [rundir_path], 

filename=op.join(entry_path, 'stats.yaml')) 

 

core.export( 

'best', [rundir_path], 

filename=op.join(entry_path, 'event.solution.best.yaml'), 

type='event-yaml') 

 

core.export( 

'mean', [rundir_path], 

filename=op.join(entry_path, 'event.solution.mean.yaml'), 

type='event-yaml') 

 

core.export( 

'ensemble', [rundir_path], 

filename=op.join(entry_path, 'event.solution.ensemble.yaml'), 

type='event-yaml') 

 

except (environment.NoRundirAvailable, ProblemInfoNotAvailable, 

ProblemDataNotAvailable): 

 

pass 

 

if not update_without_plotting: 

from grond import plot 

pcc = report_config.plot_config_collection.get_weeded(env) 

plot.make_plots( 

env, 

plots_path=op.join(entry_path, 'plots'), 

plot_config_collection=pcc) 

 

try: 

run_info = env.get_run_info() 

except environment.NoRundirAvailable: 

run_info = None 

 

rie = ReportIndexEntry( 

path='.', 

problem_name=problem.name, 

grond_version=problem.grond_version, 

run_info=run_info) 

 

fn = op.join(entry_path, 'event.solution.best.yaml') 

if op.exists(fn): 

rie.event_best = guts.load(filename=fn) 

 

fn = op.join(entry_path, 'event.reference.yaml') 

if op.exists(fn): 

rie.event_reference = guts.load(filename=fn) 

 

fn = op.join(entry_path, 'index.yaml') 

guts.dump(rie, filename=fn) 

 

except Exception as e: 

logger.warn( 

'Failed to create report entry, removing incomplete subdirectory: ' 

'%s' % entry_path) 

raise e 

 

if op.exists(entry_path): 

shutil.rmtree(entry_path) 

 

logger.info('Done creating report entry for run "%s".' % problem.name) 

 

if make_index: 

report_index(report_config) 

 

if make_archive: 

report_archive(report_config) 

 

 

def report_index(report_config=None): 

if report_config is None: 

report_config = ReportConfig() 

 

report_base_path = report_config.report_base_path 

entries = [] 

for entry_path in iter_report_entry_dirs(report_base_path): 

 

fn = op.join(entry_path, 'index.yaml') 

if not os.path.exists(fn): 

logger.warn('Skipping indexing of incomplete report entry: %s' 

% entry_path) 

 

continue 

 

logger.info('Indexing %s...' % entry_path) 

 

rie = guts.load(filename=fn) 

report_relpath = op.relpath(entry_path, report_base_path) 

rie.path = report_relpath 

entries.append(rie) 

 

guts.dump_all( 

entries, 

filename=op.join(report_base_path, 'report_list.yaml')) 

 

guts.dump( 

ReportInfo( 

title=report_config.title, 

description=report_config.description, 

version_info=info.version_info(), 

have_archive=report_config.make_archive), 

filename=op.join(report_base_path, 'info.yaml')) 

 

app_dir = op.join(op.split(__file__)[0], 'app') 

copytree(app_dir, report_base_path) 

logger.info('Created report in %s/index.html' % report_base_path) 

 

 

def report_archive(report_config): 

if report_config is None: 

report_config = ReportConfig() 

 

if not report_config.make_archive: 

return 

 

report_base_path = report_config.report_base_path 

 

logger.info('Generating report\'s archive...') 

with tarfile.open(op.join(report_base_path, 'grond-report.tar.gz'), 

mode='w:gz') as tar: 

tar.add(report_base_path, arcname='grond-report') 

 

 

def serve_ip(host): 

if host == 'localhost': 

ip = '127.0.0.1' 

elif host == 'default': 

import socket 

ip = [ 

(s.connect(('4.4.4.4', 80)), s.getsockname()[0], s.close()) 

for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] 

elif host == '*': 

ip = '' 

else: 

ip = host 

 

return ip 

 

 

class ReportHandler(SimpleHTTPRequestHandler): 

 

def _log_error(self, fmt, *args): 

logger.error(fmt % args) 

 

def _log_message(self, fmt, *args): 

logger.debug(fmt % args) 

 

def end_headers(self): 

self.send_header('Cache-Control', 'no-cache') 

SimpleHTTPRequestHandler.end_headers(self) 

 

 

ReportHandler.extensions_map.update({ 

'.yaml': 'application/x-yaml', 

'.yml': 'application/x-yaml'}) 

 

 

g_terminate = False 

 

 

def serve_report( 

addr=('127.0.0.1', 8383), 

report_config=None, 

fixed_port=False, 

open=False): 

 

if report_config is None: 

report_config = ReportConfig() 

 

path = report_config.expand_path(report_config.report_base_path) 

os.chdir(path) 

 

host, port = addr 

if fixed_port: 

ports = [port] 

else: 

ports = range(port, port+20) 

 

httpd = None 

for port in ports: 

try: 

httpd = HTTPServer((host, port), ReportHandler) 

break 

except OSError as e: 

logger.warn(str(e)) 

 

if httpd: 

logger.info( 

'Starting report web service at http://%s:%d' % (host, port)) 

 

thread = threading.Thread(None, httpd.serve_forever) 

thread.start() 

 

if open: 

import webbrowser 

if open: 

webbrowser.open('http://%s:%d' % (host, port)) 

 

def handler(signum, frame): 

global g_terminate 

g_terminate = True 

 

signal.signal(signal.SIGINT, handler) 

signal.signal(signal.SIGTERM, handler) 

 

while not g_terminate: 

time.sleep(0.1) 

 

signal.signal(signal.SIGINT, signal.SIG_DFL) 

signal.signal(signal.SIGTERM, signal.SIG_DFL) 

 

logger.info('Stopping report web service...') 

 

httpd.shutdown() 

thread.join() 

 

logger.info('... done') 

 

else: 

logger.error('Failed to start web service.') 

 

 

__all__ = ''' 

report 

report_index 

report_archive 

ReportConfig 

ReportIndexEntry 

ReportInfo 

serve_ip 

serve_report 

read_config 

write_config 

'''.split()