Пример #1
0
def test_grd3d_export_grdeclprop_no_file():
    fname = "random_file_name"
    with open(fname, "w"):
        pass
    os.chmod(fname, stat.S_IREAD)
    with pytest.raises(xtgeo.XTGeoCLibError, match="Could not open file"):
        # The input here is not very important, what is important
        # is that "existing_file" can not be opened.
        _cxtgeo.grd3d_export_grdeclprop2(
            1,
            1,
            1,
            1,
            _cxtgeo.new_intpointer(),
            _cxtgeo.new_floatpointer(),
            _cxtgeo.new_doublepointer(),
            "name",
            " %d",
            fname,
            1,
            0,
        )

    os.chmod(fname, stat.S_IWRITE)
    os.remove(fname)
Пример #2
0
def export_grdecl(self, pfile, name, append=False, binary=False, dtype=None):

    logger.info("Exporting %s to file %s, GRDECL format", name, pfile)

    if dtype is None:
        if self._isdiscrete:
            dtype = "int32"
        else:
            dtype = "float32"

    carray = _gridprop_lowlevel.update_carray(self, dtype=dtype)

    iarr = _cxtgeo.new_intpointer()
    farr = _cxtgeo.new_floatpointer()
    darr = _cxtgeo.new_doublepointer()

    if "double" in str(carray):
        ptype = 3
        darr = carray

    elif "float" in str(carray):
        ptype = 2
        farr = carray

    else:
        ptype = 1
        iarr = carray

    mode = 0
    if not binary:
        mode = 1

    appendmode = 0
    if append:
        appendmode = 1

    _cxtgeo.grd3d_export_grdeclprop2(
        self._ncol,
        self._nrow,
        self._nlay,
        ptype,
        iarr,
        farr,
        darr,
        name,
        pfile,
        mode,
        appendmode,
        XTGDEBUG,
    )

    _gridprop_lowlevel.delete_carray(self, carray)
Пример #3
0
def test_grd3d_export_grdeclprop_no_file(unreadable_file):
    with pytest.raises(xtgeo.XTGeoCLibError, match="Could not open file"):
        # The input here is not very important, what is important
        # is that "existing_file" can not be opened.
        _cxtgeo.grd3d_export_grdeclprop2(
            1,
            1,
            1,
            1,
            _cxtgeo.new_intpointer(),
            _cxtgeo.new_floatpointer(),
            _cxtgeo.new_doublepointer(),
            "name",
            " %d",
            unreadable_file,
            1,
            0,
        )
def _rkwquery(gfile, kws, name, swap):
    """Local function for _import_roff_v2, single data."""

    kwtypedict = {"int": 1, "float": 2}
    iresult = _cxtgeo.new_intpointer()
    presult = _cxtgeo.new_floatpointer()

    dtype = 0
    reclen = 0
    bytepos = 1
    for items in kws:
        if name in items[0]:
            dtype = kwtypedict.get(items[1])
            reclen = items[2]
            bytepos = items[3]
            break

    logger.debug("DTYPE is %s", dtype)

    if dtype == 0:
        raise ValueError("Cannot find property <{}> in file".format(name))

    if reclen != 1:
        raise SystemError("Stuff is rotten here...")

    _cxtgeo.grd3d_imp_roffbin_data(
        gfile.get_cfhandle(), swap, dtype, bytepos, iresult, presult
    )

    gfile.cfclose()

    # -1 indicates that it is the swap flag which is looked for!
    if dtype == 1:
        xresult = _cxtgeo.intpointer_value(iresult)
        if swap == -1:
            if xresult == 1:
                return 0
            return 1

    elif dtype == 2:
        xresult = _cxtgeo.floatpointer_value(presult)

    return xresult
