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

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

 

from __future__ import absolute_import, print_function 

 

import sys 

import os 

import re 

import math 

import threading 

import sqlite3 

import logging 

from collections import defaultdict 

 

from pyrocko.io_common import FileLoadError 

from pyrocko.guts import Object, Int, List, Tuple, String, Timestamp, Dict 

from pyrocko import util 

from pyrocko.progress import progress 

 

from . import model, io, cache 

 

from .model import to_kind_id, to_kind, separator, WaveformOrder 

from .client import fdsn, catalog 

from . import client, environment, error, pile 

 

logger = logging.getLogger('pyrocko.squirrel.base') 

 

 

guts_prefix = 'pf' 

 

 

class GeneratorWithLen(object): 

 

def __init__(self, gen, length): 

self.gen = gen 

self.length = length 

 

def __len__(self): 

return self.length 

 

def __iter__(self): 

return self.gen 

 

 

def make_task(*args, **kwargs): 

kwargs['logger'] = logger 

return progress.task(*args, **kwargs) 

 

 

def lpick(condition, seq): 

ft = [], [] 

for ele in seq: 

ft[int(bool(condition(ele)))].append(ele) 

 

return ft 

 

 

def execute_get1(connection, sql, args): 

return list(connection.execute(sql, args))[0] 

 

 

g_icount = 0 

g_lock = threading.Lock() 

 

 

def make_unique_name(): 

with g_lock: 

global g_icount 

name = '%i_%i' % (os.getpid(), g_icount) 

g_icount += 1 

 

return name 

 

 

def codes_fill(n, codes): 

return codes[:n] + ('*',) * (n-len(codes)) 

 

 

c_kind_to_ncodes = { 

'station': 4, 

'channel': 5, 

'response': 5, 

'waveform': 6, 

'event': 1, 

'waveform_promise': 6, 

'undefined': 1} 

 

 

c_inflated = ['', '*', '*', '*', '*', '*'] 

c_offsets = [0, 2, 1, 1, 1, 1, 0] 

 

 

def codes_inflate(codes): 

codes = codes[:6] 

inflated = list(c_inflated) 

ncodes = len(codes) 

offset = c_offsets[ncodes] 

inflated[offset:offset+ncodes] = codes 

return inflated 

 

 

def codes_patterns_for_kind(kind, codes): 

if not codes: 

return [] 

 

if kind in ('event', 'undefined'): 

return [codes] 

 

cfill = codes_inflate(codes)[:c_kind_to_ncodes[kind]] 

 

if kind == 'station': 

cfill2 = list(cfill) 

cfill2[3] = '[*]' 

return [cfill, cfill2] 

 

return [cfill] 

 

 

def group_channels(channels): 

groups = defaultdict(list) 

for channel in channels: 

codes = channel.codes 

gcodes = codes[:-1] + (codes[-1][:-1],) 

groups[gcodes].append(channel) 

 

return groups 

 

 

def pyrocko_station_from_channel_group(group, extra_args): 

list_of_args = [channel._get_pyrocko_station_args() for channel in group] 

args = util.consistency_merge(list_of_args + extra_args) 

from pyrocko import model as pmodel 

return pmodel.Station( 

network=args[0], 

station=args[1], 

location=args[2], 

lat=args[3], 

lon=args[4], 

elevation=args[5], 

depth=args[6], 

channels=[ch.get_pyrocko_channel() for ch in group]) 

 

 

def blocks(tmin, tmax, deltat, nsamples_block=100000): 

tblock = deltat * nsamples_block 

iblock_min = int(math.floor(tmin / tblock)) 

iblock_max = int(math.ceil(tmax / tblock)) 

for iblock in range(iblock_min, iblock_max): 

yield iblock * tblock, (iblock+1) * tblock 

 

 

def gaps(avail, tmin, tmax): 

assert tmin < tmax 

 

data = [(tmax, 1), (tmin, -1)] 

for (tmin_a, tmax_a) in avail: 

assert tmin_a < tmax_a 

data.append((tmin_a, 1)) 

data.append((tmax_a, -1)) 

 

data.sort() 

s = 1 

gaps = [] 

tmin_g = None 

for t, x in data: 

if s == 1 and x == -1: 

tmin_g = t 

elif s == 0 and x == 1 and tmin_g is not None: 

tmax_g = t 

if tmin_g != tmax_g: 

gaps.append((tmin_g, tmax_g)) 

 

s += x 

 

return gaps 

 

 

def order_key(order): 

return (order.codes, order.tmin, order.tmax) 

 

 

class Selection(object): 

 

''' 

Database backed file selection (base class for :py:class:`Squirrel`). 

 

:param database: 

Database instance or file path to database. 

:type database: 

:py:class:`Database` or :py:class:`str` 

:param persistent: 

If given a name, create a persistent selection. 

:type persistent: 

:py:class:`str` 

 

In the Squirrel framework, a selection is conceptually a list of files to 

be made available in the application. Instead of using 

:py:class:`Selection` directly, user applications should usually use its 

subclass :py:class:`Squirrel` which adds content indices to the selection 

and provides high level data querying. 

 

By default, a temporary table in the database is created to hold the names 

of the files in the selection. This table is only visible inside the 

application which created it. If a name is given to ``persistent``, a named 

selection is created, which is visible also in other applications using the 

same database. 

 

Besides the filename references, desired content kind masks and file format 

indications are stored in the selection's database table to make the user 

choice regarding these options persistent on a per-file basis. Book-keeping 

on whether files are unknown, known or if modification checks are forced is 

also handled in the selection's file-state table. 

 

Paths of files can be added to the selection using the :py:meth:`add` 

method and removed with :py:meth:`remove`. :py:meth:`undig_grouped` can be 

used to iterate over all content known to the selection. 

''' 

 

def __init__(self, database, persistent=None): 

self._conn = None 

 

if not isinstance(database, Database): 

database = get_database(database) 

 

if persistent is not None: 

assert isinstance(persistent, str) 

if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', persistent): 

raise error.SquirrelError( 

'invalid persistent selection name: %s' % persistent) 

 

self.name = 'psel_' + persistent 

else: 

self.name = 'sel_' + make_unique_name() 

 

self._persistent = persistent is not None 

self._database = database 

self._conn = self._database.get_connection() 

self._sources = [] 

 

self._names = { 

'db': 'main' if self._persistent else 'temp', 

'file_states': self.name + '_file_states', 

'bulkinsert': self.name + '_bulkinsert'} 

 

self._conn.execute(self._register_table(self._sql( 

''' 

CREATE TABLE IF NOT EXISTS %(db)s.%(file_states)s ( 

file_id integer PRIMARY KEY, 

file_state integer, 

kind_mask integer, 

format text) 

'''))) 

 

self._conn.execute(self._sql( 

''' 

CREATE INDEX 

IF NOT EXISTS %(db)s.%(file_states)s_index_file_state 

ON %(file_states)s (file_state) 

''')) 

 

def __del__(self): 

if hasattr(self, '_conn') and self._conn: 

if not self._persistent: 

self._delete() 

else: 

self._conn.commit() 

 

def _register_table(self, s): 

return self._database._register_table(s) 

 

def _sql(self, s): 

return s % self._names 

 

def get_database(self): 

''' 

Get the database to which this selection belongs. 

 

:returns: :py:class:`Database` object 

''' 

return self._database 

 

def _delete(self): 

''' 

Destroy the tables assoctiated with this selection. 

''' 

self._conn.execute(self._sql( 

'DROP TABLE %(db)s.%(file_states)s')) 

 

self._conn.commit() 

 

def add( 

self, 

paths, 

kind_mask=model.g_kind_mask_all, 

format='detect'): 

 

''' 

Add files to the selection. 

 

:param paths: 

Paths to files to be added to the selection. 

:type paths: 

iterator yielding :py:class:`str` objects 

''' 

 

if isinstance(paths, str): 

paths = [paths] 

 

task = make_task('Gathering file names') 

paths = task(paths) 

paths = util.short_to_list(200, paths) 

 

if isinstance(paths, list) and len(paths) <= 200: 

 

# short non-iterator paths: can do without temp table 

 

self._conn.executemany( 

''' 

INSERT OR IGNORE INTO files 

VALUES (NULL, ?, NULL, NULL, NULL) 

''', ((x,) for x in paths)) 

 

task = make_task('Preparing database', 3) 

task.update(0, condition='pruning stale information') 

self._conn.executemany(self._sql( 

''' 

DELETE FROM %(db)s.%(file_states)s 

WHERE file_id IN ( 

SELECT files.file_id 

FROM files 

WHERE files.path == ? ) 

AND kind_mask != ? OR format != ? 

'''), ( 

(path, kind_mask, format) for path in paths)) 

 

task.update(1, condition='adding file names to selection') 

self._conn.executemany(self._sql( 

''' 

INSERT OR IGNORE INTO %(db)s.%(file_states)s 

SELECT files.file_id, 0, ?, ? 

FROM files 

WHERE files.path = ? 

'''), ((kind_mask, format, path) for path in paths)) 

 

task.update(2, condition='updating file states') 

self._conn.executemany(self._sql( 

''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 1 

WHERE file_id IN ( 

SELECT files.file_id 

FROM files 

WHERE files.path == ? ) 

AND file_state != 0 

'''), ((path,) for path in paths)) 

 

task.update(3) 

task.done() 

 

else: 

 

self._conn.execute(self._sql( 

''' 

CREATE TEMP TABLE temp.%(bulkinsert)s 

(path text) 

''')) 

 

self._conn.executemany(self._sql( 

'INSERT INTO temp.%(bulkinsert)s VALUES (?)'), 

((x,) for x in paths)) 

 

task = make_task('Preparing database', 5) 

task.update(0, condition='adding file names to database') 

self._conn.execute(self._sql( 

''' 

INSERT OR IGNORE INTO files 

SELECT NULL, path, NULL, NULL, NULL 

FROM temp.%(bulkinsert)s 

''')) 

 

task.update(1, condition='pruning stale information') 

self._conn.execute(self._sql( 

''' 

DELETE FROM %(db)s.%(file_states)s 

WHERE file_id IN ( 

SELECT files.file_id 

FROM temp.%(bulkinsert)s 

INNER JOIN files 

ON temp.%(bulkinsert)s.path == files.path) 

AND kind_mask != ? OR format != ? 

'''), (kind_mask, format)) 

 

task.update(2, condition='adding file names to selection') 

self._conn.execute(self._sql( 

''' 

INSERT OR IGNORE INTO %(db)s.%(file_states)s 

SELECT files.file_id, 0, ?, ? 

FROM temp.%(bulkinsert)s 

INNER JOIN files 

ON temp.%(bulkinsert)s.path == files.path 

'''), (kind_mask, format)) 

 

task.update(3, condition='updating file states') 

self._conn.execute(self._sql( 

''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 1 

WHERE file_id IN ( 

SELECT files.file_id 

FROM temp.%(bulkinsert)s 

INNER JOIN files 

ON temp.%(bulkinsert)s.path == files.path) 

AND file_state != 0 

''')) 

 

