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, ) 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 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)
def _export_roff_discrete(self, pfile, name, append=False, last=True, binary=True): carray = _gridprop_lowlevel.update_carray(self, undef=-999) ptr_idum = _cxtgeo.new_intpointer() ptr_ddum = _cxtgeo.new_doublepointer() # codes: ptr_codes = _cxtgeo.new_intarray(256) ncodes = self.ncodes codenames = "" logger.info("Keys: %s", self.codes.keys()) for inum, ckey in enumerate(sorted(self.codes.keys())): if ckey is not None: codenames += str(self.codes[ckey]) codenames += "|" _cxtgeo.intarray_setitem(ptr_codes, inum, int(ckey)) else: logger.warning("For some odd reason, None is a key. Check!") mode = 0 if not binary: mode = 1 if not append: _cxtgeo.grd3d_export_roff_pstart(mode, self._ncol, self._nrow, self._nlay, pfile, XTGDEBUG) nsub = 0 isub_to_export = 0 _cxtgeo.grd3d_export_roff_prop( mode, self._ncol, self._nrow, self._nlay, nsub, isub_to_export, ptr_idum, name, "int", carray, ptr_ddum, ncodes, codenames, ptr_codes, pfile, XTGDEBUG, ) if last: _cxtgeo.grd3d_export_roff_end(mode, pfile, XTGDEBUG) _gridprop_lowlevel.delete_carray(self, carray)
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 ) 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
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)
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 _export_roff_continuous(self, pfile, name, append=False, last=True, binary=True): carray = _gridprop_lowlevel.update_carray(self, undef=-999.0) ptr_idum = _cxtgeo.new_intpointer() mode = 0 if not binary: mode = 1 if not append: _cxtgeo.grd3d_export_roff_pstart(mode, self._ncol, self._nrow, self._nlay, pfile, XTGDEBUG) # now the actual data nsub = 0 isub_to_export = 0 _cxtgeo.grd3d_export_roff_prop( mode, self._ncol, self._nrow, self._nlay, nsub, isub_to_export, ptr_idum, name, "double", ptr_idum, carray, 0, "", ptr_idum, pfile, XTGDEBUG, ) if last: _cxtgeo.grd3d_export_roff_end(mode, pfile, XTGDEBUG) _gridprop_lowlevel.delete_carray(self, carray)
def reduce_to_one_layer(self): """Reduce the grid to one single layer. This can be useful for algorithms that need to test if a point is within the full grid. Example:: >>> from xtgeo.grid3d import Grid >>> gf = Grid('gullfaks2.roff') >>> gf.nlay 47 >>> gf.reduce_to_one_layer() >>> gf.nlay 1 """ # need new pointers in C (not for coord) # Note this could probably be done with pure numpy operations self._xtgformat1() ptr_new_num_act = _cxtgeo.new_intpointer() nnum = (1 + 1) * 4 new_zcorn = np.zeros(self.ncol * self.nrow * nnum, dtype=np.float64) new_actnum = np.zeros(self.ncol * self.nrow * 1, dtype=np.int32) _cxtgeo.grd3d_reduce_onelayer( self.ncol, self.nrow, self.nlay, self._zcornsv, new_zcorn, self._actnumsv, new_actnum, ptr_new_num_act, 0, ) self._nlay = 1 self._zcornsv = new_zcorn self._actnumsv = new_actnum self._props = None self._subgrids = None
def reduce_to_one_layer(self): """Reduce the grid to one single layer. This can be useful for algorithms that need to test if a point is within the full grid. Example:: >>> from xtgeo.grid3d import Grid >>> gf = Grid('gullfaks2.roff') >>> gf.nlay 47 >>> gf.reduce_to_one_layer() >>> gf.nlay 1 """ # need new pointers in C (not for coord) ptr_new_num_act = _cxtgeo.new_intpointer() nnum = (1 + 1) * 4 ptr_new_zcorn_v = _cxtgeo.new_doublearray(self.ncol * self.nrow * nnum) ptr_new_actnum_v = _cxtgeo.new_intarray(self.ncol * self.nrow * 1) _cxtgeo.grd3d_reduce_onelayer( self.ncol, self.nrow, self.nlay, self._p_zcorn_v, ptr_new_zcorn_v, self._p_actnum_v, ptr_new_actnum_v, ptr_new_num_act, 0, XTGDEBUG, ) self._nlay = 1 self._p_zcorn_v = ptr_new_zcorn_v self._p_actnum_v = ptr_new_actnum_v self._props = None self._subgrids = None
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
def _export_roff_v1(self, gfile, option): """Export grid to ROFF format (binary)""" self._xtgformat1() gfile = xtgeo._XTGeoFile(gfile, mode="wb") gfile.check_folder(raiseerror=OSError) logger.debug("Export to ROFF...") nsubs = 0 if self.subgrids is None: logger.debug("Create a pointer for subgrd_v ...") subgrd_v = _cxtgeo.new_intpointer() else: nsubs = len(self.subgrids) subgrd_v = _cxtgeo.new_intarray(nsubs) for inum, (sname, sarray) in enumerate(self.subgrids.items()): logger.info("INUM SUBGRID: %s %s", inum, sname) _cxtgeo.intarray_setitem(subgrd_v, inum, len(sarray)) # get the geometrics list to find the xshift, etc gx = self.get_geometrics() _cxtgeo.grd3d_export_roff_grid( option, self._ncol, self._nrow, self._nlay, nsubs, 0, gx[3], gx[5], gx[7], self._coordsv, self._zcornsv, self._actnumsv, subgrd_v, gfile.name, ) # end tag _cxtgeo.grd3d_export_roff_end(option, gfile.name)
def export_roff(self, gfile, option): """Export grid to ROFF format (binary)""" logger.debug("Export to ROFF...") nsubs = 0 if self.subgrids is None: logger.debug("Create a pointer for subgrd_v ...") subgrd_v = _cxtgeo.new_intpointer() else: nsubs = len(self.subgrids) subgrd_v = _cxtgeo.new_intarray(nsubs) for inum, (sname, sarray) in enumerate(self.subgrids.items()): logger.info("INUM SUBGRID: %s %s", inum, sname) _cxtgeo.intarray_setitem(subgrd_v, inum, len(sarray)) # get the geometrics list to find the xshift, etc gx = self.get_geometrics() _cxtgeo.grd3d_export_roff_grid( option, self._ncol, self._nrow, self._nlay, nsubs, 0, gx[3], gx[5], gx[7], self._p_coord_v, self._p_zcorn_v, self._p_actnum_v, subgrd_v, gfile, XTGDEBUG, ) # skip parameters for now (cf Perl code) # end tag _cxtgeo.grd3d_export_roff_end(option, gfile, XTGDEBUG)
def import_ecl_grdecl(self, gfile): # make a temporary file fds, tmpfile = mkstemp(prefix="tmpxtgeo") os.close(fds) with open(gfile.name) 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) ncoord, nzcorn, ntot = self.vectordimensions logger.info("Reading...") self._coordsv = np.zeros(ncoord, dtype=np.float64) self._zcornsv = np.zeros(nzcorn, dtype=np.float64) self._actnumsv = np.zeros(ntot, dtype=np.int32) ptr_num_act = _cxtgeo.new_intpointer() cfhandle = gfile.get_cfhandle() _cxtgeo.grd3d_import_grdecl( cfhandle, self._ncol, self._nrow, self._nlay, self._coordsv, self._zcornsv, self._actnumsv, ptr_num_act, ) # close and remove tmpfile gfile.cfclose() os.remove(tmpfile) nact = _cxtgeo.intpointer_value(ptr_num_act) logger.info("Number of active cells: %s", nact) self._subgrids = None
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
def refine_vertically(self, rfactor, zoneprop=None): """Refine vertically, proportionally See details in caller. """ rfactord = OrderedDict() # case 1 rfactor as scalar value. if isinstance(rfactor, int): if self.subgrids: subgrids = self.get_subgrids() for i, _ in enumerate(self.subgrids.keys()): rfactord[i + 1] = rfactor else: rfactord[0] = rfactor subgrids = OrderedDict() subgrids[1] = self.nlay # case 2 rfactor is a dict else: rfactord = OrderedDict(sorted(rfactor.items())) # redefined to ordered # 2a: zoneprop is present if zoneprop is not None: oldsubgrids = None if self.subgrids: oldsubgrids = self.get_subgrids() subgrids = self.subgrids_from_zoneprop(zoneprop) if oldsubgrids: if subgrids.values() != oldsubgrids.values(): xtg.warn("ISSUES!!!") # 2b: zoneprop is not present elif zoneprop is None and self.subgrids: subgrids = self.get_subgrids() elif zoneprop is None and not self.subgrids: raise ValueError("You gave in a dict, but no zoneprops and " "subgrids are not preesent in the grid") else: raise ValueError("Some major unexpected issue in routine...") if len(subgrids) != len(rfactord): raise RuntimeError("Subgrids and refinements: different definition!") self.set_subgrids(subgrids) # Now, based on dict, give a value per subgrid for key, val in rfactor newsubgrids = OrderedDict() newnlay = 0 for (_x, rfi), (snam, sran) in zip(rfactord.items(), subgrids.items()): newsubgrids[snam] = sran * rfi newnlay += newsubgrids[snam] logger.debug("New layers: %s", newnlay) # rfac is an array with length nlay; has N refinements per single K layer rfac = _cxtgeo.new_intarray(self.nlay) totvector = [] for (_tmp1, rfi), (_tmp2, arr) in zip(rfactord.items(), self.subgrids.items()): for _elem in range(len(arr)): totvector.append(rfi) for inn, rfi in enumerate(totvector): _cxtgeo.intarray_setitem(rfac, inn, rfi) ref_num_act = _cxtgeo.new_intpointer() ref_p_zcorn_v = _cxtgeo.new_doublearray(self.ncol * self.nrow * (newnlay + 1) * 4) ref_p_actnum_v = _cxtgeo.new_intarray(self.ncol * self.nrow * newnlay) ier = _cxtgeo.grd3d_refine_vert( self.ncol, self.nrow, self.nlay, self._p_coord_v, self._p_zcorn_v, self._p_actnum_v, newnlay, ref_p_zcorn_v, ref_p_actnum_v, ref_num_act, rfac, 0, XTGDEBUG, ) if ier != 0: raise RuntimeError("An error occured in the C routine " "grd3d_refine_vert, code {}".format(ier)) # update instance: self._nlay = newnlay self._p_zcorn_v = ref_p_zcorn_v self._p_actnum_v = ref_p_actnum_v if self.subgrids is None or len(self.subgrids) <= 1: self.subgrids = None else: self.set_subgrids(newsubgrids) return self
def make_hybridgrid(grid, **kwargs): """Make hybrid grid. It changes the grid geometry status of the object. Input: grid (object): A grid object TODO region (object): A region parameter (property object) etc... """ nhdiv = kwargs.get("nhdiv") toplevel = kwargs.get("toplevel") bottomlevel = kwargs.get("bottomlevel") region = kwargs.get("region", None) region_number = kwargs.get("region_number", None) logger.debug("nhdiv: %s", nhdiv) logger.debug("toplevel: %s", toplevel) logger.debug("bottomlevel: %s", bottomlevel) logger.debug("region: %s", region) logger.debug("region_number: %s", region_number) xtg_verbose_level = xtg.syslevel newnlay = grid.nlay * 2 + nhdiv hyb_num_act = _cxtgeo.new_intpointer() hyb_p_zcorn_v = _cxtgeo.new_doublearray(grid.ncol * grid.nrow * (newnlay + 1) * 4) hyb_p_actnum_v = _cxtgeo.new_intarray(grid.ncol * grid.nrow * newnlay) if region is None: _cxtgeo.grd3d_convert_hybrid( grid.ncol, grid.nrow, grid.nlay, grid._p_coord_v, grid._p_zcorn_v, grid._p_actnum_v, newnlay, hyb_p_zcorn_v, hyb_p_actnum_v, hyb_num_act, toplevel, bottomlevel, nhdiv, xtg_verbose_level, ) else: region.discrete_to_continuous() carray_reg = _gridprop_lowlevel.update_carray(region) _cxtgeo.grd3d_convert_hybrid2( grid.ncol, grid.nrow, grid.nlay, grid._p_coord_v, grid._p_zcorn_v, grid._p_actnum_v, newnlay, hyb_p_zcorn_v, hyb_p_actnum_v, hyb_num_act, toplevel, bottomlevel, nhdiv, carray_reg, region_number, xtg_verbose_level, ) _gridprop_lowlevel.delete_carray(region, carray_reg) grid._nlay = newnlay grid._p_zcorn_v = hyb_p_zcorn_v grid._p_actnum_v = hyb_p_actnum_v return grid
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
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
def crop(self, spec, props=None): # pylint: disable=too-many-locals """Do cropping of geometry (and properties). If props is 'all' then all properties assosiated (linked) to then grid are also cropped, and the instances are updated. Args: spec (tuple): A nested tuple on the form ((i1, i2), (j1, j2), (k1, k2)) where 1 represents start number, and 2 reperesent end. The range is inclusive for both ends, and the number start index is 1 based. props (list or str): None is default, while properties can be listed. If 'all', then all GridProperty objects which are linked to the Grid instance are updated. Returns: The instance is updated (cropped) """ (ic1, ic2), (jc1, jc2), (kc1, kc2) = spec if (ic1 < 1 or ic2 > self.ncol or jc1 < 1 or jc2 > self.nrow or kc1 < 1 or kc2 > self.nlay): raise ValueError("Boundary for tuples not matching grid" "NCOL, NROW, NLAY") oldnlay = self._nlay # compute size of new cropped grid nncol = ic2 - ic1 + 1 nnrow = jc2 - jc1 + 1 nnlay = kc2 - kc1 + 1 ntot = nncol * nnrow * nnlay ncoord = (nncol + 1) * (nnrow + 1) * 2 * 3 nzcorn = nncol * nnrow * (nnlay + 1) * 4 new_num_act = _cxtgeo.new_intpointer() new_coordsv = np.zeros(ncoord, dtype=np.float64) new_zcornsv = np.zeros(nzcorn, dtype=np.float64) new_actnumsv = np.zeros(ntot, dtype=np.int32) _cxtgeo.grd3d_crop_geometry( self.ncol, self.nrow, self.nlay, self._coordsv, self._zcornsv, self._actnumsv, new_coordsv, new_zcornsv, new_actnumsv, ic1, ic2, jc1, jc2, kc1, kc2, new_num_act, 0, ) self._coordsv = new_coordsv self._zcornsv = new_zcornsv self._actnumsv = new_actnumsv self._ncol = nncol self._nrow = nnrow self._nlay = nnlay if isinstance(self.subgrids, dict): newsub = OrderedDict() # easier to work with numpies than lists newarr = np.array(range(1, oldnlay + 1)) newarr[newarr < kc1] = 0 newarr[newarr > kc2] = 0 newaxx = newarr.copy() - kc1 + 1 for sub, arr in self.subgrids.items(): arrx = np.array(arr) arrxmap = newaxx[arrx[0] - 1:arrx[-1]] arrxmap = arrxmap[arrxmap > 0] if arrxmap.size > 0: newsub[sub] = arrxmap.astype(np.int32).tolist() self.subgrids = newsub # crop properties if props is not None: if props == "all": props = self.props for prop in props: logger.info("Crop %s", prop.name) prop.crop(spec)