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

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

from __future__ import absolute_import, print_function 

 

import hashlib 

import numpy as num 

 

from pyrocko import util 

from pyrocko.guts import Object, String, Timestamp, Float, Int, Unicode, \ 

Tuple, List 

from pyrocko.guts_array import Array 

 

separator = '\t' 

 

g_content_kinds = [ 

'undefined', 

'waveform', 

'station', 

'channel', 

'response', 

'event', 

'waveform_promise'] 

 

g_content_kind_ids = ( 

UNDEFINED, WAVEFORM, STATION, CHANNEL, RESPONSE, EVENT, 

WAVEFORM_PROMISE) = range(len(g_content_kinds)) 

 

g_tmin = int(util.str_to_time('1900-01-01 00:00:00')) 

g_tmax = int(util.str_to_time('2100-01-01 00:00:00')) 

 

 

def to_kind(kind_id): 

return g_content_kinds[kind_id] 

 

 

def to_kinds(kind_ids): 

return [g_content_kinds[kind_id] for kind_id in kind_ids] 

 

 

def to_kind_id(kind): 

return g_content_kinds.index(kind) 

 

 

def to_kind_ids(kinds): 

return [g_content_kinds.index(kind) for kind in kinds] 

 

 

g_kind_mask_all = 0xff 

 

 

def to_kind_mask(kinds): 

if kinds: 

return sum(1 << kind_id for kind_id in to_kind_ids(kinds)) 

else: 

return g_kind_mask_all 

 

 

def str_or_none(x): 

if x is None: 

return None 

else: 

return str(x) 

 

 

def float_or_none(x): 

if x is None: 

return None 

else: 

return float(x) 

 

 

def int_or_none(x): 

if x is None: 

return None 

else: 

return int(x) 

 

 

def int_or_g_tmin(x): 

if x is None: 

return g_tmin 

else: 

return int(x) 

 

 

def int_or_g_tmax(x): 

if x is None: 

return g_tmax 

else: 

return int(x) 

 

 

def tmin_or_none(x): 

if x == g_tmin: 

return None 

else: 

return x 

 

 

def tmax_or_none(x): 

if x == g_tmax: 

return None 

else: 

return x 

 

 

def time_or_none_to_str(x): 

if x is None: 

return '...'.ljust(17) 

else: 

return util.time_to_str(x) 

 

 

def tsplit(t): 

if t is None: 

return None, 0.0 

 

seconds = num.floor(t) 

offset = t - seconds 

return int(seconds), float(offset) 

 

 

def tjoin(seconds, offset, deltat): 

if seconds is None: 

return None 

 

if deltat is not None and deltat < 1e-3: 

return util.hpfloat(seconds) + util.hpfloat(offset) 

else: 

return seconds + offset 

 

 

tscale_min = 1 

tscale_max = 365 * 24 * 3600 # last edge is one above 

tscale_logbase = 20 

 

tscale_edges = [tscale_min] 

while True: 

tscale_edges.append(tscale_edges[-1]*tscale_logbase) 

if tscale_edges[-1] >= tscale_max: 

break 

 

 

tscale_edges = num.array(tscale_edges) 

 

 

def tscale_to_kscale(tscale): 

 

# 0 <= x < tscale_edges[1]: 0 

# tscale_edges[1] <= x < tscale_edges[2]: 1 

# ... 

# tscale_edges[len(tscale_edges)-1] <= x: len(tscale_edges) 

 

return int(num.searchsorted(tscale_edges, tscale)) 

 

 

class Content(Object): 

''' 

Base class for content types in the Squirrel framework. 

''' 

 

@property 

def str_codes(self): 

return '.'.join(self.codes) 

 

@property 

def str_time_span(self): 

tmin, tmax = self.time_span 

if tmin == tmax: 

return '%s' % time_or_none_to_str(tmin) 

else: 

return '%s - %s' % ( 

time_or_none_to_str(tmin), time_or_none_to_str(tmax)) 

 

@property 

def summary(self): 

return '%s %-16s %s' % ( 

self.__class__.__name__, self.str_codes, self.str_time_span) 

 

def __lt__(self, other): 

return self.__key__() < other.__key__() 

 