task.update(4, condition='dropping temporary data') 

self._conn.execute(self._sql( 

'DROP TABLE temp.%(bulkinsert)s')) 

task.update(5) 

task.done() 

 

def remove(self, paths): 

''' 

Remove files from the selection. 

 

:param paths: 

Paths to files to be removed from the selection. 

:type paths: 

:py:class:`list` of :py:class:`str` 

''' 

if isinstance(paths, str): 

paths = [paths] 

 

self._conn.executemany(self._sql( 

''' 

DELETE FROM %(db)s.%(file_states)s 

WHERE %(db)s.%(file_states)s.file_id IN 

(SELECT files.file_id 

FROM files 

WHERE files.path == ?) 

'''), ((path,) for path in paths)) 

 

def iter_paths(self): 

''' 

Iterate over all file paths currently belonging to the selection. 

 

:returns: Iterator yielding file paths. 

''' 

 

sql = self._sql(''' 

SELECT 

files.path 

FROM %(db)s.%(file_states)s 

INNER JOIN files 

ON files.file_id = %(db)s.%(file_states)s.file_id 

ORDER BY %(db)s.%(file_states)s.file_id 

''') 

 

for values in self._conn.execute(sql): 

yield values[0] 

 

def get_paths(self): 

''' 

Get all file paths currently belonging to the selection. 

 

:returns: List of file paths. 

''' 

return list(self.iter_paths()) 

 

def _set_file_states_known(self): 

''' 

Set file states to "known" (2). 

''' 

self._conn.execute(self._sql( 

''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 2 

WHERE file_state < 2 

''')) 

 

def _set_file_states_force_check(self): 

''' 

Set file states to "request force check" (1). 

''' 

self._conn.execute(self._sql( 

''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 1 

''')) 

 

def undig_grouped(self, skip_unchanged=False): 

''' 

Get inventory of cached content for all files in the selection. 

 

:param: 

skip_unchanged: if ``True`` only inventory of modified files is 

yielded (:py:meth:`flag_modified` must be called beforehand). 

 

This generator yields tuples ``((format, path), nuts)`` where ``path`` 

is the path to the file, ``format`` is the format assignation or 

``'detect'`` and ``nuts`` is a list of 

:py:class:`~pyrocko.squirrel.Nut` objects representing the contents of 

the file. 

''' 

 

if skip_unchanged: 

where = ''' 

WHERE %(db)s.%(file_states)s.file_state == 0 

''' 

else: 

where = '' 

 

nfiles = execute_get1(self._conn, self._sql(''' 

SELECT 

COUNT() 

FROM %(db)s.%(file_states)s 

''' + where), ())[0] 

 

def gen(): 

sql = self._sql(''' 

SELECT 

%(db)s.%(file_states)s.format, 

files.path, 

files.format, 

files.mtime, 

files.size, 

nuts.file_segment, 

nuts.file_element, 

kind_codes.kind_id, 

kind_codes.codes, 

nuts.tmin_seconds, 

nuts.tmin_offset, 

nuts.tmax_seconds, 

nuts.tmax_offset, 

kind_codes.deltat 

FROM %(db)s.%(file_states)s 

LEFT OUTER JOIN files 

ON %(db)s.%(file_states)s.file_id = files.file_id 

LEFT OUTER JOIN nuts 

ON files.file_id = nuts.file_id 

LEFT OUTER JOIN kind_codes 

ON nuts.kind_codes_id == kind_codes.kind_codes_id 

''' + where + ''' 

ORDER BY %(db)s.%(file_states)s.file_id 

''') 

 

nuts = [] 

format_path = None 

for values in self._conn.execute(sql): 

if format_path is not None and values[1] != format_path[1]: 

yield format_path, nuts 

nuts = [] 

 

if values[2] is not None: 

nuts.append(model.Nut(values_nocheck=values[1:])) 

 

format_path = values[:2] 

 

if format_path is not None: 

yield format_path, nuts 

 

return GeneratorWithLen(gen(), nfiles) 

 

def flag_modified(self, check=True): 

''' 

Mark files which have been modified. 

 

:param check: 

If ``True`` query modification times of known files on disk. If 

``False``, only flag unknown files. 

 

Assumes file state is 0 for newly added files, 1 for files added again 

to the selection (forces check), or 2 for all others (no checking is 

done for those). 

 

Sets file state to 0 for unknown or modified files, 2 for known and not 

modified files. 

''' 

 

sql = self._sql(''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 0 

WHERE ( 

SELECT mtime 

FROM files 

WHERE files.file_id == %(db)s.%(file_states)s.file_id) IS NULL 

AND file_state == 1 

''') 

 

self._conn.execute(sql) 

 

if not check: 

 

sql = self._sql(''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = 2 

WHERE file_state == 1 

''') 

 

self._conn.execute(sql) 

 

return 

 

def iter_file_states(): 

sql = self._sql(''' 

SELECT 

files.file_id, 

files.path, 

files.format, 

files.mtime, 

files.size 

FROM %(db)s.%(file_states)s 

INNER JOIN files 

ON %(db)s.%(file_states)s.file_id == files.file_id 

WHERE %(db)s.%(file_states)s.file_state == 1 

ORDER BY %(db)s.%(file_states)s.file_id 

''') 

 

for (file_id, path, fmt, mtime_db, 

size_db) in self._conn.execute(sql): 

 

try: 

mod = io.get_backend(fmt) 

file_stats = mod.get_stats(path) 

 

except FileLoadError: 

yield 0, file_id 

continue 

except io.UnknownFormat: 

continue 

 

if (mtime_db, size_db) != file_stats: 

yield 0, file_id 

else: 

yield 2, file_id 

 

# could better use callback function here... 

 

sql = self._sql(''' 

UPDATE %(db)s.%(file_states)s 

SET file_state = ? 

WHERE file_id = ? 

''') 

 

self._conn.executemany(sql, iter_file_states()) 

 

 

class SquirrelStats(Object): 

''' 

Container to hold statistics about contents available from a Squirrel. 

 

See also :py:meth:`Squirrel.get_stats`. 

''' 

 

nfiles = Int.T( 

help='Number of files in selection.') 

nnuts = Int.T( 

help='Number of index nuts in selection.') 

codes = List.T( 

Tuple.T(content_t=String.T()), 

help='Available code sequences in selection, e.g. ' 

'(agency, network, station, location) for stations nuts.') 

kinds = List.T( 

String.T(), 

help='Available content types in selection.') 

total_size = Int.T( 

help='Aggregated file size of files is selection.') 

counts = Dict.T( 

String.T(), Dict.T(Tuple.T(content_t=String.T()), Int.T()), 

help='Breakdown of how many nuts of any content type and code ' 

'sequence are available in selection, ``counts[kind][codes]``.') 

tmin = Timestamp.T( 

optional=True, 

help='Earliest start time of all nuts in selection.') 

tmax = Timestamp.T( 

optional=True, 

help='Latest end time of all nuts in selection.') 

 

def __str__(self): 

kind_counts = dict( 

(kind, sum(self.counts[kind].values())) for kind in self.kinds) 

 

codes = ['.'.join(x) for x in self.codes] 

 

if len(codes) > 20: 

scodes = '\n' + util.ewrap(codes[:10], indent=' ') \ 

+ '\n [%i more]\n' % (len(codes) - 20) \ 

+ util.ewrap(codes[-10:], indent=' ') 

else: 

scodes = '\n' + util.ewrap(codes, indent=' ') \ 

if codes else '<none>' 

 

stmin = util.tts(self.tmin) if self.tmin is not None else '<none>' 

stmax = util.tts(self.tmax) if self.tmax is not None else '<none>' 

 

s = ''' 

Available codes: %s 

Number of files: %i 

Total size of known files: %s 

Number of index nuts: %i 

Available content kinds: %s 

Time span of indexed contents: %s - %s''' % ( 

scodes, 

self.nfiles, 

util.human_bytesize(self.total_size), 

self.nnuts, 

', '.join('%s: %i' % ( 

kind, kind_counts[kind]) for kind in sorted(self.kinds)), 

stmin, stmax) 

 

return s 

 

 

class Squirrel(Selection): 

''' 

Prompt, lazy, indexing, caching, dynamic seismological dataset access. 

 

:param env: 

Squirrel environment instance or directory path to use as starting 

point for its detection. By default, the current directory is used as 

starting point. When searching for a usable environment the directory 

``'.squirrel'`` or ``'squirrel'`` in the current (or starting point) 

directory is used if it exists, otherwise the parent directories are 

search upwards for the existence of such a directory. If no such 

directory is found, the user's global Squirrel environment 

``'$HOME/.pyrocko/squirrel'`` is used. 

:type env: 

:py:class:`SquirrelEnvironment` or :py:class:`str` 

:param database: 

Database instance or path to database. By default the 

database found in the detected Squirrel environment is used. 

:type database: 

:py:class:`Database` or :py:class:`str` 

:param cache_path: 

Directory path to use for data caching. By default, the ``'cache'`` 

directory in the detected Squirrel environment is used. 

:type cache_path: 

:py:class:`str` 

:param persistent: 

If given a name, create a persistent selection. 

:type persistent: 

:py:class:`str` 

 

Provides a unified interface to query seismic waveforms, station and sensor 

metadata, and event information from local file collections and remote data 

sources. Query results are promptly returned, even for very large 

collections, thanks to a highly optimized database setup working behind the 

scenes. Assemblage of a data selection is very fast for known files as all 

content indices are cached in a database. Unknown files are automatically 

indexed when added to the selection. 

 

Features 

 

- Efficient[1] lookup of data relevant for a selected time window. 

- Metadata caching and indexing. 

- Modified files are re-indexed as needed. 

- SQL database (sqlite) is used behind the scenes. 

- Can handle selections with millions of files. 

- Data can be added and removed at run-time, efficiently[1]. 

- Just-in-time download of missing data. 

- Disk-cache of meta-data query results with expiration time. 

- Efficient event catalog synchronization. 

- Always-up-to-date data coverage indices. 

- Always-up-to-date indices of available station/channel codes. 

 

[1] O log N performance, where N is the number of data entities (nuts). 

 

Queries are restricted to the contents offered by the files which have 

been added to the Squirrel (which usually is a subset of the information 

collected in the attached global file meta-information database). 

 

By default, temporary tables are created in the attached database to hold 

the names of the files in the selection as well as various indices and 

counters. These tables are only visible inside the application which 

created it. If a name is given to ``persistent``, a named selection is 

created, which is visible also in other applications using the same 

database. 

 

Paths of files can be added to the selection using the 

:py:meth:`add` method. 

''' 

 

