[segyio] 143/376: No default dt in low level c, must be specified

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 ac8ca21d54a21f6fc01ac9631713764e87da7e9a
Author: Kjell W. Kongsvik <kwko at statoil.com>
Date:   Thu Dec 1 13:30:34 2016 +0100

    No default dt in low level c, must be specified
---
 mex/SegySpec.m                    |  3 ++-
 mex/segy_interpret_segycube_mex.c |  8 -------
 mex/segyspec_mex.c                | 29 +++++++++++++++---------
 python/segyio/__init__.py         |  2 +-
 python/segyio/_segyio.c           |  9 +++-----
 python/segyio/tools.py            |  6 ++---
 src/segyio/segy.c                 | 46 ++++++++++++++++++---------------------
 src/segyio/segy.h                 |  4 ++--
 src/spec/segyspec.c               |  4 ++--
 src/spec/segyspec.h               |  2 +-
 tests/test_segy.py                | 30 +++++++++++++++++++++++--
 tests/test_segyspec.c             | 11 +++++++---
 tests/test_segyspec_mex.m         | 14 ++++++------
 13 files changed, 96 insertions(+), 72 deletions(-)

diff --git a/mex/SegySpec.m b/mex/SegySpec.m
index 480eed4..35d49a6 100644
--- a/mex/SegySpec.m
+++ b/mex/SegySpec.m
@@ -20,7 +20,8 @@ classdef SegySpec
     methods
 
         function obj = SegySpec(filename, inline_field, crossline_field, t0)
-            spec = segyspec_mex(filename, int32(inline_field), int32(crossline_field), t0);
+            dt = 4;
+            spec = segyspec_mex(filename, int32(inline_field), int32(crossline_field), t0, dt);
             obj.filename = filename;
 
             if (isempty(spec))
