Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

""" 

Module for handling ObsPy Trace objects. 

 

:copyright: 

    The ObsPy Development Team (devs@obspy.org) 

:license: 

    GNU Lesser General Public License, Version 3 

    (http://www.gnu.org/copyleft/lesser.html) 

""" 

from copy import deepcopy, copy 

from obspy.core.utcdatetime import UTCDateTime 

from obspy.core.util import AttribDict, createEmptyDataChunk 

from obspy.core.util.base import _getFunctionFromEntryPoint 

from obspy.core.util.misc import flatnotmaskedContiguous 

import math 

import numpy as np 

import warnings 

 

 

class Stats(AttribDict): 

    """ 

    A container for additional header information of a ObsPy Trace object. 

 

    A ``Stats`` object may contain all header information (also known as meta 

    data) of a :class:`~obspy.core.trace.Trace` object. Those headers may be 

    accessed or modified either in the dictionary style or directly via a 

    corresponding attribute. There are various default attributes which are 

    required by every waveform import and export modules within ObsPy such as 

    :mod:`obspy.mseed`. 

 

    :type header: dict or :class:`~obspy.core.trace.Stats`, optional 

    :param header: Dictionary containing meta information of a single 

        :class:`~obspy.core.trace.Trace` object. Possible keywords are 

        summarized in the following `Default Attributes`_ section. 

 

    .. rubric:: Basic Usage 

 

    >>> stats = Stats() 

    >>> stats.network = 'BW' 

    >>> stats['network'] 

    'BW' 

    >>> stats['station'] = 'MANZ' 

    >>> stats.station 

    'MANZ' 

 

    .. rubric:: _`Default Attributes` 

 

    ``sampling_rate`` : float, optional 

        Sampling rate in hertz (default value is 1.0). 

    ``delta`` : float, optional 

        Sample distance in seconds (default value is 1.0). 

    ``calib`` : float, optional 

        Calibration factor (default value is 1.0). 

    ``npts`` : int, optional 

        Number of sample points (default value is 0, which implies that no data 

        is present). 

    ``network`` : string, optional 

        Network code (default is an empty string). 

    ``location`` : string, optional 

        Location code (default is an empty string). 

    ``station`` : string, optional 

        Station code (default is an empty string). 

    ``channel`` : string, optional 

        Channel code (default is an empty string). 

    ``starttime`` : :class:`~obspy.core.utcdatetime.UTCDateTime`, optional 

        Date and time of the first data sample given in UTC (default value is 

        "1970-01-01T00:00:00.0Z"). 

    ``endtime`` : :class:`~obspy.core.utcdatetime.UTCDateTime`, optional 

        Date and time of the last data sample given in UTC 

        (default value is "1970-01-01T00:00:00.0Z"). 

 

    .. rubric:: Notes 

 

    (1) The attributes ``sampling_rate`` and ``delta`` are linked to each 

        other. If one of the attributes is modified the other will be 

        recalculated. 

 

        >>> stats = Stats() 

        >>> stats.sampling_rate 

        1.0 

        >>> stats.delta = 0.005 

        >>> stats.sampling_rate 

        200.0 

 

    (2) The attributes ``starttime``, ``npts``, ``sampling_rate`` and ``delta`` 

        are monitored and used to automatically calculate the ``endtime``. 

 

        >>> stats = Stats() 

        >>> stats.npts = 60 

        >>> stats.delta = 1.0 

        >>> stats.starttime = UTCDateTime(2009, 1, 1, 12, 0, 0) 

        >>> stats.endtime 

        UTCDateTime(2009, 1, 1, 12, 0, 59) 

        >>> stats.delta = 0.5 

        >>> stats.endtime 

        UTCDateTime(2009, 1, 1, 12, 0, 29, 500000) 

 

        .. note:: 

            The attribute ``endtime`` is currently calculated as 

            ``endtime = starttime + (npts-1) * delta``. This behaviour may 

            change in the future to ``endtime = starttime + npts * delta``. 

 

    (3) The attribute ``endtime`` is read only and can not be modified. 

 

        >>> stats = Stats() 

        >>> stats.endtime = UTCDateTime(2009, 1, 1, 12, 0, 0) 

        Traceback (most recent call last): 

        ... 

        AttributeError: Attribute "endtime" in Stats object is read only! 

        >>> stats['endtime'] = UTCDateTime(2009, 1, 1, 12, 0, 0) 

        Traceback (most recent call last): 

        ... 

        AttributeError: Attribute "endtime" in Stats object is read only! 

 

    (4) 

        The attribute ``npts`` will be automatically updated from the 

        :class:`~obspy.core.trace.Trace` object. 

 

        >>> trace = Trace() 

        >>> trace.stats.npts 

        0 

        >>> trace.data = np.array([1, 2, 3, 4]) 

        >>> trace.stats.npts 

        4 

    """ 

    readonly = ['endtime'] 

    defaults = { 

        'sampling_rate': 1.0, 

        'delta': 1.0, 

        'starttime': UTCDateTime(0), 

        'endtime': UTCDateTime(0), 

        'npts': 0, 

        'calib': 1.0, 

        'network': '', 

        'station': '', 

        'location': '', 

        'channel': '', 

    } 

 

    def __init__(self, header={}): 

        """ 

        """ 

        super(Stats, self).__init__(header) 

 

    def __setitem__(self, key, value): 

        """ 

        """ 

        # keys which need to refresh derived values 

        if key in ['delta', 'sampling_rate', 'starttime', 'npts']: 

            # ensure correct data type 

            if key == 'delta': 

                key = 'sampling_rate' 

                value = 1.0 / float(value) 

            elif key == 'sampling_rate': 

                value = float(value) 

            elif key == 'starttime': 

                value = UTCDateTime(value) 

            elif key == 'npts': 

                value = int(value) 

            # set current key 

            super(Stats, self).__setitem__(key, value) 

            # set derived value: delta 

            try: 

                delta = 1.0 / float(self.sampling_rate) 

            except ZeroDivisionError: 

                delta = 0 

            self.__dict__['delta'] = delta 

            # set derived value: endtime 

            if self.npts == 0: 

                timediff = 0 

            else: 

                timediff = (self.npts - 1) * delta 

            self.__dict__['endtime'] = self.starttime + timediff 

            return 

        # prevent a calibration factor of 0 

        if key == 'calib' and value == 0: 

            msg = 'Calibration factor set to 0.0!' 

            warnings.warn(msg, UserWarning) 

        # all other keys 

        if isinstance(value, dict): 

            super(Stats, self).__setitem__(key, AttribDict(value)) 

        else: 

            super(Stats, self).__setitem__(key, value) 

 

    __setattr__ = __setitem__ 

 

    def __str__(self): 

        """ 

        Return better readable string representation of Stats object. 

        """ 

        priorized_keys = ['network', 'station', 'location', 'channel', 

                          'starttime', 'endtime', 'sampling_rate', 'delta', 

                          'npts', 'calib'] 

        return self._pretty_str(priorized_keys) 

 

 