def __init__( 

self, env=None, database=None, cache_path=None, persistent=None): 

 

if not isinstance(env, environment.SquirrelEnvironment): 

env = environment.get_environment(env) 

 

if database is None: 

database = env.database_path 

 

if cache_path is None: 

cache_path = env.cache_path 

 

if persistent is None: 

persistent = env.persistent 

 

Selection.__init__(self, database=database, persistent=persistent) 

c = self._conn 

self._content_caches = { 

'waveform': cache.ContentCache(), 

'default': cache.ContentCache()} 

 

self._cache_path = cache_path 

 

self._pile = None 

 

self._names.update({ 

'nuts': self.name + '_nuts', 

'kind_codes_count': self.name + '_kind_codes_count', 

'coverage': self.name + '_coverage'}) 

 

c.execute(self._register_table(self._sql( 

''' 

CREATE TABLE IF NOT EXISTS %(db)s.%(nuts)s ( 

nut_id integer PRIMARY KEY, 

file_id integer, 

file_segment integer, 

file_element integer, 

kind_id integer, 

kind_codes_id integer, 

tmin_seconds integer, 

tmin_offset integer, 

tmax_seconds integer, 

tmax_offset integer, 

kscale integer) 

'''))) 

 

c.execute(self._register_table(self._sql( 

''' 

CREATE TABLE IF NOT EXISTS %(db)s.%(kind_codes_count)s ( 

kind_codes_id integer PRIMARY KEY, 

count integer) 

'''))) 

 

c.execute(self._sql( 

''' 

CREATE UNIQUE INDEX IF NOT EXISTS %(db)s.%(nuts)s_file_element 

ON %(nuts)s (file_id, file_segment, file_element) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE INDEX IF NOT EXISTS %(db)s.%(nuts)s_index_file_id 

ON %(nuts)s (file_id) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE INDEX IF NOT EXISTS %(db)s.%(nuts)s_index_tmin_seconds 

ON %(nuts)s (tmin_seconds) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE INDEX IF NOT EXISTS %(db)s.%(nuts)s_index_tmax_seconds 

ON %(nuts)s (tmax_seconds) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE INDEX IF NOT EXISTS %(db)s.%(nuts)s_index_kscale 

ON %(nuts)s (kind_id, kscale, tmin_seconds) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_delete_nuts 

BEFORE DELETE ON main.files FOR EACH ROW 

BEGIN 

DELETE FROM %(nuts)s WHERE file_id == old.file_id; 

END 

''')) 

 

# trigger only on size to make silent update of mtime possible 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_delete_nuts2 

BEFORE UPDATE OF size ON main.files FOR EACH ROW 

BEGIN 

DELETE FROM %(nuts)s WHERE file_id == old.file_id; 

END 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS 

%(db)s.%(file_states)s_delete_files 

BEFORE DELETE ON %(db)s.%(file_states)s FOR EACH ROW 

BEGIN 

DELETE FROM %(nuts)s WHERE file_id == old.file_id; 

END 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_inc_kind_codes 

BEFORE INSERT ON %(nuts)s FOR EACH ROW 

BEGIN 

INSERT OR IGNORE INTO %(kind_codes_count)s VALUES 

(new.kind_codes_id, 0); 

UPDATE %(kind_codes_count)s 

SET count = count + 1 

WHERE new.kind_codes_id 

== %(kind_codes_count)s.kind_codes_id; 

END 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_dec_kind_codes 

BEFORE DELETE ON %(nuts)s FOR EACH ROW 

BEGIN 

UPDATE %(kind_codes_count)s 

SET count = count - 1 

WHERE old.kind_codes_id 

== %(kind_codes_count)s.kind_codes_id; 

END 

''')) 

 

c.execute(self._register_table(self._sql( 

''' 

CREATE TABLE IF NOT EXISTS %(db)s.%(coverage)s ( 

kind_codes_id integer, 

time_seconds integer, 

time_offset integer, 

step integer) 

'''))) 

 

c.execute(self._sql( 

''' 

CREATE UNIQUE INDEX IF NOT EXISTS %(db)s.%(coverage)s_time 

ON %(coverage)s (kind_codes_id, time_seconds, time_offset) 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_add_coverage 

AFTER INSERT ON %(nuts)s FOR EACH ROW 

BEGIN 

INSERT OR IGNORE INTO %(coverage)s VALUES 

(new.kind_codes_id, new.tmin_seconds, new.tmin_offset, 0) 

; 

UPDATE %(coverage)s 

SET step = step + 1 

WHERE new.kind_codes_id == %(coverage)s.kind_codes_id 

AND new.tmin_seconds == %(coverage)s.time_seconds 

AND new.tmin_offset == %(coverage)s.time_offset 

; 

INSERT OR IGNORE INTO %(coverage)s VALUES 

(new.kind_codes_id, new.tmax_seconds, new.tmax_offset, 0) 

; 

UPDATE %(coverage)s 

SET step = step - 1 

WHERE new.kind_codes_id == %(coverage)s.kind_codes_id 

AND new.tmax_seconds == %(coverage)s.time_seconds 

AND new.tmax_offset == %(coverage)s.time_offset 

; 

DELETE FROM %(coverage)s 

WHERE new.kind_codes_id == %(coverage)s.kind_codes_id 

AND new.tmin_seconds == %(coverage)s.time_seconds 

AND new.tmin_offset == %(coverage)s.time_offset 

AND step == 0 

; 

DELETE FROM %(coverage)s 

WHERE new.kind_codes_id == %(coverage)s.kind_codes_id 

AND new.tmax_seconds == %(coverage)s.time_seconds 

AND new.tmax_offset == %(coverage)s.time_offset 

AND step == 0 

; 

END 

''')) 

 

c.execute(self._sql( 

''' 

CREATE TRIGGER IF NOT EXISTS %(db)s.%(nuts)s_remove_coverage 

BEFORE DELETE ON %(nuts)s FOR EACH ROW 

BEGIN 

INSERT OR IGNORE INTO %(coverage)s VALUES 

(old.kind_codes_id, old.tmin_seconds, old.tmin_offset, 0) 

; 

UPDATE %(coverage)s 

SET step = step - 1 

WHERE old.kind_codes_id == %(coverage)s.kind_codes_id 

AND old.tmin_seconds == %(coverage)s.time_seconds 

AND old.tmin_offset == %(coverage)s.time_offset 

; 

INSERT OR IGNORE INTO %(coverage)s VALUES 

(old.kind_codes_id, old.tmax_seconds, old.tmax_offset, 0) 

; 

UPDATE %(coverage)s 

SET step = step + 1 

WHERE old.kind_codes_id == %(coverage)s.kind_codes_id 

AND old.tmax_seconds == %(coverage)s.time_seconds 

AND old.tmax_offset == %(coverage)s.time_offset 

; 

DELETE FROM %(coverage)s 

WHERE old.kind_codes_id == %(coverage)s.kind_codes_id 

AND old.tmin_seconds == %(coverage)s.time_seconds 

AND old.tmin_offset == %(coverage)s.time_offset 

AND step == 0 

; 

DELETE FROM %(coverage)s 

WHERE old.kind_codes_id == %(coverage)s.kind_codes_id 

AND old.tmax_seconds == %(coverage)s.time_seconds 

AND old.tmax_offset == %(coverage)s.time_offset 

AND step == 0 

; 

END 

''')) 

 

def _delete(self): 

'''Delete database tables associated with this Squirrel.''' 

 

for s in ''' 

DROP TRIGGER %(db)s.%(nuts)s_delete_nuts; 

DROP TRIGGER %(db)s.%(nuts)s_delete_nuts2; 

DROP TRIGGER %(db)s.%(file_states)s_delete_files; 

DROP TRIGGER %(db)s.%(nuts)s_inc_kind_codes; 

DROP TRIGGER %(db)s.%(nuts)s_dec_kind_codes; 

DROP TABLE %(db)s.%(nuts)s; 

DROP TABLE %(db)s.%(kind_codes_count)s; 

DROP TRIGGER IF EXISTS %(db)s.%(nuts)s_add_coverage; 

DROP TRIGGER IF EXISTS %(db)s.%(nuts)s_remove_coverage; 

DROP TABLE IF EXISTS %(db)s.%(coverage)s; 

'''.strip().splitlines(): 

 

self._conn.execute(self._sql(s)) 

 

Selection._delete(self) 

 

self._conn.commit() 

 

def add(self, 

paths, 

kinds=None, 

format='detect', 

check=True, 

progress_viewer='terminal'): 

 

''' 

Add files to the selection. 

 

:param paths: 

Iterator yielding paths to files or directories to be added to the 

selection. Recurses into directories. If given a ``str``, it 

is treated as a single path to be added. 

:type paths: 

:py:class:`list` of :py:class:`str` 

:param kinds: 

Content types to be made available through the Squirrel selection. 

By default, all known content types are accepted. 

:type kinds: 

:py:class:`list` of :py:class:`str` 

:param format: 

File format identifier or ``'detect'`` to enable auto-detection. 

:type format: 

:py:class:`str` 

 

Complexity: O(log N) 

''' 

 

if isinstance(kinds, str): 

kinds = (kinds,) 

 

if isinstance(paths, str): 

paths = [paths] 

 

kind_mask = model.to_kind_mask(kinds) 

 

with progress.view(progress_viewer): 

Selection.add( 

self, util.iter_select_files( 

paths, 

show_progress=False, 

pass_through=lambda path: path.startswith('virtual:') 

), kind_mask, format) 

 

self._load(check) 

self._update_nuts() 

 

def reload(self): 

''' 

Check for modifications and reindex modified files. 

 

Based on file modification times. 

