[segyio] 234/376: Make file.samples a list, not scalar
Jørgen Kvalsvik
jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:38 UTC 2017
This is an automated email from the git hooks/post-receive script.
jokva-guest pushed a commit to branch debian
in repository segyio.
commit 52c14ca9378c699160770535622fc690fe6908b3
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date: Wed Mar 8 14:45:45 2017 +0100
Make file.samples a list, not scalar
Analoguous to file.xlines, file.ilines, file.samples is now a list of
samples and their identifiers. If possible, this is a list derived from
the binary- and trace headers with a recording delay t0 and a sample
interval. If these can't be figured out they're assumed to be 0 and
4000μs (4ms) respectively.
As with the other identifiers, the cardinality is now obtained with
len().
---
python/examples/copy-sub-cube.py | 2 +-
python/examples/make-file.py | 6 +++---
python/examples/make-ps-file.py | 6 +++---
python/segyio/_raw_trace.py | 2 +-
python/segyio/_trace.py | 17 ++++++++++-------
python/segyio/create.py | 10 +++++-----
python/segyio/open.py | 9 +++++++--
python/segyio/segy.py | 22 +++++++++++++++-------
python/segyio/tools.py | 7 ++++---
python/test/segy.py | 36 +++++++++++++++++++-----------------
python/test/tools.py | 12 ++++++------
11 files changed, 74 insertions(+), 55 deletions(-)
diff --git a/python/examples/copy-sub-cube.py b/python/examples/copy-sub-cube.py
index 900680e..08ae176 100644
--- a/python/examples/copy-sub-cube.py
+++ b/python/examples/copy-sub-cube.py
@@ -14,7 +14,7 @@ def main():
spec = segyio.spec()
spec.sorting = int(src.sorting)
spec.format = int(src.format)
- spec.samples = 50
+ spec.samples = range(50)
spec.ilines = src.ilines[:5]
spec.xlines = src.xlines[:5]
diff --git a/python/examples/make-file.py b/python/examples/make-file.py
index 8fb76cd..d2a216e 100644
--- a/python/examples/make-file.py
+++ b/python/examples/make-file.py
@@ -16,7 +16,7 @@ def main():
# the absolute minimal specification for a N-by-M volume
spec.sorting = 2
spec.format = 1
- spec.samples = int(sys.argv[2])
+ spec.samples = range(int(sys.argv[2]))
spec.ilines = range(*map(int, sys.argv[3:5]))
spec.xlines = range(*map(int, sys.argv[5:7]))
@@ -31,13 +31,13 @@ def main():
# looking up an inline's i's jth crosslines' k should be roughly equal
# to i.j0k
trace = np.arange(start = start,
- stop = start + step * spec.samples,
+ stop = start + step * len(spec.samples),
step = step,
dtype = np.single)
# one inline is N traces concatenated. We fill in the xline number
line = np.concatenate([trace + (xl / 100.0) for xl in spec.xlines])
- line = line.reshape( (len(spec.xlines), spec.samples) )
+ line = line.reshape( (len(spec.xlines), len(spec.samples)) )
# write the line itself to the file
# write the inline number in all this line's headers
diff --git a/python/examples/make-ps-file.py b/python/examples/make-ps-file.py
index 0fed619..de5101b 100644
--- a/python/examples/make-ps-file.py
+++ b/python/examples/make-ps-file.py
@@ -20,7 +20,7 @@ def main():
# N-by-M volume with K offsets volume
spec.sorting = 2
spec.format = 1
- spec.samples = int(sys.argv[2])
+ spec.samples = range(int(sys.argv[2]))
spec.ilines = range(*map(int, sys.argv[3:5]))
spec.xlines = range(*map(int, sys.argv[5:7]))
spec.offsets = range(*map(int, sys.argv[7:9]))
@@ -37,14 +37,14 @@ def main():
# looking up an inline's i's jth crosslines' k should be roughly equal
# to (offset*100) + i.j0k.
trace = np.arange(start = start,
- stop = start + step * spec.samples,
+ stop = start + step * len(spec.samples),
step = step,
dtype = np.single)
nx, no = len(spec.xlines), len(spec.offsets)
# one inline is N traces concatenated. We fill in the xline number
line = np.concatenate([trace + (xl / 100.0) for xl in spec.xlines])
- line = line.reshape( (nx, spec.samples) )
+ line = line.reshape( (nx, len(spec.samples)) )
for ilindex, ilno in enumerate(spec.ilines):
iline = line + ilno
diff --git a/python/segyio/_raw_trace.py b/python/segyio/_raw_trace.py
index d745300..4a7113e 100644
--- a/python/segyio/_raw_trace.py
+++ b/python/segyio/_raw_trace.py
@@ -13,7 +13,7 @@ class RawTrace(object):
start, stop, step = index.indices(f.tracecount)
mstart, mstop = min(start, stop), max(start, stop)
length = max(0, (mstop - mstart + (step - (1 if step > 0 else -1))))
- buf = np.zeros(shape = (length, f.samples), dtype = np.single)
+ buf = np.zeros(shape = (length, len(f.samples)), dtype = np.single)
return self.trace._readtr(index, buf)
diff --git a/python/segyio/_trace.py b/python/segyio/_trace.py
index ae71d6d..9643f1d 100644
--- a/python/segyio/_trace.py
+++ b/python/segyio/_trace.py
@@ -41,7 +41,7 @@ class Trace:
if val.dtype != np.single:
raise TypeError("Numpy array must be of type single")
- shape = (self._file.samples,)
+ shape = (len(self._file.samples),)
if val.shape[0] < shape[0]:
raise TypeError("Array wrong shape. Expected minimum %s, was %s" % (shape, val.shape))
@@ -66,14 +66,14 @@ class Trace:
return "Trace(traces = {}, samples = {})".format(len(self), self._file.samples)
def _trace_buffer(self, buf=None):
- samples = self._file.samples
+ shape = self._file.samples.shape
if buf is None:
- buf = np.empty(shape=samples, dtype=np.single)
+ buf = np.empty(shape=shape, dtype=np.single)
elif not isinstance(buf, np.ndarray):
raise TypeError("Buffer must be None or numpy.ndarray")
elif buf.dtype != np.single:
- buf = np.empty(shape=samples, dtype=np.single)
+ buf = np.empty(shape=shape, dtype=np.single)
return buf
@@ -84,8 +84,8 @@ class Trace:
trace0 = self._file._tr0
bsz = self._file._bsz
fmt = self._file._fmt
- samples = self._file.samples
- return segyio._segyio.read_trace(self._file.xfd, traceno, tracecount, buf, trace0, bsz, fmt, samples)
+ smp = len(self._file.samples)
+ return segyio._segyio.read_trace(self._file.xfd, traceno, tracecount, buf, trace0, bsz, fmt, smp)
def _writetr(self, traceno, buf):
self.write_trace(traceno, buf, self._file)
@@ -97,7 +97,10 @@ class Trace:
:type buf: ?
:type segy: segyio.SegyFile
"""
- segyio._segyio.write_trace(segy.xfd, traceno, buf, segy._tr0, segy._bsz, segy._fmt, segy.samples)
+ segyio._segyio.write_trace(segy.xfd, traceno,
+ buf,
+ segy._tr0, segy._bsz,
+ segy._fmt, len(segy.samples))
@property
def raw(self):
diff --git a/python/segyio/create.py b/python/segyio/create.py
index edd0840..15918aa 100644
--- a/python/segyio/create.py
+++ b/python/segyio/create.py
@@ -45,7 +45,7 @@ def create(filename, spec):
>>> spec = segyio.spec()
>>> spec.ilines = [1, 2, 3, 4]
>>> spec.xlines = [11, 12, 13]
- >>> spec.samples = 50
+ >>> spec.samples = list(range(50))
>>> spec.sorting = 2
>>> spec.format = 1
>>> with segyio.create(path, spec) as f:
@@ -57,7 +57,7 @@ def create(filename, spec):
... spec = segyio.spec()
... spec.sorting = src.sorting
... spec.format = src.format
- ... spec.samples = src.samples - 50
+ ... spec.samples = src.samples[:len(src.samples) - 50]
... spec.ilines = src.ilines
... spec.xline = src.xlines
... with segyio.create(dstpath, spec) as dst:
@@ -69,9 +69,9 @@ def create(filename, spec):
"""
f = segyio.SegyFile(filename, "w+")
- f._samples = spec.samples
+ f._samples = numpy.asarray(spec.samples, dtype = numpy.intc)
f._ext_headers = spec.ext_headers
- f._bsz = _segyio.trace_bsize(f.samples)
+ f._bsz = _segyio.trace_bsize(len(f.samples))
txt_hdr_sz = _segyio.textheader_size()
bin_hdr_sz = _segyio.binheader_size()
@@ -99,7 +99,7 @@ def create(filename, spec):
f.bin = {
3213: f.tracecount,
3217: 4000,
- 3221: f.samples,
+ 3221: len(f.samples),
3225: f.format,
3505: f.ext_headers,
}
diff --git a/python/segyio/open.py b/python/segyio/open.py
index e340793..04f9222 100644
--- a/python/segyio/open.py
+++ b/python/segyio/open.py
@@ -58,12 +58,17 @@ def open(filename, mode="r", iline=189, xline=193, strict = True):
try:
metrics = segyio._segyio.init_metrics(f.xfd, f.bin.buf)
- f._samples = metrics['sample_count']
f._tr0 = metrics['trace0']
f._fmt = metrics['format']
f._bsz = metrics['trace_bsize']
- f._ext_headers = (f._tr0 - 3600) // 3200 # should probably be from C
f._tracecount = metrics['trace_count']
+ f._ext_headers = (f._tr0 - 3600) // 3200 # should probably be from C
+
+ dt = segyio.tools.dt(f, fallback_dt = 4000.0)
+ t0 = f.header[0][segyio.TraceField.DelayRecordingTime]
+ sample_count = metrics['sample_count']
+ f._samples = numpy.array([t0 + i * dt for i in range(sample_count)],
+ dtype = numpy.intc)
except:
f.close()
diff --git a/python/segyio/segy.py b/python/segyio/segy.py
index 23ca5c6..af3a28c 100644
--- a/python/segyio/segy.py
+++ b/python/segyio/segy.py
@@ -477,12 +477,16 @@ class SegyFile(object):
return buf
def _line_buffer(self, length, buf=None):
- shape = (length, self.samples)
+ shape = (length, len(self.samples))
return self._shape_buffer(shape, buf)
def _fread_line(self, trace0, length, stride, buf):
offsets = len(self.offsets)
- return _segyio.read_line(self.xfd, trace0, length, stride, offsets, buf, self._tr0, self._bsz, self._fmt, self.samples)
+ return _segyio.read_line(self.xfd, trace0,
+ length, stride, offsets,
+ buf,
+ self._tr0, self._bsz,
+ self._fmt, len(self.samples))
@property
def ilines(self):
@@ -856,8 +860,8 @@ class SegyFile(object):
if self.unstructured:
raise ValueError(self._unstructured_errmsg)
- indices = np.asarray(list(range(self.samples)), dtype=np.uintc)
- other_indices = np.asarray([0], dtype=np.uintc)
+ indices = np.asarray(list(range(len(self.samples))), dtype=np.intc)
+ other_indices = np.asarray([0], dtype=np.intc)
buffn = self._depth_buffer
slice_trace_count = self._iline_length * self._xline_length
@@ -865,10 +869,14 @@ class SegyFile(object):
tr0 = self._tr0
bsz = self._bsz
fmt = self._fmt
- samples = self.samples
def readfn(depth, length, stride, buf):
- _segyio.depth_slice(self.xfd, depth, slice_trace_count, offsets, buf, tr0, bsz, fmt, samples)
+ _segyio.depth_slice(self.xfd, depth,
+ slice_trace_count, offsets,
+ buf,
+ tr0, bsz,
+ fmt,
+ len(self.samples))
return buf
def writefn(depth, length, stride, val):
@@ -880,7 +888,7 @@ class SegyFile(object):
trace_buf[depth] = buf_view[i]
self.trace[i] = trace_buf
- return Line(self, self.samples, 1, indices, other_indices, buffn, readfn, writefn, "Depth")
+ return Line(self, len(self.samples), 1, indices, other_indices, buffn, readfn, writefn, "Depth")
@depth_slice.setter
def depth_slice(self, value):
diff --git a/python/segyio/tools.py b/python/segyio/tools.py
index 2b51679..d7bd667 100644
--- a/python/segyio/tools.py
+++ b/python/segyio/tools.py
@@ -27,7 +27,7 @@ def sample_indexes(segyfile, t0=0.0, dt_override=None):
if dt_override is None:
dt_override = dt(segyfile)
- return [t0 + t * dt_override for t in range(segyfile.samples)]
+ return [t0 + t * dt_override for t in range(len(segyfile.samples))]
def create_text_header(lines):
@@ -117,6 +117,7 @@ def cube(f):
ilsort = f.sorting == segyio.TraceSortingFormat.INLINE_SORTING
fast = f.ilines if ilsort else f.xlines
slow = f.xlines if ilsort else f.ilines
- fast, slow, offs, samples = len(fast), len(slow), len(f.offsets), f.samples
- dims = (fast, slow, samples) if offs == 1 else (fast, slow, offs, samples)
+ fast, slow, offs = len(fast), len(slow), len(f.offsets)
+ smps = len(f.samples)
+ dims = (fast, slow, smps) if offs == 1 else (fast, slow, offs, smps)
return f.trace.raw[:].reshape(dims)
diff --git a/python/test/segy.py b/python/test/segy.py
index 643ab2b..fe90c2c 100644
--- a/python/test/segy.py
+++ b/python/test/segy.py
@@ -25,7 +25,7 @@ def mklines(fname):
spec = segyio.spec()
spec.format = 5
spec.sorting = 2
- spec.samples = 10
+ spec.samples = range(10)
spec.ilines = range(1, 11)
spec.xlines = range(1, 6)
@@ -62,7 +62,7 @@ class TestSegy(TestCase):
def test_inline_4(self):
with segyio.open(self.filename, "r") as f:
- sample_count = f.samples
+ sample_count = len(f.samples)
self.assertEqual(50, sample_count)
data = f.iline[4]
@@ -96,31 +96,33 @@ class TestSegy(TestCase):
data = f.xline[22]
+ size = len(f.samples)
+
# first iline
# first sample
self.assertAlmostEqual(1.22, data[0, 0], places = 5)
# middle sample
- self.assertAlmostEqual(1.22024, data[0, f.samples//2-1], places = 6)
+ self.assertAlmostEqual(1.22024, data[0, size//2-1], places = 6)
# last sample
- self.assertAlmostEqual(1.22049, data[0, f.samples-1], places = 6)
+ self.assertAlmostEqual(1.22049, data[0, size-1], places = 6)
# middle iline
middle_line = 2
# first sample
self.assertAlmostEqual(3.22, data[middle_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(3.22024, data[middle_line, f.samples//2-1], places = 6)
+ self.assertAlmostEqual(3.22024, data[middle_line, size//2-1], places = 6)
# last sample
- self.assertAlmostEqual(3.22049, data[middle_line, f.samples-1], places = 6)
+ self.assertAlmostEqual(3.22049, data[middle_line, size-1], places = 6)
# last iline
last_line = len(f.ilines)-1
# first sample
self.assertAlmostEqual(5.22, data[last_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(5.22024, data[last_line, f.samples//2-1], places = 6)
+ self.assertAlmostEqual(5.22024, data[last_line, size//2-1], places = 6)
# last sample
- self.assertAlmostEqual(5.22049, data[last_line, f.samples-1], places = 6)
+ self.assertAlmostEqual(5.22049, data[last_line, size-1], places = 6)
def test_iline_slicing(self):
with segyio.open(self.filename, "r") as f:
@@ -169,7 +171,7 @@ class TestSegy(TestCase):
self.assertEqual(ilines, list(f.ilines))
self.assertEqual(25, f.tracecount)
self.assertEqual(len(f.trace), f.tracecount)
- self.assertEqual(50, f.samples)
+ self.assertEqual(50, len(f.samples))
def test_open_nostrict(self):
with segyio.open(self.filename, strict = False):
@@ -606,7 +608,7 @@ class TestSegy(TestCase):
spec = segyio.spec()
spec.format = int(src.format)
spec.sorting = int(src.sorting)
- spec.samples = 20 # reduces samples per trace
+ spec.samples = src.samples[:20] # reduces samples per trace
spec.ilines = src.ilines
spec.xlines = src.xlines
@@ -626,7 +628,7 @@ class TestSegy(TestCase):
dst.xline[lineno] = src.xline[lineno]
with segyio.open(dst_file, "r") as dst:
- self.assertEqual(20, dst.samples)
+ self.assertEqual(20, len(dst.samples))
self.assertEqual([x + 100 for x in src.ilines], list(dst.ilines))
def test_create_from_naught(self):
@@ -635,7 +637,7 @@ class TestSegy(TestCase):
spec = segyio.spec()
spec.format = 5
spec.sorting = 2
- spec.samples = 150
+ spec.samples = range(150)
spec.ilines = range(1, 11)
spec.xlines = range(1, 6)
@@ -669,7 +671,7 @@ class TestSegy(TestCase):
spec = segyio.spec()
spec.format = 5
spec.sorting = 2
- spec.samples = 7
+ spec.samples = range(7)
spec.ilines = range(1, 4)
spec.xlines = range(1, 3)
spec.offsets = range(1, 6)
@@ -760,7 +762,7 @@ class TestSegy(TestCase):
self.assertIsInstance(f.sorting, int)
self.assertIsInstance(f.ext_headers, int)
self.assertIsInstance(f.tracecount, int)
- self.assertIsInstance(f.samples, int)
+ self.assertIsInstance(f.samples, np.ndarray)
self.assertIsInstance(f.depth_slice, Line)
self.assertIsInstance(f.depth_slice[1], np.ndarray)
@@ -800,9 +802,9 @@ class TestSegy(TestCase):
def test_depth_slice_reading(self):
with segyio.open(self.filename, "r") as f:
- self.assertEqual(len(f.depth_slice), f.samples)
+ self.assertEqual(len(f.depth_slice), len(f.samples))
- for depth_sample in range(f.samples):
+ for depth_sample in range(len(f.samples)):
depth_slice = f.depth_slice[depth_sample]
self.assertIsInstance(depth_slice, np.ndarray)
self.assertEqual(depth_slice.shape, (5, 5))
@@ -820,7 +822,7 @@ class TestSegy(TestCase):
self.assertAlmostEqual(depth_slice[i][j], f.iline[x][j][index], places=6)
with self.assertRaises(KeyError):
- slice = f.depth_slice[f.samples]
+ slice = f.depth_slice[len(f.samples)]
def test_depth_slice_writing(self):
with TestContext("depth_slice_writing") as context:
diff --git a/python/test/tools.py b/python/test/tools.py
index 5c2a7b7..6db9bd0 100644
--- a/python/test/tools.py
+++ b/python/test/tools.py
@@ -43,13 +43,13 @@ class ToolsTest(TestCase):
def test_sample_indexes(self):
with segyio.open(self.filename, "r") as f:
indexes = segyio.sample_indexes(f)
- self.assertListEqual(indexes, [t * 4.0 for t in range(f.samples)])
+ self.assertListEqual(indexes, [t * 4.0 for t in range(len(f.samples))])
indexes = segyio.sample_indexes(f, t0=1.5)
- self.assertListEqual(indexes, [1.5 + t * 4.0 for t in range(f.samples)])
+ self.assertListEqual(indexes, [1.5 + t * 4.0 for t in range(len(f.samples))])
indexes = segyio.sample_indexes(f, t0=1.5, dt_override=3.21)
- self.assertListEqual(indexes, [1.5 + t * 3.21 for t in range(f.samples)])
+ self.assertListEqual(indexes, [1.5 + t * 3.21 for t in range(len(f.samples))])
def test_empty_text_header_creation(self):
text_header = segyio.create_text_header({})
@@ -69,7 +69,7 @@ class ToolsTest(TestCase):
def test_native(self):
with open(self.filename, 'rb') as f, segyio.open(self.filename) as sgy:
f.read(3600+240)
- filetr = f.read(4 * sgy.samples)
+ filetr = f.read(4 * len(sgy.samples))
segytr = sgy.trace[0]
filetr = np.frombuffer(filetr, dtype = np.single)
@@ -85,11 +85,11 @@ class ToolsTest(TestCase):
def test_cube_identity(self):
with segyio.open(self.filename) as f:
x = segyio.tools.collect(f.trace[:])
- x = x.reshape((len(f.ilines), len(f.xlines), f.samples))
+ x = x.reshape((len(f.ilines), len(f.xlines), len(f.samples)))
self.assertTrue(np.all(x == segyio.tools.cube(f)))
def test_cube_identity_prestack(self):
with segyio.open(self.prestack) as f:
- dims = (len(f.ilines), len(f.xlines), len(f.offsets), f.samples)
+ dims = (len(f.ilines), len(f.xlines), len(f.offsets), len(f.samples))
x = segyio.tools.collect(f.trace[:]).reshape(dims)
self.assertTrue(np.all(x == segyio.tools.cube(f)))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git
More information about the debian-science-commits
mailing list