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

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

""" 

The obspy.segy core test suite. 

""" 

 

import numpy as np 

from obspy import UTCDateTime, read 

from obspy.core.util import NamedTemporaryFile 

from obspy.segy.core import isSEGY, readSEGY, writeSEGY, SEGYCoreWritingError 

from obspy.segy.core import SEGYSampleIntervalError 

from obspy.segy.core import isSU, readSU, writeSU 

from obspy.segy.segy import SEGYError 

from obspy.segy.segy import readSEGY as readSEGYInternal 

from obspy.segy.tests.header import FILES, DTYPES 

import os 

from struct import unpack 

import unittest 

 

 

class SEGYCoreTestCase(unittest.TestCase): 

    """ 

    Test cases for SEG Y reading and writing.. 

    """ 

    def setUp(self): 

        # directory where the test files are located 

        self.dir = os.path.dirname(__file__) 

        self.path = os.path.join(self.dir, 'data') 

        # All the files and information about them. These files will be used in 

        # most tests. data_sample_enc is the encoding of the data value and 

        # sample_size the size in bytes of these samples. 

        self.files = FILES 

        self.dtypes = DTYPES 

 

    def test_isSEGYFile(self): 

        """ 

        Tests the isSEGY method. 

        """ 

        # Test all files in the test directory. 

        for file in self.files.keys(): 

            file = os.path.join(self.path, file) 

            self.assertEqual(isSEGY(file), True) 

        # Also check all the other files in the test directory and they should 

        # not work. Just check certain files to ensure reproducibility. 

        files = ['test_core.py', 'test_segy.py', '__init__.py'] 

        for file in files: 

            file = os.path.join(self.dir, file) 

            self.assertEqual(isSEGY(file), False) 

 

    def test_isSUFile(self): 

        """ 

        Tests the isSU method. 

        """ 

        # Test all SEG Y files in the test directory. 

        for file in self.files.keys(): 

            file = os.path.join(self.path, file) 

            self.assertEqual(isSU(file), False) 

        # Also check all the other files in the test directory and they should 

        # not work. Just check certain files to ensure reproducibility. 

        files = ['test_core.py', 'test_segy.py', '__init__.py'] 

        for file in files: 

            file = os.path.join(self.dir, file) 

            self.assertEqual(isSU(file), False) 

        # Check an actual Seismic Unix file. 

        file = os.path.join(self.path, '1.su_first_trace') 

        self.assertEqual(isSU(file), True) 

 

    def test_readHeadOnly(self): 

        """ 

        Tests headonly flag on readSEGY and readSU functions. 

        """ 

        # readSEGY 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        st = readSEGY(file, headonly=True) 

        self.assertEquals(st[0].stats.npts, 8000) 

        self.assertEquals(len(st[0].data), 0) 

        # readSU 

        file = os.path.join(self.path, '1.su_first_trace') 

        st = readSU(file, headonly=True) 

        self.assertEquals(st[0].stats.npts, 8000) 

        self.assertEquals(len(st[0].data), 0) 

 

    def test_enforcingTextualHeaderEncodingWhileReading(self): 

        """ 

        Tests whether or not the enforcing of the encoding of the textual file 

        header actually works. 

        """ 

        # File ld0042_file_00018.sgy_first_trace has an EBCDIC encoding. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        # Read once with EBCDIC encoding and check if it is correct. 

        st1 = readSEGY(file, textual_header_encoding='EBCDIC') 

        self.assertTrue(st1.stats.textual_file_header[3:21] \ 

                        == 'CLIENT: LITHOPROBE') 

        # This should also be written the stats dictionary. 

        self.assertEqual(st1.stats.textual_file_header_encoding, 

                         'EBCDIC') 

        # Reading again with ASCII should yield bad results. Lowercase keyword 

        # argument should also work. 

        st2 = readSEGY(file, textual_header_encoding='ascii') 

        self.assertFalse(st2.stats.textual_file_header[3:21] \ 

                        == 'CLIENT: LITHOPROBE') 

        self.assertEqual(st2.stats.textual_file_header_encoding, 

                         'ASCII') 

        # Autodection should also write the textual file header encoding to the 

        # stats dictionary. 

        st3 = readSEGY(file) 

        self.assertEqual(st3.stats.textual_file_header_encoding, 

                         'EBCDIC') 

 

    def test_enforcingEndiannessWhileWriting(self): 

        """ 

        Tests whether or not the enforcing of the endianness while writing 

        works. 

        """ 

        # File ld0042_file_00018.sgy_first_trace is in big endian. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st1 = readSEGY(file) 

        # First write should be big endian. 

        out_file = NamedTemporaryFile().name 

        writeSEGY(st1, out_file) 

        st2 = readSEGY(out_file) 

        os.remove(out_file) 

        self.assertEqual(st2.stats.endian, '>') 

        # Do once again to enforce big endian. 

        writeSEGY(st1, out_file, byteorder='>') 

        st3 = readSEGY(out_file) 

        os.remove(out_file) 

        self.assertEqual(st3.stats.endian, '>') 

        # Enforce little endian. 

        writeSEGY(st1, out_file, byteorder='<') 

        st4 = readSEGY(out_file) 

        os.remove(out_file) 

        self.assertEqual(st4.stats.endian, '<') 

 

    def test_settingDataEncodingWorks(self): 

        """ 

        Test whether or not the enforcing the data encoding works. 

        """ 

        # File ld0042_file_00018.sgy_first_trace uses IBM floating point 

        # representation. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st = readSEGY(file) 

        # First test if it even works. 

        out_file = NamedTemporaryFile().name 

        writeSEGY(st, out_file) 

        with open(out_file, 'rb') as f: 

            data1 = f.read() 

        os.remove(out_file) 

        # Write again and enforce encoding one which should yield the same 

        # result. 

        writeSEGY(st, out_file, data_encoding=1) 

        with open(out_file, 'rb') as f: 

            data2 = f.read() 

        os.remove(out_file) 

        self.assertTrue(data1 == data2) 

        # Writing IEEE floats which should not require any dtype changes. 

        writeSEGY(st, out_file, data_encoding=5) 

        with open(out_file, 'rb') as f: 

            data3 = f.read() 

        os.remove(out_file) 

        self.assertFalse(data1 == data3) 

 

    def test_readingAndWritingDifferentDataEncodings(self): 

        """ 

        Writes and reads different data encodings and checks if the data 

        remains the same. 

        """ 

        # The file uses IBM data encoding. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st = readSEGY(file) 

        data = st[0].data 

        # All working encodings with corresponding dtypes. 

        encodings = {1: 'float32', 

                     2: 'int32', 

                     3: 'int16', 

                     5: 'float32'} 

        out_file = NamedTemporaryFile().name 

        # Loop over all encodings. 

        for data_encoding, dtype in encodings.iteritems(): 

            this_data = np.require(data.copy(), dtype) 

            st[0].data = this_data 

            writeSEGY(st, out_file, data_encoding=data_encoding) 

            # Read again and compare data. 

            this_stream = readSEGY(out_file) 

            os.remove(out_file) 

            # Both should now be equal. Usually converting from IBM to IEEE 

            # floating point numbers might result in small rouning errors but 

            # in this case it seems to work. Might be different on different 

            # computers. 

            np.testing.assert_array_equal(this_data, this_stream[0].data) 

 

    def test_notMatchingDataEncodingAndDtypeRaises(self): 

        """ 

        obspy.segy does not automatically convert to the corresponding dtype. 

        """ 

        encodings = [1, 2, 3, 5] 

        # The file uses IBM data encoding. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st = readSEGY(file) 

        # Use float64 as the wrong encoding in every case. 

        st[0].data = np.require(st[0].data, 'float64') 

        out_file = NamedTemporaryFile().name 

        # Loop over all encodings. 

        for data_encoding in encodings: 

            self.assertRaises(SEGYCoreWritingError, writeSEGY, st, out_file, 

                              data_encoding=data_encoding) 

        os.remove(out_file) 

 

    def test_invalidDataEncodingRaises(self): 

        """ 

        Using an invalid data encoding raises an error. 

        """ 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st = readSEGY(file) 

        out_file = NamedTemporaryFile().name 

        self.assertRaises(SEGYCoreWritingError, writeSEGY, st, out_file, 

                          data_encoding=0) 

        self.assertRaises(SEGYCoreWritingError, writeSEGY, st, out_file, 

                          data_encoding='') 

        os.remove(out_file) 

 

    def test_enforcingTextualHeaderEncodingWhileWriting(self): 

        """ 

        Tests whether or not the enforcing of the endianness while writing 

        works. 

        """ 

        # File ld0042_file_00018.sgy_first_trace has an EBCDIC encoding. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        st1 = readSEGY(file) 

        # Save the header to compare it later on. 

        with open(file, 'rb') as f: 

            header = f.read(3200) 

        # First write should remain EBCDIC. 

        out_file = NamedTemporaryFile().name 

        writeSEGY(st1, out_file) 

        st2 = readSEGY(out_file) 

        # Compare header. 

        with open(out_file, 'rb') as f: 

            new_header = f.read(3200) 

        self.assertTrue(header == new_header) 

        os.remove(out_file) 

        self.assertEqual(st2.stats.textual_file_header_encoding, 

                         'EBCDIC') 

        # Do once again to enforce EBCDIC. 

        writeSEGY(st1, out_file, textual_header_encoding='EBCDIC') 

        st3 = readSEGY(out_file) 

        # Compare header. 

        with open(out_file, 'rb') as f: 

            new_header = f.read(3200) 

        self.assertTrue(header == new_header) 

        os.remove(out_file) 

        self.assertEqual(st3.stats.textual_file_header_encoding, 

                         'EBCDIC') 

        # Enforce ASCII 

        writeSEGY(st1, out_file, textual_header_encoding='ASCII') 

        st4 = readSEGY(out_file) 

        # Compare header. Should not be equal this time. 

        with open(out_file, 'rb') as f: 

            new_header = f.read(3200) 

        self.assertFalse(header == new_header) 

        os.remove(out_file) 

        self.assertEqual(st4.stats.textual_file_header_encoding, 

                         'ASCII') 

 

    def test_enforcingEndiannessWhileReading(self): 

        """ 

        Tests whether or not enforcing the endianness while reading a file 

        works. It will actually just deactivate the autodetection in case it 

        produced a wrong result. Using a wrong endianness while reading a file 

        will still produce an error because the data format will most likely be 

        wrong and therefore obspy.segy cannot unpack the data. 

        """ 

        # File ld0042_file_00018.sgy_first_trace is in big endian. 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        # This should work and write big endian to the stats dictionary. 

        st1 = readSEGY(file) 

        self.assertEqual(st1.stats.endian, '>') 

        # Doing the same with the right endianness should still work. 

        st2 = readSEGY(file, byteorder='>') 

        self.assertEqual(st2.stats.endian, '>') 

        # The wrong endianness should yield an key error because the routine to 

        # unpack the wrong data format code cannot be found. 

        self.assertRaises(KeyError, readSEGY, file, byteorder='<') 

 

    def test_readingUsingCore(self): 

        """ 

        This tests checks whether or not all necessary information is read 

        during reading with core. It actually just assumes the internal 

        SEGYFile object, which is thoroughly tested in 

        obspy.segy.tests.test_segy, is correct and compared all values to it. 

        This seems to be the easiest way to test everything. 

        """ 

        for file, _ in self.files.iteritems(): 

            file = os.path.join(self.path, file) 

            # Read the file with the internal SEGY representation. 

            segy_file = readSEGYInternal(file) 

            # Read again using core. 

            st = readSEGY(file) 

            # They all should have length one because all additional traces 

            # have been removed. 

            self.assertEqual(len(st), 1) 

            # Assert the data is the same. 

            np.testing.assert_array_equal(segy_file.traces[0].data, st[0].data) 

            # Textual header. 

            self.assertEqual(segy_file.textual_file_header, 

                             st.stats.textual_file_header) 

            # Textual_header_encoding. 

            self.assertEqual(segy_file.textual_header_encoding, 

                             st.stats.textual_file_header_encoding) 

            # Endianness. 

            self.assertEqual(segy_file.endian, st.stats.endian) 

            # Data encoding. 

            self.assertEqual(segy_file.data_encoding, 

                             st.stats.data_encoding) 

            # Test the file and trace binary headers. 

            for key, value in \ 

                    segy_file.binary_file_header.__dict__.iteritems(): 

                self.assertEqual(getattr(st.stats.binary_file_header, 

                                 key), value) 

            for key, value in \ 

                    segy_file.traces[0].header.__dict__.iteritems(): 

                self.assertEqual(getattr(st[0].stats.segy.trace_header, key), 

                                 value) 

 

    def test_writingUsingCore(self): 

        """ 

        Tests the writing of SEGY rev1 files using obspy.core. It just compares 

        the output of writing using obspy.core with the output of writing the 

        files using the internal SEGY object which is thoroughly tested in 

        obspy.segy.tests.test_segy. 

        """ 

        for file, _ in self.files.iteritems(): 

            file = os.path.join(self.path, file) 

            # Read the file with the internal SEGY representation. 

            segy_file = readSEGYInternal(file) 

            # Read again using core. 

            st = readSEGY(file) 

            # Create two temporary files to write to. 

            out_file1 = NamedTemporaryFile().name 

            out_file2 = NamedTemporaryFile().name 

            # Write twice. 

            segy_file.write(out_file1) 

            writeSEGY(st, out_file2) 

            # Read and delete files. 

            with open(out_file1, 'rb') as f1: 

                data1 = f1.read() 

            with open(out_file2, 'rb') as f2: 

                data2 = f2.read() 

            os.remove(out_file1) 

            os.remove(out_file2) 

            # Test if they are equal. 

            self.assertEqual(data1[3200:3600], data2[3200:3600]) 

 

    def test_invalidValuesForTextualHeaderEncoding(self): 

        """ 

        Invalid keyword arguments should be caught gracefully. 

        """ 

        file = os.path.join(self.path, 'ld0042_file_00018.sgy_first_trace') 

        self.assertRaises(SEGYError, readSEGY, file, 

                          textual_header_encoding='BLUB') 

 

    def test_settingDeltaandSamplingRateinStats(self): 

        """ 

        Just checks if the delta and sampling rate attributes are correctly 

        set. 

        Testing the delta value is enough because the stats attribute takes 

        care that delta/sampling rate always match. 

        """ 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        segy = readSEGY(file) 

        self.assertEqual(segy[0].stats.delta, 250E-6) 

        # The same with the Seismic Unix file. 

        file = os.path.join(self.path, '1.su_first_trace') 

        su = readSU(file) 

        self.assertEqual(su[0].stats.delta, 250E-6) 

 

    def test_writingNewSamplingRate(self): 

        """ 

        Setting a new sample rate works. 

        """ 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        segy = readSEGY(file) 

        segy[0].stats.sampling_rate = 20 

        outfile = NamedTemporaryFile().name 

        writeSEGY(segy, outfile) 

        new_segy = readSEGY(outfile) 

        os.remove(outfile) 

        self.assertEqual(new_segy[0].stats.sampling_rate, 20) 

        # The same with the Seismic Unix file. 

        file = os.path.join(self.path, '1.su_first_trace') 

        _su = readSU(file) 

 

    def test_readingDate(self): 

        """ 

        Reads one file with a set date. The date has been read with SeisView 2 

        by the DMNG. 

        """ 

        # Date as read by SeisView 2. 

        date = UTCDateTime(year=2005, julday=353, hour=15, minute=7, second=54) 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        segy = readSEGY(file) 

        self.assertEqual(date, segy[0].stats.starttime) 

        # The same with the Seismic Unix file. 

        file = os.path.join(self.path, '1.su_first_trace') 

        su = readSU(file) 

        self.assertEqual(date, su[0].stats.starttime) 

 

    def test_largeSampleRateIntervalRaises(self): 

        """ 

        SEG Y supports a sample interval from 1 to 65535 microseconds in steps 

        of 1 microsecond. Larger intervals cannot be supported due to the 

        definition of the SEG Y format. Therefore the smallest possible 

        sampling rate is ~ 15.26 Hz. 

        """ 

        outfile = NamedTemporaryFile().name 

        # Test for SEG Y. 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        segy = readSEGY(file) 

        # Set the largest possible delta value which should just work. 

        segy[0].stats.delta = 0.065535 

        writeSEGY(segy, outfile) 

        os.remove(outfile) 

        # Slightly larger should raise. 

        segy[0].stats.delta = 0.065536 

        self.assertRaises(SEGYSampleIntervalError, writeSEGY, segy, outfile) 

        # Same for SU. 

        file = os.path.join(self.path, '1.su_first_trace') 

        su = readSU(file) 

        # Set the largest possible delta value which should just work. 

        su[0].stats.delta = 0.065535 

        writeSU(su, outfile) 

        os.remove(outfile) 

        # Slightly larger should raise. 

        su[0].stats.delta = 0.065536 

        self.assertRaises(SEGYSampleIntervalError, writeSU, su, outfile) 

 

    def test_writingSUFileWithNoHeader(self): 

        """ 

        If the trace has no trace.su attribute, one should still be able to 

        write a SeismicUnix file. 

 

        This is not recommended because most Trace.stats attributes will be 

        lost while writing SU. 

        """ 

        st = read() 

        del st[1:] 

        st[0].data = np.require(st[0].data, 'float32') 

        outfile = NamedTemporaryFile().name 

        st.write(outfile, format='SU') 

        st2 = read(outfile) 

        os.remove(outfile) 

        # Compare new and old stream objects. All the other header attributes 

        # will not be set. 

        np.testing.assert_array_equal(st[0].data, st2[0].data) 

        self.assertEqual(st[0].stats.starttime, st2[0].stats.starttime) 

        self.assertEqual(st[0].stats.endtime, st2[0].stats.endtime) 

        self.assertEqual(st[0].stats.sampling_rate, st2[0].stats.sampling_rate) 

        # Writing and reading this new stream object should not change 

        # anything. 

        st2.write(outfile, format='SU') 

        st3 = read(outfile) 

        os.remove(outfile) 

        np.testing.assert_array_equal(st2[0].data, st3[0].data) 

        # Remove the su attributes because they will not be equal due to lazy 

        # header attributes. 

        del st2[0].stats.su 

        del st3[0].stats.su 

        self.assertEqual(st2[0].stats, st3[0].stats) 

 

    def test_writingModifiedDate(self): 

        """ 

        Tests if the date in Trace.stats.starttime is correctly written in SU 

        and SEGY files. 

        """ 

        # Define new date! 

        new_date = UTCDateTime(2010, 7, 7, 2, 2, 2) 

        outfile = NamedTemporaryFile().name 

        # Test for SEGY. 

        file = os.path.join(self.path, 'example.y_first_trace') 

        segy = readSEGY(file) 

        segy[0].stats.starttime = new_date 

        writeSEGY(segy, outfile) 

        segy_new = readSEGY(outfile) 

        os.remove(outfile) 

        self.assertEqual(new_date, segy_new[0].stats.starttime) 

        # Test for SU. 

        file = os.path.join(self.path, '1.su_first_trace') 

        su = readSU(file) 

        su[0].stats.starttime = new_date 

        writeSU(su, outfile) 

        su_new = readSU(outfile) 

        os.remove(outfile) 

        self.assertEqual(new_date, su_new[0].stats.starttime) 

 

    def test_writingStarttimeTimestamp0(self): 

        """ 

        If the starttime of the Trace is UTCDateTime(0) it will be interpreted 

        as a missing starttime is not written. Test if this holds True. 

        """ 

        file = os.path.join(self.path, '1.sgy_first_trace') 

        # This file has a set date! 

        with open(file, 'rb') as f: 

            f.seek(3600 + 156, 0) 

            date_time = f.read(10) 

        year, julday, hour, minute, second = unpack('>5h', date_time) 

        self.assertEqual([year == 2005, julday == 353, hour == 15, minute == 7, 

                          second == 54], 5 * [True]) 

        # Read and set zero time. 

        segy = readSEGY(file) 

        segy[0].stats.starttime = UTCDateTime(0) 

        outfile = NamedTemporaryFile().name 

        writeSEGY(segy, outfile) 

        # Check the new date. 

        with open(outfile, 'rb') as f: 

            f.seek(3600 + 156, 0) 

            date_time = f.read(10) 

        os.remove(outfile) 

        year, julday, hour, minute, second = unpack('>5h', date_time) 

        self.assertEqual([year == 0, julday == 0, hour == 0, minute == 0, 

                          second == 0], 5 * [True]) 

        # The same for SU. 

        file = os.path.join(self.path, '1.su_first_trace') 

        # This file has a set date! 

        with open(file, 'rb') as f: 

            f.seek(156, 0) 

            date_time = f.read(10) 

        year, julday, hour, minute, second = unpack('<5h', date_time) 

        self.assertEqual([year == 2005, julday == 353, hour == 15, minute == 7, 

                          second == 54], 5 * [True]) 

        # Read and set zero time. 

        su = readSU(file) 

        su[0].stats.starttime = UTCDateTime(0) 

        outfile = NamedTemporaryFile().name 

        writeSU(su, outfile) 

        # Check the new date. 

        with open(outfile, 'rb') as f: 

            f.seek(156, 0) 

            date_time = f.read(10) 

        os.remove(outfile) 

        year, julday, hour, minute, second = unpack('<5h', date_time) 

        self.assertEqual([year == 0, julday == 0, hour == 0, minute == 0, 

                          second == 0], 5 * [True]) 

 

    def test_TwoDigitYearsSEGY(self): 

        """ 

        Even tough not specified in the 1975 SEG Y rev 1 standard, 2 digit 

        years should be read correctly. Some programs produce them. 

 

        Every two digit year < 30 will be mapped to 2000-2029 and every two 

        digit year >=30 <100 will be mapped to 1930-1999. 

        """ 

        # Read two artificial test files and check the years. 

        filename = os.path.join(self.path, 'one_trace_year_11.sgy') 

        st = readSEGY(filename) 

        self.assertEqual(2011, st[0].stats.starttime.year) 

        filename = os.path.join(self.path, 'one_trace_year_99.sgy') 

        st = readSEGY(filename) 

        self.assertEqual(1999, st[0].stats.starttime.year) 

 

    def test_TwoDigitYearsSU(self): 

        """ 

        Same test as test_TwoDigitYearsSEGY just for Seismic Unix files. 

        """ 

        # Read two artificial test files and check the years. 

        filename = os.path.join(self.path, 'one_trace_year_11.su') 

        st = readSU(filename) 

        self.assertEqual(2011, st[0].stats.starttime.year) 

        filename = os.path.join(self.path, 'one_trace_year_99.su') 

        st = readSU(filename) 

        self.assertEqual(1999, st[0].stats.starttime.year) 

 

    def test_issue377(self): 

        """ 

        Tests that readSEGY() and stream.write() should handle negative trace 

        header values. 

        """ 

        filename = os.path.join(self.path, 'one_trace_year_11.sgy') 

        st = readSEGY(filename) 

        st[0].stats.segy.trace_header['source_coordinate_x'] = -1 

        outfile = NamedTemporaryFile().name 

        st.write(outfile, format='SEGY') 

        os.remove(outfile) 

 

 

def suite(): 

    return unittest.makeSuite(SEGYCoreTestCase, 'test') 

 

 

if __name__ == '__main__': 

    unittest.main(defaultTest='suite')