def su_specfem3d_obspy(prefix='SEM', channel=None, suffix='', byteorder='<', verbose=False): """ Reads Seismic Unix file """ from obspy.segy.core import readSU if channel in ['x']: wildcard = '%s/*_dx_SU%s' % (prefix, suffix) elif channel in ['y']: wildcard = '%s/*_dy_SU%s' % (prefix, suffix) elif channel in ['z']: wildcard = '%s/*_dz_SU%s' % (prefix, suffix) elif channel in ['p']: wildcard = '%s/*_dp_SU%s' % (prefix, suffix) else: raise ValueError('CHANNEL must be one of the following: x y z p') filenamess = _glob.glob(wildcard) sort_by = lambda x: int(unix.basename(x).split('_')[0]) filenames = sorted(filenamess, key=sort_by) streamobj = readSU(filenames.pop(), byteorder=byteorder) for filename in filenames: streamobj += readSU(filename, byteorder=byteorder) return streamobj
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_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 with NamedTemporaryFile() as tf: outfile = tf.name writeSEGY(segy, outfile) new_segy = readSEGY(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') readSU(file)
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. """ with NamedTemporaryFile() as tf: outfile = tf.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) # 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) # Slightly larger should raise. su[0].stats.delta = 0.065536 self.assertRaises(SEGYSampleIntervalError, writeSU, su, outfile)
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_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_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) with NamedTemporaryFile() as tf: outfile = tf.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) 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) self.assertEqual(new_date, su_new[0].stats.starttime)
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_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_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_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) with NamedTemporaryFile() as tf: outfile = tf.name writeSEGY(segy, outfile) # Check the new date. with open(outfile, '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 == 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) with NamedTemporaryFile() as tf: outfile = tf.name writeSU(su, outfile) # Check the new date. with open(outfile, 'rb') as f: f.seek(156, 0) date_time = f.read(10) year, julday, hour, minute, second = unpack('<5h', date_time) self.assertEqual([year == 0, julday == 0, hour == 0, minute == 0, second == 0], 5 * [True])