''' 

 

self._set_file_states_force_check() 

self._load(check=True) 

self._update_nuts() 

 

def add_virtual(self, nuts, virtual_paths=None): 

''' 

Add content which is not backed by files. 

 

:param nuts: 

Content pieces to be added. 

:type nuts: 

iterator yielding :py:class:`~pyrocko.squirrel.Nut` objects 

 

:param virtual_paths: 

List of virtual paths to prevent creating a temporary list of the 

nuts while aggregating the file paths for the selection. 

:type virtual_paths: 

:py:class:`list` of :py:class:`str` 

 

Stores to the main database and the selection. 

''' 

 

if isinstance(virtual_paths, str): 

virtual_paths = [virtual_paths] 

 

if virtual_paths is None: 

nuts_add = [] 

virtual_paths = set() 

for nut in nuts: 

virtual_paths.add(nut.file_path) 

nuts_add.append(nut) 

else: 

nuts_add = nuts 

 

Selection.add(self, virtual_paths) 

self.get_database().dig(nuts_add) 

self._update_nuts() 

 

def _load(self, check): 

for _ in io.iload( 

self, 

content=[], 

skip_unchanged=True, 

check=check): 

pass 

 

def _update_nuts(self): 

task = make_task('Aggregating selection') 

self._conn.set_progress_handler(task.update, 100000) 

nrows = self._conn.execute(self._sql( 

''' 

INSERT INTO %(db)s.%(nuts)s 

SELECT NULL, 

nuts.file_id, nuts.file_segment, nuts.file_element, 

nuts.kind_id, nuts.kind_codes_id, 

nuts.tmin_seconds, nuts.tmin_offset, 

nuts.tmax_seconds, nuts.tmax_offset, 

nuts.kscale 

FROM %(db)s.%(file_states)s 

INNER JOIN nuts 

ON %(db)s.%(file_states)s.file_id == nuts.file_id 

INNER JOIN kind_codes 

ON nuts.kind_codes_id == 

kind_codes.kind_codes_id 

WHERE %(db)s.%(file_states)s.file_state != 2 

AND (((1 << kind_codes.kind_id) 

& %(db)s.%(file_states)s.kind_mask) != 0) 

''')).rowcount 

 

task.update(nrows) 

self._set_file_states_known() 

self._conn.set_progress_handler(None, 0) 

task.done() 

 

def add_source(self, source): 

''' 

Add remote resource. 

 

:param source: 

Remote data access client instance. 

:type source: 

subclass of :py:class:`~pyrocko.squirrel.Source` 

''' 

 

self._sources.append(source) 

source.setup(self) 

 

def add_fdsn(self, *args, **kwargs): 

''' 

Add FDSN site for transparent remote data access. 

 

Arguments are passed to :py:class:`~pyrocko.squirrel.FDSNSource`. 

''' 

 

self.add_source(fdsn.FDSNSource(*args, **kwargs)) 

 

def add_catalog(self, *args, **kwargs): 

''' 

Add online catalog for transparent event data access. 

 

Arguments are passed to :py:class:`~pyrocko.squirrel.CatalogSource`. 

''' 

 

self.add_source(catalog.CatalogSource(*args, **kwargs)) 

 

def _get_selection_args( 

self, obj=None, tmin=None, tmax=None, time=None, codes=None): 

 

if time is not None: 

tmin = time 

tmax = time 

 

if obj is not None: 

tmin = tmin if tmin is not None else obj.tmin 

tmax = tmax if tmax is not None else obj.tmax 

codes = codes if codes is not None else obj.codes 

 

if isinstance(codes, str): 

codes = tuple(codes.split('.')) 

 

return tmin, tmax, codes 

 

def iter_nuts( 

self, kind=None, tmin=None, tmax=None, codes=None, naiv=False, 

kind_codes_ids=None): 

 

''' 

Iterate content entities matching given constraints. 

 

:param kind: 

Content kind (or kinds) to extract. 

:type kind: 

:py:class:`str`, :py:class:`list` of :py:class:`str` 

 

:param tmin: 

Start time of query interval. 

:type tmin: 

timestamp 

 

:param tmax: 

End time of query interval. 

:type tmax: 

timestamp 

 

:param codes: 

Pattern of content codes to be matched. 

:type codes: 

:py:class:`tuple` of :py:class:`str` 

 

:param naiv: 

Bypass time span lookup through indices (slow, for testing). 

:type naiv: 

:py:class:`bool` 

 

:param kind_codes_ids: 

Kind-codes IDs of contents to be retrieved (internal use). 

:type kind_codes_ids: 

:py:class:`list` of :py:class:`str` 

 

Complexity: O(log N) for the time selection part due to heavy use of 

database indices. 

 

Yields :py:class:`~pyrocko.squirrel.Nut` objects representing the 

intersecting content. 

 

Query time span is treated as a half-open interval ``[tmin, tmax)``. 

However, if ``tmin`` equals ``tmax``, the edge logics are modified to 

closed-interval so that content intersecting with the time instant ``t 

= tmin = tmax`` is returned (otherwise nothing would be returned as 

``[t, t)`` never matches anything). 

 

Time spans of content entities to be matched are also treated as half 

open intervals, e.g. content span ``[0, 1)`` is matched by query span 

``[0, 1)`` but not by ``[-1, 0)`` or ``[1, 2)``. Also here, logics are 

modified to closed-interval when the content time span is an empty 

interval, i.e. to indicate a time instant. E.g. time instant 0 is 

matched by ``[0, 1)`` but not by ``[-1, 0)`` or ``[1, 2)``. 

''' 

 

if not isinstance(kind, str): 

if kind is None: 

kind = model.g_content_kinds 

for kind_ in kind: 

for nut in self.iter_nuts(kind_, tmin, tmax, codes): 

yield nut 

 

return 

 

extra_cond = [] 

tmin_cond = [] 

args = [] 

 

if tmin is not None or tmax is not None: 

assert kind is not None 

if tmin is None: 

tmin = self.get_time_span()[0] 

if tmax is None: 

tmax = self.get_time_span()[1] + 1.0 

 

tmin_seconds, tmin_offset = model.tsplit(tmin) 

tmax_seconds, tmax_offset = model.tsplit(tmax) 

if naiv: 

extra_cond.append('%(db)s.%(nuts)s.tmin_seconds <= ?') 

args.append(tmax_seconds) 

else: 

tscale_edges = model.tscale_edges 

 

for kscale in range(tscale_edges.size + 1): 

if kscale != tscale_edges.size: 

tscale = int(tscale_edges[kscale]) 

tmin_cond.append(''' 

(%(db)s.%(nuts)s.kind_id = ? 

AND %(db)s.%(nuts)s.kscale == ? 

AND %(db)s.%(nuts)s.tmin_seconds BETWEEN ? AND ?) 

''') 

args.extend( 

(to_kind_id(kind), kscale, 

tmin_seconds - tscale - 1, tmax_seconds + 1)) 

 

else: 

tmin_cond.append(''' 

(%(db)s.%(nuts)s.kind_id == ? 

AND %(db)s.%(nuts)s.kscale == ? 

AND %(db)s.%(nuts)s.tmin_seconds <= ?) 

''') 

 

args.extend( 

(to_kind_id(kind), kscale, tmax_seconds + 1)) 

 

extra_cond.append('%(db)s.%(nuts)s.tmax_seconds >= ?') 

args.append(tmin_seconds) 

 

elif kind is not None: 

extra_cond.append('kind_codes.kind_id == ?') 

args.append(to_kind_id(kind)) 

 

if codes is not None: 

pats = codes_patterns_for_kind(kind, codes) 

if pats: 

extra_cond.append( 

' ( %s ) ' % ' OR '.join( 

('kind_codes.codes GLOB ?',) * len(pats))) 

args.extend(separator.join(pat) for pat in pats) 

 

if kind_codes_ids is not None: 

extra_cond.append( 

' ( kind_codes.kind_codes_id IN ( %s ) ) ' % ', '.join( 

'?'*len(kind_codes_ids))) 

 

args.extend(kind_codes_ids) 

 

sql = (''' 

SELECT 

files.path, 

files.format, 

files.mtime, 

files.size, 

%(db)s.%(nuts)s.file_segment, 

%(db)s.%(nuts)s.file_element, 

kind_codes.kind_id, 

kind_codes.codes, 

%(db)s.%(nuts)s.tmin_seconds, 

%(db)s.%(nuts)s.tmin_offset, 

%(db)s.%(nuts)s.tmax_seconds, 

%(db)s.%(nuts)s.tmax_offset, 

kind_codes.deltat 

FROM files 

INNER JOIN %(db)s.%(nuts)s 

ON files.file_id == %(db)s.%(nuts)s.file_id 

INNER JOIN kind_codes 

ON %(db)s.%(nuts)s.kind_codes_id == kind_codes.kind_codes_id 

''') 

 

cond = [] 

if tmin_cond: 

cond.append(' ( ' + ' OR '.join(tmin_cond) + ' ) ') 

 

cond.extend(extra_cond) 

 

if cond: 

sql += ''' WHERE ''' + ' AND '.join(cond) 

 

sql = self._sql(sql) 

if tmin is None and tmax is None: 

for row in self._conn.execute(sql, args): 

nut = model.Nut(values_nocheck=row) 

yield nut 

else: 

assert tmin is not None and tmax is not None 

if tmin == tmax: 

for row in self._conn.execute(sql, args): 

nut = model.Nut(values_nocheck=row) 

if (nut.tmin <= tmin < nut.tmax) \ 

or (nut.tmin == nut.tmax and tmin == nut.tmin): 

 

yield nut 

else: 

for row in self._conn.execute(sql, args): 

nut = model.Nut(values_nocheck=row) 

if (tmin < nut.tmax and nut.tmin < tmax) \ 

or (nut.tmin == nut.tmax 

and tmin <= nut.tmin < tmax): 

 

yield nut 

 

def get_nuts(self, *args, **kwargs): 

''' 

Get content entities matching given constraints. 

 

Like :py:meth:`iter_nuts` but returns results as a list. 

''' 

 

return list(self.iter_nuts(*args, **kwargs)) 

 

def _split_nuts( 

self, kind, tmin=None, tmax=None, codes=None, path=None): 

 

tmin_seconds, tmin_offset = model.tsplit(tmin) 

tmax_seconds, tmax_offset = model.tsplit(tmax) 

 

tscale_edges = model.tscale_edges 

 

tmin_cond = [] 

args = [] 

for kscale in range(tscale_edges.size + 1): 

if kscale != tscale_edges.size: 

tscale = int(tscale_edges[kscale]) 

tmin_cond.append(''' 

