Beispiel #1
0
def swapaxes(self):
    """Swap the axes inline vs xline, keep origin."""

    ncol = _cxtgeo.new_intpointer()
    nrow = _cxtgeo.new_intpointer()
    yflip = _cxtgeo.new_intpointer()
    xinc = _cxtgeo.new_doublepointer()
    yinc = _cxtgeo.new_doublepointer()
    rota = _cxtgeo.new_doublepointer()

    _cxtgeo.intpointer_assign(ncol, self._ncol)
    _cxtgeo.intpointer_assign(nrow, self._nrow)
    _cxtgeo.intpointer_assign(yflip, self._yflip)

    _cxtgeo.doublepointer_assign(xinc, self._xinc)
    _cxtgeo.doublepointer_assign(yinc, self._yinc)
    _cxtgeo.doublepointer_assign(rota, self._rotation)

    values1d = self.values.reshape(-1)
    traceid1d = self._traceidcodes.reshape(-1)

    ier = _cxtgeo.cube_swapaxes(
        ncol,
        nrow,
        self.nlay,
        yflip,
        self.xori,
        xinc,
        self.yori,
        yinc,
        rota,
        values1d,
        traceid1d,
        0,
        XTGDEBUG,
    )
    if ier != 0:
        raise Exception

    self._ncol = _cxtgeo.intpointer_value(ncol)
    self._nrow = _cxtgeo.intpointer_value(nrow)
    self._yflip = _cxtgeo.intpointer_value(yflip)

    self._xinc = _cxtgeo.doublepointer_value(xinc)
    self._yinc = _cxtgeo.doublepointer_value(yinc)
    self._rotation = _cxtgeo.doublepointer_value(rota)

    ilines = self._xlines.copy()
    xlines = self._ilines.copy()
    self._xlines = xlines
    self._ilines = ilines

    self._traceidcodes = traceid1d.reshape((self._ncol, self._nrow))
    self._values = values1d.reshape((self._ncol, self._nrow, self.nlay))
