示例#1
0
文件: segyio_c.py 项目: vkazei/segyio
def test_get_and_putfield():
    hdr = bytearray(_segyio.thsize())

    with pytest.raises(BufferError):
        _segyio.getfield(".", 0)

    with pytest.raises(TypeError):
        _segyio.getfield([], 0)

    with pytest.raises(TypeError):
        _segyio.putfield({}, 0, 1)

    with pytest.raises(KeyError):
        _segyio.getfield(hdr, 0)

    with pytest.raises(KeyError):
        _segyio.putfield(hdr, 0, 1)

    _segyio.putfield(hdr, 1, 127)
    _segyio.putfield(hdr, 5, 67)
    _segyio.putfield(hdr, 9, 19)

    assert _segyio.getfield(hdr, 1) == 127
    assert _segyio.getfield(hdr, 5) == 67
    assert _segyio.getfield(hdr, 9) == 19
示例#2
0
    def test_read_and_write_trace(self, mmap=False):
        with TestContext("read_and_write_trace"):
            binary = bytearray(_segyio.binsize())
            _segyio.putfield(binary, 3213, 100)
            _segyio.putfield(binary, 3221, 25)
            f = _segyio.segyiofd("trace-wrt.sgy", "w+", binary)
            if mmap: f.mmap()

            buf = numpy.ones(25, dtype=numpy.single)
            buf[11] = 3.1415
            f.puttr(0, buf)
            buf[:] = 42.0
            f.puttr(1, buf)

            f.flush()

            buf = numpy.zeros(25, dtype=numpy.single)

            f.gettr(buf, 0, 1, 1)

            self.assertAlmostEqual(buf[10], 1.0, places=4)
            self.assertAlmostEqual(buf[11], 3.1415, places=4)

            f.gettr(buf, 1, 1, 1)

            self.assertAlmostEqual(sum(buf), 42.0 * 25, places=4)

            f.close()
示例#3
0
    def test_get_and_putfield(self):
        hdr = bytearray(_segyio.thsize())

        with self.assertRaises(BufferError):
            _segyio.getfield(".", 0)

        with self.assertRaises(TypeError):
            _segyio.getfield([], 0)

        with self.assertRaises(TypeError):
            _segyio.putfield({}, 0, 1)

        with self.assertRaises(IndexError):
            _segyio.getfield(hdr, 0)

        with self.assertRaises(IndexError):
            _segyio.putfield(hdr, 0, 1)

        _segyio.putfield(hdr, 1, 127)
        _segyio.putfield(hdr, 5, 67)
        _segyio.putfield(hdr, 9, 19)

        self.assertEqual(_segyio.getfield(hdr, 1), 127)
        self.assertEqual(_segyio.getfield(hdr, 5), 67)
        self.assertEqual(_segyio.getfield(hdr, 9), 19)
示例#4
0
文件: segyio_c.py 项目: vkazei/segyio
def test_read_and_write_trace(tmpdir):
    binary = bytearray(_segyio.binsize())
    _segyio.putfield(binary, 3221, 25)
    f = get_instance_segyiofd(tmpdir,
                              "trace-wrt.sgy",
                              "w+",
                              tracecount=100,
                              binary=binary)
    read_and_write_trace(f, False)
示例#5
0
    def test_read_and_write_traceheader(self, mmap=False):
        with TestContext("read_and_write_trace_header") as context:
            context.copy_file(self.filename)

            f = _segyio.segyiofd("small.sgy", "r+")
            if mmap: f.mmap()

            ilb = 189
            xlb = 193

            def mkempty():
                return bytearray(_segyio.thsize())

            with self.assertRaises(TypeError):
                trace_header = f.getth("+")

            with self.assertRaises(TypeError):
                trace_header = f.getth(0, None)

            trace_header = f.getth(0, mkempty())

            self.assertEqual(_segyio.getfield(trace_header, ilb), 1)
            self.assertEqual(_segyio.getfield(trace_header, xlb), 20)

            trace_header = f.getth(1, mkempty())

            self.assertEqual(_segyio.getfield(trace_header, ilb), 1)
            self.assertEqual(_segyio.getfield(trace_header, xlb), 21)

            _segyio.putfield(trace_header, ilb, 99)
            _segyio.putfield(trace_header, xlb, 42)

            f.putth(0, trace_header)

            trace_header = f.getth(0, mkempty())

            self.assertEqual(_segyio.getfield(trace_header, ilb), 99)
            self.assertEqual(_segyio.getfield(trace_header, xlb), 42)

            f.close()
示例#6
0
文件: segyio_c.py 项目: vkazei/segyio
def read_and_write_traceheader(f, mmap):
    if mmap:
        f.mmap()

    ilb = 189
    xlb = 193

    def mkempty():
        return bytearray(_segyio.thsize())

    with pytest.raises(TypeError):
        f.getth("+")

    with pytest.raises(TypeError):
        f.getth(0, None)

    trace_header = f.getth(0, mkempty())

    assert _segyio.getfield(trace_header, ilb) == 1
    assert _segyio.getfield(trace_header, xlb) == 20

    trace_header = f.getth(1, mkempty())

    assert _segyio.getfield(trace_header, ilb) == 1
    assert _segyio.getfield(trace_header, xlb) == 21

    _segyio.putfield(trace_header, ilb, 99)
    _segyio.putfield(trace_header, xlb, 42)

    f.putth(0, trace_header)

    trace_header = f.getth(0, mkempty())

    assert _segyio.getfield(trace_header, ilb) == 99
    assert _segyio.getfield(trace_header, xlb) == 42

    f.close()
