def _scan_ecl_keywords(pfile, maxkeys=100000, dataframe=False): # In case pfile is not a file name but a swig pointer to a file handle, # the file must not be closed ultramax = int(1000000 / 9) # cf *swig_bnd_char_1m in cxtgeo.i if maxkeys > ultramax: raise ValueError("maxkeys value is too large, must be < {}".format(ultramax)) rectypes = _cxtgeo.new_intarray(maxkeys) reclens = _cxtgeo.new_longarray(maxkeys) recstarts = _cxtgeo.new_longarray(maxkeys) fhandle, pclose = _get_fhandle(pfile) nkeys, keywords = _cxtgeo.grd3d_scan_eclbinary( fhandle, rectypes, reclens, recstarts, maxkeys, XTGDEBUG ) _close_fhandle(fhandle, pclose) keywords = keywords.replace(" ", "") keywords = keywords.split("|") # record types translation (cf: grd3d_scan_eclbinary.c in cxtgeo) rct = { "1": "INTE", "2": "REAL", "3": "DOUB", "4": "CHAR", "5": "LOGI", "6": "MESS", "-1": "????", } rc = [] rl = [] rs = [] for i in range(nkeys): rc.append(rct[str(_cxtgeo.intarray_getitem(rectypes, i))]) rl.append(_cxtgeo.longarray_getitem(reclens, i)) rs.append(_cxtgeo.longarray_getitem(recstarts, i)) _cxtgeo.delete_intarray(rectypes) _cxtgeo.delete_longarray(reclens) _cxtgeo.delete_longarray(recstarts) result = list(zip(keywords, rc, rl, rs)) if dataframe: cols = ["KEYWORD", "TYPE", "NITEMS", "BYTESTART"] df = pd.DataFrame.from_records(result, columns=cols) return df return result
def __del__(self): if self._p_coord_v is not None: # logger.info("Deleting Grid instance %s", id(self)) _cxtgeo.delete_doublearray(self._p_coord_v) _cxtgeo.delete_doublearray(self._p_zcorn_v) _cxtgeo.delete_intarray(self._p_actnum_v) self._p_coord_v = None if self.props is not None: for prop in self.props: # logger.info("Deleting property instance %s", prop.name) prop.__del__()
def scan_dates(pfile, maxdates=1000, dataframe=False): """Quick scan dates in a simulation restart file. Cf. grid_properties.py description """ seq = _cxtgeo.new_intarray(maxdates) day = _cxtgeo.new_intarray(maxdates) mon = _cxtgeo.new_intarray(maxdates) yer = _cxtgeo.new_intarray(maxdates) local_fhandle = False fhandle = pfile if isinstance(pfile, str): pfile = xtgeo._XTGeoCFile(pfile) fhandle = pfile.fhandle local_fhandle = True nstat = _cxtgeo.grd3d_ecl_tsteps(fhandle, seq, day, mon, yer, maxdates, XTGDEBUG) if local_fhandle: pfile.close(cond=local_fhandle) sq = [] da = [] for i in range(nstat): sq.append(_cxtgeo.intarray_getitem(seq, i)) dday = _cxtgeo.intarray_getitem(day, i) dmon = _cxtgeo.intarray_getitem(mon, i) dyer = _cxtgeo.intarray_getitem(yer, i) date = "{0:4}{1:02}{2:02}".format(dyer, dmon, dday) da.append(int(date)) for item in [seq, day, mon, yer]: _cxtgeo.delete_intarray(item) zdates = list(zip(sq, da)) # list for PY3 if dataframe: cols = ["SEQNUM", "DATE"] df = pd.DataFrame.from_records(zdates, columns=cols) return df return zdates
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)
def delete_carray(self, carray): """Delete carray SWIG C pointer, return carray as None""" logger.debug("Enter delete carray values method for %d", id(self)) if carray is None: return None if "int" in str(carray): _cxtgeo.delete_intarray(carray) carray = None elif "float" in str(carray): _cxtgeo.delete_floatarray(carray) carray = None elif "double" in str(carray): _cxtgeo.delete_doublearray(carray) carray = None else: raise RuntimeError("BUG?") return carray
def scan_dates(pfile, fformat="unrst", maxdates=1000, dataframe=False): """Quick scan dates in a simulation restart file. Cf. grid_properties.py description """ logger.info("Format supported as default is %s", fformat) seq = _cxtgeo.new_intarray(maxdates) day = _cxtgeo.new_intarray(maxdates) mon = _cxtgeo.new_intarray(maxdates) yer = _cxtgeo.new_intarray(maxdates) fhandle, pclose = _get_fhandle(pfile) nstat = _cxtgeo.grd3d_ecl_tsteps(fhandle, seq, day, mon, yer, maxdates, XTGDEBUG) _close_fhandle(fhandle, pclose) sq = [] da = [] for i in range(nstat): sq.append(_cxtgeo.intarray_getitem(seq, i)) dday = _cxtgeo.intarray_getitem(day, i) dmon = _cxtgeo.intarray_getitem(mon, i) dyer = _cxtgeo.intarray_getitem(yer, i) date = "{0:4}{1:02}{2:02}".format(dyer, dmon, dday) da.append(int(date)) for item in [seq, day, mon, yer]: _cxtgeo.delete_intarray(item) zdates = list(zip(sq, da)) # list for PY3 if dataframe: cols = ["SEQNUM", "DATE"] df = pd.DataFrame.from_records(zdates, columns=cols) return df return zdates
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
def get_ijk_from_grid(self, grid, grid_id=""): """Getting IJK from a grid as well logs.""" wxarr = self.get_carray("X_UTME") wyarr = self.get_carray("Y_UTMN") wzarr = self.get_carray("Z_TVDSS") nlen = self.nrow wivec = _cxtgeo.new_intarray(nlen) wjvec = _cxtgeo.new_intarray(nlen) wkvec = _cxtgeo.new_intarray(nlen) onelayergrid = grid.copy() onelayergrid.reduce_to_one_layer() cstatus = _cxtgeo.grd3d_well_ijk( grid.ncol, grid.nrow, grid.nlay, grid._p_coord_v, grid._p_zcorn_v, grid._p_actnum_v, onelayergrid._p_zcorn_v, onelayergrid._p_actnum_v, self.nrow, wxarr, wyarr, wzarr, wivec, wjvec, wkvec, 0, XTG_DEBUG, ) if cstatus != 0: raise RuntimeError("Error from C routine, code is {}".format(cstatus)) indarray = _cxtgeo.swig_carr_to_numpy_i1d(nlen, wivec).astype("float") jndarray = _cxtgeo.swig_carr_to_numpy_i1d(nlen, wjvec).astype("float") kndarray = _cxtgeo.swig_carr_to_numpy_i1d(nlen, wkvec).astype("float") indarray[indarray == 0] = np.nan jndarray[jndarray == 0] = np.nan kndarray[kndarray == 0] = np.nan icellname = "ICELL" + grid_id jcellname = "JCELL" + grid_id kcellname = "KCELL" + grid_id self._df[icellname] = indarray self._df[jcellname] = jndarray self._df[kcellname] = kndarray for cellname in [icellname, jcellname, kcellname]: self._wlogtype[cellname] = "DISC" self._wlogrecord[icellname] = { ncel: str(ncel) for ncel in range(1, grid.ncol + 1) } self._wlogrecord[jcellname] = { ncel: str(ncel) for ncel in range(1, grid.nrow + 1) } self._wlogrecord[kcellname] = { ncel: str(ncel) for ncel in range(1, grid.nlay + 1) } _cxtgeo.delete_intarray(wivec) _cxtgeo.delete_intarray(wjvec) _cxtgeo.delete_intarray(wkvec) _cxtgeo.delete_doublearray(wxarr) _cxtgeo.delete_doublearray(wyarr) _cxtgeo.delete_doublearray(wzarr) del onelayergrid