コード例 #1
0
def update_carray(self, undef=None, discrete=None, dtype=None, order="F"):
    """Copy (update) values from numpy to SWIG, 1D array, returns a pointer
    to SWIG C array. If discrete is defined as True or False, force
    the SWIG array to be of that kind.

    Note that dtype will "override" current datatype if set. The resulting
    carray will be in Fortran order, unless order is specified as 'C'
    """

    dstatus = self._isdiscrete
    if discrete is not None:
        dstatus = bool(discrete)

    if undef is None:
        undef = xtgeo.UNDEF
        if dstatus:
            undef = xtgeo.UNDEF_INT

    logger.debug("Entering conversion from numpy to C array ...")

    values = self._values.copy()

    if not dtype:
        if dstatus:
            values = values.astype(np.int32)
        else:
            values = values.astype(np.float64)
    else:
        values = values.astype(dtype)

    values = ma.filled(values, undef)

    if order == "F":
        values = np.asfortranarray(values)
        values1d = np.ravel(values, order="K")

    if values1d.dtype == "float64" and dstatus:
        values1d = values1d.astype("int32")
        logger.debug("Casting has been done")

    if values1d.dtype == "float64":
        logger.debug("Convert to carray (double)")
        carray = _cxtgeo.new_doublearray(self.ntotal)
        _cxtgeo.swig_numpy_to_carr_1d(values1d, carray)
    elif values1d.dtype == "float32":
        logger.debug("Convert to carray (float)")
        carray = _cxtgeo.new_floatarray(self.ntotal)
        _cxtgeo.swig_numpy_to_carr_f1d(values1d, carray)
    elif values1d.dtype == "int32":
        logger.debug("Convert to carray (int32)")
        carray = _cxtgeo.new_intarray(self.ntotal)
        _cxtgeo.swig_numpy_to_carr_i1d(values1d, carray)
    else:
        raise RuntimeError(
            "Unsupported dtype, probable bug in {}".format(__name__))
    return carray
コード例 #2
0
    def _convert_np_carr_int(self, np_array):
        """Convert numpy 1D array to C array, assuming int type.

        The numpy is always a double (float64), so need to convert first
        """

        carr = _cxtgeo.new_intarray(self.nrow)

        np_array = np_array.astype(np.int32)

        _cxtgeo.swig_numpy_to_carr_i1d(np_array, carr)

        return carr
コード例 #3
0
def _export_segy_xtgeo(self, sfile):
    """Export SEGY via XTGeo internal C routine."""

    values1d = self.values.reshape(-1)

    ilinesp = _cxtgeo.new_intarray(len(self._ilines))
    xlinesp = _cxtgeo.new_intarray(len(self._xlines))
    tracidp = _cxtgeo.new_intarray(self.ncol * self.nrow)

    ilns = self._ilines.astype(np.int32)
    xlns = self._xlines.astype(np.int32)
    trid = self._traceidcodes.flatten().astype(np.int32)

    _cxtgeo.swig_numpy_to_carr_i1d(ilns, ilinesp)
    _cxtgeo.swig_numpy_to_carr_i1d(xlns, xlinesp)
    _cxtgeo.swig_numpy_to_carr_i1d(trid, tracidp)

    status = _cxtgeo.cube_export_segy(
        sfile,
        self.ncol,
        self.nrow,
        self.nlay,
        values1d,
        self.xori,
        self.xinc,
        self.yori,
        self.yinc,
        self.zori,
        self.zinc,
        self.rotation,
        self.yflip,
        1,
        ilinesp,
        xlinesp,
        tracidp,
        0,
        XTGDEBUG,
    )

    if status != 0:
        raise RuntimeError("Error when exporting to SEGY (xtgeo engine)")

    _cxtgeo.delete_intarray(ilinesp)
    _cxtgeo.delete_intarray(xlinesp)
コード例 #4
0
def _convert_to_xtgeo_grid(self, rox, roxgrid, corners):
    """Convert from RMS API to XTGeo API"""

    # pylint: disable=too-many-statements

    logger.info("Converting to XTGeo internals...")
    logger.info("Call the ROXAPI grid indexer")
    indexer = roxgrid.grid_indexer

    ncol, nrow, nlay = indexer.dimensions
    ntot = ncol * nrow * nlay

    # update other attributes
    self._ncol = ncol
    self._nrow = nrow
    self._nlay = nlay

    if corners is None:
        logger.info("Asked for dimensions_only: No geometry read!")
        return

    logger.info("Get active cells")
    mybuffer = np.ndarray(indexer.dimensions, dtype=np.int32)

    mybuffer.fill(0)

    logger.info("Get cell numbers")
    cellno = indexer.get_cell_numbers_in_range((0, 0, 0), indexer.dimensions)

    logger.info("Reorder...")
    ijk = indexer.get_indices(cellno)

    iind = ijk[:, 0]
    jind = ijk[:, 1]
    kind = ijk[:, 2]

    pvalues = np.ones(len(cellno))
    pvalues[cellno] = 1
    mybuffer[iind, jind, kind] = pvalues[cellno]

    actnum = mybuffer

    if rox.version_required("1.3"):
        logger.info("Handedness (new) %s", indexer.ijk_handedness)
    else:
        logger.info("Handedness (old) %s", indexer.handedness)

    corners = corners.ravel(order="K")
    actnum = actnum.ravel(order="K")

    logger.info("Convert to C pointers...")

    nnum = ncol * nrow * nlay * 24
    ccorners = _cxtgeo.new_doublearray(nnum)
    ntot = ncol * nrow * nlay
    cactnum = _cxtgeo.new_intarray(ntot)
    ncoord = (ncol + 1) * (nrow + 1) * 2 * 3
    nzcorn = ncol * nrow * (nlay + 1) * 4

    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.swig_numpy_to_carr_1d(corners, ccorners)
    _cxtgeo.swig_numpy_to_carr_i1d(actnum, cactnum)

    # next task is to convert geometry to cxtgeo internal format
    logger.info("Run XTGeo C code...")
    _cxtgeo.grd3d_conv_roxapi_grid(
        ncol,
        nrow,
        nlay,
        ntot,
        cactnum,
        ccorners,
        self._p_coord_v,
        self._p_zcorn_v,
        self._p_actnum_v,
        XTGDEBUG,
    )
    logger.info("Run XTGeo C code... done")

    _cxtgeo.delete_doublearray(ccorners)
    _cxtgeo.delete_intarray(cactnum)
    logger.info("Converting to XTGeo internals... done")

    # subgrids
    if len(indexer.zonation) > 1:
        logger.debug("Zonation length (N subzones) is %s",
                     len(indexer.zonation))
        subz = OrderedDict()
        for inum, zrange in indexer.zonation.items():
            logger.debug("inum: %s, zrange: %s", inum, zrange)
            zname = roxgrid.zone_names[inum]
            logger.debug("zname is: %s", zname)
            zra = [nn + 1 for ira in zrange for nn in ira]  # nested lists
            subz[zname] = zra

        self.subgrids = subz