def __key__(self): 

return self.codes, self.time_span_g_clipped 

 

@property 

def time_span_g_clipped(self): 

tmin, tmax = self.time_span 

return ( 

tmin if tmin is not None else g_tmin, 

tmax if tmax is not None else g_tmax) 

 

 

class Waveform(Content): 

''' 

A continuous seismic waveform snippet. 

''' 

 

agency = String.T(default='', help='Agency code (2-5)') 

network = String.T(default='', help='Deployment/network code (1-8)') 

station = String.T(default='', help='Station code (1-5)') 

location = String.T(default='', help='Location code (0-2)') 

channel = String.T(default='', help='Channel code (3)') 

extra = String.T(default='', help='Extra/custom code') 

 

tmin = Timestamp.T() 

tmax = Timestamp.T() 

 

deltat = Float.T(optional=True) 

 

data = Array.T( 

optional=True, 

shape=(None,), 

serialize_as='base64+meta', 

help='numpy array with data samples') 

 

def pyrocko_trace(self): 

from pyrocko import trace 

return trace.Trace( 

network=self.network, 

station=self.station, 

location=self.location, 

channel=self.channel, 

tmin=self.tmin, 

tmax=self.tmax, 

deltat=self.deltat, 

ydata=self.data) 

 

@property 

def codes(self): 

return ( 

self.agency, self.network, self.station, self.location, 

self.channel, self.extra) 

 

@property 

def time_span(self): 

return (self.tmin, self.tmax) 

 

 

class WaveformPromise(Content): 

''' 

Information about a waveform potentially available at a remote site. 

''' 

 

agency = String.T(default='', help='Agency code (2-5)') 

network = String.T(default='', help='Deployment/network code (1-8)') 

station = String.T(default='', help='Station code (1-5)') 

location = String.T(default='', help='Location code (0-2)') 

channel = String.T(default='', help='Channel code (3)') 

extra = String.T(default='', help='Extra/custom code') 

 

tmin = Timestamp.T() 

tmax = Timestamp.T() 

 

deltat = Float.T(optional=True) 

 

source_hash = String.T() 

 

@property 

def codes(self): 

return ( 

self.agency, self.network, self.station, self.location, 

self.channel, self.extra) 

 

@property 

def time_span(self): 

return (self.tmin, self.tmax) 

 

 

class InvalidWaveform(Exception): 

pass 

 

 

class WaveformOrder(Object): 

source_id = String.T() 

codes = Tuple.T(None, String.T()) 

deltat = Float.T() 

tmin = Timestamp.T() 

tmax = Timestamp.T() 

gaps = List.T(Tuple.T(2, Timestamp.T())) 

 

@property 

def client(self): 

return self.source_id.split(':')[1] 

 

def describe(self, site='?'): 

return '%s:%s %s [%s - %s]' % ( 

self.client, site, '.'.join(self.codes), 

util.time_to_str(self.tmin), util.time_to_str(self.tmax)) 

 

def validate(self, tr): 

if tr.ydata.size == 0: 

raise InvalidWaveform( 

'waveform with zero data samples') 

 

if tr.deltat != self.deltat: 

raise InvalidWaveform( 

'incorrect sampling interval - waveform: %g s, ' 

'meta-data: %g s' % ( 

tr.deltat, self.deltat)) 

 

if not num.all(num.isfinite(tr.ydata)): 

raise InvalidWaveform('waveform has NaN values') 

 

 

def order_summary(orders): 

codes = sorted(set(order.codes[1:-1] for order in orders)) 

if len(codes) >= 2: 

return '%i orders, %s - %s' % ( 

len(orders), 

'.'.join(codes[0]), 

'.'.join(codes[-1])) 

 

else: 

return '%i orders, %s' % ( 

len(orders), 

'.'.join(codes[0])) 

 

 

class Station(Content): 

''' 

A seismic station. 

''' 

 

agency = String.T(default='', help='Agency code (2-5)') 

network = String.T(default='', help='Deployment/network code (1-8)') 

station = String.T(default='', help='Station code (1-5)') 

location = String.T(default='', optional=True, help='Location code (0-2)') 

 