class Trace(object): 

    """ 

    An object containing data of a continuous series, such as a seismic trace. 

 

    :type data: :class:`~numpy.ndarray` or :class:`~numpy.ma.MaskedArray` 

    :param data: Array of data samples 

    :type header: dict or :class:`~obspy.core.trace.Stats` 

    :param header: Dictionary containing header fields 

 

    :var id: A SEED compatible identifier of the trace. 

    :var stats: A container :class:`~obspy.core.trace.Stats` for additional 

        header information of the trace. 

    :var data: Data samples in a :class:`~numpy.ndarray` or 

        :class:`~numpy.ma.MaskedArray` 

 

    .. rubric:: Supported Operations 

 

    ``trace = traceA + traceB`` 

        Merges traceA and traceB into one new trace object. 

        See also: :meth:`Trace.__add__`. 

    ``len(trace)`` 

        Returns the number of samples contained in the trace. That is 

        it es equal to ``len(trace.data)``. 

        See also: :meth:`Trace.__len__`. 

    ``str(trace)`` 

        Returns basic information about the trace object. 

        See also: :meth:`Trace.__str__`. 

    """ 

 

    def __init__(self, data=np.array([]), header=None): 

        # make sure Trace gets initialized with ndarray as self.data 

        # otherwise we could end up with e.g. a list object in self.data 

        if not isinstance(data, np.ndarray): 

            msg = "Trace.data must be a NumPy array." 

            raise ValueError(msg) 

        # set some defaults if not set yet 

        if header is None: 

            # Default values: For detail see 

            # http://www.obspy.org/wiki/\ 

            # KnownIssues#DefaultParameterValuesinPython 

            header = {} 

        header.setdefault('npts', len(data)) 

        self.stats = Stats(header) 

        # set data without changing npts in stats object (for headonly option) 

        super(Trace, self).__setattr__('data', data) 

 

    def __eq__(self, other): 

        """ 

        Implements rich comparison of Trace objects for "==" operator. 

 

        Traces are the same, if both their data and stats are the same. 

        """ 

        # check if other object is a Trace 

        if not isinstance(other, Trace): 

            return False 

        # comparison of Stats objects is supported by underlying AttribDict 

        if not self.stats == other.stats: 

            return False 

        # comparison of ndarrays is supported by NumPy 

        if not np.array_equal(self, other): 

            return False 

 

        return True 

 

    def __ne__(self, other): 

        """ 

        Implements rich comparison of Trace objects for "!=" operator. 

 

        Calls __eq__() and returns the opposite. 

        """ 

        return not self.__eq__(other) 

 

    def __lt__(self, other): 

        """ 

        Too ambiguous, throw an Error. 

        """ 

        raise NotImplementedError("Too ambiguous, therefore not implemented.") 

 

    def __le__(self, other): 

        """ 

        Too ambiguous, throw an Error. 

        """ 

        raise NotImplementedError("Too ambiguous, therefore not implemented.") 

 

    def __gt__(self, other): 

        """ 

        Too ambiguous, throw an Error. 

        """ 

        raise NotImplementedError("Too ambiguous, therefore not implemented.") 

 

    def __ge__(self, other): 

        """ 

        Too ambiguous, throw an Error. 

        """ 

        raise NotImplementedError("Too ambiguous, therefore not implemented.") 

 

    def __str__(self, id_length=None): 

        """ 

        Returns short summary string of the current trace. 

 

        :rtype: str 

        :return: Short summary string of the current trace containing the SEED 

            identifier, start time, end time, sampling rate and number of 

            points of the current trace. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(header={'station':'FUR', 'network':'GR'}) 

        >>> str(tr)  # doctest: +ELLIPSIS 

        'GR.FUR.. | 1970-01-01T00:00:00.000000Z - ... | 1.0 Hz, 0 samples' 

        """ 

        # set fixed id width 

        if id_length: 

            out = "%%-%ds" % (id_length) 

            trace_id = out % self.id 

        else: 

            trace_id = "%s" % self.id 

        out = '' 

        # output depending on delta or sampling rate bigger than one 

        if self.stats.sampling_rate < 0.1: 

            if hasattr(self.stats, 'preview') and self.stats.preview: 

                out = out + ' | '\ 

                      "%(starttime)s - %(endtime)s | " + \ 

                      "%(delta).1f s, %(npts)d samples [preview]" 

            else: 

                out = out + ' | '\ 

                      "%(starttime)s - %(endtime)s | " + \ 

                      "%(delta).1f s, %(npts)d samples" 

        else: 

            if hasattr(self.stats, 'preview') and self.stats.preview: 

                out = out + ' | '\ 

                      "%(starttime)s - %(endtime)s | " + \ 

                      "%(sampling_rate).1f Hz, %(npts)d samples [preview]" 

            else: 

                out = out + ' | '\ 

                      "%(starttime)s - %(endtime)s | " + \ 

                      "%(sampling_rate).1f Hz, %(npts)d samples" 

        # check for masked array 

        if np.ma.count_masked(self.data): 

            out += ' (masked)' 

        return trace_id + out % (self.stats) 

 

    def __len__(self): 

        """ 

        Returns number of data samples of the current trace. 

 

        :rtype: int 

        :return: Number of data samples. 

 

        .. rubric:: Example 

 

        >>> trace = Trace(data=np.array([1, 2, 3, 4])) 

        >>> trace.count() 

        4 

        >>> len(trace) 

        4 

        """ 

        return len(self.data) 

 

    count = __len__ 

 

    def __setattr__(self, key, value): 

        """ 

        __setattr__ method of Trace object. 

        """ 

        # any change in Trace.data will dynamically set Trace.stats.npts 

        if key == 'data': 

            if not isinstance(value, np.ndarray): 

                msg = "Trace.data must be a NumPy array." 

                ValueError(msg) 

            self.stats.npts = len(value) 

        return super(Trace, self).__setattr__(key, value) 

 

    def __getitem__(self, index): 

        """ 

        __getitem__ method of Trace object. 

 

        :rtype: list 

        :return: List of data points 

        """ 

        return self.data[index] 

 

    def __mul__(self, num): 

        """ 

        Creates a new Stream containing num copies of this trace. 

 

        :rtype num: int 

        :param num: Number of copies. 

        :returns: New ObsPy Stream object. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> tr = read()[0] 

        >>> st = tr * 5 

        >>> len(st) 

        5 

        """ 

        if not isinstance(num, int): 

            raise TypeError("Integer expected") 

        from obspy import Stream 

        st = Stream() 

        for _i in range(num): 

            st += self.copy() 

        return st 

 

    def __div__(self, num): 

        """ 

        Splits Trace into new Stream containing num Traces of the same size. 

 

        :type num: int 

        :param num: Number of traces in returned Stream. Last trace may contain 

            lesser samples. 

        :returns: New ObsPy Stream object. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> tr = read()[0] 

        >>> print tr  # doctest: +ELLIPSIS 

        BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z ... | 100.0 Hz, 3000 samples 

        >>> st = tr / 7 

        >>> print st  # doctest: +ELLIPSIS 

        7 Trace(s) in Stream: 

        BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:07.290000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:11.580000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:15.870000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:20.160000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:24.450000Z ... | 100.0 Hz, 429 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:28.740000Z ... | 100.0 Hz, 426 samples 

        """ 

        if not isinstance(num, int): 

            raise TypeError("Integer expected") 

        from obspy import Stream 

        total_length = np.size(self.data) 

        rest_length = total_length % num 

        if rest_length: 

            packet_length = (total_length // num) 

        else: 

            packet_length = (total_length // num) - 1 

        tstart = self.stats.starttime 

        tend = tstart + (self.stats.delta * packet_length) 

        st = Stream() 

        for _i in range(num): 

            st.append(self.slice(tstart, tend).copy()) 

            tstart = tend + self.stats.delta 

            tend = tstart + (self.stats.delta * packet_length) 

        return st 

 

    def __mod__(self, num): 

        """ 

        Splits Trace into new Stream containing Traces with num samples. 

 

        :type num: int 

        :param num: Number of samples in each trace in returned Stream. Last 

            trace may contain lesser samples. 

        :returns: New ObsPy Stream object. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> tr = read()[0] 

        >>> print tr  # doctest: +ELLIPSIS 

        BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z ... | 100.0 Hz, 3000 samples 

        >>> st = tr % 800 

        >>> print st  # doctest: +ELLIPSIS 

        4 Trace(s) in Stream: 

        BW.RJOB..EHZ | 2009-08-24T00:20:03.000000Z ... | 100.0 Hz, 800 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:11.000000Z ... | 100.0 Hz, 800 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:19.000000Z ... | 100.0 Hz, 800 samples 

        BW.RJOB..EHZ | 2009-08-24T00:20:27.000000Z ... | 100.0 Hz, 600 samples 

        """ 

        if not isinstance(num, int): 

            raise TypeError("Integer expected") 

        elif num <= 0: 

            raise ValueError("Positive Integer expected") 

        from obspy import Stream 

        st = Stream() 

        total_length = np.size(self.data) 

        if num >= total_length: 

            st.append(self.copy()) 

            return st 

        tstart = self.stats.starttime 

        tend = tstart + (self.stats.delta * (num - 1)) 

        while True: 

            st.append(self.slice(tstart, tend).copy()) 

            tstart = tend + self.stats.delta 

            tend = tstart + (self.stats.delta * (num - 1)) 

            if tstart > self.stats.endtime: 

                break 

        return st 

 

    def __add__(self, trace, method=0, interpolation_samples=0, 

                fill_value=None, sanity_checks=True): 

        """ 

        Adds another Trace object to current trace. 

 

        :type method: ``0`` or ``1``, optional 

        :param method: Method to handle overlaps of traces. Defaults to ``0``. 

            See the `Handling Overlaps`_ section below for further details. 

        :type fill_value: int or float, ``'latest'`` or ``'interpolate'``, 

            optional 

        :param fill_value: Fill value for gaps. Defaults to ``None``. Traces 

            will be converted to NumPy masked arrays if no value is given and 

            gaps are present. If the keyword ``'latest'`` is provided it will 

            use the latest value before the gap. If keyword ``'interpolate'`` 

            is provided, missing values are linearly interpolated (not 

            changing the data type e.g. of integer valued traces). 

            See the `Handling Gaps`_ section below for further details. 

        :type interpolation_samples: int, optional 

        :param interpolation_samples: Used only for ``method=1``. It specifies 

            the number of samples which are used to interpolate between 

            overlapping traces. Defaults to ``0``. If set to ``-1`` all 

            overlapping samples are interpolated. 

        :type sanity_checks: boolean, optional 

        :param sanity_checks: Enables some sanity checks before merging traces. 

            Defaults to ``True``. 

 

        Trace data will be converted into a NumPy masked array data type if 

        any gaps are present. This behavior may be prevented by setting the 

        ``fill_value`` parameter. The ``method`` argument controls the 

        handling of overlapping data values. 

 

        Sampling rate, data type and trace.id of both traces must match. 

 

        .. rubric:: _`Handling Overlaps` 

 

        ======  =============================================================== 

        Method  Description 

        ======  =============================================================== 

        0       Discard overlapping data. Overlaps are essentially treated the 

                same way as gaps:: 

 

                    Trace 1: AAAAAAAA 

                    Trace 2:     FFFFFFFF 

                    1 + 2  : AAAA----FFFF 

 

                Contained traces with differing data will be marked as gap:: 

 

                    Trace 1: AAAAAAAAAAAA 

                    Trace 2:     FF 

                    1 + 2  : AAAA--AAAAAA 

        1       Discard data of the previous trace assuming the following trace 

                contains data with a more correct time value. The parameter 

                ``interpolation_samples`` specifies the number of samples used 

                to linearly interpolate between the two traces in order to 

                prevent steps. Note that if there are gaps inside, the 

                returned array is still a masked array, only if ``fill_value`` 

                is set, the returned array is a normal array and gaps are 

                filled with fill value. 

 

                No interpolation (``interpolation_samples=0``):: 

 

                    Trace 1: AAAAAAAA 

                    Trace 2:     FFFFFFFF 

                    1 + 2  : AAAAFFFFFFFF 

 

                Interpolate first two samples (``interpolation_samples=2``):: 

 

                    Trace 1: AAAAAAAA 

                    Trace 2:     FFFFFFFF 

                    1 + 2  : AAAACDFFFFFF (interpolation_samples=2) 

 

                Interpolate all samples (``interpolation_samples=-1``):: 

 

                    Trace 1: AAAAAAAA 

                    Trace 2:     FFFFFFFF 

                    1 + 2  : AAAABCDEFFFF 

 

                Any contained traces with different data will be discarded:: 

 

                    Trace 1: AAAAAAAAAAAA (contained trace) 

                    Trace 2:     FF 

                    1 + 2  : AAAAAAAAAAAA 

        ======  =============================================================== 

 

        .. rubric:: _`Handling gaps` 

 

        1. Traces with gaps and ``fill_value=None`` (default):: 

 

            Trace 1: AAAA 

            Trace 2:         FFFF 

            1 + 2  : AAAA----FFFF 

 

        2. Traces with gaps and given ``fill_value=0``:: 

 

            Trace 1: AAAA 

            Trace 2:         FFFF 

            1 + 2  : AAAA0000FFFF 

 

        3. Traces with gaps and given ``fill_value='latest'``:: 

 

            Trace 1: ABCD 

            Trace 2:         FFFF 

            1 + 2  : ABCDDDDDFFFF 

 

        4. Traces with gaps and given ``fill_value='interpolate'``:: 

 

            Trace 1: AAAA 

            Trace 2:         FFFF 

            1 + 2  : AAAABCDEFFFF 

        """ 

        if sanity_checks: 

            if not isinstance(trace, Trace): 

                raise TypeError 

            #  check id 

            if self.getId() != trace.getId(): 

                raise TypeError("Trace ID differs") 

            #  check sample rate 

            if self.stats.sampling_rate != trace.stats.sampling_rate: 

                raise TypeError("Sampling rate differs") 

            #  check calibration factor 

            if self.stats.calib != trace.stats.calib: 

                raise TypeError("Calibration factor differs") 

            # check data type 

            if self.data.dtype != trace.data.dtype: 

                raise TypeError("Data type differs") 

        # check times 

        if self.stats.starttime <= trace.stats.starttime: 

            lt = self 

            rt = trace 

        else: 

            rt = self 

            lt = trace 

        # check whether to use the latest value to fill a gap 

        if fill_value == "latest": 

            fill_value = lt.data[-1] 

        elif fill_value == "interpolate": 

            fill_value = (lt.data[-1], rt.data[0]) 

        sr = self.stats.sampling_rate 

        delta = (rt.stats.starttime - lt.stats.endtime) * sr 

        delta = int(round(delta)) - 1 

        delta_endtime = lt.stats.endtime - rt.stats.endtime 

        # create the returned trace 

        out = self.__class__(header=deepcopy(lt.stats)) 

        # check if overlap or gap 

        if delta < 0 and delta_endtime < 0: 

            # overlap 

            delta = abs(delta) 

            if np.all(np.equal(lt.data[-delta:], rt.data[:delta])): 

                # check if data are the same 

                data = [lt.data[:-delta], rt.data] 

            elif method == 0: 

                overlap = createEmptyDataChunk(delta, lt.data.dtype, 

                                               fill_value) 

                data = [lt.data[:-delta], overlap, rt.data[delta:]] 

            elif method == 1 and interpolation_samples >= -1: 

                try: 

                    ls = lt.data[-delta - 1] 

                except: 

                    ls = lt.data[0] 

                if interpolation_samples == -1: 

                    interpolation_samples = delta 

                elif interpolation_samples > delta: 

                    interpolation_samples = delta 

                try: 

                    rs = rt.data[interpolation_samples] 

                except IndexError: 

                    # contained trace 

                    data = [lt.data] 

                else: 

                    # include left and right sample (delta + 2) 

                    interpolation = np.linspace(ls, rs, 

                                                interpolation_samples + 2) 

                    # cut ls and rs and ensure correct data type 

                    interpolation = np.require(interpolation[1:-1], 

                                               lt.data.dtype) 

                    data = [lt.data[:-delta], interpolation, 

                            rt.data[interpolation_samples:]] 

            else: 

                raise NotImplementedError 

        elif delta < 0 and delta_endtime >= 0: 

            # contained trace 

            delta = abs(delta) 

            lenrt = len(rt) 

            t1 = len(lt) - delta 

            t2 = t1 + lenrt 

            if np.all(lt.data[t1:t2] == rt.data): 

                # check if data are the same 

                data = [lt.data] 

            elif method == 0: 

                gap = createEmptyDataChunk(lenrt, lt.data.dtype, fill_value) 

                data = [lt.data[:t1], gap, lt.data[t2:]] 

            elif method == 1: 

                data = [lt.data] 

            else: 

                raise NotImplementedError 

        elif delta == 0: 

            # exact fit - merge both traces 

            data = [lt.data, rt.data] 

        else: 

            # gap 

            # use fixed value or interpolate in between 

            gap = createEmptyDataChunk(delta, lt.data.dtype, fill_value) 

            data = [lt.data, gap, rt.data] 

        # merge traces depending on numpy array type 

        if True in [isinstance(_i, np.ma.masked_array) for _i in data]: 

            data = np.ma.concatenate(data) 

        else: 

            data = np.concatenate(data) 

            data = np.require(data, dtype=lt.data.dtype) 

        out.data = data 

        return out 

 

    def getId(self): 

        """ 

        Returns a SEED compatible identifier of the trace. 

 

        :rtype: str 

        :return: SEED identifier 

 

        The SEED identifier contains the network, station, location and channel 

        code for the current Trace object. 

 

        .. rubric:: Example 

 

        >>> meta = {'station': 'MANZ', 'network': 'BW', 'channel': 'EHZ'} 

        >>> tr = Trace(header=meta) 

        >>> tr.getId() 

        'BW.MANZ..EHZ' 

        >>> tr.id 

        'BW.MANZ..EHZ' 

        """ 

        out = "%(network)s.%(station)s.%(location)s.%(channel)s" 

        return out % (self.stats) 

 

    id = property(getId) 

 

    def plot(self, **kwargs): 

        """ 

        Creates a simple graph of the current trace. 

 

        Various options are available to change the appearance of the waveform 

        plot. Please see :meth:`~obspy.core.stream.Stream.plot` method for all 

        possible options. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> st = read() 

        >>> tr = st[0] 

        >>> tr.plot() # doctest: +SKIP 

 

        .. plot:: 

 

            from obspy import read 

            st = read() 

            tr = st[0] 

            tr.plot() 

        """ 

        from obspy.imaging.waveform import WaveformPlotting 

        waveform = WaveformPlotting(stream=self, **kwargs) 

        return waveform.plotWaveform() 

 

    def spectrogram(self, **kwargs): 

        """ 

        Creates a spectrogram plot of the trace. 

 

        For details on kwargs that can be used to customize the spectrogram 

        plot see :func:`~obspy.imaging.spectrogram.spectrogram`. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> st = read() 

        >>> tr = st[0] 

        >>> tr.spectrogram() # doctest: +SKIP 

 

        .. plot:: 

 

            from obspy import read 

            st = read() 

            tr = st[0] 

            tr.spectrogram(sphinx=True) 

        """ 

        # set some default values 

        if 'samp_rate' not in kwargs: 

            kwargs['samp_rate'] = self.stats.sampling_rate 

        if 'title' not in kwargs: 

            kwargs['title'] = str(self) 

        from obspy.imaging.spectrogram import spectrogram 

        return spectrogram(data=self.data, **kwargs) 

 

    def write(self, filename, format, **kwargs): 

        """ 

        Saves current trace into a file. 

 

        :type filename: string 

        :param filename: The name of the file to write. 

        :type format: string 

        :param format: The format to write must be specified. One of 

            ``"MSEED"``, ``"GSE2"``, ``"SAC"``, ``"SACXY"``, ``"Q"``, 

            ``"SH_ASC"``, ``"SEGY"``, ``"SU"``, ``"WAV"``, ``"PICKLE"``. See 

            :meth:`obspy.core.stream.Stream.write` method for all possible 

            formats. 

        :param kwargs: Additional keyword arguments passed to the underlying 

            waveform writer method. 

 

        .. rubric:: Example 

 

        >>> tr = Trace() 

        >>> tr.write("out.mseed", format="MSEED") # doctest: +SKIP 

        """ 

        # we need to import here in order to prevent a circular import of 

        # Stream and Trace classes 

        from obspy import Stream 

        Stream([self]).write(filename, format, **kwargs) 

 

    def _ltrim(self, starttime, pad=False, nearest_sample=True, 

               fill_value=None): 

        """ 

        Cuts current trace to given start time. For more info see 

        :meth:`~obspy.core.trace.Trace.trim`. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.arange(0, 10)) 

        >>> tr.stats.delta = 1.0 

        >>> tr._ltrim(tr.stats.starttime + 8) 

        >>> tr.data 

        array([8, 9]) 

        >>> tr.stats.starttime 

        UTCDateTime(1970, 1, 1, 0, 0, 8) 

        """ 

        org_dtype = self.data.dtype 

        if isinstance(starttime, float) or isinstance(starttime, int): 

            starttime = UTCDateTime(self.stats.starttime) + starttime 

        elif not isinstance(starttime, UTCDateTime): 

            raise TypeError 

        # check if in boundary 

        if nearest_sample: 

            delta = round((starttime - self.stats.starttime) * 

                          self.stats.sampling_rate) 

            # due to rounding and npts starttime must always be right of 

            # self.stats.starttime, rtrim relies on it 

            if delta < 0 and pad: 

                npts = abs(delta) + 10  # use this as a start 

                newstarttime = self.stats.starttime - npts / \ 

                        float(self.stats.sampling_rate) 

                newdelta = round((starttime - newstarttime) * 

                                 self.stats.sampling_rate) 

                delta = newdelta - npts 

            delta = int(delta) 

        else: 

            delta = int(math.floor(round((self.stats.starttime - starttime) * 

                                          self.stats.sampling_rate, 7))) * -1 

        # Adjust starttime only if delta is greater than zero or if the values 

        # are padded with masked arrays. 

        if delta > 0 or pad: 

            self.stats.starttime += delta * self.stats.delta 

        if delta == 0 or (delta < 0 and not pad): 

            return 

        elif delta < 0 and pad: 

            try: 

                gap = createEmptyDataChunk(abs(delta), self.data.dtype, 

                                           fill_value) 

            except ValueError: 

                # createEmptyDataChunk returns negative ValueError ?? for 

                # too large number of points, e.g. 189336539799 

                raise Exception("Time offset between starttime and " 

                                "trace.starttime too large") 

            self.data = np.ma.concatenate((gap, self.data)) 

            return 

        elif starttime > self.stats.endtime: 

            self.data = np.empty(0, dtype=org_dtype) 

            return 

        elif delta > 0: 

            self.data = self.data[delta:] 

 

    def _rtrim(self, endtime, pad=False, nearest_sample=True, fill_value=None): 

        """ 

        Cuts current trace to given end time. For more info see 

        :meth:`~obspy.core.trace.Trace.trim`. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.arange(0, 10)) 

        >>> tr.stats.delta = 1.0 

        >>> tr._rtrim(tr.stats.starttime + 2) 

        >>> tr.data 

        array([0, 1, 2]) 

        >>> tr.stats.endtime 

        UTCDateTime(1970, 1, 1, 0, 0, 2) 

        """ 

        org_dtype = self.data.dtype 

        if isinstance(endtime, float) or isinstance(endtime, int): 

            endtime = UTCDateTime(self.stats.endtime) - endtime 

        elif not isinstance(endtime, UTCDateTime): 

            raise TypeError 

        # check if in boundary 

        if nearest_sample: 

            delta = round((endtime - self.stats.starttime) * 

                           self.stats.sampling_rate) - self.stats.npts + 1 

            delta = int(delta) 

        else: 

            # solution for #127, however some tests need to be changed 

            #delta = -1*int(math.floor(round((self.stats.endtime - endtime) * \ 

            #                       self.stats.sampling_rate, 7))) 

            delta = int(math.floor(round((endtime - self.stats.endtime) * 

                                   self.stats.sampling_rate, 7))) 

        if delta == 0 or (delta > 0 and not pad): 

            return 

        if delta > 0 and pad: 

            try: 

                gap = createEmptyDataChunk(delta, self.data.dtype, fill_value) 

            except ValueError: 

                # createEmptyDataChunk returns negative ValueError ?? for 

                # too large number of pointes, e.g. 189336539799 

                raise Exception("Time offset between starttime and " + 

                                "trace.starttime too large") 

            self.data = np.ma.concatenate((self.data, gap)) 

            return 

        elif endtime < self.stats.starttime: 

            self.stats.starttime = self.stats.endtime + \ 

                                   delta * self.stats.delta 

            self.data = np.empty(0, dtype=org_dtype) 

            return 

        # cut from right 

        delta = abs(delta) 

        total = len(self.data) - delta 

        if endtime == self.stats.starttime: 

            total = 1 

        self.data = self.data[:total] 

 

    def trim(self, starttime=None, endtime=None, pad=False, 

             nearest_sample=True, fill_value=None): 

        """ 

        Cuts current trace to given start and end time. 

 

        :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime`, optional 

        :param starttime: Specify the start time. 

        :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime`, optional 

        :param endtime: Specify the end time. 

        :type pad: bool, optional 

        :param pad: Gives the possibility to trim at time points outside the 

            time frame of the original trace, filling the trace with the 

            given ``fill_value``. Defaults to ``False``. 

        :type nearest_sample: bool, optional 

        :param nearest_sample: If set to ``True``, the closest sample is 

            selected, if set to ``False``, the next sample containing the time 

            is selected. Defaults to ``True``. 

 

                Given the following trace containing 4 samples, "|" are the 

                sample points, "A" is the requested starttime:: 

 

                    |        A|         |         | 

 

                ``nearest_sample=True`` will select the second sample point, 

                ``nearest_sample=False`` will select the first sample point. 

 

        :type fill_value: int, float or ``None``, optional 

        :param fill_value: Fill value for gaps. Defaults to ``None``. Traces 

            will be converted to NumPy masked arrays if no value is given and 

            gaps are present. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.arange(0, 10)) 

        >>> tr.stats.delta = 1.0 

        >>> t = tr.stats.starttime 

        >>> tr.trim(t + 2.000001, t + 7.999999) 

        >>> tr.data 

        array([2, 3, 4, 5, 6, 7, 8]) 

        """ 

        # check time order and swap eventually 

        if starttime and endtime and starttime > endtime: 

            raise ValueError("startime is larger than endtime") 

        # cut it 

        if starttime: 

            self._ltrim(starttime, pad, nearest_sample=nearest_sample, 

                        fill_value=fill_value) 

        if endtime: 

            self._rtrim(endtime, pad, nearest_sample=nearest_sample, 

                        fill_value=fill_value) 

 

    def slice(self, starttime=None, endtime=None): 

        """ 

        Returns a new Trace object with data going from start to end time. 

 

        :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime` 

        :param starttime: Specify the start time of slice. 

        :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime` 

        :param endtime: Specify the end time of slice. 

        :return: New :class:`~obspy.core.trace.Trace` object. Does not copy 

            data but just passes a reference to it. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.arange(0, 10)) 

        >>> tr.stats.delta = 1.0 

        >>> t = tr.stats.starttime 

        >>> tr2 = tr.slice(t + 2, t + 8) 

        >>> tr2.data 

        array([2, 3, 4, 5, 6, 7, 8]) 

        """ 

        tr = copy(self) 

        tr.stats = deepcopy(self.stats) 

        tr.trim(starttime=starttime, endtime=endtime) 

        return tr 

 

    def verify(self): 

        """ 

        Verifies current trace object against available meta data. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.array([1,2,3,4])) 

        >>> tr.stats.npts = 100 

        >>> tr.verify()  #doctest: +ELLIPSIS 

        Traceback (most recent call last): 

        ... 

        Exception: ntps(100) differs from data size(4) 

        """ 

        if len(self) != self.stats.npts: 

            msg = "ntps(%d) differs from data size(%d)" 

            raise Exception(msg % (self.stats.npts, len(self.data))) 

        delta = self.stats.endtime - self.stats.starttime 

        if delta < 0: 

            msg = "End time(%s) before start time(%s)" 

            raise Exception(msg % (self.stats.endtime, self.stats.starttime)) 

        sr = self.stats.sampling_rate 

        if self.stats.starttime != self.stats.endtime: 

            if int(round(delta * sr)) + 1 != len(self.data): 

                msg = "Sample rate(%f) * time delta(%.4lf) + 1 != data len(%d)" 

                raise Exception(msg % (sr, delta, len(self.data))) 

            # Check if the endtime fits the starttime, npts and sampling_rate. 

            if self.stats.endtime != self.stats.starttime + \ 

                (self.stats.npts - 1) / float(self.stats.sampling_rate): 

                msg = "Endtime is not the time of the last sample." 

                raise Exception(msg) 

        elif self.stats.npts not in [0, 1]: 

            msg = "Data size should be 0, but is %d" 

            raise Exception(msg % self.stats.npts) 

        if not isinstance(self.stats, Stats): 

            msg = "Attribute stats must be an instance of obspy.core.Stats" 

            raise Exception(msg) 

        if isinstance(self.data, np.ndarray) and \ 

           self.data.dtype.byteorder not in ["=", "|"]: 

            msg = "Trace data should be stored as numpy.ndarray in the " + \ 

                  "system specific byte order." 

            raise Exception(msg) 

 

    def simulate(self, paz_remove=None, paz_simulate=None, 

                 remove_sensitivity=True, simulate_sensitivity=True, **kwargs): 

        """ 

        Correct for instrument response / Simulate new instrument response. 

 

        :type paz_remove: dict, None 

        :param paz_remove: Dictionary containing keys ``'poles'``, ``'zeros'``, 

            ``'gain'`` (A0 normalization factor). Poles and zeros must be a 

            list of complex floating point numbers, gain must be of type float. 

            Poles and Zeros are assumed to correct to m/s, SEED convention. 

            Use ``None`` for no inverse filtering. 

        :type paz_simulate: dict, None 

        :param paz_simulate: Dictionary containing keys ``'poles'``, 

            ``'zeros'``, ``'gain'``. Poles and zeros must be a list of complex 

            floating point numbers, gain must be of type float. Or ``None`` for 

            no simulation. 

        :type remove_sensitivity: bool 

        :param remove_sensitivity: Determines if data is divided by 

            ``paz_remove['sensitivity']`` to correct for overall sensitivity of 

            recording instrument (seismometer/digitizer) during instrument 

            correction. 

        :type simulate_sensitivity: bool 

        :param simulate_sensitivity: Determines if data is multiplied with 

            ``paz_simulate['sensitivity']`` to simulate overall sensitivity of 

            new instrument (seismometer/digitizer) during instrument 

            simulation. 

 

        This function corrects for the original instrument response given by 

        `paz_remove` and/or simulates a new instrument response given by 

        `paz_simulate`. 

        For additional information and more options to control the instrument 

        correction/simulation (e.g. water level, demeaning, tapering, ...) see 

        :func:`~obspy.signal.invsim.seisSim`. 

 

        `paz_remove` and `paz_simulate` are expected to be dictionaries 

        containing information on poles, zeros and gain (and usually also 

        sensitivity). 

 

        If both `paz_remove` and `paz_simulate` are specified, both steps are 

        performed in one go in the frequency domain, otherwise only the 

        specified step is performed. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> from obspy.signal import cornFreq2Paz 

        >>> st = read() 

        >>> tr = st[0] 

        >>> tr.plot() # doctest: +SKIP 

        >>> paz_sts2 = {'poles': [-0.037004+0.037016j, -0.037004-0.037016j, 

        ...                       -251.33+0j, 

        ...                       -131.04-467.29j, -131.04+467.29j], 

        ...             'zeros': [0j, 0j], 

        ...             'gain': 60077000.0, 

        ...             'sensitivity': 2516778400.0} 

        >>> paz_1hz = cornFreq2Paz(1.0, damp=0.707) 

        >>> paz_1hz['sensitivity'] = 1.0 

        >>> tr.simulate(paz_remove=paz_sts2, paz_simulate=paz_1hz) 

        >>> tr.plot() # doctest: +SKIP 

 

        .. plot:: 

 

            from obspy import read 

            from obspy.signal import cornFreq2Paz 

            st = read() 

            tr = st[0] 

            tr.plot() 

            paz_sts2 = {'poles': [-0.037004+0.037016j, -0.037004-0.037016j, 

                                  -251.33+0j, 

                                  -131.04-467.29j, -131.04+467.29j], 

                        'zeros': [0j, 0j], 

                        'gain': 60077000.0, 

                        'sensitivity': 2516778400.0} 

            paz_1hz = cornFreq2Paz(1.0, damp=0.707) 

            paz_1hz['sensitivity'] = 1.0 

            tr.simulate(paz_remove=paz_sts2, paz_simulate=paz_1hz) 

            tr.plot() 

        """ 

        # XXX accepting string "self" and using attached PAZ then 

        if paz_remove == 'self': 

            paz_remove = self.stats.paz 

 

        from obspy.signal import seisSim 

        self.data = seisSim(self.data, self.stats.sampling_rate, 

                paz_remove=paz_remove, paz_simulate=paz_simulate, 

                remove_sensitivity=remove_sensitivity, 

                simulate_sensitivity=simulate_sensitivity, **kwargs) 

 

        # add processing information to the stats dictionary 

        if paz_remove: 

            proc_info = "simulate:inverse:%s:sensitivity=%s" % \ 

                    (paz_remove, remove_sensitivity) 

            self._addProcessingInfo(proc_info) 

        if paz_simulate: 

            proc_info = "simulate:forward:%s:sensitivity=%s" % \ 

                    (paz_simulate, simulate_sensitivity) 

            self._addProcessingInfo(proc_info) 

 

    def filter(self, type, **options): 

        """ 

        Filters the data of the current trace. 

 

        :type type: str 

        :param type: String that specifies which filter is applied (e.g. 

            ``"bandpass"``). See the `Supported Filter`_ section below for 

            further details. 

        :param options: Necessary keyword arguments for the respective filter 

            that will be passed on. (e.g. ``freqmin=1.0``, ``freqmax=20.0`` for 

            ``"bandpass"``) 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Filter` 

 

        ``'bandpass'`` 

            Butterworth-Bandpass (uses :func:`obspy.signal.filter.bandpass`). 

 

        ``'bandstop'`` 

            Butterworth-Bandstop (uses :func:`obspy.signal.filter.bandstop`). 

 

        ``'lowpass'`` 

            Butterworth-Lowpass (uses :func:`obspy.signal.filter.lowpass`). 

 

        ``'highpass'`` 

            Butterworth-Highpass (uses :func:`obspy.signal.filter.highpass`). 

 

        ``'lowpassCheby2'`` 

            Cheby2-Lowpass (uses :func:`obspy.signal.filter.lowpassCheby2`). 

 

        ``'lowpassFIR'`` (experimental) 

            FIR-Lowpass (uses :func:`obspy.signal.filter.lowpassFIR`). 

 

        ``'remezFIR'`` (experimental) 

            Minimax optimal bandpass using Remez algorithm (uses 

            :func:`obspy.signal.filter.remezFIR`). 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> st = read() 

        >>> tr = st[0] 

        >>> tr.filter("highpass", freq=1.0) 

        >>> tr.plot() # doctest: +SKIP 

 

        .. plot:: 

 

            from obspy import read 

            st = read() 

            tr = st[0] 

            tr.filter("highpass", freq=1.0) 

            tr.plot() 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('filter', type) 

        # filtering 

        # the options dictionary is passed as kwargs to the function that is 

        # mapped according to the filter_functions dictionary 

        self.data = func(self.data, df=self.stats.sampling_rate, **options) 

        # add processing information to the stats dictionary 

        proc_info = "filter:%s:%s" % (type, options) 

        self._addProcessingInfo(proc_info) 

 

    def trigger(self, type, **options): 

        """ 

        Runs a triggering algorithm on the data of the current trace. 

 

        :param type: String that specifies which trigger is applied (e.g. 

            ``'recstalta'``). See the `Supported Trigger`_ section below for 

            further details. 

        :param options: Necessary keyword arguments for the respective trigger 

            that will be passed on. 

            (e.g. ``sta=3``, ``lta=10``) 

            Arguments ``sta`` and ``lta`` (seconds) will be mapped to ``nsta`` 

            and ``nlta`` (samples) by multiplying with sampling rate of trace. 

            (e.g. ``sta=3``, ``lta=10`` would call the trigger with 3 and 10 

            seconds average, respectively) 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Trigger` 

 

        ``'classicstalta'`` 

            Computes the classic STA/LTA characteristic function (uses 

            :func:`obspy.signal.trigger.classicSTALTA`). 

 

        ``'recstalta'`` 

            Recursive STA/LTA (uses :func:`obspy.signal.trigger.recSTALTA`). 

 

        ``'recstaltapy'`` 

            Recursive STA/LTA written in Python (uses 

            :func:`obspy.signal.trigger.recSTALTAPy`). 

 

        ``'delayedstalta'`` 

            Delayed STA/LTA. (uses :func:`obspy.signal.trigger.delayedSTALTA`). 

 

        ``'carlstatrig'`` 

            Computes the carlSTATrig characteristic function (uses 

            :func:`obspy.signal.trigger.carlSTATrig`). 

 

        ``'zdetect'`` 

            Z-detector (uses :func:`obspy.signal.trigger.zDetect`). 

 

        .. rubric:: Example 

 

        >>> from obspy import read 

        >>> st = read() 

        >>> tr = st[0] 

        >>> tr.filter("highpass", freq=1.0) 

        >>> tr.plot() # doctest: +SKIP 

        >>> tr.trigger("recstalta", sta=3, lta=10) 

        >>> tr.plot() # doctest: +SKIP 

 

        .. plot:: 

 

            from obspy import read 

            st = read() 

            tr = st[0] 

            tr.filter("highpass", freq=1.0) 

            tr.plot() 

            tr.trigger('recstalta', sta=3, lta=10) 

            tr.plot() 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('trigger', type) 

        # convert the two arguments sta and lta to nsta and nlta as used by 

        # actual triggering routines (needs conversion to int, as samples are 

        # used in length of trigger averages)... 

        spr = self.stats.sampling_rate 

        for key in ['sta', 'lta']: 

            if key in options: 

                options['n%s' % (key)] = int(options.pop(key) * spr) 

        # triggering 

        # the options dictionary is passed as kwargs to the function that is 

        # mapped according to the trigger_functions dictionary 

        self.data = func(self.data, **options) 

        # add processing information to the stats dictionary 

        proc_info = "trigger:%s:%s" % (type, options) 

        self._addProcessingInfo(proc_info) 

 

    def resample(self, sampling_rate, window='hanning', no_filter=True, 

                 strict_length=False): 

        """ 

        Resample trace data using Fourier method. 

 

        :type sampling_rate: float 

        :param sampling_rate: The sampling rate of the resampled signal. 

        :type window: array_like, callable, string, float, or tuple, optional 

        :param window: Specifies the window applied to the signal in the 

            Fourier domain. Defaults to ``'hanning'`` window. See 

            :func:`scipy.signal.resample` for details. 

        :type strict_length: bool, optional 

        :param strict_length: Leave traces unchanged for which endtime of trace 

            would change. Defaults to ``False``. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        Uses :func:`scipy.signal.resample`. Because a Fourier method is used, 

        the signal is assumed to be periodic. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.array([0.5, 0, 0.5, 1, 0.5, 0, 0.5, 1])) 

        >>> len(tr) 

        8 

        >>> tr.stats.sampling_rate 

        1.0 

        >>> tr.resample(4.0) 

        >>> len(tr) 

        32 

        >>> tr.stats.sampling_rate 

        4.0 

        >>> tr.data  # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS 

        array([ 0.5       ,  0.40432914,  0.3232233 ,  0.26903012,  0.25 ... 

        """ 

        from scipy.signal import resample 

        factor = self.stats.sampling_rate / float(sampling_rate) 

        # check if endtime changes and this is not explicitly allowed 

        if strict_length and len(self.data) % factor != 0.0: 

            msg = "Endtime of trace would change and strict_length=True." 

            raise ValueError(msg) 

        # do automatic lowpass filtering 

        if not no_filter: 

            # be sure filter still behaves good 

            if factor > 16: 

                msg = "Automatic filter design is unstable for resampling " + \ 

                      "factors (current sampling rate/new sampling rate) " + \ 

                      "above 16. Manual resampling is necessary." 

                raise ArithmeticError(msg) 

            freq = self.stats.sampling_rate * 0.5 / float(factor) 

            self.filter('lowpassCheby2', freq=freq, maxorder=12) 

        # resample 

        num = int(self.stats.npts / factor) 

        self.data = resample(self.data, num, window=window) 

        self.stats.sampling_rate = sampling_rate 

        # add processing information to the stats dictionary 

        proc_info = "resample:%d:%s" % (sampling_rate, window) 

        self._addProcessingInfo(proc_info) 

 

    def decimate(self, factor, no_filter=False, strict_length=False): 

        """ 

        Downsample trace data by an integer factor. 

 

        :type factor: int 

        :param factor: Factor by which the sampling rate is lowered by 

            decimation. 

        :type no_filter: bool, optional 

        :param no_filter: Deactivates automatic filtering if set to ``True``. 

            Defaults to ``False``. 

        :type strict_length: bool, optional 

        :param strict_length: Leave traces unchanged for which endtime of trace 

            would change. Defaults to ``False``. 

 

        Currently a simple integer decimation is implemented. 

        Only every ``decimation_factor``-th sample remains in the trace, all 

        other samples are thrown away. Prior to decimation a lowpass filter is 

        applied to ensure no aliasing artifacts are introduced. The automatic 

        filtering can be deactivated with ``no_filter=True``. 

 

        If the length of the data array modulo ``decimation_factor`` is not 

        zero then the endtime of the trace is changing on sub-sample scale. To 

        abort downsampling in case of changing endtimes set 

        ``strict_length=True``. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: Example 

 

        For the example we switch off the automatic pre-filtering so that 

        the effect of the downsampling routine becomes clearer: 

 

        >>> tr = Trace(data=np.arange(10)) 

        >>> tr.stats.sampling_rate 

        1.0 

        >>> tr.data 

        array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

        >>> tr.decimate(4, strict_length=False, no_filter=True) 

        >>> tr.stats.sampling_rate 

        0.25 

        >>> tr.data 

        array([0, 4, 8]) 

        """ 

        # check if endtime changes and this is not explicitly allowed 

        if strict_length and len(self.data) % factor: 

            msg = "Endtime of trace would change and strict_length=True." 

            raise ValueError(msg) 

 

        # do automatic lowpass filtering 

        if not no_filter: 

            # be sure filter still behaves good 

            if factor > 16: 

                msg = "Automatic filter design is unstable for decimation " + \ 

                      "factors above 16. Manual decimation is necessary." 

                raise ArithmeticError(msg) 

            freq = self.stats.sampling_rate * 0.5 / float(factor) 

            self.filter('lowpassCheby2', freq=freq, maxorder=12) 

 

        # actual downsampling, as long as sampling_rate is a float we would not 

        # need to convert to float, but let's do it as a safety measure 

        from obspy.signal import integerDecimation 

        self.data = integerDecimation(self.data, factor) 

        self.stats.sampling_rate = self.stats.sampling_rate / float(factor) 

 

        # add processing information to the stats dictionary 

        proc_info = "downsample:integerDecimation:%s" % factor 

        self._addProcessingInfo(proc_info) 

 

    def max(self): 

        """ 

        Returns the value of the absolute maximum amplitude in the trace. 

 

        :return: Value of absolute maximum of ``trace.data``. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.array([0, -3, 9, 6, 4])) 

        >>> tr.max() 

        9 

        >>> tr = Trace(data=np.array([0, -3, -9, 6, 4])) 

        >>> tr.max() 

        -9 

        >>> tr = Trace(data=np.array([0.3, -3.5, 9.0, 6.4, 4.3])) 

        >>> tr.max() 

        9.0 

        """ 

        value = self.data.max() 

        _min = self.data.min() 

        if abs(_min) > abs(value): 

            value = _min 

        return value 

 

    def std(self): 

        """ 

        Method to get the standard deviation of amplitudes in the trace. 

 

        :return: Standard deviation of ``trace.data``. 

 

        Standard deviation is calculated by numpy method 

        :meth:`~numpy.ndarray.std` on ``trace.data``. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.array([0, -3, 9, 6, 4])) 

        >>> tr.std() 

        4.2614551505325036 

        >>> tr = Trace(data=np.array([0.3, -3.5, 9.0, 6.4, 4.3])) 

        >>> tr.std() 

        4.4348618918744247 

        """ 

        return self.data.std() 

 

    def differentiate(self, type='gradient', **options): 

        """ 

        Method to differentiate the trace with respect to time. 

 

        :type type: ``'gradient'``, optional 

        :param type: Method to use for differentiation. Defaults to 

            ``'gradient'``. See the `Supported Methods`_ section below for 

            further details. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Methods` 

 

        ``'gradient'`` 

            The gradient is computed using central differences in the interior 

            and first differences at the boundaries. The returned gradient 

            hence has the same shape as the input array. (uses 

            :func:`numpy.gradient`) 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('differentiate', type) 

        # differentiate 

        self.data = func(self.data, self.stats.delta, **options) 

        # add processing information to the stats dictionary 

        proc_info = "differentiate:%s" % type 

        self._addProcessingInfo(proc_info) 

 

    def integrate(self, type='cumtrapz', **options): 

        """ 

        Method to integrate the trace with respect to time. 

 

        :type type: ``'cumtrapz'``, optional 

        :param type: Method to use for integration. Defaults to 

            ``'cumtrapz'``. See the `Supported Methods`_ section below for 

            further details. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Methods` 

 

        ``'cumtrapz'`` 

            Trapezoidal rule to cumulatively compute integral (uses 

            :func:`scipy.integrate.cumtrapz`). Result has one sample less then 

            the input! 

 

        ``'trapz'`` 

            Trapezoidal rule to compute integral from samples (uses 

            :func:`scipy.integrate.trapz`). 

 

        ``'simps'`` 

            Simpson's rule to compute integral from samples (uses 

            :func:`scipy.integrate.simps`). 

 

        ``'romb'`` 

            Romberg Integration to compute integral from (2**k + 1) 

            evenly-spaced samples. (uses :func:`scipy.integrate.romb`). 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('integrate', type) 

        # handle function specific settings 

        if func.__module__.startswith('scipy'): 

            # scipy needs to set dx keyword if not given in options 

            if 'dx' not in options: 

                options['dx'] = self.stats.delta 

            args = [self.data] 

        else: 

            args = [self.data, self.stats.delta] 

        # integrating 

        self.data = func(*args, **options) 

        # add processing information to the stats dictionary 

        proc_info = "integrate:%s" % (type) 

        self._addProcessingInfo(proc_info) 

 

    def detrend(self, type='simple', **options): 

        """ 

        Method to remove a linear trend from the trace. 

 

        :type type: ``'linear'``, ``'constant'``, ``'demean'`` or ``'simple'``, 

            optional 

        :param type: Method to use for detrending. Defaults to ``'simple'``. 

            See the `Supported Methods`_ section below for further details. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Methods` 

 

        ``'simple'`` 

            Subtracts a linear function defined by first/last sample of the 

            trace (uses :func:`obspy.signal.detrend.simple`). 

 

        ``'linear'`` 

            Fitting a linear function to the trace with least squares and 

            subtracting it (uses :func:`scipy.signal.detrend`). 

 

        ``'constant'`` or ``'demean'`` 

            Mean of data is subtracted (uses :func:`scipy.signal.detrend`). 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('detrend', type) 

        # handle function specific settings 

        if func.__module__.startswith('scipy'): 

            # scipy need to set the type keyword 

            if type == 'demean': 

                type = 'constant' 

            options['type'] = type 

        # detrending 

        self.data = func(self.data, **options) 

        # add processing information to the stats dictionary 

        proc_info = "detrend:%s:%s" % (type, options) 

        self._addProcessingInfo(proc_info) 

 

    def taper(self, type='cosine', *args, **kwargs): 

        """ 

        Method to taper the trace. 

 

        Optional (and sometimes necessary) options to the tapering function can 

        be provided as args and kwargs. See respective function definitions in 

        `Supported Methods`_ section below. 

 

        :type type: str 

        :param type: Type of taper to use for detrending. Defaults to 

            ``'cosine'``.  See the `Supported Methods`_ section below for 

            further details. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: _`Supported Methods` 

 

        ``'cosine'`` 

            Cosine taper, for additional options like taper percentage see: 

            :func:`obspy.signal.invsim.cosTaper`. 

        ``'barthann'`` 

            Modified Bartlett-Hann window. (uses: 

            :func:`scipy.signal.barthann`) 

        ``'bartlett'`` 

            Bartlett window. (uses: :func:`scipy.signal.bartlett`) 

        ``'blackman'`` 

            Blackman window. (uses: :func:`scipy.signal.blackman`) 

        ``'blackmanharris'`` 

            Minimum 4-term Blackman-Harris window. (uses: 

            :func:`scipy.signal.blackmanharris`) 

        ``'bohman'`` 

            Bohman window. (uses: :func:`scipy.signal.bohman`) 

        ``'boxcar'`` 

            Boxcar window. (uses: :func:`scipy.signal.boxcar`) 

        ``'chebwin'`` 

            Dolph-Chebyshev window. (uses: :func:`scipy.signal.chebwin`) 

        ``'flattop'`` 

            Flat top window. (uses: :func:`scipy.signal.flattop`) 

        ``'gaussian'`` 

            Gaussian window with standard-deviation std. (uses: 

            :func:`scipy.signal.gaussian`) 

        ``'general_gaussian'`` 

            Generalized Gaussian window. (uses: 

            :func:`scipy.signal.general_gaussian`) 

        ``'hamming'`` 

            Hamming window. (uses: :func:`scipy.signal.hamming`) 

        ``'hann'`` 

            Hann window. (uses: :func:`scipy.signal.hann`) 

        ``'kaiser'`` 

            Kaiser window with shape parameter beta. (uses: 

            :func:`scipy.signal.kaiser`) 

        ``'nuttall'`` 

            Minimum 4-term Blackman-Harris window according to Nuttall. 

            (uses: :func:`scipy.signal.nuttall`) 

        ``'parzen'`` 

            Parzen window. (uses: :func:`scipy.signal.parzen`) 

        ``'slepian'`` 

            Slepian window. (uses: :func:`scipy.signal.slepian`) 

        ``'triang'`` 

            Triangular window. (uses: :func:`scipy.signal.triang`) 

        """ 

        type = type.lower() 

        # retrieve function call from entry points 

        func = _getFunctionFromEntryPoint('taper', type) 

        # tapering. tapering functions are expected to accept the number of 

        # samples as first argument and return an array of values between 0 and 

        # 1 with the same length as the data 

        self.data = self.data * func(self.stats.npts, *args, **kwargs) 

        # add processing information to the stats dictionary 

        proc_info = "taper:%s:%s:%s" % (type, args, kwargs) 

        self._addProcessingInfo(proc_info) 

 

    def normalize(self, norm=None): 

        """ 

        Method to normalize the trace to its absolute maximum. 

 

        :type norm: ``None`` or float 

        :param norm: If not ``None``, trace is normalized by dividing by 

            specified value ``norm`` instead of dividing by its absolute 

            maximum. If a negative value is specified then its absolute value 

            is used. 

 

        If ``trace.data.dtype`` was integer it is changing to float. 

 

        .. note:: 

 

            This operation is performed in place on the actual data arrays. The 

            raw data is not accessible anymore afterwards. To keep your 

            original data, use :meth:`~obspy.core.trace.Trace.copy` to create 

            a copy of your trace object. 

            This also makes an entry with information on the applied processing 

            in ``stats.processing`` of this trace. 

 

        .. rubric:: Example 

 

        >>> tr = Trace(data=np.array([0, -3, 9, 6])) 

        >>> tr.normalize() 

        >>> tr.data 

        array([ 0.        , -0.33333333,  1.        ,  0.66666667]) 

        >>> tr.stats.processing 

        ['normalize:9'] 

        >>> tr = Trace(data=np.array([0.3, -3.5, -9.2, 6.4])) 

        >>> tr.normalize() 

        >>> tr.data 

        array([ 0.0326087 , -0.38043478, -1.        ,  0.69565217]) 

        >>> tr.stats.processing 

        ['normalize:-9.2'] 

        """ 

        # normalize, use norm-kwarg otherwise normalize to 1 

        if norm: 

            norm = norm 

            if norm < 0: 

                msg = "Normalizing with negative values is forbidden. " + \ 

                      "Using absolute value." 

                warnings.warn(msg) 

        else: 

            norm = self.max() 

 

        self.data = self.data.astype("float64") 

        self.data /= abs(norm) 

 

        # add processing information to the stats dictionary 

        proc_info = "normalize:%s" % norm 

        self._addProcessingInfo(proc_info) 

 

    def copy(self): 

        """ 

        Returns a deepcopy of the trace. 

 

        :return: Copy of trace. 

 

        This actually copies all data in the trace and does not only provide 

        another pointer to the same data. At any processing step if the 

        original data has to be available afterwards, this is the method to 

        use to make a copy of the trace. 

 

        .. rubric:: Example 

 

        Make a Trace and copy it: 

 

        >>> tr = Trace(data=np.random.rand(10)) 

        >>> tr2 = tr.copy() 

 

        The two objects are not the same: 

 

        >>> tr2 is tr 

        False 

 

        But they have equal data (before applying further processing): 

 

        >>> tr2 == tr 

        True 

 

        The following example shows how to make an alias but not copy the 

        data. Any changes on ``tr3`` would also change the contents of ``tr``. 

 

        >>> tr3 = tr 

        >>> tr3 is tr 

        True 

        >>> tr3 == tr 

        True 

        """ 

        return deepcopy(self) 

 

    def _addProcessingInfo(self, info): 

        """ 

        Adds the given informational string to the `processing` field in the 

        trace's :class:`~obspy.core.trace.stats.Stats` object. 

        """ 

        proc = self.stats.setdefault('processing', []) 

        proc.append(info) 

 

    def split(self): 

        """ 

        Splits Trace object containing gaps using a NumPy masked array into 

        several traces. 

 

        :rtype: list 

        :returns: List of split traces. A gapless trace will still be 

            returned as list with only one entry. 

        """ 

        if not isinstance(self.data, np.ma.masked_array): 

            # no gaps 

            return [self] 

        slices = flatnotmaskedContiguous(self.data) 

        trace_list = [] 

        for slice in slices: 

            if slice.step: 

                raise NotImplementedError("step not supported") 

            stats = self.stats.copy() 

            tr = Trace(header=stats) 

            tr.stats.starttime += (stats.delta * slice.start) 

            tr.data = self.data[slice.start:slice.stop] 

            trace_list.append(tr) 

        return trace_list 

 

    def times(self): 

        """ 

        For convenient plotting compute a Numpy array of seconds since 

        starttime corresponding to the samples in Trace. 

 

        :rtype: :class:`~numpy.ndarray` or :class:`~numpy.ma.MaskedArray` 

        :returns: An array of time samples in an :class:`~numpy.ndarray` if 

            the trace doesn't have any gaps or a :class:`~numpy.ma.MaskedArray` 

            otherwise. 

        """ 

        timeArray = np.arange(self.stats.npts) 

        timeArray = timeArray / self.stats.sampling_rate 

        # Check if the data is a ma.maskedarray 

        if isinstance(self.data, np.ma.masked_array): 

            timeArray = np.ma.array(timeArray, mask=self.data.mask) 

        return timeArray 

 

 

if __name__ == '__main__': 

    import doctest 

    doctest.testmod(exclude_empty=True)