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

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

 

from copy import deepcopy 

import numpy as np 

from numpy.ma import is_masked 

from obspy import UTCDateTime, Trace, read 

import unittest 

import math 

 

 

class TraceTestCase(unittest.TestCase): 

    """ 

    Test suite for obspy.core.trace.Trace. 

    """ 

    def test_len(self): 

        """ 

        Tests the __len__ and count methods of the Trace class. 

        """ 

        trace = Trace(data=np.arange(1000)) 

        self.assertEquals(len(trace), 1000) 

        self.assertEquals(trace.count(), 1000) 

 

    def test_ltrim(self): 

        """ 

        Tests the ltrim method of the Trace class. 

        """ 

        # set up 

        trace = Trace(data=np.arange(1000)) 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        trace.stats.starttime = start 

        trace.stats.sampling_rate = 200.0 

        end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000) 

        # verify 

        trace.verify() 

        # ltrim 100 samples 

        tr = deepcopy(trace) 

        tr._ltrim(0.5) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[0:5], 

                                      np.array([100, 101, 102, 103, 104])) 

        self.assertEquals(len(tr.data), 900) 

        self.assertEquals(tr.stats.npts, 900) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start + 0.5) 

        self.assertEquals(tr.stats.endtime, end) 

        # ltrim 202 samples 

        tr = deepcopy(trace) 

        tr._ltrim(1.010) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[0:5], 

                                      np.array([202, 203, 204, 205, 206])) 

        self.assertEquals(len(tr.data), 798) 

        self.assertEquals(tr.stats.npts, 798) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start + 1.010) 

        self.assertEquals(tr.stats.endtime, end) 

        # ltrim to UTCDateTime 

        tr = deepcopy(trace) 

        tr._ltrim(UTCDateTime(2000, 1, 1, 0, 0, 1, 10000)) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[0:5], 

                                      np.array([202, 203, 204, 205, 206])) 

        self.assertEquals(len(tr.data), 798) 

        self.assertEquals(tr.stats.npts, 798) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start + 1.010) 

        self.assertEquals(tr.stats.endtime, end) 

        # some sanity checks 

        # negative start time as datetime 

        tr = deepcopy(trace) 

        tr._ltrim(start - 1, pad=True) 

        tr.verify() 

        self.assertEquals(tr.stats.starttime, start - 1) 

        np.testing.assert_array_equal(trace.data, tr.data[200:]) 

        self.assertEquals(tr.stats.endtime, trace.stats.endtime) 

        # negative start time as integer 

        tr = deepcopy(trace) 

        tr._ltrim(-100, pad=True) 

        tr.verify() 

        self.assertEquals(tr.stats.starttime, start - 100) 

        delta = 100 * trace.stats.sampling_rate 

        np.testing.assert_array_equal(trace.data, tr.data[delta:]) 

        self.assertEquals(tr.stats.endtime, trace.stats.endtime) 

        # start time > end time 

        tr = deepcopy(trace) 

        tr._ltrim(trace.stats.endtime + 100) 

        tr.verify() 

        self.assertEquals(tr.stats.starttime, 

                          trace.stats.endtime + 100) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        self.assertEquals(tr.stats.endtime, tr.stats.starttime) 

        # start time == end time 

        tr = deepcopy(trace) 

        tr._ltrim(5) 

        tr.verify() 

        self.assertEquals(tr.stats.starttime, 

                          trace.stats.starttime + 5) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        self.assertEquals(tr.stats.endtime, tr.stats.starttime) 

        # start time == end time 

        tr = deepcopy(trace) 

        tr._ltrim(5.1) 

        tr.verify() 

        self.assertEquals(tr.stats.starttime, 

                          trace.stats.starttime + 5.1) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        self.assertEquals(tr.stats.endtime, tr.stats.starttime) 

 

    def test_rtrim(self): 

        """ 

        Tests the rtrim method of the Trace class. 

        """ 

        # set up 

        trace = Trace(data=np.arange(1000)) 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        trace.stats.starttime = start 

        trace.stats.sampling_rate = 200.0 

        end = UTCDateTime(2000, 1, 1, 0, 0, 4, 995000) 

        trace.verify() 

        # rtrim 100 samples 

        tr = deepcopy(trace) 

        tr._rtrim(0.5) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[-5:], 

                                      np.array([895, 896, 897, 898, 899])) 

        self.assertEquals(len(tr.data), 900) 

        self.assertEquals(tr.stats.npts, 900) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start) 

        self.assertEquals(tr.stats.endtime, end - 0.5) 

        # rtrim 202 samples 

        tr = deepcopy(trace) 

        tr._rtrim(1.010) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[-5:], 

                                      np.array([793, 794, 795, 796, 797])) 

        self.assertEquals(len(tr.data), 798) 

        self.assertEquals(tr.stats.npts, 798) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start) 

        self.assertEquals(tr.stats.endtime, end - 1.010) 

        # rtrim 1 minute via UTCDateTime 

        tr = deepcopy(trace) 

        tr._rtrim(UTCDateTime(2000, 1, 1, 0, 0, 3, 985000)) 

        tr.verify() 

        np.testing.assert_array_equal(tr.data[-5:], 

                                      np.array([793, 794, 795, 796, 797])) 

        self.assertEquals(len(tr.data), 798) 

        self.assertEquals(tr.stats.npts, 798) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start) 

        self.assertEquals(tr.stats.endtime, end - 1.010) 

        # some sanity checks 

        # negative end time 

        tr = deepcopy(trace) 

        t = UTCDateTime(1999, 12, 31) 

        tr._rtrim(t) 

        tr.verify() 

        self.assertEquals(tr.stats.endtime, t) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        # negative end time with given seconds 

        tr = deepcopy(trace) 

        tr._rtrim(100) 

        tr.verify() 

        self.assertEquals(tr.stats.endtime, trace.stats.endtime - 100) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        self.assertEquals(tr.stats.endtime, tr.stats.starttime) 

        # end time > start time 

        tr = deepcopy(trace) 

        t = UTCDateTime(2001) 

        tr._rtrim(t) 

        tr.verify() 

        self.assertEquals(tr.stats.endtime, t) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        self.assertEquals(tr.stats.endtime, tr.stats.starttime) 

        # end time > start time given seconds 

        tr = deepcopy(trace) 

        tr._rtrim(5.1) 

        tr.verify() 

        delta = int(math.floor(round(5.1 * trace.stats.sampling_rate, 7))) 

        endtime = trace.stats.starttime + trace.stats.delta * \ 

                  (trace.stats.npts - delta - 1) 

        self.assertEquals(tr.stats.endtime, endtime) 

        np.testing.assert_array_equal(tr.data, np.empty(0)) 

        # end time == start time 

        # returns one sample! 

        tr = deepcopy(trace) 

        tr._rtrim(4.995) 

        #XXX I do not understand why this fails!!! 

        #tr.verify() 

        np.testing.assert_array_equal(tr.data, np.array([0])) 

        self.assertEquals(len(tr.data), 1) 

        self.assertEquals(tr.stats.npts, 1) 

        self.assertEquals(tr.stats.sampling_rate, 200.0) 

        self.assertEquals(tr.stats.starttime, start) 

        self.assertEquals(tr.stats.endtime, start) 

 

    def test_rtrim_with_padding(self): 

        """ 

        Tests the _rtrim() method of the Trace class with padding. It has 

        already been tested in the two sided trimming tests. This is just to 

        have an explicit test. Also tests issue #429. 

        """ 

        # set up 

        trace = Trace(data=np.arange(10)) 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        trace.stats.starttime = start 

        trace.stats.sampling_rate = 1.0 

        trace.verify() 

 

        # Pad with no fill_value will mask the additional values. 

        tr = trace.copy() 

        end = tr.stats.endtime 

        tr._rtrim(end + 10, pad=True) 

        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10) 

        np.testing.assert_array_equal(tr.data[0:10], np.arange(10)) 

        # Check that the first couple of entries are not masked. 

        self.assertFalse(tr.data[0:10].mask.any()) 

        # All the other entries should be masked. 

        self.assertTrue(tr.data[10:].mask.all()) 

 

        # Pad with fill_value. 

        tr = trace.copy() 

        end = tr.stats.endtime 

        tr._rtrim(end + 10, pad=True, fill_value=-33) 

        self.assertEqual(tr.stats.endtime, trace.stats.endtime + 10) 

        # The first ten entries should not have changed. 

        np.testing.assert_array_equal(tr.data[0:10], np.arange(10)) 

        # The rest should be filled with the fill_value. 

        np.testing.assert_array_equal(tr.data[10:], np.ones(10) * -33) 

 

    def test_trim(self): 

        """ 

        Tests the trim method of the Trace class. 

        """ 

        # set up 

        trace = Trace(data=np.arange(1001)) 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        trace.stats.starttime = start 

        trace.stats.sampling_rate = 200.0 

        end = UTCDateTime(2000, 1, 1, 0, 0, 5, 0) 

        trace.verify() 

        # rtrim 100 samples 

        trace.trim(0.5, 0.5) 

        trace.verify() 

        np.testing.assert_array_equal(trace.data[-5:], 

                                      np.array([896, 897, 898, 899, 900])) 

        np.testing.assert_array_equal(trace.data[:5], 

                                      np.array([100, 101, 102, 103, 104])) 

        self.assertEquals(len(trace.data), 801) 

        self.assertEquals(trace.stats.npts, 801) 

        self.assertEquals(trace.stats.sampling_rate, 200.0) 

        self.assertEquals(trace.stats.starttime, start + 0.5) 

        self.assertEquals(trace.stats.endtime, end - 0.5) 

 

    def test_trimAllDoesNotChangeDtype(self): 

        """ 

        If a Trace is completely trimmed, e.g. no data samples are remaining, 

        the dtype should remain unchanged. 

 

        A trace with no data samples is not really senseful but the dtype 

        should not be changed anyways. 

        """ 

        # Choose non native dtype. 

        tr = Trace(np.arange(100, dtype='int16')) 

        tr.trim(UTCDateTime(10000), UTCDateTime(20000)) 

        # Assert the result. 

        self.assertEqual(len(tr.data), 0) 

        self.assertEqual(tr.data.dtype, 'int16') 

 

    def test_addTraceWithGap(self): 

        """ 

        Tests __add__ method of the Trace class. 

        """ 

        # set up 

        tr1 = Trace(data=np.arange(1000)) 

        tr1.stats.sampling_rate = 200 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        tr1.stats.starttime = start 

        tr2 = Trace(data=np.arange(0, 1000)[::-1]) 

        tr2.stats.sampling_rate = 200 

        tr2.stats.starttime = start + 10 

        # verify 

        tr1.verify() 

        tr2.verify() 

        # add 

        trace = tr1 + tr2 

        # stats 

        self.assertEquals(trace.stats.starttime, start) 

        self.assertEquals(trace.stats.endtime, start + 14.995) 

        self.assertEquals(trace.stats.sampling_rate, 200) 

        self.assertEquals(trace.stats.npts, 3000) 

        # data 

        self.assertEquals(len(trace), 3000) 

        self.assertEquals(trace[0], 0) 

        self.assertEquals(trace[999], 999) 

        self.assertTrue(is_masked(trace[1000])) 

        self.assertTrue(is_masked(trace[1999])) 

        self.assertEquals(trace[2000], 999) 

        self.assertEquals(trace[2999], 0) 

        # verify 

        trace.verify() 

 

    def test_addTraceWithOverlap(self): 

        """ 

        Tests __add__ method of the Trace class. 

        """ 

        # set up 

        tr1 = Trace(data=np.arange(1000)) 

        tr1.stats.sampling_rate = 200 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        tr1.stats.starttime = start 

        tr2 = Trace(data=np.arange(0, 1000)[::-1]) 

        tr2.stats.sampling_rate = 200 

        tr2.stats.starttime = start + 4 

        # add 

        trace = tr1 + tr2 

        # stats 

        self.assertEquals(trace.stats.starttime, start) 

        self.assertEquals(trace.stats.endtime, start + 8.995) 

        self.assertEquals(trace.stats.sampling_rate, 200) 

        self.assertEquals(trace.stats.npts, 1800) 

        # data 

        self.assertEquals(len(trace), 1800) 

        self.assertEquals(trace[0], 0) 

        self.assertEquals(trace[799], 799) 

        self.assertTrue(trace[800].mask) 

        self.assertTrue(trace[999].mask) 

        self.assertEquals(trace[1000], 799) 

        self.assertEquals(trace[1799], 0) 

        # verify 

        trace.verify() 

 

    def test_addSameTrace(self): 

        """ 

        Tests __add__ method of the Trace class. 

        """ 

        # set up 

        tr1 = Trace(data=np.arange(1001)) 

        # add 

        trace = tr1 + tr1 

        # should return exact the same values 

        self.assertEquals(trace.stats, tr1.stats) 

        np.testing.assert_array_equal(trace.data, tr1.data) 

        # verify 

        trace.verify() 

 

    def test_addTraceWithinTrace(self): 

        """ 

        Tests __add__ method of the Trace class. 

        """ 

        # set up 

        tr1 = Trace(data=np.arange(1001)) 

        tr1.stats.sampling_rate = 200 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        tr1.stats.starttime = start 

        tr2 = Trace(data=np.arange(201)) 

        tr2.stats.sampling_rate = 200 

        tr2.stats.starttime = start + 1 

        # add 

        trace = tr1 + tr2 

        # should return exact the same values like trace 1 

        self.assertEquals(trace.stats, tr1.stats) 

        mask = np.zeros(len(tr1)).astype("bool") 

        mask[200:401] = True 

        np.testing.assert_array_equal(trace.data.mask, mask) 

        np.testing.assert_array_equal(trace.data.data[:200], tr1.data[:200]) 

        np.testing.assert_array_equal(trace.data.data[401:], tr1.data[401:]) 

        # add the other way around 

        trace = tr2 + tr1 

        # should return exact the same values like trace 1 

        self.assertEquals(trace.stats, tr1.stats) 

        np.testing.assert_array_equal(trace.data.mask, mask) 

        np.testing.assert_array_equal(trace.data.data[:200], tr1.data[:200]) 

        np.testing.assert_array_equal(trace.data.data[401:], tr1.data[401:]) 

        # verify 

        trace.verify() 

 

    def test_addGapAndOverlap(self): 

        """ 

        Test order of merging traces. 

        """ 

        # set up 

        tr1 = Trace(data=np.arange(1000)) 

        tr1.stats.sampling_rate = 200 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        tr1.stats.starttime = start 

        tr2 = Trace(data=np.arange(1000)[::-1]) 

        tr2.stats.sampling_rate = 200 

        tr2.stats.starttime = start + 4 

        tr3 = Trace(data=np.arange(1000)[::-1]) 

        tr3.stats.sampling_rate = 200 

        tr3.stats.starttime = start + 12 

        # overlap 

        overlap = tr1 + tr2 

        self.assertEqual(len(overlap), 1800) 

        mask = np.zeros(1800).astype("bool") 

        mask[800:1000] = True 

        np.testing.assert_array_equal(overlap.data.mask, mask) 

        np.testing.assert_array_equal(overlap.data.data[:800], tr1.data[:800]) 

        np.testing.assert_array_equal(overlap.data.data[1000:], tr2.data[200:]) 

        # overlap + gap 

        overlap_gap = overlap + tr3 

        self.assertEqual(len(overlap_gap), 3400) 

        mask = np.zeros(3400).astype("bool") 

        mask[800:1000] = True 

        mask[1800:2400] = True 

        np.testing.assert_array_equal(overlap_gap.data.mask, mask) 

        np.testing.assert_array_equal(overlap_gap.data.data[:800], 

                                      tr1.data[:800]) 

        np.testing.assert_array_equal(overlap_gap.data.data[1000:1800], 

                                      tr2.data[200:]) 

        np.testing.assert_array_equal(overlap_gap.data.data[2400:], tr3.data) 

        # gap 

        gap = tr2 + tr3 

        self.assertEqual(len(gap), 2600) 

        mask = np.zeros(2600).astype("bool") 

        mask[1000:1600] = True 

        np.testing.assert_array_equal(gap.data.mask, mask) 

        np.testing.assert_array_equal(gap.data.data[:1000], tr2.data) 

        np.testing.assert_array_equal(gap.data.data[1600:], tr3.data) 

 

    def test_slice(self): 

        """ 

        Tests the slicing of trace objects. 

        """ 

        tr = Trace(data=np.arange(10, dtype='int32')) 

        mempos = tr.data.ctypes.data 

        t = tr.stats.starttime 

        tr1 = tr.slice(t + 2, t + 8) 

        tr1.data[0] = 10 

        self.assertEqual(tr.data[2], 10) 

        self.assertEqual(tr.data.ctypes.data, mempos) 

        self.assertEqual(tr.data[2:9].ctypes.data, tr1.data.ctypes.data) 

        self.assertEqual(tr1.data.ctypes.data - 8, mempos) 

 

    def test_slice_noStarttimeOrEndtime(self): 

        """ 

        Tests the slicing of trace objects with no starttime or endtime 

        provided. Compares results against the equivalent trim() operation 

        """ 

        tr_orig = Trace(data=np.arange(10, dtype='int32')) 

        tr = tr_orig.copy() 

        # two time points outside the trace and two inside 

        t1 = tr.stats.starttime - 2 

        t2 = tr.stats.starttime + 2 

        t3 = tr.stats.endtime - 3 

        t4 = tr.stats.endtime + 2 

        # test 1: only removing data at left side 

        tr_trim = tr_orig.copy() 

        tr_trim.trim(starttime=t2) 

        self.assertEqual(tr_trim, tr.slice(starttime=t2)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t2, endtime=t4)) 

        # test 2: only removing data at right side 

        tr_trim = tr_orig.copy() 

        tr_trim.trim(endtime=t3) 

        self.assertEqual(tr_trim, tr.slice(endtime=t3)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t1, endtime=t3)) 

        # test 3: not removing data at all 

        tr_trim = tr_orig.copy() 

        tr_trim.trim(starttime=t1, endtime=t4) 

        self.assertEqual(tr_trim, tr.slice()) 

        self.assertEqual(tr_trim, tr.slice(starttime=t1)) 

        self.assertEqual(tr_trim, tr.slice(endtime=t4)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t1, endtime=t4)) 

        tr_trim.trim() 

        self.assertEqual(tr_trim, tr.slice()) 

        self.assertEqual(tr_trim, tr.slice(starttime=t1)) 

        self.assertEqual(tr_trim, tr.slice(endtime=t4)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t1, endtime=t4)) 

        # test 4: removing data at left and right side 

        tr_trim = tr_orig.copy() 

        tr_trim.trim(starttime=t2, endtime=t3) 

        self.assertEqual(tr_trim, tr.slice(t2, t3)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t2, endtime=t3)) 

        # test 5: no data left after operation 

        tr_trim = tr_orig.copy() 

        tr_trim.trim(starttime=t4) 

        self.assertEqual(tr_trim, tr.slice(starttime=t4)) 

        self.assertEqual(tr_trim, tr.slice(starttime=t4, endtime=t4 + 1)) 

 

    def test_trimFloatingPoint(self): 

        """ 

        Tests the slicing of trace objects. 

        """ 

        # Create test array that allows for easy testing. 

        tr = Trace(data=np.arange(11)) 

        org_stats = deepcopy(tr.stats) 

        org_data = deepcopy(tr.data) 

        # Save memory position of array. 

        mem_pos = tr.data.ctypes.data 

        # Just some sanity tests. 

        self.assertEqual(tr.stats.starttime, UTCDateTime(0)) 

        self.assertEqual(tr.stats.endtime, UTCDateTime(10)) 

        # Create temp trace object used for testing. 

        st = tr.stats.starttime 

        # This is supposed to include the start- and endtimes and should 

        # therefore cut right at 2 and 8. 

        temp = deepcopy(tr) 

        temp.trim(st + 2.1, st + 7.1) 

        # Should be identical. 

        temp2 = deepcopy(tr) 

        temp2.trim(st + 2.0, st + 8.0) 

        self.assertEqual(temp.stats.starttime, UTCDateTime(2)) 

        self.assertEqual(temp.stats.endtime, UTCDateTime(7)) 

        self.assertEqual(temp.stats.npts, 6) 

        self.assertEqual(temp2.stats.npts, 7) 

        #self.assertEqual(temp.stats, temp2.stats) 

        np.testing.assert_array_equal(temp.data, temp2.data[:-1]) 

        # Create test array that allows for easy testing. 

        # Check if the data is the same. 

        self.assertNotEqual(temp.data.ctypes.data, tr.data[2:9].ctypes.data) 

        np.testing.assert_array_equal(tr.data[2:8], temp.data) 

        # Using out of bounds times should not do anything but create 

        # a copy of the stats. 

        temp = deepcopy(tr) 

        temp.trim(st - 2.5, st + 200) 

        # The start- and endtimes should not change. 

        self.assertEqual(temp.stats.starttime, UTCDateTime(0)) 

        self.assertEqual(temp.stats.endtime, UTCDateTime(10)) 

        self.assertEqual(temp.stats.npts, 11) 

        # Alter the new stats to make sure the old one stays intact. 

        temp.stats.starttime = UTCDateTime(1000) 

        self.assertEqual(org_stats, tr.stats) 

        # Check if the data adress is not the same, that is it is a copy 

        self.assertNotEqual(temp.data.ctypes.data, tr.data.ctypes.data) 

        np.testing.assert_array_equal(tr.data, temp.data) 

        # Make sure the original Trace object did not change. 

        np.testing.assert_array_equal(tr.data, org_data) 

        self.assertEqual(tr.data.ctypes.data, mem_pos) 

        self.assertEqual(tr.stats, org_stats) 

        # Use more complicated times and sampling rate. 

        tr = Trace(data=np.arange(111)) 

        tr.stats.starttime = UTCDateTime(111.11111) 

        tr.stats.sampling_rate = 50.0 

        org_stats = deepcopy(tr.stats) 

        org_data = deepcopy(tr.data) 

        # Save memory position of array. 

        mem_pos = tr.data.ctypes.data 

        # Create temp trace object used for testing. 

        temp = deepcopy(tr) 

        temp.trim(UTCDateTime(111.22222), UTCDateTime(112.99999), 

                  nearest_sample=False) 

        # Should again be identical. XXX NOT! 

        temp2 = deepcopy(tr) 

        temp2.trim(UTCDateTime(111.21111), UTCDateTime(113.01111), 

                   nearest_sample=False) 

        np.testing.assert_array_equal(temp.data, temp2.data[1:-1]) 

        # Check stuff. 

        self.assertEqual(temp.stats.starttime, UTCDateTime(111.23111)) 

        self.assertEqual(temp.stats.endtime, UTCDateTime(112.991110)) 

        # Check if the data is the same. 

        temp = deepcopy(tr) 

        temp.trim(UTCDateTime(0), UTCDateTime(1000 * 1000)) 

        self.assertNotEqual(temp.data.ctypes.data, tr.data.ctypes.data) 

        # starttime must be in conformance with sampling rate 

        t = UTCDateTime(111.11111) 

        self.assertEqual(temp.stats.starttime, t) 

        delta = int((tr.stats.starttime - t) * tr.stats.sampling_rate + .5) 

        np.testing.assert_array_equal(tr.data, temp.data[delta:delta + 111]) 

        # Make sure the original Trace object did not change. 

        np.testing.assert_array_equal(tr.data, org_data) 

        self.assertEqual(tr.data.ctypes.data, mem_pos) 

        self.assertEqual(tr.stats, org_stats) 

 

    def test_trimFloatingPointWithPadding1(self): 

        """ 

        Tests the slicing of trace objects with the use of the padding option. 

        """ 

        # Create test array that allows for easy testing. 

        tr = Trace(data=np.arange(11)) 

        org_stats = deepcopy(tr.stats) 

        org_data = deepcopy(tr.data) 

        # Save memory position of array. 

        mem_pos = tr.data.ctypes.data 

        # Just some sanity tests. 

        self.assertEqual(tr.stats.starttime, UTCDateTime(0)) 

        self.assertEqual(tr.stats.endtime, UTCDateTime(10)) 

        # Create temp trace object used for testing. 

        st = tr.stats.starttime 

        # Using out of bounds times should not do anything but create 

        # a copy of the stats. 

        temp = deepcopy(tr) 

        temp.trim(st - 2.5, st + 200, pad=True) 

        self.assertEqual(temp.stats.starttime.timestamp, -2.0) 

        self.assertEqual(temp.stats.endtime.timestamp, 200) 

        self.assertEqual(temp.stats.npts, 203) 

        mask = np.zeros(203).astype("bool") 

        mask[:2] = True 

        mask[13:] = True 

        np.testing.assert_array_equal(temp.data.mask, mask) 

        # Alter the new stats to make sure the old one stays intact. 

        temp.stats.starttime = UTCDateTime(1000) 

        self.assertEqual(org_stats, tr.stats) 

        # Check if the data adress is not the same, that is it is a copy 

        self.assertNotEqual(temp.data.ctypes.data, tr.data.ctypes.data) 

        np.testing.assert_array_equal(tr.data, temp.data[2:13]) 

        # Make sure the original Trace object did not change. 

        np.testing.assert_array_equal(tr.data, org_data) 

        self.assertEqual(tr.data.ctypes.data, mem_pos) 

        self.assertEqual(tr.stats, org_stats) 

 

    def test_trimFloatingPointWithPadding2(self): 

        """ 

        Use more complicated times and sampling rate. 

        """ 

        tr = Trace(data=np.arange(111)) 

        tr.stats.starttime = UTCDateTime(111.11111) 

        tr.stats.sampling_rate = 50.0 

        org_stats = deepcopy(tr.stats) 

        org_data = deepcopy(tr.data) 

        # Save memory position of array. 

        mem_pos = tr.data.ctypes.data 

        # Create temp trace object used for testing. 

        temp = deepcopy(tr) 

        temp.trim(UTCDateTime(111.22222), UTCDateTime(112.99999), 

                  nearest_sample=False) 

        # Should again be identical.#XXX not 

        temp2 = deepcopy(tr) 

        temp2.trim(UTCDateTime(111.21111), UTCDateTime(113.01111), 

                   nearest_sample=False) 

        np.testing.assert_array_equal(temp.data, temp2.data[1:-1]) 

        # Check stuff. 

        self.assertEqual(temp.stats.starttime, UTCDateTime(111.23111)) 

        self.assertEqual(temp.stats.endtime, UTCDateTime(112.991110)) 

        # Check if the data is the same. 

        temp = deepcopy(tr) 

        temp.trim(UTCDateTime(0), UTCDateTime(1000 * 1000), pad=True) 

        self.assertNotEqual(temp.data.ctypes.data, tr.data.ctypes.data) 

        # starttime must be in conformance with sampling rate 

        t = UTCDateTime(1969, 12, 31, 23, 59, 59, 991110) 

        self.assertEqual(temp.stats.starttime, t) 

        delta = int((tr.stats.starttime - t) * tr.stats.sampling_rate + .5) 

        np.testing.assert_array_equal(tr.data, temp.data[delta:delta + 111]) 

        # Make sure the original Trace object did not change. 

        np.testing.assert_array_equal(tr.data, org_data) 

        self.assertEqual(tr.data.ctypes.data, mem_pos) 

        self.assertEqual(tr.stats, org_stats) 

 

    def test_addOverlapsDefaultMethod(self): 

        """ 

        Test __add__ method of the Trace object. 

        """ 

        #1 

        # overlapping trace with differing data 

        # Trace 1: 0000000 

        # Trace 2:      1111111 

        tr1 = Trace(data=np.zeros(7)) 

        tr2 = Trace(data=np.ones(7)) 

        tr2.stats.starttime = tr1.stats.starttime + 5 

        # 1 + 2  : 00000--11111 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ma.masked_array)) 

        self.assertEqual(tr.data.tolist(), 

                         [0, 0, 0, 0, 0, None, None, 1, 1, 1, 1, 1]) 

        # 2 + 1  : 00000--11111 

        tr = tr2 + tr1 

        self.assertTrue(isinstance(tr.data, np.ma.masked_array)) 

        self.assertEqual(tr.data.tolist(), 

                         [0, 0, 0, 0, 0, None, None, 1, 1, 1, 1, 1]) 

        #2 

        # overlapping trace with same data 

        # Trace 1: 0000000 

        # Trace 2:      0000000 

        tr1 = Trace(data=np.zeros(7)) 

        tr2 = Trace(data=np.zeros(7)) 

        tr2.stats.starttime = tr1.stats.starttime + 5 

        # 1 + 2  : 000000000000 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ndarray)) 

        np.testing.assert_array_equal(tr.data, np.zeros(12)) 

        # 2 + 1  : 000000000000 

        tr = tr2 + tr1 

        self.assertTrue(isinstance(tr.data, np.ndarray)) 

        np.testing.assert_array_equal(tr.data, np.zeros(12)) 

        #3 

        # contained trace with same data 

        # Trace 1: 1111111111 

        # Trace 2:      11 

        tr1 = Trace(data=np.ones(10)) 

        tr2 = Trace(data=np.ones(2)) 

        tr2.stats.starttime = tr1.stats.starttime + 5 

        # 1 + 2  : 1111111111 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ndarray)) 

        np.testing.assert_array_equal(tr.data, np.ones(10)) 

        # 2 + 1  : 1111111111 

        tr = tr2 + tr1 

        self.assertTrue(isinstance(tr.data, np.ndarray)) 

        np.testing.assert_array_equal(tr.data, np.ones(10)) 

        #4 

        # contained trace with differing data 

        # Trace 1: 0000000000 

        # Trace 2:      11 

        tr1 = Trace(data=np.zeros(10)) 

        tr2 = Trace(data=np.ones(2)) 

        tr2.stats.starttime = tr1.stats.starttime + 5 

        # 1 + 2  : 00000--000 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ma.masked_array)) 

        self.assertEqual(tr.data.tolist(), 

                         [0, 0, 0, 0, 0, None, None, 0, 0, 0]) 

        # 2 + 1  : 00000--000 

        tr = tr2 + tr1 

        self.assertTrue(isinstance(tr.data, np.ma.masked_array)) 

        self.assertEqual(tr.data.tolist(), 

                         [0, 0, 0, 0, 0, None, None, 0, 0, 0]) 

        #5 

        # completely contained trace with same data until end 

        # Trace 1: 1111111111 

        # Trace 2: 1111111111 

        tr1 = Trace(data=np.ones(10)) 

        tr2 = Trace(data=np.ones(10)) 

        # 1 + 2  : 1111111111 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ndarray)) 

        np.testing.assert_array_equal(tr.data, np.ones(10)) 

        #6 

        # completely contained trace with differing data 

        # Trace 1: 0000000000 

        # Trace 2: 1111111111 

        tr1 = Trace(data=np.zeros(10)) 

        tr2 = Trace(data=np.ones(10)) 

        # 1 + 2  : ---------- 

        tr = tr1 + tr2 

        self.assertTrue(isinstance(tr.data, np.ma.masked_array)) 

        self.assertEqual(tr.data.tolist(), [None] * 10) 

 

    def test_addWithDifferentSamplingRates(self): 

        """ 

        Test __add__ method of the Trace object. 

        """ 

        # 1 - different sampling rates for the same channel should fail 

        tr1 = Trace(data=np.zeros(5)) 

        tr1.stats.sampling_rate = 200 

        tr2 = Trace(data=np.zeros(5)) 

        tr2.stats.sampling_rate = 50 

        self.assertRaises(TypeError, tr1.__add__, tr2) 

        self.assertRaises(TypeError, tr2.__add__, tr1) 

        # 2 - different sampling rates for the different channels works 

        tr1 = Trace(data=np.zeros(5)) 

        tr1.stats.sampling_rate = 200 

        tr1.stats.channel = 'EHE' 

        tr2 = Trace(data=np.zeros(5)) 

        tr2.stats.sampling_rate = 50 

        tr2.stats.channel = 'EHZ' 

        tr3 = Trace(data=np.zeros(5)) 

        tr3.stats.sampling_rate = 200 

        tr3.stats.channel = 'EHE' 

        tr4 = Trace(data=np.zeros(5)) 

        tr4.stats.sampling_rate = 50 

        tr4.stats.channel = 'EHZ' 

        # same sampling rate and ids should not fail 

        tr1 + tr3 

        tr3 + tr1 

        tr2 + tr4 

        tr4 + tr2 

 

    def test_addWithDifferentDatatypesOrID(self): 

        """ 

        Test __add__ method of the Trace object. 

        """ 

        # 1 - different data types for the same channel should fail 

        tr1 = Trace(data=np.zeros(5, dtype="int32")) 

        tr2 = Trace(data=np.zeros(5, dtype="float32")) 

        self.assertRaises(TypeError, tr1.__add__, tr2) 

        self.assertRaises(TypeError, tr2.__add__, tr1) 

        # 2 - different sampling rates for the different channels works 

        tr1 = Trace(data=np.zeros(5, dtype="int32")) 

        tr1.stats.channel = 'EHE' 

        tr2 = Trace(data=np.zeros(5, dtype="float32")) 

        tr2.stats.channel = 'EHZ' 

        tr3 = Trace(data=np.zeros(5, dtype="int32")) 

        tr3.stats.channel = 'EHE' 

        tr4 = Trace(data=np.zeros(5, dtype="float32")) 

        tr4.stats.channel = 'EHZ' 

        # same data types and ids should not fail 

        tr1 + tr3 

        tr3 + tr1 

        tr2 + tr4 

        tr4 + tr2 

        # adding traces with different ids should raise 

        self.assertRaises(TypeError, tr1.__add__, tr2) 

        self.assertRaises(TypeError, tr3.__add__, tr4) 

        self.assertRaises(TypeError, tr2.__add__, tr1) 

        self.assertRaises(TypeError, tr4.__add__, tr3) 

 

    def test_comparisons(self): 

        """ 

        Tests all rich comparison operators (==, !=, <, <=, >, >=) 

        The latter four are not implemented due to ambiguous meaning and bounce 

        an error. 

        """ 

        # create test traces 

        tr0 = Trace(np.arange(3)) 

        tr1 = Trace(np.arange(3)) 

        tr2 = Trace(np.arange(3), {'station': 'X'}) 

        tr3 = Trace(np.arange(3), {'processing': 

                                   ["filter:lowpass:{'freq': 10}"]}) 

        tr4 = Trace(np.arange(5)) 

        tr5 = Trace(np.arange(5), {'station': 'X'}) 

        tr6 = Trace(np.arange(5), {'processing': 

                                   ["filter:lowpass:{'freq': 10}"]}) 

        # tests that should raise a NotImplementedError (i.e. <=, <, >=, >) 

        self.assertRaises(NotImplementedError, tr1.__lt__, tr1) 

        self.assertRaises(NotImplementedError, tr1.__le__, tr1) 

        self.assertRaises(NotImplementedError, tr1.__gt__, tr1) 

        self.assertRaises(NotImplementedError, tr1.__ge__, tr1) 

        self.assertRaises(NotImplementedError, tr1.__lt__, tr2) 

        self.assertRaises(NotImplementedError, tr1.__le__, tr2) 

        self.assertRaises(NotImplementedError, tr1.__gt__, tr2) 

        self.assertRaises(NotImplementedError, tr1.__ge__, tr2) 

        # normal tests 

        self.assertEqual(tr0 == tr0, True) 

        self.assertEqual(tr0 == tr1, True) 

        self.assertEqual(tr0 == tr2, False) 

        self.assertEqual(tr0 == tr3, False) 

        self.assertEqual(tr0 == tr4, False) 

        self.assertEqual(tr0 == tr5, False) 

        self.assertEqual(tr0 == tr6, False) 

        self.assertEqual(tr5 == tr0, False) 

        self.assertEqual(tr5 == tr1, False) 

        self.assertEqual(tr5 == tr2, False) 

        self.assertEqual(tr5 == tr3, False) 

        self.assertEqual(tr5 == tr4, False) 

        self.assertEqual(tr5 == tr5, True) 

        self.assertEqual(tr5 == tr6, False) 

        self.assertEqual(tr3 == tr6, False) 

        self.assertEqual(tr0 != tr0, False) 

        self.assertEqual(tr0 != tr1, False) 

        self.assertEqual(tr0 != tr2, True) 

        self.assertEqual(tr0 != tr3, True) 

        self.assertEqual(tr0 != tr4, True) 

        self.assertEqual(tr0 != tr5, True) 

        self.assertEqual(tr0 != tr6, True) 

        self.assertEqual(tr5 != tr0, True) 

        self.assertEqual(tr5 != tr1, True) 

        self.assertEqual(tr5 != tr2, True) 

        self.assertEqual(tr5 != tr3, True) 

        self.assertEqual(tr5 != tr4, True) 

        self.assertEqual(tr5 != tr5, False) 

        self.assertEqual(tr5 != tr6, True) 

        self.assertEqual(tr3 != tr6, True) 

        # some weirder tests against non-Trace objects 

        for object in [0, 1, 0.0, 1.0, "", "test", True, False, [], [tr0], 

                       set(), set(tr0), {}, {"test": "test"}, [], None, ]: 

            self.assertEqual(tr0 == object, False) 

            self.assertEqual(tr0 != object, True) 

 

    def test_nearestSample(self): 

        """ 

        This test case shows that the libmseed is actually flooring the 

        starttime to the next sample value, regardless if it is the nearest 

        sample. The flag nearest_sample=True tries to avoids this and 

        rounds it to the next actual possible sample point. 

        """ 

        # set up 

        trace = Trace(data=np.empty(10000)) 

        trace.stats.starttime = UTCDateTime("2010-06-20T20:19:40.000000Z") 

        trace.stats.sampling_rate = 200.0 

        # ltrim 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.494999Z") 

        tr._ltrim(t - 3, nearest_sample=True) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.starttime, 

                         UTCDateTime("2010-06-20T20:19:48.495000Z")) 

        # Lots of tests follow that thoroughly check the cutting behavior 

        # using nearest_sample=True/False 

        # rtrim 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.494999Z") 

        tr._rtrim(t + 7, nearest_sample=True) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.495000Z") 

        tr._rtrim(t + 7, nearest_sample=True) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.495111Z") 

        tr._rtrim(t + 7, nearest_sample=True) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.497501Z") 

        tr._rtrim(t + 7, nearest_sample=True) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.500000Z")) 

        # rtrim 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.494999Z") 

        tr._rtrim(t + 7, nearest_sample=False) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.490000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.495000Z") 

        tr._rtrim(t + 7, nearest_sample=False) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.495111Z") 

        tr._rtrim(t + 7, nearest_sample=False) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

        tr = deepcopy(trace) 

        t = UTCDateTime("2010-06-20T20:19:51.497500Z") 

        tr._rtrim(t + 7, nearest_sample=False) 

        # see that it is actually rounded to the next sample point 

        self.assertEqual(tr.stats.endtime, 

                         UTCDateTime("2010-06-20T20:19:58.495000Z")) 

 

    def test_maskedArrayToString(self): 

        """ 

        Masked arrays should be marked using __str__. 

        """ 

        st = read() 

        overlaptrace = st[0].copy() 

        overlaptrace.stats.starttime += 1 

        st.append(overlaptrace) 

        st.merge() 

        out = st[0].__str__() 

        self.assertTrue(out.endswith('(masked)')) 

 

    def test_detrend(self): 

        """ 

        Test detrend method of trace 

        """ 

        t = np.arange(10) 

        data = 0.1 * t + 1. 

        tr = Trace(data=data.copy()) 

 

        tr.detrend(type='simple') 

        np.testing.assert_array_almost_equal(tr.data, np.zeros(10)) 

 

        tr.data = data.copy() 

        tr.detrend(type='linear') 

        np.testing.assert_array_almost_equal(tr.data, np.zeros(10)) 

 

        data = np.zeros(10) 

        data[3:7] = 1. 

 

        tr.data = data.copy() 

        tr.detrend(type='simple') 

        np.testing.assert_almost_equal(tr.data[0], 0.) 

        np.testing.assert_almost_equal(tr.data[-1], 0.) 

 

        tr.data = data.copy() 

        tr.detrend(type='linear') 

        np.testing.assert_almost_equal(tr.data[0], -0.4) 

        np.testing.assert_almost_equal(tr.data[-1], -0.4) 

 

    def test_differentiate(self): 

        """ 

        Test differentiation method of trace 

        """ 

        t = np.linspace(0., 1., 11) 

        data = 0.1 * t + 1. 

        tr = Trace(data=data) 

        tr.stats.delta = 0.1 

        tr.differentiate(type='gradient') 

        np.testing.assert_array_almost_equal(tr.data, np.ones(11) * 0.1) 

 

    def test_integrate(self): 

        """ 

        Test integration method of trace 

        """ 

        data = np.ones(101) * 0.01 

        tr = Trace(data=data) 

        tr.stats.delta = 0.1 

        tr.integrate(type='cumtrapz') 

        np.testing.assert_almost_equal(tr.data[-1], 0.1) 

 

    def test_issue317(self): 

        """ 

        Tests times after breaking a stream into parts and merging it again. 

        """ 

        # create a sample trace 

        org_trace = Trace(data=np.arange(22487)) 

        org_trace.stats.starttime = UTCDateTime() 

        org_trace.stats.sampling_rate = 0.999998927116 

        num_pakets = 10 

        # break org_trace into set of contiguous packet data 

        traces = [] 

        packet_length = int(np.size(org_trace.data) / num_pakets) 

        delta_time = org_trace.stats.delta 

        tstart = org_trace.stats.starttime 

        tend = tstart + delta_time * float(packet_length - 1) 

        for i in range(num_pakets): 

            tr = Trace(org_trace.data, org_trace.stats) 

            tr = tr.slice(tstart, tend) 

            traces.append(tr) 

            tstart = tr.stats.endtime + delta_time 

            tend = tstart + delta_time * float(packet_length - 1) 

        # reconstruct original trace by adding together packet traces 

        sum_trace = traces[0].copy() 

        npts = traces[0].stats.npts 

        for i in range(1, len(traces)): 

            sum_trace = sum_trace.__add__(traces[i].copy(), method=0, 

                                          interpolation_samples=0, 

                                          fill_value='latest', 

                                          sanity_checks=True) 

            # check npts 

            self.assertEquals(traces[i].stats.npts, npts) 

            self.assertEquals(sum_trace.stats.npts, (i + 1) * npts) 

            # check data 

            np.testing.assert_array_equal(traces[i].data, 

                                          np.arange(i * npts, (i + 1) * npts)) 

            np.testing.assert_array_equal(sum_trace.data, 

                                          np.arange(0, (i + 1) * npts)) 

            # check delta 

            self.assertEquals(traces[i].stats.delta, org_trace.stats.delta) 

            self.assertEquals(sum_trace.stats.delta, org_trace.stats.delta) 

            # check sampling rates 

            self.assertAlmostEquals(traces[i].stats.sampling_rate, 

                                    org_trace.stats.sampling_rate) 

            self.assertAlmostEquals(sum_trace.stats.sampling_rate, 

                                    org_trace.stats.sampling_rate) 

            # check endtimes 

            self.assertEquals(traces[i].stats.endtime, sum_trace.stats.endtime) 

 

    def test_verify(self): 

        """ 

        Tests verify method. 

        """ 

        # empty Trace 

        tr = Trace() 

        tr.verify() 

        # Trace with a single sample (issue #357) 

        tr = Trace(data=np.array([1])) 

        tr.verify() 

        # example Trace 

        tr = read()[0] 

        tr.verify() 

 

    def test_percent_in_str(self): 

        """ 

        Tests if __str__ method is working with percent sign (%). 

        """ 

        tr = Trace() 

        tr.stats.station = '%t3u' 

        self.assertTrue(tr.__str__().startswith(".%t3u.. | 1970")) 

 

    def test_taper(self): 

        """ 

        Test taper method of trace 

        """ 

        data = np.ones(10) 

        tr = Trace(data=data) 

        tr.taper() 

        for i in range(len(data)): 

            self.assertTrue(tr.data[i] <= 1.) 

            self.assertTrue(tr.data[i] >= 0.) 

 

    def test_times(self): 

        """ 

        Test if the correct times array is returned for normal traces and 

        traces with gaps. 

        """ 

        tr = Trace(data=np.ones(100)) 

        tr.stats.sampling_rate = 20 

        start = UTCDateTime(2000, 1, 1, 0, 0, 0, 0) 

        tr.stats.starttime = start 

        tm = tr.times() 

        self.assertAlmostEquals(tm[-1], tr.stats.endtime - tr.stats.starttime) 

        tr.data = np.ma.ones(100) 

        tr.data[30:40] = np.ma.masked 

        tm = tr.times() 

        self.assertTrue(np.alltrue(tr.data.mask == tm.mask)) 

 

    def test_modulo_operation(self): 

        """ 

        Method for testing the modulo operation. Mainly tests part not covered 

        by the doctests. 

        """ 

        tr = Trace(data=np.arange(25)) 

        # Wrong type raises. 

        self.assertRaises(TypeError, tr.__mod__, 5.0) 

        self.assertRaises(TypeError, tr.__mod__, "123") 

        # Needs to be a positive integer. 

        self.assertRaises(ValueError, tr.__mod__, 0) 

        self.assertRaises(ValueError, tr.__mod__, -11) 

        # If num is more then the number of samples, a copy will be returned. 

        st = tr % 500 

        self.assertTrue(tr == st[0]) 

        self.assertEqual(len(st), 1) 

        self.assertFalse(tr.data is st[0].data) 

 

    def test_plot(self): 

        """ 

        Tests plot method if matplotlib is installed 

        """ 

        try: 

            import matplotlib 

        except ImportError: 

            return 

        tr = Trace(data=np.arange(25)) 

        tr.plot(show=False) 

 

    def test_spectrogram(self): 

        """ 

        Tests spectrogram method if matplotlib is installed 

        """ 

        try: 

            import matplotlib 

        except ImportError: 

            return 

        tr = Trace(data=np.arange(25)) 

        tr.stats.sampling_rate = 20 

        tr.spectrogram(show=False) 

 

 

def suite(): 

    return unittest.makeSuite(TraceTestCase, 'test') 

 

 

if __name__ == '__main__': 

    unittest.main(defaultTest='suite')