tmin = Timestamp.T(optional=True) 

tmax = Timestamp.T(optional=True) 

 

lat = Float.T() 

lon = Float.T() 

elevation = Float.T(optional=True) 

depth = Float.T(optional=True) 

 

description = Unicode.T(optional=True) 

 

@property 

def codes(self): 

return ( 

self.agency, self.network, self.station, 

self.location if self.location is not None else '*') 

 

@property 

def time_span(self): 

return (self.tmin, self.tmax) 

 

def get_pyrocko_station(self): 

from pyrocko import model 

return model.Station( 

network=self.network, 

station=self.station, 

location=self.location if self.location is not None else '*', 

lat=self.lat, 

lon=self.lon, 

elevation=self.elevation, 

depth=self.depth) 

 

def _get_pyrocko_station_args(self): 

return ( 

'*', 

self.network, 

self.station, 

self.location if self.location is not None else '*', 

self.lat, 

self.lon, 

self.elevation, 

self.depth) 

 

 

class Channel(Content): 

''' 

A channel of a seismic station. 

''' 

 

agency = String.T(default='', help='Agency code (2-5)') 

network = String.T(default='', help='Deployment/network code (1-8)') 

station = String.T(default='', help='Station code (1-5)') 

location = String.T(default='', help='Location code (0-2)') 

channel = String.T(default='', help='Channel code (3)') 

 

tmin = Timestamp.T(optional=True) 

tmax = Timestamp.T(optional=True) 

 

lat = Float.T() 

lon = Float.T() 

elevation = Float.T(optional=True) 

depth = Float.T(optional=True) 

 

dip = Float.T(optional=True) 

azimuth = Float.T(optional=True) 

deltat = Float.T(optional=True) 

 

@property 

def codes(self): 

return ( 

self.agency, self.network, self.station, self.location, 

self.channel) 

 

@property 

def time_span(self): 

return (self.tmin, self.tmax) 

 

def get_pyrocko_channel(self): 

from pyrocko import model 

return model.Channel( 

name=self.channel, 

azimuth=self.azimuth, 

dip=self.dip) 

 

def _get_pyrocko_station_args(self): 

return ( 

self.channel, 

self.network, 

self.station, 

self.location, 

self.lat, 

self.lon, 

self.elevation, 

self.depth) 

 

def _get_pyrocko_channel_args(self): 

return ( 

'*', 

self.channel, 

self.azimuth, 

self.dip) 

 

 

class Response(Content): 

''' 

The instrument response of a seismic station channel. 

''' 

 

pass 

 

 

class Event(Content): 

''' 

A seismic event. 

''' 

 

name = String.T(optional=True) 

time = Timestamp.T() 

duration = Float.T(optional=True) 

 

lat = Float.T() 

lon = Float.T() 

depth = Float.T(optional=True) 

 

magnitude = Float.T(optional=True) 

 

def get_pyrocko_event(self): 

from pyrocko import model 

model.Event( 

name=self.name, 

time=self.time, 

lat=self.lat, 

lon=self.lon, 

depth=self.depth, 

magnitude=self.magnitude, 

duration=self.duration) 

 

@property 

def time_span(self): 

return (self.time, self.time) 

 

 

def ehash(s): 

return hashlib.sha1(s.encode('utf8')).hexdigest() 

 

 

class Nut(Object): 

''' 

Index entry referencing an elementary piece of content. 

 

So-called *nuts* are used in Pyrocko's Squirrel framework to hold common 

meta-information about individual pieces of waveforms, stations, channels, 

etc. together with the information where it was found or generated. 

''' 

 

file_path = String.T(optional=True) 

file_format = String.T(optional=True) 

file_mtime = Timestamp.T(optional=True) 

file_size = Int.T(optional=True) 

 

file_segment = Int.T(optional=True) 

file_element = Int.T(optional=True) 

 

kind_id = Int.T() 

codes = String.T() 

 

tmin_seconds = Timestamp.T() 

tmin_offset = Float.T(default=0.0, optional=True) 

tmax_seconds = Timestamp.T() 

tmax_offset = Float.T(default=0.0, optional=True) 

 

deltat = Float.T(optional=True) 

 

