[segyio] 146/376: Implemented sample_indexes and moved dt tests into tools test
Jørgen Kvalsvik
jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:23 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 3241eca057e1d339fbe3e871ac3c66e59b1fff62
Author: Jean-Paul Balabanian <jepebe at users.noreply.github.com>
Date: Mon Dec 5 15:01:34 2016 +0100
Implemented sample_indexes and moved dt tests into tools test
---
python/segyio/__init__.py | 2 +-
python/segyio/tools.py | 29 ++++++++++++++++++++++++--
tests/CMakeLists.txt | 1 +
tests/test_segy.py | 29 +-------------------------
tests/test_tools.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 82 insertions(+), 31 deletions(-)
diff --git a/python/segyio/__init__.py b/python/segyio/__init__.py
index a3ae0ba..556bb4c 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 dt
+from .tools import dt, sample_indexes
__version__ = '1.0.4'
__copyright__ = 'Copyright 2016, Statoil ASA'
diff --git a/python/segyio/tools.py b/python/segyio/tools.py
index f80f601..1d92803 100644
--- a/python/segyio/tools.py
+++ b/python/segyio/tools.py
@@ -1,5 +1,30 @@
import segyio
+import numpy as np
-def dt(f, fallback_dt=4):
- return segyio._segyio.get_dt(f.xfd, fallback_dt)
+def dt(segyfile, fallback_dt=4):
+ """
+ Find a *dt* value in the SegyFile. If none is found use the provided *fallback_dt* value.
+
+ :type segyfile: segyio.SegyFile
+ :type fallback_dt: float
+ :rtype: float
+ """
+ return segyio._segyio.get_dt(segyfile.xfd, fallback_dt)
+
+
+def sample_indexes(segyfile, t0=0.0, dt_override=None):
+ """
+ Creates a list of values representing the samples in a trace at depth or time.
+ The list starts at *t0* and is incremented with am*dt* for the number of samples.
+ If a *dt_override* is not provided it will try to find a *dt* in the file.
+
+ :type segyfile: segyio.SegyFile
+ :type t0: float
+ :type dt_override: float or None
+ :rtype: list[float]
+ """
+ if dt_override is None:
+ dt_override = dt(segyfile)
+
+ return [t0 + t * dt_override for t in range(segyfile.samples)]
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index fc72111..3e9b8bd 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -20,5 +20,6 @@ if(BUILD_PYTHON)
add_python_test(python.segy test_segy.py)
add_python_test(python.h.segy test_segyio_c.py)
add_python_test(python.enum.segy test_enum.py)
+ add_python_test(python.tools test_tools.py)
add_python_test(python.test_context test_test_context.py)
endif()
diff --git a/tests/test_segy.py b/tests/test_segy.py
index 1b8221c..31b05fc 100644
--- a/tests/test_segy.py
+++ b/tests/test_segy.py
@@ -20,6 +20,7 @@ try:
except ImportError: # will be 3.x series
pass
+
def mklines(fname):
spec = segyio.spec()
spec.format = 5
@@ -167,34 +168,6 @@ class TestSegy(TestCase):
self.assertEqual(len(f.trace), f.tracecount)
self.assertEqual(50, f.samples)
- def test_dt_fallback(self):
- with TestContext("dt_fallback") as context:
- context.copy_file(self.filename)
- with segyio.open("small.sgy", "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):
- with TestContext("dt_no_fallback") as context:
- context.copy_file(self.filename)
- dt_us = 6000
- with segyio.open("small.sgy", "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_tools.py b/tests/test_tools.py
new file mode 100644
index 0000000..92130f0
--- /dev/null
+++ b/tests/test_tools.py
@@ -0,0 +1,52 @@
+from unittest import TestCase
+
+from segyio import BinField
+from segyio import TraceField
+import numpy as np
+from test_context import TestContext
+import segyio
+
+
+class ToolsTest(TestCase):
+ def setUp(self):
+ self.filename = "test-data/small.sgy"
+
+ def test_dt_fallback(self):
+ with TestContext("dt_fallback") as context:
+ context.copy_file(self.filename)
+ with segyio.open("small.sgy", "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):
+ with TestContext("dt_no_fallback") as context:
+ context.copy_file(self.filename)
+ dt_us = 6000
+ with segyio.open("small.sgy", "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_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)])
+
+ indexes = segyio.sample_indexes(f, t0=1.5)
+ self.assertListEqual(indexes, [1.5 + t * 4.0 for t in range(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)])
+
--
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