(%(db)s.%(nuts)s.kind_id = ? 

AND %(db)s.%(nuts)s.kscale == ? 

AND %(db)s.%(nuts)s.tmin_seconds BETWEEN ? AND ?) 

''') 

args.extend( 

(to_kind_id(kind), kscale, 

tmin_seconds - tscale - 1, tmax_seconds + 1)) 

 

else: 

tmin_cond.append(''' 

(%(db)s.%(nuts)s.kind_id == ? 

AND %(db)s.%(nuts)s.kscale == ? 

AND %(db)s.%(nuts)s.tmin_seconds <= ?) 

''') 

 

args.extend( 

(to_kind_id(kind), kscale, tmax_seconds + 1)) 

 

extra_cond = ['%(db)s.%(nuts)s.tmax_seconds >= ?'] 

args.append(tmin_seconds) 

if codes is not None: 

pats = codes_patterns_for_kind(kind, codes) 

if pats: 

extra_cond.append( 

' ( %s ) ' % ' OR '.join( 

('kind_codes.codes GLOB ?',) * len(pats))) 

args.extend(separator.join(pat) for pat in pats) 

 

if path is not None: 

extra_cond.append('files.path == ?') 

args.append(path) 

 

sql = self._sql(''' 

SELECT 

%(db)s.%(nuts)s.nut_id, 

%(db)s.%(nuts)s.tmin_seconds, 

%(db)s.%(nuts)s.tmin_offset, 

%(db)s.%(nuts)s.tmax_seconds, 

%(db)s.%(nuts)s.tmax_offset, 

kind_codes.deltat 

FROM files 

INNER JOIN %(db)s.%(nuts)s 

ON files.file_id == %(db)s.%(nuts)s.file_id 

INNER JOIN kind_codes 

ON %(db)s.%(nuts)s.kind_codes_id == kind_codes.kind_codes_id 

WHERE ( ''' + ' OR '.join(tmin_cond) + ''' ) 

AND ''' + ' AND '.join(extra_cond)) 

 

insert = [] 

delete = [] 

for row in self._conn.execute(sql, args): 

nut_id, nut_tmin_seconds, nut_tmin_offset, \ 

nut_tmax_seconds, nut_tmax_offset, nut_deltat = row 

 

nut_tmin = model.tjoin( 

nut_tmin_seconds, nut_tmin_offset, nut_deltat) 

nut_tmax = model.tjoin( 

nut_tmax_seconds, nut_tmax_offset, nut_deltat) 

 

if nut_tmin < tmax and tmin < nut_tmax: 

if nut_tmin < tmin: 

insert.append(( 

nut_tmin_seconds, nut_tmin_offset, 

tmin_seconds, tmin_offset, 

model.tscale_to_kscale( 

tmin_seconds - nut_tmin_seconds), 

nut_id)) 

 

if tmax < nut_tmax: 

insert.append(( 

tmax_seconds, tmax_offset, 

nut_tmax_seconds, nut_tmax_offset, 

model.tscale_to_kscale( 

nut_tmax_seconds - tmax_seconds), 

nut_id)) 

 

delete.append((nut_id,)) 

 

sql_add = ''' 

INSERT INTO %(db)s.%(nuts)s ( 

file_id, file_segment, file_element, kind_id, 

kind_codes_id, tmin_seconds, tmin_offset, 

tmax_seconds, tmax_offset, kscale ) 

SELECT 

file_id, file_segment, file_element, 

kind_id, kind_codes_id, ?, ?, ?, ?, ? 

FROM %(db)s.%(nuts)s 

WHERE nut_id == ? 

''' 

self._conn.executemany(self._sql(sql_add), insert) 

 

sql_delete = '''DELETE FROM %(db)s.%(nuts)s WHERE nut_id == ?''' 

self._conn.executemany(self._sql(sql_delete), delete) 

 

def get_time_span(self): 

''' 

Get time interval over all content in selection. 

 

Complexity O(1), independent of the number of nuts. 

 

:returns: (tmin, tmax) 

''' 

sql = self._sql(''' 

SELECT MIN(tmin_seconds), MIN(tmin_offset) 

FROM %(db)s.%(nuts)s WHERE 

tmin_seconds == (SELECT MIN(tmin_seconds) FROM %(db)s.%(nuts)s) 

''') 

tmin = None 

for tmin_seconds, tmin_offset in self._conn.execute(sql): 

tmin = model.tjoin(tmin_seconds, tmin_offset, 0.0) 

 

sql = self._sql(''' 

SELECT MAX(tmax_seconds), MAX(tmax_offset) 

FROM %(db)s.%(nuts)s WHERE 

tmax_seconds == (SELECT MAX(tmax_seconds) FROM %(db)s.%(nuts)s) 

''') 

tmax = None 

for (tmax_seconds, tmax_offset) in self._conn.execute(sql): 

tmax = model.tjoin(tmax_seconds, tmax_offset, 0.0) 

 

return tmin, tmax 

 

def get_deltat_span(self, kind): 

''' 

Get min and max sampling interval of all content of given kind. 

 

:param kind: Content kind 

:type kind: :py:class:`str` 

 

:returns: (deltat_min, deltat_max) 

''' 

 

deltats = [ 

deltat for deltat in self.get_deltats(kind) 

if deltat is not None] 

 

if deltats: 

return min(deltats), max(deltats) 

else: 

return None, None 

 

def get_waveform_deltat_span(self): 

return self.get_deltat_span('waveform') 

 

def iter_kinds(self, codes=None): 

''' 

Iterate over content types available in selection. 

 

:param codes: if given, get kinds only for selected codes identifier 

 

Complexity: O(1), independent of number of nuts 

''' 

 

return self._database._iter_kinds( 

codes=codes, 

kind_codes_count='%(db)s.%(kind_codes_count)s' % self._names) 

 

def iter_deltats(self, kind=None): 

''' 

Iterate over sampling intervals available in selection. 

 

:param kind: if given, get sampling intervals only for a given content 

type 

:type kind: :py:class:`str` 

 

Complexity: O(1), independent of number of nuts 

''' 

return self._database._iter_deltats( 

kind=kind, 

kind_codes_count='%(db)s.%(kind_codes_count)s' % self._names) 

 

def iter_codes(self, kind=None): 

''' 

Iterate over content identifier code sequences available in selection. 

 

:param kind: if given, get codes only for a given content type 

:type kind: :py:class:`str` 

 

Complexity: O(1), independent of number of nuts 

''' 

return self._database._iter_codes( 

kind=kind, 

kind_codes_count='%(db)s.%(kind_codes_count)s' % self._names) 

 

def iter_counts(self, kind=None): 

''' 

Iterate over number of occurrences of any (kind, codes) combination. 

 

:param kind: if given, get counts only for selected content type 

 

Yields tuples ``((kind, codes), count)`` 

 

Complexity: O(1), independent of number of nuts 

''' 

return self._database._iter_counts( 

kind=kind, 

kind_codes_count='%(db)s.%(kind_codes_count)s' % self._names) 

 

def get_kinds(self, codes=None): 

''' 

Get content types available in selection. 

 

:param codes: if given, get kinds only for selected codes identifier 

 

Complexity: O(1), independent of number of nuts 

 

:returns: sorted list of available content types 

''' 

return sorted(list(self.iter_kinds(codes=codes))) 

 

def get_deltats(self, kind=None): 

''' 

Get sampling intervals available in selection. 

 

:param kind: if given, get codes only for selected content type 

 

Complexity: O(1), independent of number of nuts 

 

:returns: sorted list of available sampling intervals 

''' 

return sorted(list(self.iter_deltats(kind=kind))) 

 

def get_codes(self, kind=None): 

''' 

Get identifier code sequences available in selection. 

 

:param kind: if given, get codes only for selected content type 

 

Complexity: O(1), independent of number of nuts 

 

:returns: sorted list of available codes as tuples of strings 

''' 

return sorted(list(self.iter_codes(kind=kind))) 

 

def get_counts(self, kind=None): 

''' 

Get number of occurrences of any (kind, codes) combination. 

 

:param kind: if given, get codes only for selected content type 

 

Complexity: O(1), independent of number of nuts 

 

:returns: ``dict`` with ``counts[kind][codes] or ``counts[codes]`` 

if kind is not ``None`` 

''' 

d = {} 

for (k, codes, deltat), count in self.iter_counts(): 

if k not in d: 

v = d[k] = {} 

else: 

v = d[k] 

 

if codes not in v: 

v[codes] = 0 

 

v[codes] += count 

 

if kind is not None: 

return d[kind] 

else: 

return d 

 

def resolve_kind_codes(self, kind, codes_list): 

 

args = [to_kind_id(kind)] 

pats = [] 

for codes in codes_list: 

pats.extend(codes_patterns_for_kind(kind, codes)) 

 

codes_cond = ' ( %s ) ' % ' OR '.join( 

('kind_codes.codes GLOB ?',) * len(pats)) 

 

args.extend(separator.join(pat) for pat in pats) 

 

sql = self._sql(''' 

SELECT kind_codes_id, codes, deltat FROM kind_codes 

WHERE 

kind_id == ? 

AND ''' + codes_cond) 

 

return list(map(list, self._conn.execute(sql, args))) 

 

def update(self, constraint=None, **kwargs): 

''' 

Update inventory of remote content for a given selection. 

 

This function triggers all attached remote sources, to check for 

updates in the metadata. The sources will only submit queries when 

their expiration date has passed, or if the selection spans into 

previously unseen times or areas. 

''' 

 

if constraint is None: 

constraint = client.Constraint(**kwargs) 

 

for source in self._sources: 

source.update_channel_inventory(self, constraint) 

source.update_event_inventory(self, constraint) 

 

def update_waveform_promises(self, constraint=None, **kwargs): 

 

if constraint is None: 

constraint = client.Constraint(**kwargs) 

 

for source in self._sources: 

source.update_waveform_promises(self, constraint) 

 

def get_nfiles(self): 

''' 

Get number of files in selection. 

''' 

 

sql = self._sql('''SELECT COUNT(*) FROM %(db)s.%(file_states)s''') 

for row in self._conn.execute(sql): 

return row[0] 

 

def get_nnuts(self): 

''' 

Get number of nuts in selection. 