content = Content.T(optional=True) 

 

content_in_db = False 

 

def __init__( 

self, 

file_path=None, 

file_format=None, 

file_mtime=None, 

file_size=None, 

file_segment=None, 

file_element=None, 

kind_id=0, 

codes='', 

tmin_seconds=None, 

tmin_offset=0.0, 

tmax_seconds=None, 

tmax_offset=0.0, 

deltat=None, 

content=None, 

tmin=None, 

tmax=None, 

values_nocheck=None): 

 

if values_nocheck is not None: 

(self.file_path, self.file_format, self.file_mtime, 

self.file_size, 

self.file_segment, self.file_element, 

self.kind_id, self.codes, 

self.tmin_seconds, self.tmin_offset, 

self.tmax_seconds, self.tmax_offset, 

self.deltat) = values_nocheck 

 

self.content = None 

else: 

if tmin is not None: 

tmin_seconds, tmin_offset = tsplit(tmin) 

 

if tmax is not None: 

tmax_seconds, tmax_offset = tsplit(tmax) 

 

self.kind_id = int(kind_id) 

self.codes = str(codes) 

self.tmin_seconds = int_or_g_tmin(tmin_seconds) 

self.tmin_offset = float(tmin_offset) 

self.tmax_seconds = int_or_g_tmax(tmax_seconds) 

self.tmax_offset = float(tmax_offset) 

self.deltat = float_or_none(deltat) 

self.file_path = str_or_none(file_path) 

self.file_segment = int_or_none(file_segment) 

self.file_element = int_or_none(file_element) 

self.file_format = str_or_none(file_format) 

self.file_mtime = float_or_none(file_mtime) 

self.file_size = int_or_none(file_size) 

self.content = content 

 

Object.__init__(self, init_props=False) 

 

def __eq__(self, other): 

return (isinstance(other, Nut) and 

self.equality_values == other.equality_values) 

 

def hash(self): 

return ehash(','.join(str(x) for x in self.key)) 

 

def __ne__(self, other): 

return not (self == other) 

 

def get_io_backend(self): 

from . import io 

return io.get_backend(self.file_format) 

 

def file_modified(self): 

return self.get_io_backend().get_stats(self.file_path) \ 

!= (self.file_mtime, self.file_size) 

 

@property 

def dkey(self): 

return (self.kind_id, self.tmin_seconds, self.tmin_offset, self.codes) 

 

@property 

def key(self): 

return ( 

self.file_path, 

self.file_segment, 

self.file_element, 

self.file_mtime) 

 

@property 

def equality_values(self): 

return ( 

self.file_segment, self.file_element, 

self.kind_id, self.codes, 

self.tmin_seconds, self.tmin_offset, 

self.tmax_seconds, self.tmax_offset, self.deltat) 

 

@property 

def tmin(self): 

return tjoin(self.tmin_seconds, self.tmin_offset, self.deltat) 

 

@property 

def tmax(self): 

return tjoin(self.tmax_seconds, self.tmax_offset, self.deltat) 

 

@property 

def kscale(self): 

if self.tmin_seconds is None or self.tmax_seconds is None: 

return 0 

return tscale_to_kscale(self.tmax_seconds - self.tmin_seconds) 

 

@property 

def waveform_kwargs(self): 

agency, network, station, location, channel, extra = \ 

self.codes.split(separator) 

 

return dict( 

agency=agency, 

network=network, 

station=station, 

location=location, 

channel=channel, 

extra=extra, 

tmin=self.tmin, 

tmax=self.tmax, 

deltat=self.deltat) 

 

@property 

def waveform_promise_kwargs(self): 

agency, network, station, location, channel = \ 

self.codes.split(separator) 

 

return dict( 

agency=agency, 

network=network, 

station=station, 

location=location, 

channel=channel, 

tmin=self.tmin, 

tmax=self.tmax, 

deltat=self.deltat) 

 

@property 

def station_kwargs(self): 

agency, network, station, location = self.codes.split(separator) 

return dict( 

agency=agency, 

network=network, 

station=station, 

location=location if location != '*' else None, 

tmin=tmin_or_none(self.tmin), 

tmax=tmax_or_none(self.tmax)) 

 

