diff --git a/python/segyio/create.py b/python/segyio/create.py index bddec98c1..583ae8af3 100644 --- a/python/segyio/create.py +++ b/python/segyio/create.py @@ -135,19 +135,19 @@ def create(filename, spec): if not structured(spec): tracecount = spec.tracecount else: - tracecount = len(spec.ilines) * len(spec.xlines) * len(spec.offsets) + tracecount = len(spec.ilines) * len(spec.xlines) * len(spec.offsets) ext_headers = spec.ext_headers if hasattr(spec, 'ext_headers') else 0 samples = numpy.asarray(spec.samples, dtype = numpy.single) binary = bytearray(_segyio.binsize()) - _segyio.putfield(binary, 3213, tracecount) _segyio.putfield(binary, 3217, 4000) _segyio.putfield(binary, 3221, len(samples)) _segyio.putfield(binary, 3225, int(spec.format)) _segyio.putfield(binary, 3505, int(ext_headers)) - f = segyio.SegyFile(str(filename), "w+", binary = binary) + f = segyio.SegyFile(str(filename), "w+", tracecount = tracecount, + binary = binary) f._il = int(spec.iline) f._xl = int(spec.xline) @@ -161,7 +161,7 @@ def create(filename, spec): f._xlines = numpy.copy(numpy.asarray(spec.xlines, dtype=numpy.intc)) line_metrics = _segyio.line_metrics(f.sorting, - f.tracecount, + tracecount, len(f.ilines), len(f.xlines), len(f.offsets)) diff --git a/python/segyio/segy.py b/python/segyio/segy.py index a43cb7755..1050c165d 100644 --- a/python/segyio/segy.py +++ b/python/segyio/segy.py @@ -36,7 +36,7 @@ class SegyFile(object): _unstructured_errmsg = "File opened in unstructured mode." - def __init__(self, filename, mode, iline=189, xline=193, binary=None): + def __init__(self, filename, mode, iline=189, xline=193, tracecount=0, binary=None): """ Constructor, internal. """ @@ -64,7 +64,7 @@ def __init__(self, filename, mode, iline=189, xline=193, binary=None): self._xline = None self._gather = None - self.xfd = _segyio.segyiofd(filename, mode, binary) + self.xfd = _segyio.segyiofd(filename, mode, tracecount, binary) metrics = self.xfd.metrics() self._fmt = metrics['format'] self._tracecount = metrics['tracecount'] diff --git a/python/segyio/segyio.cpp b/python/segyio/segyio.cpp index 7eb9c988f..ab56dd07b 100644 --- a/python/segyio/segyio.cpp +++ b/python/segyio/segyio.cpp @@ -221,9 +221,13 @@ namespace fd { int init( segyiofd* self, PyObject* args, PyObject* ) { char* filename = NULL; char* mode = NULL; + int tracecount = 0; buffer_guard buffer; - if( !PyArg_ParseTuple( args, "ss|z*", &filename, &mode, &buffer ) ) + if( !PyArg_ParseTuple( args, "ss|iz*", &filename, + &mode, + &tracecount, + &buffer ) ) return -1; const char* binary = buffer.buf< const char >(); @@ -261,7 +265,6 @@ int init( segyiofd* self, PyObject* args, PyObject* ) { return -1; } - int tracecount = 0; char bin[ SEGY_BINARY_HEADER_SIZE ] = {}; if( !binary ) { @@ -273,13 +276,6 @@ int init( segyiofd* self, PyObject* args, PyObject* ) { } binary = bin; - } else { - /* - * usually we wouldn't trust the bin-traces in the binary header, but - * this code path is only taken with create() (or similarly - * pre-verified header), in which case we can just read the tracecount. - */ - segy_get_bfield( binary, SEGY_BIN_TRACES, &tracecount ); } const long trace0 = segy_trace0( binary ); diff --git a/python/test/segyio_c.py b/python/test/segyio_c.py index df1b6e419..e6abee257 100644 --- a/python/test/segyio_c.py +++ b/python/test/segyio_c.py @@ -424,18 +424,20 @@ def mkempty(): @tmpfiles("test-data/small.sgy") def test_read_and_write_trace_mmap(tmpdir): binary = bytearray(_segyio.binsize()) - _segyio.putfield(binary, 3213, 100) _segyio.putfield(binary, 3221, 25) - f = get_instance_segyiofd(tmpdir, "trace-wrt.sgy", "w+", binary) + f = get_instance_segyiofd(tmpdir, "trace-wrt.sgy", "w+", + tracecount=100, + binary=binary) read_and_write_trace(f, True) @tmpfiles("test-data/small.sgy") def test_read_and_write_trace(tmpdir): binary = bytearray(_segyio.binsize()) - _segyio.putfield(binary, 3213, 100) _segyio.putfield(binary, 3221, 25) - f = get_instance_segyiofd(tmpdir, "trace-wrt.sgy", "w+", binary) + f = get_instance_segyiofd(tmpdir, "trace-wrt.sgy", "w+", + tracecount=100, + binary=binary) read_and_write_trace(f, False)