def _rkwxvec(fhandle, kws, name, swap, strict=True): """Local function for returning swig pointers to C arrays. If strict is True, a ValueError will be raised if keyword is not found. If strict is False, None will be returned """ kwtypedict = { "int": 1, "float": 2, "double": 3, "char": 4, "bool": 5, "byte": 6 } 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: if strict: raise ValueError("Cannot find property <{}> in file".format(name)) return None if reclen <= 1: raise SystemError("Stuff is rotten here...") xvec = None if dtype == 1: xvec = _cxtgeo.new_floatarray(reclen) _cxtgeo.grd3d_imp_roffbin_ivec(fhandle, swap, bytepos, xvec, reclen) elif dtype == 2: xvec = _cxtgeo.new_floatarray(reclen) _cxtgeo.grd3d_imp_roffbin_fvec(fhandle, swap, bytepos, xvec, reclen) elif dtype >= 4: xvec = _cxtgeo.new_intarray(reclen) # convert char/byte/bool to int _cxtgeo.grd3d_imp_roffbin_bvec(fhandle, swap, bytepos, xvec, reclen) else: raise ValueError("Unhandled dtype: {}".format(dtype)) return xvec
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 and not dtype: 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
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