@property 

def channel_kwargs(self): 

agency, network, station, location, channel \ 

= self.codes.split(separator) 

 

return dict( 

agency=agency, 

network=network, 

station=station, 

location=location, 

channel=channel, 

tmin=tmin_or_none(self.tmin), 

tmax=tmax_or_none(self.tmax), 

deltat=self.deltat) 

 

@property 

def event_kwargs(self): 

return dict( 

name=self.codes, 

time=self.tmin, 

duration=(self.tmax - self.tmin) or None) 

 

@property 

def trace_kwargs(self): 

agency, network, station, location, channel, extra = \ 

self.codes.split(separator) 

 

return dict( 

network=network, 

station=station, 

location=location, 

channel=channel, 

tmin=self.tmin, 

tmax=self.tmax, 

deltat=self.deltat) 

 

@property 

def dummy_trace(self): 

return DummyTrace(self) 

 

@property 

def codes_tuple(self): 

return tuple(self.codes.split(separator)) 

 

def oneline(self): 

return '%s, %s, %s - %s' % ( 

to_kind(self.kind_id), 

'.'.join(self.codes.split(separator)), 

util.time_to_str(self.tmin), 

util.time_to_str(self.tmax)) 

 

 

def make_waveform_nut( 

agency='', network='', station='', location='', channel='', extra='', 

**kwargs): 

 

codes = separator.join( 

(agency, network, station, location, channel, extra)) 

 

return Nut( 

kind_id=WAVEFORM, 

codes=codes, 

**kwargs) 

 

 

def make_waveform_promise_nut( 

agency='', network='', station='', location='', channel='', extra='', 

**kwargs): 

 

codes = separator.join( 

(agency, network, station, location, channel, extra)) 

 

return Nut( 

kind_id=WAVEFORM_PROMISE, 

codes=codes, 

**kwargs) 

 

 

def make_station_nut( 

agency='', network='', station='', location='', **kwargs): 

 

codes = separator.join((agency, network, station, location)) 

 

return Nut( 

kind_id=STATION, 

codes=codes, 

**kwargs) 

 

 

def make_channel_nut( 

agency='', network='', station='', location='', channel='', **kwargs): 

 

codes = separator.join((agency, network, station, location, channel)) 

 

return Nut( 

kind_id=CHANNEL, 

codes=codes, 

**kwargs) 

 

 

def make_event_nut(name='', **kwargs): 

 

codes = name 

 

return Nut( 

kind_id=EVENT, codes=codes, 

**kwargs) 

 

 

def group_channels(nuts): 

by_ansl = {} 

for nut in nuts: 

if nut.kind_id != CHANNEL: 

continue 

 

ansl = nut.codes[:4] 

 

if ansl not in by_ansl: 

by_ansl[ansl] = {} 

 

group = by_ansl[ansl] 

 

k = nut.codes[4][:-1], nut.deltat, nut.tmin, nut.tmax 

 

if k not in group: 

group[k] = set() 

 

group.add(nut.codes[4]) 

 

return by_ansl 

 

 

class DummyTrace(object): 

 

def __init__(self, nut): 

self.nut = nut 

self._nslc = None 

 

@property 

def tmin(self): 

return self.nut.tmin 

 

@property 

def tmax(self): 

return self.nut.tmax 

 

@property 

def deltat(self): 

return self.nut.deltat 

 

@property 

def nslc_id(self): 

if self._nslc is None: 

self._nslc = self.nut.codes_tuple[1:5] 

 

return self._nslc 

 

@property 

def network(self): 

return self.nslc_id[0] 

 

@property 

def station(self): 

return self.nslc_id[1] 

 

@property 

def location(self): 

return self.nslc_id[2] 

 

@property 

def channel(self): 

return self.nslc_id[3] 

 

def overlaps(self, tmin, tmax): 

return not (tmax < self.nut.tmin or self.nut.tmax < tmin) 

 

 

__all__ = [ 

'to_kind', 

'to_kinds', 

'to_kind_id', 

'to_kind_ids', 

'Content', 

'Waveform', 

'WaveformPromise', 

'Station', 

'Channel', 

'Nut', 

]