diff --git a/mex/segy_interpret_segycube_mex.c b/mex/segy_interpret_segycube_mex.c
deleted file mode 100644
index bc8d829..0000000
--- a/mex/segy_interpret_segycube_mex.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <errno.h>
-#include <string.h>
-
-#include <segyio/segy.h>
-#include "segyutil.h"
-
-#include "matrix.h"
-#include "mex.h"
diff --git a/mex/segyspec_mex.c b/mex/segyspec_mex.c
index 72f3e1e..3e7e14a 100644
--- a/mex/segyspec_mex.c
+++ b/mex/segyspec_mex.c
@@ -29,7 +29,7 @@ mxArray *createPLHSStruct() {
 void checkInputOutputSizes(int nlhs, int nrhs ) {
 
     /* check for proper number of arguments */
-    if(nrhs!=4) {
+    if(nrhs!=5) {
         mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Four inputs required.");
     }
     if(nlhs!=1) {
@@ -44,14 +44,16 @@ void checkInputOutput(int nlhs, mxArray **plhs,
     checkInputOutputSizes(nlhs, nrhs);
 
     /* First input must be a string */
-    if ( mxIsChar(prhs[0]) != 1)
-        mexErrMsgIdAndTxt( "SegyIo:segyspec:inputNotString",
-                           "Input must be a string.");
+    if ( mxIsChar(prhs[0]) != 1) {
+        mexErrMsgIdAndTxt("SegyIo:segyspec:inputNotString",
+                          "Input must be a string.");
+    }
 
     /* First input must be a row vector */
-    if (mxGetM(prhs[0])!=1)
-        mexErrMsgIdAndTxt( "SegyIo:segyspec:inputNotVector",
-                           "Input must be a row vector.");
+    if (mxGetM(prhs[0])!=1) {
+        mexErrMsgIdAndTxt("SegyIo:segyspec:inputNotVector",
+                          "Input must be a row vector.");
+    }
 
     /* make sure the second input argument is int */
     if( !mxIsNumeric(prhs[1]) ||
@@ -62,7 +64,7 @@ void checkInputOutput(int nlhs, mxArray **plhs,
     /* make sure the third input argument is int */
     if( !mxIsNumeric(prhs[2]) ||
         mxGetNumberOfElements(prhs[2])!=1 ) {
-        mexErrMsgIdAndTxt("SegyIo:segyspec:notScalar","Input multiplier must be a int16.");
+        mexErrMsgIdAndTxt("SegyIo:segyspec:notScalar","Input multiplier must be a numeric.");
     }
 
     /* make sure the fourth input argument is double */
@@ -71,6 +73,12 @@ void checkInputOutput(int nlhs, mxArray **plhs,
         mexErrMsgIdAndTxt("SegyIo:segyspec:notScalar","Input multiplier must be a double.");
     }
 
+    /* make sure the fifth input argument is double */
+    if( !mxIsDouble(prhs[4]) ||
+        mxGetNumberOfElements(prhs[4])!=1 ) {
+        mexErrMsgIdAndTxt("SegyIo:segyspec:notScalar","Input multiplier must be a double.");
+    }
+
 
 }
 
@@ -85,10 +93,11 @@ void mexFunction( int nlhs, mxArray *plhs[],
 
     int il = (int)mxGetScalar(prhs[1]);
     int xl = (int)mxGetScalar(prhs[2]);
-    float t0 = (float)mxGetScalar(prhs[3]);
+    double t0 = mxGetScalar(prhs[3]);
+    double dt = mxGetScalar(prhs[4]);
 
     SegySpec spec;
-    int errc = segyCreateSpec(&spec, filename, il, xl, t0);
+    int errc = segyCreateSpec(&spec, filename, il, xl, t0, dt);
     if (errc != 0) {
         goto FAILURE;
     }
diff --git a/python/segyio/__init__.py b/python/segyio/__init__.py
index 3d48b24..a3ae0ba 100644
--- a/python/segyio/__init__.py
+++ b/python/segyio/__init__.py
@@ -86,7 +86,7 @@ from .binfield import BinField
 from .open import open
 from .create import create
 from .segy import SegyFile, spec
-from .tools import get_dt
+from .tools import dt
 
 __version__    = '1.0.4'
 __copyright__  = 'Copyright 2016, Statoil ASA'
diff --git a/python/segyio/_segyio.c b/python/segyio/_segyio.c
index 1294aba..91ca173 100644
--- a/python/segyio/_segyio.c
+++ b/python/segyio/_segyio.c
@@ -506,19 +506,16 @@ static PyObject *py_get_dt(PyObject *self, PyObject *args) {
     errno = 0;
 
     PyObject *file_capsule = NULL;
-    PyArg_ParseTuple(args, "O", &file_capsule);
+    double dt;
+    PyArg_ParseTuple(args, "Od", &file_capsule, &dt);
     segy_file *p_FILE = get_FILE_pointer_from_capsule(file_capsule);
 
     if (PyErr_Occurred()) { return NULL; }
 
-    float dt;
     int error = segy_sample_interval(p_FILE, &dt);
     if (error != 0) { return py_handle_segy_error(error, errno); }
 
-    PyObject *dict = PyDict_New();
-    PyDict_SetItemString(dict, "dt", Py_BuildValue("f", dt));
-
-    return Py_BuildValue("O", dict);
+    return PyFloat_FromDouble(dt);
 }
 
 
diff --git a/python/segyio/tools.py b/python/segyio/tools.py
index 093f462..f80f601 100644
--- a/python/segyio/tools.py
+++ b/python/segyio/tools.py
@@ -1,7 +1,5 @@
 import segyio
 
 
-def get_dt(file):
-    dt = segyio._segyio.get_dt(file.xfd)
-
-    return dt['dt']
+def dt(f, fallback_dt=4):
+    return segyio._segyio.get_dt(f.xfd, fallback_dt)
diff --git a/src/segyio/segy.c b/src/segyio/segy.c
index bed09c8..09a4904 100644
--- a/src/segyio/segy.c
+++ b/src/segyio/segy.c
@@ -639,7 +639,7 @@ int segy_traces( segy_file* fp,
     return SEGY_OK;
 }
 
-int segy_sample_interval( segy_file* fp, float* dt) {
+int segy_sample_interval( segy_file* fp, double* dt) {
 
     char bin_header[ SEGY_BINARY_HEADER_SIZE ];
     char trace_header[SEGY_TRACE_HEADER_SIZE];
@@ -658,37 +658,33 @@ int segy_sample_interval( segy_file* fp, float* dt) {
         return err;
     }
 
-    int _binary_header_dt;
-    segy_get_bfield(bin_header, BIN_Interval, &_binary_header_dt);
-    double binary_header_dt = _binary_header_dt/1000.0;
-
-    int _trace_header_dt;
-    segy_get_field(trace_header, TRACE_SAMPLE_INTERVAL, &_trace_header_dt);
-    double trace_header_dt = _trace_header_dt/1000.0;
-
-
-    *dt = trace_header_dt;
-
-    if (binary_header_dt == 0.0 && trace_header_dt == 0.0) {
-        fprintf(stderr, "Trace sampling rate in SEGY header and trace header set to 0.0. Will default to 4 ms.\n");
-        *dt = 4.0;
-    } else if (binary_header_dt == 0.0) {
-        *dt = trace_header_dt;
-    } else if (trace_header_dt == 0.0) {
-        *dt = binary_header_dt;
-    } else if (trace_header_dt != binary_header_dt) {
-        fprintf(stderr, "Trace sampling rate in SEGY header and trace header are not equal. Will use SEGY header sampling rate of: %f\n",
-                binary_header_dt);
-        *dt = binary_header_dt;
+    // microseconds: us
+    int binary_header_dt_us;
+    int trace_header_dt_us;
+
+    segy_get_bfield(bin_header, BIN_Interval, &binary_header_dt_us);
+    segy_get_field(trace_header, TRACE_SAMPLE_INTERVAL, &trace_header_dt_us);
+
+    // milliseconds: ms
+    double binary_header_dt_ms = binary_header_dt_us/1000.0;
+    double trace_header_dt_ms = trace_header_dt_us/1000.0;
+
+    if (trace_header_dt_us==0 && binary_header_dt_us==0) {
+        //noop
+    } else if (binary_header_dt_us == 0) {
+        *dt = trace_header_dt_ms;
+    } else if (trace_header_dt_us == 0) {
+        *dt = binary_header_dt_ms;
+    } else if (trace_header_dt_us == binary_header_dt_us) {
+        *dt = trace_header_dt_ms;
     }
 
     return 0;
 
 }
 
-int segy_sample_indexes( segy_file* fp, double* buf, double t0, size_t count) {
+int segy_sample_indexes( segy_file* fp, double* buf, double t0, double dt, size_t count) {
 
-    float dt;
     int err = segy_sample_interval(fp, &dt);
     if (err != 0) {
         return err;
diff --git a/src/segyio/segy.h b/src/segyio/segy.h
index 326579a..32e7494 100644
--- a/src/segyio/segy.h
+++ b/src/segyio/segy.h
@@ -45,7 +45,7 @@ unsigned int segy_binheader_size();
 int segy_binheader( segy_file*, char* buf );
 int segy_write_binheader( segy_file*, const char* buf );
 unsigned int segy_samples( const char* binheader );
-int segy_sample_interval( segy_file*, float* dt);
+int segy_sample_interval( segy_file*, double* dt);
 /* exception: the int returned is an enum, SEGY_SORTING, not an error code */
 int segy_format( const char* binheader );
 int segy_get_field( const char* traceheader, int field, int32_t* f );
@@ -59,7 +59,7 @@ long segy_trace0( const char* binheader );
 /* number of traces in this file */
 int segy_traces( segy_file*, size_t*, long trace0, unsigned int trace_bsize );
 
-int segy_sample_indexes(segy_file* fp, double* buf, double t0, size_t count);
+int segy_sample_indexes(segy_file* fp, double* buf, double t0, double dt, size_t count);
 
 /* text header operations */
 int segy_read_textheader( segy_file*, char *buf);
diff --git a/src/spec/segyspec.c b/src/spec/segyspec.c
index 25c6e43..f3e08e5 100644
--- a/src/spec/segyspec.c
+++ b/src/spec/segyspec.c
@@ -11,7 +11,7 @@ static char* copyString(const char* path) {
 }
 
 
-int segyCreateSpec(SegySpec* spec, const char* file, unsigned int inline_field, unsigned int crossline_field, double t0) {
+int segyCreateSpec(SegySpec* spec, const char* file, unsigned int inline_field, unsigned int crossline_field, double t0, double dt) {
 
     int errc = 0;
 
@@ -36,7 +36,7 @@ int segyCreateSpec(SegySpec* spec, const char* file, unsigned int inline_field,
     spec->sample_count = segy_samples( header );
 
     spec->sample_indexes = malloc(sizeof(double) * spec->sample_count);
-    errc = segy_sample_indexes(fp, spec->sample_indexes, t0, spec->sample_count);
+    errc = segy_sample_indexes(fp, spec->sample_indexes, t0, dt, spec->sample_count);
     if (errc != 0) {
         goto CLEANUP;
     }
diff --git a/src/spec/segyspec.h b/src/spec/segyspec.h
index e14a839..f23dd9f 100644
--- a/src/spec/segyspec.h
+++ b/src/spec/segyspec.h
@@ -28,6 +28,6 @@ typedef struct {
 
 } SegySpec;
 
-int segyCreateSpec(SegySpec* spec, const char* file, unsigned int inline_field, unsigned int crossline_field, double t0);
+int segyCreateSpec(SegySpec* spec, const char* file, unsigned int inline_field, unsigned int crossline_field, double t0, double dt);
 
 #endif //SEGYIO_SEGYSPEC_H
diff --git a/tests/test_segy.py b/tests/test_segy.py
index 26e3b04..0eeaa8c 100644
--- a/tests/test_segy.py
+++ b/tests/test_segy.py
@@ -165,8 +165,34 @@ class TestSegy(TestCase):
             self.assertEqual(25, f.tracecount)
             self.assertEqual(len(f.trace), f.tracecount)
             self.assertEqual(50, f.samples)
-            dt = segyio.get_dt(f)
-            self.assertEqual(4, dt)
+
+    def test_dt_fallback(self):
+        f_name = self.filename.replace( ".sgy", "_dt_test.sgy")
+        shutil.copyfile(self.filename, f_name)
+        with segyio.open(f_name, "r+") as f:
+            # Both zero
+            f.bin[BinField.Interval] = 0
+            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 0
+            f.flush()
+            fallback_dt = 4
+            np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
+
+            # dt in bin header different from first trace
+            f.bin[BinField.Interval] = 6000
+            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = 1000
+            f.flush()
+            fallback_dt = 4
+            np.testing.assert_almost_equal(segyio.dt(f, fallback_dt), fallback_dt)
+
+    def test_dt_no_fallback(self):
+        f_name = self.filename.replace( ".sgy", "_dt_test.sgy")
+        shutil.copyfile(self.filename, f_name)
+        dt_us = 6000
+        with segyio.open(f_name, "r+") as f:
+            f.bin[BinField.Interval] = dt_us
+            f.header[0][TraceField.TRACE_SAMPLE_INTERVAL] = dt_us
+            f.flush()
+            np.testing.assert_almost_equal(segyio.dt(f), dt_us/1000)
 
     def test_traces_slicing(self):
         with segyio.open(self.filename, "r") as f:
diff --git a/tests/test_segyspec.c b/tests/test_segyspec.c
index acfb223..f980e58 100644
--- a/tests/test_segyspec.c
+++ b/tests/test_segyspec.c
@@ -10,10 +10,11 @@
 static void testSegyInspection() {
     const char *path = "test-data/small.sgy";
     double t0 = 1111.0;
+    double dt = -1;
 
     SegySpec spec;
 
-    segyCreateSpec(&spec, path, INLINE_3D, CROSSLINE_3D, t0);
+    segyCreateSpec(&spec, path, INLINE_3D, CROSSLINE_3D, t0, dt);
 
     assertTrue(spec.sample_format == IBM_FLOAT_4_BYTE, "Expected the float format to be IBM Float");
 
@@ -26,8 +27,10 @@ static void testSegyInspection() {
 
     assertTrue(spec.sample_count == 50, "Expected sample count to be 50");
 
+    dt = spec.sample_indexes[1]-spec.sample_indexes[0];
+    assertTrue(dt == 4, "Expect dt to be 4");
     for(unsigned int i = 0; i < spec.sample_count; i++) {
-        double t = t0 + i * 4.0;
+        double t = t0 + i * dt;
         assertTrue(spec.sample_indexes[i] == t, "Sample index not equal to expected value");
     }
 
@@ -57,8 +60,10 @@ static void testSegyInspection() {
 static void testAlloc(){
     const char *path = "test-data/small.sgy";
     double t0 = 1111.0;
+    double dt = 4;
+
     SegySpec spec;
-    segyCreateSpec(&spec, path, INLINE_3D, CROSSLINE_3D, t0);
+    segyCreateSpec(&spec, path, INLINE_3D, CROSSLINE_3D, t0, dt);
 
     if (spec.crossline_indexes != NULL)
         free(spec.crossline_indexes);
diff --git a/tests/test_segyspec_mex.m b/tests/test_segyspec_mex.m
index e3c29d0..bc20931 100644
--- a/tests/test_segyspec_mex.m
+++ b/tests/test_segyspec_mex.m
@@ -41,11 +41,17 @@ assert(spec.trace_sorting_format == TraceSortingFormat.INLINE);
 spec = SegySpec(filename, TraceField.INLINE_3D, TraceField.CROSSLINE_3D, t0);
 assert(length(spec.offset_count) == 1);
 
-%% samples
+%% sample_indexes
 spec = SegySpec(filename, TraceField.INLINE_3D, TraceField.CROSSLINE_3D, t0);
 sample_indexes = spec.sample_indexes;
 assert(length(sample_indexes) == 50);
 
+for i = 1:length(sample_indexes)
+    t = t0 + (i-1) * 4;
+    assert(sample_indexes(i) == t);
+end
+
+
 %% first_trace_pos
 spec = SegySpec(filename, TraceField.INLINE_3D, TraceField.CROSSLINE_3D, t0);
 first_trace_pos = spec.first_trace_pos;
@@ -66,12 +72,6 @@ spec = SegySpec(filename, TraceField.INLINE_3D, TraceField.CROSSLINE_3D, t0);
 trace_bsize = spec.trace_bsize;
 assert(trace_bsize == 50*4);
 
-
-for i = 1:length(sample_indexes)
-    t = t0 + (i-1) * 4;
-    assert(sample_indexes(i) == t);
-end
-
 %% xline
 spec = SegySpec(filename, TraceField.INLINE_3D, TraceField.CROSSLINE_3D, t0);
 assert(length(spec.crossline_indexes)==5)

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