''' 

 

sql = self._sql('''SELECT COUNT(*) FROM %(db)s.%(nuts)s''') 

for row in self._conn.execute(sql): 

return row[0] 

 

def get_total_size(self): 

''' 

Get aggregated file size available in selection. 

''' 

 

sql = self._sql(''' 

SELECT SUM(files.size) FROM %(db)s.%(file_states)s 

INNER JOIN files 

ON %(db)s.%(file_states)s.file_id = files.file_id 

''') 

 

for row in self._conn.execute(sql): 

return row[0] or 0 

 

def get_stats(self): 

''' 

Get statistics on contents available through this selection. 

''' 

 

tmin, tmax = self.get_time_span() 

 

return SquirrelStats( 

nfiles=self.get_nfiles(), 

nnuts=self.get_nnuts(), 

kinds=self.get_kinds(), 

codes=self.get_codes(), 

total_size=self.get_total_size(), 

counts=self.get_counts(), 

tmin=tmin, 

tmax=tmax) 

 

def get_content(self, nut, cache='default', accessor='default'): 

''' 

Get and possibly load full content for a given index entry from file. 

 

Loads the actual content objects (channel, station, waveform, ...) from 

file. For efficiency sibling content (all stuff in the same file 

segment) will also be loaded as a side effect. The loaded contents are 

cached in the Squirrel object. 

''' 

 

content_cache = self._content_caches[cache] 

if not content_cache.has(nut): 

 

for nut_loaded in io.iload( 

nut.file_path, 

segment=nut.file_segment, 

format=nut.file_format, 

database=self._database): 

 

content_cache.put(nut_loaded) 

 

try: 

return content_cache.get(nut, accessor) 

except KeyError: 

raise error.NotAvailable( 

'Unable to retrieve content: %s, %s, %s, %s' % nut.key) 

 

def advance_accessor(self, accessor, cache=None): 

for cache_ in ( 

self._content_caches.keys() if cache is None else [cache]): 

 

self._content_caches[cache_].advance_accessor(accessor) 

 

def clear_accessor(self, accessor, cache=None): 

for cache_ in ( 

self._content_caches.keys() if cache is None else [cache]): 

 

self._content_caches[cache_].clear_accessor(accessor) 

 

def check_duplicates(self, nuts): 

d = defaultdict(list) 

for nut in nuts: 

d[nut.codes].append(nut) 

 

for codes, group in d.items(): 

if len(group) > 1: 

logger.warn( 

'Multiple entries matching codes %s' 

% '.'.join(codes.split(separator))) 

 

def get_stations(self, *args, **kwargs): 

args = self._get_selection_args(*args, **kwargs) 

nuts = sorted( 

self.iter_nuts('station', *args), key=lambda nut: nut.dkey) 

self.check_duplicates(nuts) 

return [self.get_content(nut) for nut in nuts] 

 

def get_channels(self, *args, **kwargs): 

args = self._get_selection_args(*args, **kwargs) 

nuts = sorted( 

self.iter_nuts('channel', *args), key=lambda nut: nut.dkey) 

self.check_duplicates(nuts) 

return [self.get_content(nut) for nut in nuts] 

 

def get_events(self, *args, **kwargs): 

args = self._get_selection_args(*args, **kwargs) 

nuts = sorted( 

self.iter_nuts('event', *args), key=lambda nut: nut.dkey) 

self.check_duplicates(nuts) 

return [self.get_content(nut) for nut in nuts] 

 

def _redeem_promises(self, *args): 

 

tmin, tmax, _ = args 

 

waveforms = list(self.iter_nuts('waveform', *args)) 

promises = list(self.iter_nuts('waveform_promise', *args)) 

 

codes_to_avail = defaultdict(list) 

for nut in waveforms: 

codes_to_avail[nut.codes].append((nut.tmin, nut.tmax+nut.deltat)) 

 

def tts(x): 

if isinstance(x, tuple): 

return tuple(tts(e) for e in x) 

elif isinstance(x, list): 

return list(tts(e) for e in x) 

else: 

return util.time_to_str(x) 

 

orders = [] 

for promise in promises: 

waveforms_avail = codes_to_avail[promise.codes] 

for block_tmin, block_tmax in blocks( 

max(tmin, promise.tmin), 

min(tmax, promise.tmax), 

promise.deltat): 

 

orders.append( 

WaveformOrder( 

source_id=promise.file_path, 

codes=tuple(promise.codes.split(separator)), 

tmin=block_tmin, 

tmax=block_tmax, 

deltat=promise.deltat, 

gaps=gaps(waveforms_avail, block_tmin, block_tmax))) 

 

orders_noop, orders = lpick(lambda order: order.gaps, orders) 

 

order_keys_noop = set(order_key(order) for order in orders_noop) 

if len(order_keys_noop) != 0 or len(orders_noop) != 0: 

logger.info( 

'Waveform orders already satisified with cached/local data: ' 

'%i (%i)' % (len(order_keys_noop), len(orders_noop))) 

 

source_ids = [] 

sources = {} 

for source in self._sources: 

source_ids.append(source._source_id) 

sources[source._source_id] = source 

 

source_priority = dict( 

(source_id, i) for (i, source_id) in enumerate(source_ids)) 

 

order_groups = defaultdict(list) 

for order in orders: 

order_groups[order_key(order)].append(order) 

 

for k, order_group in order_groups.items(): 

order_group.sort( 

key=lambda order: source_priority[order.source_id]) 

 

if len(order_groups) != 0 or len(orders) != 0: 

logger.info( 

'Waveform orders standing for download: %i (%i)' 

% (len(order_groups), len(orders))) 

 

def release_order_group(order): 

del order_groups[order_key(order)] 

 

def split_promise(order): 

self._split_nuts( 

'waveform_promise', 

order.tmin, order.tmax, 

codes=order.codes, 

path=order.source_id) 

 

def noop(order): 

pass 

 

def success(order): 

release_order_group(order) 

split_promise(order) 

 

for order in orders_noop: 

split_promise(order) 

 

while order_groups: 

orders_now = [] 

empty = [] 

for k, order_group in order_groups.items(): 

try: 

orders_now.append(order_group.pop(0)) 

except IndexError: 

empty.append(k) 

 

for k in empty: 

del order_groups[k] 

 

by_source_id = defaultdict(list) 

for order in orders_now: 

by_source_id[order.source_id].append(order) 

 

# TODO: parallelize this loop 

for source_id in by_source_id: 

sources[source_id].download_waveforms( 

self, by_source_id[source_id], 

success=success, 

error_permanent=split_promise, 

error_temporary=noop) 

 

def get_waveform_nuts(self, *args, **kwargs): 

args = self._get_selection_args(*args, **kwargs) 

self._redeem_promises(*args) 

return sorted( 

self.iter_nuts('waveform', *args), key=lambda nut: nut.dkey) 

 

def get_waveforms(self, *args, **kwargs): 

nuts = self.get_waveform_nuts(*args, **kwargs) 

# self.check_duplicates(nuts) 

return [self.get_content(nut, 'waveform') for nut in nuts] 

 

def get_pyrocko_stations(self, *args, **kwargs): 

from pyrocko import model as pmodel 

 

by_nsl = defaultdict(lambda: (list(), list())) 

for station in self.get_stations(*args, **kwargs): 

sargs = station._get_pyrocko_station_args() 

nsl = sargs[1:4] 

by_nsl[nsl][0].append(sargs) 

 

for channel in self.get_channels(*args, **kwargs): 

sargs = channel._get_pyrocko_station_args() 

nsl = sargs[1:4] 

sargs_list, channels_list = by_nsl[nsl] 

sargs_list.append(sargs) 

channels_list.append(channel) 

 

pstations = [] 

nsls = list(by_nsl.keys()) 

nsls.sort() 

for nsl in nsls: 

sargs_list, channels_list = by_nsl[nsl] 

sargs = util.consistency_merge(sargs_list) 

 

by_c = defaultdict(list) 

for ch in channels_list: 

by_c[ch.channel].append(ch._get_pyrocko_channel_args()) 

 

chas = list(by_c.keys()) 

chas.sort() 

pchannels = [] 

for cha in chas: 

list_of_cargs = by_c[cha] 

cargs = util.consistency_merge(list_of_cargs) 

pchannels.append(pmodel.Channel( 

name=cargs[0], 

azimuth=cargs[1], 

dip=cargs[2])) 

 

pstations.append(pmodel.Station( 

network=sargs[0], 

station=sargs[1], 

location=sargs[2], 

lat=sargs[3], 

lon=sargs[4], 

elevation=sargs[5], 

depth=sargs[6] or 0.0, 

channels=pchannels)) 

 

return pstations 

 

@property 

def pile(self): 

if self._pile is None: 

self._pile = pile.Pile(self) 

 

return self._pile 

 

def snuffle(self): 

self.pile.snuffle() 

 

def gather_codes_keys(self, kind, gather, selector): 

return set( 

gather(codes) 

for codes in self.iter_codes(kind) 

if selector is None or selector(codes)) 

 

def __str__(self): 

return str(self.get_stats()) 

 

def get_coverage( 

self, kind, tmin=None, tmax=None, codes_list=None, limit=None): 

 

tmin_seconds, tmin_offset = model.tsplit(tmin) 

tmax_seconds, tmax_offset = model.tsplit(tmax) 

 

kdata_all = [] 

for pattern in codes_list: 

kdata = self.resolve_kind_codes(kind, [pattern]) 

for row in kdata: 

row[0:0] = [pattern] 

 

kdata_all.extend(kdata) 

 

kind_codes_ids = [x[1] for x in kdata_all] 

 

counts_at_tmin = {} 

if tmin is not None: 

for nut in self.iter_nuts( 

kind, tmin, tmin, kind_codes_ids=kind_codes_ids): 

 

codes = nut.codes 

if codes not in counts_at_tmin: 

counts_at_tmin[codes] = 0 

 

counts_at_tmin[codes] += 1 

 

coverage = [] 

for pattern, kind_codes_id, codes, deltat in kdata_all: 

entry = [pattern, codes, deltat, None, None, []] 

for i, order in [(0, 'ASC'), (1, 'DESC')]: 

sql = self._sql(''' 

SELECT 

time_seconds, 

time_offset 

FROM %(db)s.%(coverage)s 

WHERE 

kind_codes_id == ? 

ORDER BY 

kind_codes_id ''' + order + ''', 

time_seconds ''' + order + ''', 

time_offset ''' + order + ''' 

LIMIT 1 