示例#7
0
文件: create.py 项目: x-malet/segyio
def create(filename, spec):
    """Create a new segy file.

    Create a new segy file with the geometry and properties given by `spec`.
    This enables creating SEGY files from your data. The created file supports
    all segyio modes, but has an emphasis on writing. The spec must be
    complete, otherwise an exception will be raised. A default, empty spec can
    be created with ``segyio.spec()``.

    Very little data is written to the file, so just calling create is not
    sufficient to re-read the file with segyio. Rather, every trace header and
    trace must be written to the file to be considered complete.

    Create should be used together with python's ``with`` statement. This ensure
    the data is written. Please refer to the examples.

    The ``segyio.spec()`` function will default offsets and everything in the
    mandatory group, except format and samples, and requires the caller to fill
    in *all* the fields in either of the exclusive groups.

    If any field is missing from the first exclusive group, and the tracecount
    is set, the resulting file will be considered unstructured. If the
    tracecount is set, and all fields of the first exclusive group are
    specified, the file is considered structured and the tracecount is inferred
    from the xlines/ilines/offsets. The offsets are defaulted to ``[1]`` by
    ``segyio.spec()``.

    Parameters
    ----------
    filename : str
        Path to file to create
    spec : segyio.spec
        Structure of the segy file

    Returns
    -------
    file : segyio.SegyFile
        An open segyio file handle, similar to that returned by `segyio.open`

    See also
    --------
    segyio.spec : template for the `spec` argument


    Notes
    -----

    .. versionadded:: 1.1

    .. versionchanged:: 1.4
       Support for creating unstructured files

    The ``spec`` is any object that has the following attributes

    Mandatory::

        iline   : int or segyio.BinField
        xline   : int or segyio.BinField
        samples : array of int
        format  : { 1, 5 }
            1 = IBM float, 5 = IEEE float

    Exclusive::

        ilines  : array_like of int
        xlines  : array_like of int
        offsets : array_like of int
        sorting : int or segyio.TraceSortingFormat

        OR

        tracecount : int

    Optional::

        ext_headers : int


    Examples
    --------

    Create a file:

    >>> spec = segyio.spec()
    >>> spec.ilines  = [1, 2, 3, 4]
    >>> spec.xlines  = [11, 12, 13]
    >>> spec.samples = list(range(50))
    >>> spec.sorting = 2
    >>> spec.format  = 1
    >>> with segyio.create(path, spec) as f:
    ...     ## fill the file with data
    ...     pass
    ...

    Copy a file, but shorten all traces by 50 samples:

    >>> with segyio.open(srcpath) as src:
    ...     spec = segyio.spec()
    ...     spec.sorting = src.sorting
    ...     spec.format = src.format
    ...     spec.samples = src.samples[:len(src.samples) - 50]
    ...     spec.ilines = src.ilines
    ...     spec.xline = src.xlines
    ...     with segyio.create(dstpath, spec) as dst:
    ...         dst.text[0] = src.text[0]
    ...         dst.bin = src.bin
    ...         dst.header = src.header
    ...         dst.trace = src.trace

    Copy a file, but shorten all traces by 50 samples (since v1.4):

    >>> with segyio.open(srcpath) as src:
    ...     spec = segyio.tools.metadata(src)
    ...     spec.samples = spec.samples[:len(spec.samples) - 50]
    ...     with segyio.create(dstpath, spec) as dst:
    ...         dst.text[0] = src.text[0]
    ...         dst.bin = src.bin
    ...         dst.header = src.header
    ...         dst.trace = src.trace
    """


    if not structured(spec):
        tracecount = spec.tracecount
    else:
        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, 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+", tracecount = tracecount,
                                             binary = binary)

    f._il            = int(spec.iline)
    f._xl            = int(spec.xline)
    f._samples       = samples

    if structured(spec):
        f._sorting       = spec.sorting
        f._offsets       = numpy.copy(numpy.asarray(spec.offsets, dtype = numpy.intc))

        f._ilines        = numpy.copy(numpy.asarray(spec.ilines, dtype=numpy.intc))
        f._xlines        = numpy.copy(numpy.asarray(spec.xlines, dtype=numpy.intc))

        line_metrics = _segyio.line_metrics(f.sorting,
                                            tracecount,
                                            len(f.ilines),
                                            len(f.xlines),
                                            len(f.offsets))

        f._iline_length = line_metrics['iline_length']
        f._iline_stride = line_metrics['iline_stride']

        f._xline_length = line_metrics['xline_length']
        f._xline_stride = line_metrics['xline_stride']

    f.text[0] = default_text_header(f._il, f._xl, segyio.TraceField.offset)
    f.xfd.putbin(binary)

    return f
示例#8
0
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)
    read_and_write_trace(f, True)