[segyio] 140/376: Faster depth slice reading

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:22 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 1974fbbfea7b3ae6734a75ed59a51d5da3afe60c
Author: jeanpaul <jpb at prador.net>
Date:   Sat Dec 3 08:11:48 2016 +0100

    Faster depth slice reading
---
 python/segyio/_segyio.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
 python/segyio/segy.py   | 13 ++++++----
 2 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/python/segyio/_segyio.c b/python/segyio/_segyio.c
index f7062ea..83a372a 100644
--- a/python/segyio/_segyio.c
+++ b/python/segyio/_segyio.c
@@ -938,6 +938,68 @@ static PyObject *py_read_line(PyObject *self, PyObject *args) {
     return buffer_in;
 }
 
+static PyObject *py_read_depth_slice(PyObject *self, PyObject *args) {
+    errno = 0;
+    PyObject *file_capsule = NULL;
+    int depth;
+    int count;
+    int offsets;
+    PyObject *buffer_out;
+    long trace0;
+    unsigned int trace_bsize;
+    int format;
+    unsigned int samples;
+
+    PyArg_ParseTuple(args, "OiiiOlIiI", &file_capsule,
+                                        &depth,
+                                        &count,
+                                        &offsets,
+                                        &buffer_out,
+                                        &trace0, &trace_bsize,
+                                        &format, &samples);
+
+    segy_file *p_FILE = get_FILE_pointer_from_capsule(file_capsule);
+
+    if (PyErr_Occurred()) { return NULL; }
+
+    if (!PyObject_CheckBuffer(buffer_out)) {
+        PyErr_SetString(PyExc_TypeError, "The destination buffer is not of the correct type.");
+        return NULL;
+    }
+    Py_buffer buffer;
+    PyObject_GetBuffer(buffer_out, &buffer, PyBUF_FORMAT | PyBUF_C_CONTIGUOUS | PyBUF_WRITEABLE);
+
+    Py_ssize_t trace_no = 0;
+    int error = 0;
+    float* trace_buffer = malloc(trace_bsize * samples);
+    float* buf = buffer.buf;
+
+    for(trace_no = 0; error == 0 && trace_no < count; ++trace_no) {
+        error = segy_readtrace(p_FILE, trace_no * offsets, trace_buffer, trace0, trace_bsize);
+
+        if (!error) {
+            buf[trace_no] = trace_buffer[depth];
+        }
+    }
+    free(trace_buffer);
+
+    if (error != 0) {
+        PyBuffer_Release( &buffer );
+        return py_handle_segy_error_with_index_and_name(error, errno, trace_no, "Depth");
+    }
+
+    error = segy_to_native(format, count, buffer.buf);
+    PyBuffer_Release( &buffer );
+
+    if (error != 0) {
+        PyErr_SetString(PyExc_TypeError, "Unable to convert buffer to native format.");
+        return NULL;
+    }
+
+    Py_IncRef(buffer_out);
+    return buffer_out;
+}
+
 /*  define functions in module */
 static PyMethodDef SegyMethods[] = {
         {"open",               (PyCFunction) py_FILE_open,          METH_VARARGS, "Opens a file."},
@@ -970,6 +1032,7 @@ static PyMethodDef SegyMethods[] = {
         {"read_trace",         (PyCFunction) py_read_trace,         METH_VARARGS, "Read trace data."},
         {"write_trace",        (PyCFunction) py_write_trace,        METH_VARARGS, "Write trace data."},
         {"read_line",          (PyCFunction) py_read_line,          METH_VARARGS, "Read a xline/iline from file."},
+        {"depth_slice",        (PyCFunction) py_read_depth_slice,   METH_VARARGS, "Read a depth slice."},
         {NULL, NULL, 0, NULL}
 };
 
diff --git a/python/segyio/segy.py b/python/segyio/segy.py
index b8fd98f..d74f61b 100644
--- a/python/segyio/segy.py
+++ b/python/segyio/segy.py
@@ -747,12 +747,15 @@ class SegyFile(object):
         other_indices = np.asarray([0], dtype=np.uintc)
         buffn = self._depth_buffer
 
-        def readfn(depth, length, stride, buf):
-            buf_view = buf.reshape(self._iline_length * self._xline_length)
-
-            for i, trace_buf in enumerate(self.trace):
-                buf_view[i] = trace_buf[depth]
+        slice_trace_count = self._iline_length * self._xline_length
+        offsets = len(self.offsets)
+        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)
             return buf
 
         def writefn(depth, length, stride, val):

-- 
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