''') 

 

for row in self._conn.execute(sql, [kind_codes_id]): 

entry[3+i] = model.tjoin(row[0], row[1], deltat) 

 

args = [kind_codes_id] 

 

sql_time = '' 

if tmin is not None: 

# intentionally < because (== tmin) is queried from nuts 

sql_time += ' AND ( ? < time_seconds ' \ 

'OR ( ? == time_seconds AND ? < time_offset ) ) ' 

args.extend([tmin_seconds, tmin_seconds, tmin_offset]) 

 

if tmax is not None: 

sql_time += ' AND ( time_seconds < ? ' \ 

'OR ( ? == time_seconds AND time_offset < ? ) ) ' 

args.extend([tmax_seconds, tmax_seconds, tmax_offset]) 

 

sql_limit = '' 

if limit is not None: 

sql_limit = ' LIMIT ?' 

args.append(limit) 

 

sql = self._sql(''' 

SELECT 

time_seconds, 

time_offset, 

step 

FROM %(db)s.%(coverage)s 

WHERE 

kind_codes_id == ? 

''' + sql_time + ''' 

ORDER BY 

kind_codes_id, 

time_seconds, 

time_offset 

''' + sql_limit) 

 

rows = list(self._conn.execute(sql, args)) 

 

if limit is not None and len(rows) == limit: 

entry[-1] = None 

else: 

counts = counts_at_tmin.get(codes, 0) 

if tmin is not None: 

entry[-1].append((tmin, counts)) 

 

for row in rows: 

t = model.tjoin(row[0], row[1], deltat) 

counts += row[2] 

entry[-1].append((t, counts)) 

 

if tmax is not None: 

entry[-1].append((tmax, counts)) 

 

coverage.append(entry) 

 

return coverage 

 

def print_tables(self, table_names=None, stream=None): 

''' 

Dump raw database tables in textual form (for debugging purposes). 

 

:param table_names: 

Names of tables to be dumped or ``None`` to dump all. 

:type table_names: 

:py:class:`list` of :py:class:`str` 

:param stream: 

Open file or ``None`` to dump to standard output. 

''' 

 

if stream is None: 

stream = sys.stdout 

 

if isinstance(table_names, str): 

table_names = [table_names] 

 

if table_names is None: 

table_names = [ 

'selection_file_states', 

'selection_nuts', 

'selection_kind_codes_count', 

'files', 'nuts', 'kind_codes', 'kind_codes_count'] 

 

m = { 

'selection_file_states': '%(db)s.%(file_states)s', 

'selection_nuts': '%(db)s.%(nuts)s', 

'selection_kind_codes_count': '%(db)s.%(kind_codes_count)s', 

'files': 'files', 

'nuts': 'nuts', 

'kind_codes': 'kind_codes', 

'kind_codes_count': 'kind_codes_count'} 

 

for table_name in table_names: 

self._database.print_table( 

m[table_name] % self._names, stream=stream) 

 

 

class DatabaseStats(Object): 

''' 

Container to hold statistics about contents cached in meta-information db. 

''' 

 

nfiles = Int.T( 

help='number of files in database') 

nnuts = Int.T( 

help='number of index nuts in database') 

codes = List.T( 

Tuple.T(content_t=String.T()), 

help='available code sequences in database, e.g. ' 

'(agency, network, station, location) for stations nuts.') 

kinds = List.T( 

String.T(), 

help='available content types in database') 

total_size = Int.T( 

help='aggregated file size of files referenced in database') 

counts = Dict.T( 

String.T(), Dict.T(Tuple.T(content_t=String.T()), Int.T()), 

help='breakdown of how many nuts of any content type and code ' 

'sequence are available in database, ``counts[kind][codes]``') 

 

def __str__(self): 

kind_counts = dict( 

(kind, sum(self.counts[kind].values())) for kind in self.kinds) 

 

codes = ['.'.join(x) for x in self.codes] 

 

if len(codes) > 20: 

scodes = '\n' + util.ewrap(codes[:10], indent=' ') \ 

+ '\n [%i more]\n' % (len(codes) - 20) \ 

+ util.ewrap(codes[-10:], indent=' ') 

else: 

scodes = '\n' + util.ewrap(codes, indent=' ') \ 

if codes else '<none>' 

 

s = ''' 

Available codes: %s 

Number of files: %i 

Total size of known files: %s 

Number of index nuts: %i 

Available content kinds: %s''' % ( 

scodes, 

self.nfiles, 

util.human_bytesize(self.total_size), 

self.nnuts, 

', '.join('%s: %i' % ( 

kind, kind_counts[kind]) for kind in sorted(self.kinds))) 

 

return s 

 

 

g_databases = {} 

 

 

def get_database(path=None): 

path = os.path.abspath(path) 

if path not in g_databases: 

g_databases[path] = Database(path) 

 

return g_databases[path] 

 

 

class Database(object): 

''' 

Shared meta-information database used by Squirrel. 

''' 

 

def __init__(self, database_path=':memory:', log_statements=False): 

self._database_path = database_path 

try: 

util.ensuredirs(database_path) 

logger.debug('Opening connection to database: %s' % database_path) 

self._conn = sqlite3.connect(database_path) 

except sqlite3.OperationalError: 

raise error.SquirrelError( 

'Cannot connect to database: %s' % database_path) 

 

self._conn.text_factory = str 

self._tables = {} 

 

self._initialize_db() 

self._need_commit = False 

if log_statements: 

self._conn.set_trace_callback(self._log_statement) 

 

def _log_statement(self, statement): 

logger.debug(statement) 

 

def get_connection(self): 

return self._conn 

 

def _register_table(self, s): 

m = re.search(r'(\S+)\s*\(([^)]+)\)', s) 

table_name = m.group(1) 

dtypes = m.group(2) 

table_header = [] 

for dele in dtypes.split(','): 

table_header.append(dele.split()[:2]) 

 

self._tables[table_name] = table_header 

 

return s 

 

def _initialize_db(self): 

c = self._conn 

with c: 

if 1 == len(list( 

c.execute( 

''' 

SELECT name FROM sqlite_master 

WHERE type = 'table' AND name = '{files}' 

'''))): 

return 

 

c.execute( 

'''PRAGMA recursive_triggers = true''') 

 

c.execute(self._register_table( 

''' 

CREATE TABLE IF NOT EXISTS files ( 

file_id integer PRIMARY KEY, 

path text, 

format text, 

mtime float, 

size integer) 

''')) 

 

c.execute( 

''' 

CREATE UNIQUE INDEX IF NOT EXISTS index_files_path 

ON files (path) 

''') 

 

c.execute(self._register_table( 

''' 

CREATE TABLE IF NOT EXISTS nuts ( 

nut_id integer PRIMARY KEY AUTOINCREMENT, 

file_id integer, 

file_segment integer, 

file_element integer, 

kind_id integer, 

kind_codes_id integer, 

tmin_seconds integer, 

tmin_offset integer, 

tmax_seconds integer, 

tmax_offset integer, 

kscale integer) 

''')) 

 

c.execute( 

''' 

CREATE UNIQUE INDEX IF NOT EXISTS index_nuts_file_element 

ON nuts (file_id, file_segment, file_element) 

''') 

 

c.execute(self._register_table( 

''' 

CREATE TABLE IF NOT EXISTS kind_codes ( 

kind_codes_id integer PRIMARY KEY, 

kind_id integer, 

codes text, 

deltat float) 

''')) 

 

c.execute( 

''' 

CREATE UNIQUE INDEX IF NOT EXISTS index_kind_codes 

ON kind_codes (kind_id, codes, deltat) 

''') 

 

c.execute(self._register_table( 

''' 

CREATE TABLE IF NOT EXISTS kind_codes_count ( 

kind_codes_id integer PRIMARY KEY, 

count integer) 

''')) 

 

c.execute( 

''' 

CREATE INDEX IF NOT EXISTS index_nuts_file_id 

ON nuts (file_id) 

''') 

 

c.execute( 

''' 

CREATE TRIGGER IF NOT EXISTS delete_nuts_on_delete_file 

BEFORE DELETE ON files FOR EACH ROW 

BEGIN 

DELETE FROM nuts where file_id == old.file_id; 

END 

''') 

 

# trigger only on size to make silent update of mtime possible 

c.execute( 

''' 

CREATE TRIGGER IF NOT EXISTS delete_nuts_on_update_file 

BEFORE UPDATE OF size ON files FOR EACH ROW 

BEGIN 

DELETE FROM nuts where file_id == old.file_id; 

END 

''') 

 

c.execute( 

''' 

CREATE TRIGGER IF NOT EXISTS increment_kind_codes 

BEFORE INSERT ON nuts FOR EACH ROW 

BEGIN 

INSERT OR IGNORE INTO kind_codes_count 

VALUES (new.kind_codes_id, 0); 

UPDATE kind_codes_count 

SET count = count + 1 

WHERE new.kind_codes_id == kind_codes_id; 

END 

''') 

 

c.execute( 

''' 

CREATE TRIGGER IF NOT EXISTS decrement_kind_codes 

BEFORE DELETE ON nuts FOR EACH ROW 

BEGIN 

UPDATE kind_codes_count 

SET count = count - 1 

WHERE old.kind_codes_id == kind_codes_id; 

END 

''') 

 

def dig(self, nuts): 

''' 

Store or update content meta-information. 

 

Given ``nuts`` are assumed to represent an up-to-date and complete 

inventory of a set of files. Any old information about these files is 

first pruned from the database (via database triggers). If such content 

is part of a live selection, it is also removed there. Then the new 

content meta-information is inserted into the main database. The 

content is not automatically inserted into the live selections again. 

It is in the responsibility of the selection object to perform this 

step. 

''' 

 

nuts = list(nuts) 

 

if not nuts: 

return 

 

c = self._conn.cursor() 

files = set() 

kind_codes = set() 

for nut in nuts: 

files.add(( 

nut.file_path, 

nut.file_format, 

nut.file_mtime, 

nut.file_size)) 

kind_codes.add((nut.kind_id, nut.codes, nut.deltat or 0.0)) 

 

c.executemany( 

'INSERT OR IGNORE INTO files VALUES (NULL,?,?,?,?)', files) 

 

c.executemany( 

'''UPDATE files SET 

format = ?, mtime = ?, size = ? 

WHERE path == ? 

''', 

((x[1], x[2], x[3], x[0]) for x in files)) 

 

c.executemany( 

'INSERT OR IGNORE INTO kind_codes VALUES (NULL,?,?,?)', kind_codes) 

 

c.executemany( 

''' 