Пример #5
0
def _import_segy_xtgeo(sfile,
                       scanheadermode=False,
                       scantracemode=False,
                       outfile=None):
    """Import SEGY via XTGeo's C library. OLD NOT UPDATED!!

    Args:
        sfile (str): File name of SEGY file
        scanheadermode (bool, optional): If true, will scan header
        scantracemode (bool, optional): If true, will scan trace headers
        outfile (str, optional): Output file for scan dump (default None)

    Returns:
        A dictionary with relevant data.
    """
    # pylint: disable=too-many-statements, too-many-locals

    sdata = dict()

    logger.info("Import SEGY via XTGeo CLIB")

    if outfile is None:
        outfile = "/dev/null"

    ptr_gn_bitsheader = _cxtgeo.new_intpointer()
    ptr_gn_formatcode = _cxtgeo.new_intpointer()
    ptr_gf_segyformat = _cxtgeo.new_floatpointer()
    ptr_gn_samplespertrace = _cxtgeo.new_intpointer()
    ptr_gn_measuresystem = _cxtgeo.new_intpointer()

    option = 0
    if scantracemode:
        option = 0
    if scanheadermode:
        option = 1

    _cxtgeo.cube_scan_segy_hdr(
        sfile,
        ptr_gn_bitsheader,
        ptr_gn_formatcode,
        ptr_gf_segyformat,
        ptr_gn_samplespertrace,
        ptr_gn_measuresystem,
        option,
        outfile,
    )

    # get values
    gn_bitsheader = _cxtgeo.intpointer_value(ptr_gn_bitsheader)
    gn_formatcode = _cxtgeo.intpointer_value(ptr_gn_formatcode)
    gf_segyformat = _cxtgeo.floatpointer_value(ptr_gf_segyformat)
    gn_samplespertrace = _cxtgeo.intpointer_value(ptr_gn_samplespertrace)

    if scanheadermode:
        logger.info("Scan SEGY header ... %s bytes ... DONE", gn_bitsheader)
        return None

    # next is to scan first and last trace, in order to allocate
    # cube size

    ptr_ncol = _cxtgeo.new_intpointer()
    ptr_nrow = _cxtgeo.new_intpointer()
    ptr_nlay = _cxtgeo.new_intpointer()
    ptr_xori = _cxtgeo.new_doublepointer()
    ptr_yori = _cxtgeo.new_doublepointer()
    ptr_zori = _cxtgeo.new_doublepointer()
    ptr_xinc = _cxtgeo.new_doublepointer()
    ptr_yinc = _cxtgeo.new_doublepointer()
    ptr_zinc = _cxtgeo.new_doublepointer()
    ptr_rotation = _cxtgeo.new_doublepointer()
    ptr_minval = _cxtgeo.new_doublepointer()
    ptr_maxval = _cxtgeo.new_doublepointer()
    ptr_dummy = _cxtgeo.new_floatpointer()
    ptr_yflip = _cxtgeo.new_intpointer()
    ptr_zflip = _cxtgeo.new_intpointer()

    optscan = 1

    if scantracemode:
        option = 1

    logger.debug("Scan via C wrapper...")
    _cxtgeo.cube_import_segy(
        sfile,
        # input
        gn_bitsheader,
        gn_formatcode,
        gf_segyformat,
        gn_samplespertrace,
        # result (as pointers)
        ptr_ncol,
        ptr_nrow,
        ptr_nlay,
        ptr_dummy,
        ptr_xori,
        ptr_xinc,
        ptr_yori,
        ptr_yinc,
        ptr_zori,
        ptr_zinc,
        ptr_rotation,
        ptr_yflip,
        ptr_zflip,
        ptr_minval,
        ptr_maxval,
        # options
        optscan,
        option,
        outfile,
    )

    logger.debug("Scan via C wrapper... done")

    ncol = _cxtgeo.intpointer_value(ptr_ncol)
    nrow = _cxtgeo.intpointer_value(ptr_nrow)
    nlay = _cxtgeo.intpointer_value(ptr_nlay)

    if scantracemode:
        return None

    nrcl = ncol * nrow * nlay

    ptr_cval_v = _cxtgeo.new_floatarray(nrcl)

    # next is to do the actual import of the cube
    optscan = 0

    logger.debug("Import via C wrapper...")
    _cxtgeo.cube_import_segy(
        sfile,
        # input
        gn_bitsheader,
        gn_formatcode,
        gf_segyformat,
        gn_samplespertrace,
        # result (as pointers)
        ptr_ncol,
        ptr_nrow,
        ptr_nlay,
        ptr_cval_v,
        ptr_xori,
        ptr_xinc,
        ptr_yori,
        ptr_yinc,
        ptr_zori,
        ptr_zinc,
        ptr_rotation,
        ptr_yflip,
        ptr_zflip,
        ptr_minval,
        ptr_maxval,
        # options
        optscan,
        option,
        outfile,
    )

    logger.debug("Import via C wrapper...")

    sdata["ncol"] = ncol
    sdata["nrow"] = nrow
    sdata["nlay"] = nlay

    sdata["xori"] = _cxtgeo.doublepointer_value(ptr_xori)
    sdata["yori"] = _cxtgeo.doublepointer_value(ptr_yori)
    sdata["zori"] = _cxtgeo.doublepointer_value(ptr_zori)

    sdata["xinc"] = _cxtgeo.doublepointer_value(ptr_xinc)
    sdata["yinc"] = _cxtgeo.doublepointer_value(ptr_yinc)
    sdata["zinc"] = _cxtgeo.doublepointer_value(ptr_zinc)

    sdata["yflip"] = _cxtgeo.intpointer_value(ptr_yflip)
    sdata["zflip"] = _cxtgeo.intpointer_value(ptr_zflip)

    sdata["rotation"] = _cxtgeo.doublepointer_value(ptr_rotation)

    sdata["minval"] = _cxtgeo.doublepointer_value(ptr_minval)
    sdata["maxval"] = _cxtgeo.doublepointer_value(ptr_maxval)

    sdata["zmin"] = sdata["zori"]
    sdata["zmax"] = sdata["zori"] + sdata["zflip"] * sdata["zinc"] * (nlay - 1)

    # the pointer to 1D C array
    sdata["cvalues"] = ptr_cval_v
    sdata["values"] = None

    return sdata