def _rkwquery(fhandle, 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

    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(fhandle, swap, dtype, bytepos, iresult,
                                   presult, XTGDEBUG)

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

    return xresult
Beispiel #3
0
def ib_to_ijk(ib, nx, ny, nz, ibbase=0):
    """Convert a 1D index (starting from ibbase) to cell indices I J K.

    Returns I J K as a tuple.
    """

    logger.info("IB to IJK")

    ip = _cxtgeo.new_intpointer()
    jp = _cxtgeo.new_intpointer()
    kp = _cxtgeo.new_intpointer()

    _cxtgeo.x_ib2ijk(ib, ip, jp, kp, nx, ny, nz, ibbase)

    i = _cxtgeo.intpointer_value(ip)
    j = _cxtgeo.intpointer_value(jp)
    k = _cxtgeo.intpointer_value(kp)

    return (i, j, k)
Beispiel #4
0
def swapaxes(self):
    """Swap the axes columns vs rows, keep origin. Will change yflip."""

    ncol = _cxtgeo.new_intpointer()
    nrow = _cxtgeo.new_intpointer()
    yflip = _cxtgeo.new_intpointer()
    xinc = _cxtgeo.new_doublepointer()
    yinc = _cxtgeo.new_doublepointer()
    rota = _cxtgeo.new_doublepointer()

    _cxtgeo.intpointer_assign(ncol, self._ncol)
    _cxtgeo.intpointer_assign(nrow, self._nrow)
    _cxtgeo.intpointer_assign(yflip, self._yflip)

    _cxtgeo.doublepointer_assign(xinc, self._xinc)
    _cxtgeo.doublepointer_assign(yinc, self._yinc)
    _cxtgeo.doublepointer_assign(rota, self._rotation)

    val = self.get_values1d(fill_value=xtgeo.UNDEF)

    ier = _cxtgeo.surf_swapaxes(
        ncol, nrow, yflip, self.xori, xinc, self.yori, yinc, rota, val, 0, XTGDEBUG
    )
    if ier != 0:
        raise RuntimeError(
            "Unspecied runtime error from {}: Code: {}".format(__name__, ier)
        )

    self._ncol = _cxtgeo.intpointer_value(ncol)
    self._nrow = _cxtgeo.intpointer_value(nrow)
    self._yflip = _cxtgeo.intpointer_value(yflip)

    self._xinc = _cxtgeo.doublepointer_value(xinc)
    self._yinc = _cxtgeo.doublepointer_value(yinc)
    self._rotation = _cxtgeo.doublepointer_value(rota)

    ilines = self._xlines.copy()
    xlines = self._ilines.copy()

    self._ilines = ilines
    self._xlines = xlines

    self.values = val  # reshaping and masking is done in self.values
Beispiel #5
0
def ib_to_ijk(ib, nx, ny, nz, ibbase=0, forder=True):
    """Convert a 1D index (starting from ibbase) to cell indices I J K.

    The default is F-order, but ``forder=False`` gives C order

    Returns I J K as a tuple.
    """

    logger.info("IB to IJK")

    ip = _cxtgeo.new_intpointer()
    jp = _cxtgeo.new_intpointer()
    kp = _cxtgeo.new_intpointer()

    if forder:
        _cxtgeo.x_ib2ijk(ib, ip, jp, kp, nx, ny, nz, ibbase)
    else:
        _cxtgeo.x_ic2ijk(ib, ip, jp, kp, nx, ny, nz, ibbase)

    i = _cxtgeo.intpointer_value(ip)
    j = _cxtgeo.intpointer_value(jp)
    k = _cxtgeo.intpointer_value(kp)

    return (i, j, k)
def _import_roff_v1(self, pfile, name):
    """Import ROFF format, version 1"""
    # pylint: disable=too-many-locals

    # there is a todo here to get it more robust for various cases,
    # e.g. that a ROFF file may contain both a grid an numerous
    # props

    logger.info("Looking for %s in file %s", name, pfile)

    ptr_ncol = _cxtgeo.new_intpointer()
    ptr_nrow = _cxtgeo.new_intpointer()
    ptr_nlay = _cxtgeo.new_intpointer()
    ptr_ncodes = _cxtgeo.new_intpointer()
    ptr_type = _cxtgeo.new_intpointer()

    ptr_idum = _cxtgeo.new_intpointer()
    ptr_ddum = _cxtgeo.new_doublepointer()

    # read with mode 0, to scan for ncol, nrow, nlay and ndcodes, and if
    # property is found...
    ier, _codenames = _cxtgeo.grd3d_imp_prop_roffbin(
        pfile,
        0,
        ptr_type,
        ptr_ncol,
        ptr_nrow,
        ptr_nlay,
        ptr_ncodes,
        name,
        ptr_idum,
        ptr_ddum,
        ptr_idum,
        0,
        XTGDEBUG,
    )

    if ier == -1:
        msg = "Cannot find property name {}".format(name)
        logger.warning(msg)
        raise SystemExit("Error from ROFF import")

    self._ncol = _cxtgeo.intpointer_value(ptr_ncol)
    self._nrow = _cxtgeo.intpointer_value(ptr_nrow)
    self._nlay = _cxtgeo.intpointer_value(ptr_nlay)
    self._ncodes = _cxtgeo.intpointer_value(ptr_ncodes)

    ptype = _cxtgeo.intpointer_value(ptr_type)

    ntot = self._ncol * self._nrow * self._nlay

    if self._ncodes <= 1:
        self._ncodes = 1
        self._codes = {0: "undef"}

    logger.debug("Number of codes: %s", self._ncodes)

    # allocate

    if ptype == 1:  # float, assign to double
        ptr_pval_v = _cxtgeo.new_doublearray(ntot)
        ptr_ival_v = _cxtgeo.new_intarray(1)
        self._isdiscrete = False
        self._dtype = "float64"

    elif ptype > 1:
        ptr_pval_v = _cxtgeo.new_doublearray(1)
        ptr_ival_v = _cxtgeo.new_intarray(ntot)
        self._isdiscrete = True
        self._dtype = "int32"

    # number of codes and names
    ptr_ccodes_v = _cxtgeo.new_intarray(self._ncodes)

    # NB! note the SWIG trick to return modified char values; use cstring.i
    # inn the config and %cstring_bounded_output(char *p_codenames_v, NN);
    # Then the argument for *p_codevalues_v in C is OMITTED here!

    ier, cnames = _cxtgeo.grd3d_imp_prop_roffbin(
        pfile,
        1,
        ptr_type,
        ptr_ncol,
        ptr_nrow,
        ptr_nlay,
        ptr_ncodes,
        name,
        ptr_ival_v,
        ptr_pval_v,
        ptr_ccodes_v,
        0,
        XTGDEBUG,
    )

    if self._isdiscrete:
        _gridprop_lowlevel.update_values_from_carray(self,
                                                     ptr_ival_v,
                                                     np.int32,
                                                     delete=True)
    else:
        _gridprop_lowlevel.update_values_from_carray(self,
                                                     ptr_pval_v,
                                                     np.float64,
                                                     delete=True)

    # now make dictionary of codes
    if self._isdiscrete:
        cnames = cnames.replace(";", "")
        cname_list = cnames.split("|")
        cname_list.pop()  # some rubbish as last entry
        ccodes = []
        for ino in range(0, self._ncodes):
            ccodes.append(_cxtgeo.intarray_getitem(ptr_ccodes_v, ino))

        self._codes = dict(zip(ccodes, cname_list))

    self._name = name
Beispiel #7
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
Beispiel #8
0
def import_ecl_grdecl(self, gfile):

    # make a temporary file
    fds, tmpfile = mkstemp(prefix="tmpxtgeo")
    os.close(fds)

    with open(gfile) as oldfile, open(tmpfile, "w") as newfile:
        for line in oldfile:
            if not (re.search(r"^--", line) or re.search(r"^\s+$", line)):
                newfile.write(line)

    newfile.close()
    oldfile.close()

    # find ncol nrow nz
    mylist = []
    found = False
    with open(tmpfile) as xfile:
        for line in xfile:
            if found:
                logger.info(line)
                mylist = line.split()
                break
            if re.search(r"^SPECGRID", line):
                found = True

    if not found:
        logger.error("SPECGRID not found. Nothing imported!")
        return
    xfile.close()

    self._ncol, self._nrow, self._nlay = int(mylist[0]), int(mylist[1]), int(mylist[2])

    logger.info("NX NY NZ in grdecl file: %s %s %s", self._ncol, self._nrow, self._nlay)

    ntot = self._ncol * self._nrow * self._nlay
    ncoord = (self._ncol + 1) * (self._nrow + 1) * 2 * 3
    nzcorn = self._ncol * self._nrow * (self._nlay + 1) * 4

    logger.info("Reading...")

    ptr_num_act = _cxtgeo.new_intpointer()
    self._p_coord_v = _cxtgeo.new_doublearray(ncoord)
    self._p_zcorn_v = _cxtgeo.new_doublearray(nzcorn)
    self._p_actnum_v = _cxtgeo.new_intarray(ntot)

    _cxtgeo.grd3d_import_grdecl(
        self._ncol,
        self._nrow,
        self._nlay,
        self._p_coord_v,
        self._p_zcorn_v,
        self._p_actnum_v,
        ptr_num_act,
        tmpfile,
        XTGDEBUG,
    )

    # remove tmpfile
    os.remove(tmpfile)

    nact = _cxtgeo.intpointer_value(ptr_num_act)

    logger.info("Number of active cells: %s", nact)
    self._subgrids = None