INSERT INTO nuts VALUES 

(NULL, ( 

SELECT file_id FROM files 

WHERE path == ? 

),?,?,?, 

( 

SELECT kind_codes_id FROM kind_codes 

WHERE kind_id == ? AND codes == ? AND deltat == ? 

), ?,?,?,?,?) 

''', 

((nut.file_path, nut.file_segment, nut.file_element, 

nut.kind_id, 

nut.kind_id, nut.codes, nut.deltat or 0.0, 

nut.tmin_seconds, nut.tmin_offset, 

nut.tmax_seconds, nut.tmax_offset, 

nut.kscale) for nut in nuts)) 

 

self._need_commit = True 

c.close() 

 

def undig(self, path): 

sql = ''' 

SELECT 

files.path, 

files.format, 

files.mtime, 

files.size, 

nuts.file_segment, 

nuts.file_element, 

kind_codes.kind_id, 

kind_codes.codes, 

nuts.tmin_seconds, 

nuts.tmin_offset, 

nuts.tmax_seconds, 

nuts.tmax_offset, 

kind_codes.deltat 

FROM files 

INNER JOIN nuts ON files.file_id = nuts.file_id 

INNER JOIN kind_codes 

ON nuts.kind_codes_id == kind_codes.kind_codes_id 

WHERE path == ? 

''' 

 

return [model.Nut(values_nocheck=row) 

for row in self._conn.execute(sql, (path,))] 

 

def undig_all(self): 

sql = ''' 

SELECT 

files.path, 

files.format, 

files.mtime, 

files.size, 

nuts.file_segment, 

nuts.file_element, 

kind_codes.kind_id, 

kind_codes.codes, 

nuts.tmin_seconds, 

nuts.tmin_offset, 

nuts.tmax_seconds, 

nuts.tmax_offset, 

kind_codes.deltat 

FROM files 

INNER JOIN nuts ON files.file_id == nuts.file_id 

INNER JOIN kind_codes 

ON nuts.kind_codes_id == kind_codes.kind_codes_id 

''' 

 

nuts = [] 

path = None 

for values in self._conn.execute(sql): 

if path is not None and values[0] != path: 

yield path, nuts 

nuts = [] 

 

if values[1] is not None: 

nuts.append(model.Nut(values_nocheck=values)) 

 

path = values[0] 

 

if path is not None: 

yield path, nuts 

 

def undig_many(self, paths): 

selection = self.new_selection(paths) 

 

for (_, path), nuts in selection.undig_grouped(): 

yield path, nuts 

 

del selection 

 

def new_selection(self, paths=None): 

selection = Selection(self) 

if paths: 

selection.add(paths) 

return selection 

 

def commit(self): 

if self._need_commit: 

self._conn.commit() 

self._need_commit = False 

 

def undig_content(self, nut): 

return None 

 

def remove(self, path): 

''' 

Prune content meta-inforamation about a given file. 

 

All content pieces belonging to file ``path`` are removed from the 

main database and any attached live selections (via database triggers). 

''' 

 

self._conn.execute( 

'DELETE FROM files WHERE path = ?', (path,)) 

 

def reset(self, path): 

''' 

Prune information associated with a given file, but keep the file path. 

 

This method is called when reading a file failed. File attributes, 

format, size and modification time are set to NULL. File content 

meta-information is removed from the database and any attached live 

selections (via database triggers). 

''' 

 

self._conn.execute( 

''' 

UPDATE files SET 

format = NULL, 

mtime = NULL, 

size = NULL 

WHERE path = ? 

''', (path,)) 

 

def silent_touch(self, path): 

''' 

Update modification time of file without initiating reindexing. 

 

Useful to prolong validity period of data with expiration date. 

''' 

 

c = self._conn 

with c: 

 

sql = 'SELECT format, size FROM files WHERE path = ?' 

fmt, size = execute_get1(c, sql, (path,)) 

 

mod = io.get_backend(fmt) 

mod.touch(path) 

file_stats = mod.get_stats(path) 

 

if file_stats[1] != size: 

raise FileLoadError( 

'Silent update for file "%s" failed: size has changed.' 

% path) 

 

sql = ''' 

UPDATE files 

SET mtime = ? 

WHERE path = ? 

''' 

c.execute(sql, (file_stats[0], path)) 

 

def _iter_counts(self, kind=None, kind_codes_count='kind_codes_count'): 

args = [] 

sel = '' 

if kind is not None: 

sel = 'AND kind_codes.kind_id == ?' 

args.append(to_kind_id(kind)) 

 

sql = (''' 

SELECT 

kind_codes.kind_id, 

kind_codes.codes, 

kind_codes.deltat, 

%(kind_codes_count)s.count 

FROM %(kind_codes_count)s 

INNER JOIN kind_codes 

ON %(kind_codes_count)s.kind_codes_id 

== kind_codes.kind_codes_id 

WHERE %(kind_codes_count)s.count > 0 

''' + sel + ''' 

''') % {'kind_codes_count': kind_codes_count} 

 

for kind_id, codes, deltat, count in self._conn.execute(sql, args): 

yield ( 

to_kind(kind_id), 

tuple(codes.split(separator)), 

deltat), count 

 

def _iter_deltats(self, kind=None, kind_codes_count='kind_codes_count'): 

args = [] 

sel = '' 

if kind is not None: 

assert isinstance(kind, str) 

sel = 'AND kind_codes.kind_id == ?' 

args.append(to_kind_id(kind)) 

 

sql = (''' 

SELECT DISTINCT kind_codes.deltat FROM %(kind_codes_count)s 

INNER JOIN kind_codes 

ON %(kind_codes_count)s.kind_codes_id 

== kind_codes.kind_codes_id 

WHERE %(kind_codes_count)s.count > 0 

''' + sel + ''' 

ORDER BY kind_codes.deltat 

''') % {'kind_codes_count': kind_codes_count} 

 

for row in self._conn.execute(sql, args): 

yield row[0] 

 

def _iter_codes(self, kind=None, kind_codes_count='kind_codes_count'): 

args = [] 

sel = '' 

if kind is not None: 

assert isinstance(kind, str) 

sel = 'AND kind_codes.kind_id == ?' 

args.append(to_kind_id(kind)) 

 

sql = (''' 

SELECT DISTINCT kind_codes.codes FROM %(kind_codes_count)s 

INNER JOIN kind_codes 

ON %(kind_codes_count)s.kind_codes_id 

== kind_codes.kind_codes_id 

WHERE %(kind_codes_count)s.count > 0 

''' + sel + ''' 

ORDER BY kind_codes.codes 

''') % {'kind_codes_count': kind_codes_count} 

 

for row in self._conn.execute(sql, args): 

yield tuple(row[0].split(separator)) 

 

def _iter_kinds(self, codes=None, kind_codes_count='kind_codes_count'): 

args = [] 

sel = '' 

if codes is not None: 

assert isinstance(codes, tuple) 

sel = 'AND kind_codes.codes == ?' 

args.append(separator.join(codes)) 

 

sql = (''' 

SELECT DISTINCT kind_codes.kind_id FROM %(kind_codes_count)s 

INNER JOIN kind_codes 

ON %(kind_codes_count)s.kind_codes_id 

== kind_codes.kind_codes_id 

WHERE %(kind_codes_count)s.count > 0 

''' + sel + ''' 

ORDER BY kind_codes.kind_id 

''') % {'kind_codes_count': kind_codes_count} 

 

for row in self._conn.execute(sql, args): 

yield to_kind(row[0]) 

 

def iter_kinds(self, codes=None): 

return self._iter_kinds(codes=codes) 

 

def iter_codes(self, kind=None): 

return self._iter_codes(kind=kind) 

 

def iter_counts(self, kind=None): 

return self._iter_counts(kind=kind) 

 

def get_kinds(self, codes=None): 

return list(self.iter_kinds(codes=codes)) 

 

def get_codes(self, kind=None): 

return list(self.iter_codes(kind=kind)) 

 

def get_counts(self, kind=None): 

d = {} 

for (k, codes, deltat), count in self.iter_counts(): 

if k not in d: 

v = d[k] = {} 

else: 

v = d[k] 

 

if codes not in v: 

v[codes] = 0 

 

v[codes] += count 

 

if kind is not None: 

return d[kind] 

else: 

return d 

 

def get_nfiles(self): 

sql = '''SELECT COUNT(*) FROM files''' 

for row in self._conn.execute(sql): 

return row[0] 

 

def get_nnuts(self): 

sql = '''SELECT COUNT(*) FROM nuts''' 

for row in self._conn.execute(sql): 

return row[0] 

 

def get_total_size(self): 

sql = ''' 

SELECT SUM(files.size) FROM files 

''' 

 

for row in self._conn.execute(sql): 

return row[0] or 0 

 

def get_stats(self): 

return DatabaseStats( 

nfiles=self.get_nfiles(), 

nnuts=self.get_nnuts(), 

kinds=self.get_kinds(), 

codes=self.get_codes(), 

counts=self.get_counts(), 

total_size=self.get_total_size()) 

 

def __str__(self): 

return str(self.get_stats()) 

 

def print_tables(self, stream=None): 

for table in [ 

'files', 

'nuts', 

'kind_codes', 

'kind_codes_count']: 

 

self.print_table(table, stream=stream) 

 

def print_table(self, name, stream=None): 

 

if stream is None: 

stream = sys.stdout 

 

class hstr(str): 

def __repr__(self): 

return self 

 

w = stream.write 

w('\n') 

w('\n') 

w(name) 

w('\n') 

sql = 'SELECT * FROM %s' % name 

tab = [] 

if name in self._tables: 

headers = self._tables[name] 

tab.append([None for _ in headers]) 

tab.append([hstr(x[0]) for x in headers]) 

tab.append([hstr(x[1]) for x in headers]) 

tab.append([None for _ in headers]) 

 

for row in self._conn.execute(sql): 

tab.append([x for x in row]) 

 

widths = [ 

max((len(repr(x)) if x is not None else 0) for x in col) 

for col in zip(*tab)] 

 

for row in tab: 

w(' '.join( 

(repr(x).ljust(wid) if x is not None else ''.ljust(wid, '-')) 

for (x, wid) in zip(row, widths))) 

 

w('\n') 

 

w('\n') 

 

 

__all__ = [ 

'Squirrel', 

'Selection', 

'SquirrelStats', 

'Database', 

'DatabaseStats', 

]