Exemple #1
0
def _import_xtgcpprop_partial(mfile, nbyte, dtype, offset, ijrange, zerobased,
                              ncol, nrow, nlay):
    """Partial import of a property."""
    i1, i2, j1, j2 = ijrange
    if not zerobased:
        i1 -= 1
        i2 -= 1
        j1 -= 1
        j2 -= 1

    ncolnew = i2 - i1 + 1
    nrownew = j2 - j1 + 1

    if ncolnew < 1 or ncolnew > ncol or nrownew < 1 or nrownew > nrow:
        raise ValueError("The ijrange spesification is invalid.")

    vals = np.zeros(ncolnew * nrownew * nlay, dtype=dtype)

    for newnum, inum in enumerate(range(i1, i2 + 1)):
        newpos = offset + (inum * nrow * nlay + j1 * nlay) * nbyte
        ncount = nrownew * nlay
        xvals = xsys.npfromfile(mfile.file,
                                dtype=dtype,
                                count=ncount,
                                offset=newpos)
        vals[newnum * ncount:newnum * ncount + ncount] = xvals

    return vals, ncolnew, nrownew
Exemple #2
0
def import_xtgregcube(self, mfile, values=True):
    """Using pure python for experimental cube import, xtgregsurf format."""
    logger.info("Importing cube on xtgregcube format...")

    offset = 36
    with open(mfile.file, "rb") as fhandle:
        buf = fhandle.read(offset)

    # unpack header
    swap, magic, nfloat, ncol, nrow, nlay = unpack("= i i i q q q", buf)

    if swap != 1 or magic != 1201:
        raise ValueError(
            "Invalid file format (wrong swap id or magic number).")

    dtype = np.float32 if nfloat == 4 else np.float64

    vals = None
    narr = ncol * nrow * nlay

    if values:
        vals = xsys.npfromfile(mfile.file,
                               dtype=dtype,
                               count=narr,
                               offset=offset)

    # read metadata which will be at position offet + nfloat*narr +13
    pos = offset + nfloat * narr + 13

    with open(mfile.file, "rb") as fhandle:
        fhandle.seek(pos)
        jmeta = fhandle.read().decode()

    meta = json.loads(jmeta)
    req = meta["_required_"]

    reqattrs = xtgeo.MetaDataRegularCube.REQUIRED

    for myattr in reqattrs:
        setattr(self, "_" + myattr, req[myattr])

    # TODO: dead traces and traceidcodes
    if values:
        self.values = vals.reshape(self.ncol, self.nrow, self.nlay)

    else:
        self._values = None

    self._metadata.required = self
    logger.info("Importing cube on xtgregcube format... done.")
Exemple #3
0
def import_xtg(self, mfile, values=True, **kwargs):
    """Using pure python for experimental XTGEO import."""
    logger.debug("Additional, probably unused kwargs: %s", **kwargs)

    offset = 28
    with open(mfile.file, "rb") as fhandle:
        buf = fhandle.read(offset)

    # unpack header
    swap, magic, nfloat, ncol, nrow = unpack("= i i i q q", buf)

    if swap != 1 or magic != 1101:
        raise ValueError(
            "Invalid file format (wrong swap id or magic number).")

    dtype = np.float32 if nfloat == 4 else np.float64

    vals = None
    narr = ncol * nrow
    if values:
        vals = xsys.npfromfile(mfile.file,
                               dtype=dtype,
                               count=narr,
                               offset=offset)

    # read metadata which will be at position offet + nfloat*narr +13
    pos = offset + nfloat * narr + 13

    with open(mfile.file, "rb") as fhandle:
        fhandle.seek(pos)
        jmeta = fhandle.read().decode()

    meta = json.loads(jmeta, object_pairs_hook=OrderedDict)
    req = meta["_required_"]

    reqattrs = xtgeo.MetaDataRegularSurface.REQUIRED

    for myattr in reqattrs:
        setattr(self, "_" + myattr, req[myattr])

    if values:
        self.values = np.ma.masked_equal(vals.reshape(self.ncol, self.nrow),
                                         self._undef)
    else:
        self._values = None

    self._metadata.required = self
Exemple #4
0
def import_xtgcpprop(self, mfile):
    """Using pure python for experimental import."""
    #
    offset = 36
    with open(mfile.file, "rb") as fhandle:
        buf = fhandle.read(offset)

    # unpack header
    swap, magic, nbyte, ncol, nrow, nlay = unpack("= i i i q q q", buf)

    if swap != 1 or magic not in (1351, 1352):
        raise ValueError(
            "Invalid file format (wrong swap id or magic number).")

    if magic == 1351:
        dtype = np.float32 if nbyte == 4 else np.float64
    else:
        dtype = "int" + str(nbyte * 8)

    vals = None
    narr = ncol * nrow * nlay
    vals = xsys.npfromfile(mfile.file, dtype=dtype, count=narr, offset=offset)

    # read metadata which will be at position offet + nfloat*narr +13
    pos = offset + nbyte * narr + 13

    with open(mfile.file, "rb") as fhandle:
        fhandle.seek(pos)
        jmeta = fhandle.read().decode()

    meta = json.loads(jmeta)
    req = meta["_required_"]

    reqattrs = xtgeo.MetaDataCPProperty.REQUIRED

    for myattr in reqattrs:
        if "discrete" in myattr:
            self._isdiscrete = req[myattr]
        else:
            setattr(self, "_" + myattr, req[myattr])

    self._values = np.ma.masked_equal(vals.reshape(ncol, nrow, nlay),
                                      self._undef)

    self._metadata.required = self
Exemple #5
0
def import_xtgcpgeom(self, mfile, mmap):  # pylint: disable=too-many-locals, too-many-statements
    """Using pure python for experimental grid geometry import."""
    #
    offset = 36
    with open(mfile.file, "rb") as fhandle:
        buf = fhandle.read(offset)

    # unpack header
    swap, magic, nformat, ncol, nrow, nlay = unpack("= i i i q q q", buf)
    nncol = ncol + 1
    nnrow = nrow + 1
    nnlay = nlay + 1

    if swap != 1 or magic != 1301:
        raise ValueError(
            f"Error, swap magic are {swap} {magic}, expected is 1 1301")

    # subformat processing, indicating number of bytes per datatype
    # here, 844 is native XTGeo (float64, float32, int32)
    if nformat not in (444, 844, 841, 881, 884):
        raise ValueError(f"The subformat value {nformat} is not valid")

    coordfmt, zcornfmt, actnumfmt = [int(nbyte) for nbyte in str(nformat)]

    dtype_coordsv = "float" + str(coordfmt * 8)
    dtype_zcornsv = "float" + str(zcornfmt * 8)
    dtype_actnumv = "int" + str(actnumfmt * 8)

    ncoord = nncol * nnrow * 6
    nzcorn = nncol * nnrow * nnlay * 4
    nactnum = ncol * nrow * nlay

    # read numpy arrays from file
    coordsv = xsys.npfromfile(mfile.file,
                              dtype=dtype_coordsv,
                              count=ncoord,
                              offset=offset,
                              mmap=mmap)
    newoffset = offset + ncoord * coordfmt
    zcornsv = xsys.npfromfile(mfile.file,
                              dtype=dtype_zcornsv,
                              count=nzcorn,
                              offset=newoffset,
                              mmap=mmap)
    newoffset += nzcorn * zcornfmt
    actnumsv = xsys.npfromfile(mfile.file,
                               dtype=dtype_actnumv,
                               count=nactnum,
                               offset=newoffset,
                               mmap=mmap)
    newoffset += nactnum * actnumfmt

    # read metadata which will be at position offet + nfloat*narr +13
    pos = newoffset + 13

    with open(mfile.file, "rb") as fhandle:
        fhandle.seek(pos)
        jmeta = fhandle.read().decode()

    meta = json.loads(jmeta, object_pairs_hook=OrderedDict)
    req = meta["_required_"]

    # meta _optional_ *may* contain xshift, xscale etc which in case must be taken
    # into account
    opt = meta.get("_optional_", None)
    if opt:
        if {"xshift", "xscale"}.issubset(opt):
            shi = opt["xshift"]
            sca = opt["xscale"]
            coordsv[0::3] = np.where(shi != 0, coordsv[0::3] + shi,
                                     coordsv[0::3])
            coordsv[0::3] = np.where(sca != 1, coordsv[0::3] * sca,
                                     coordsv[0::3])
        if {"yshift", "yscale"}.issubset(opt):
            shi = opt["yshift"]
            sca = opt["yscale"]
            coordsv[1::3] = np.where(shi != 0, coordsv[1::3] + shi,
                                     coordsv[1::3])
            coordsv[1::3] = np.where(sca != 1, coordsv[1::3] * sca,
                                     coordsv[1::3])
        if {"zshift", "zscale"}.issubset(opt):
            shi = opt["zshift"]
            sca = opt["zscale"]
            coordsv[2::3] = np.where(shi != 0, coordsv[2::3] + shi,
                                     coordsv[2::3])
            coordsv[2::3] = np.where(sca != 1, coordsv[2::3] * sca,
                                     coordsv[2::3])
            zcornsv = (zcornsv + shi) * sca

    self._coordsv = coordsv.reshape((nncol, nnrow, 6)).astype(np.float64)
    self._zcornsv = zcornsv.reshape(
        (nncol, nnrow, nnlay, 4)).astype(np.float32)
    self._actnumsv = actnumsv.reshape((ncol, nrow, nlay)).astype(np.int32)

    reqattrs = xtgeo.MetaDataCPGeometry.REQUIRED

    for myattr in reqattrs:
        if "subgrid" in myattr:
            self.set_subgrids(reqattrs["subgrids"])
        else:
            setattr(self, "_" + myattr, req[myattr])

    self._metadata.required = self
Exemple #6
0
def import_xtgcpprop(self, mfile, ijrange=None, zerobased=False):
    """Using pure python for experimental xtgcpprop import.

    Args:
        self (obj): instance
        mfile (_XTGeoFile): Input file reference
        ijrange (list-like): List or tuple with 4 members [i_from, i_to, j_from, j_to]
            where cell indices are zero based (starts with 0)
        zerobased (bool): If ijrange basis is zero or one.

    """
    #
    offset = 36
    with open(mfile.file, "rb") as fhandle:
        buf = fhandle.read(offset)

    # unpack header
    swap, magic, nbyte, ncol, nrow, nlay = unpack("= i i i q q q", buf)

    if swap != 1 or magic not in (1351, 1352):
        raise ValueError(
            "Invalid file format (wrong swap id or magic number).")

    if magic == 1351:
        dtype = np.float32 if nbyte == 4 else np.float64
    else:
        dtype = "int" + str(nbyte * 8)

    vals = None
    narr = ncol * nrow * nlay

    ncolnew = nrownew = 0

    if ijrange:
        vals, ncolnew, nrownew = _import_xtgcpprop_partial(
            mfile, nbyte, dtype, offset, ijrange, zerobased, ncol, nrow, nlay)

    else:
        vals = xsys.npfromfile(mfile.file,
                               dtype=dtype,
                               count=narr,
                               offset=offset)

    # read metadata which will be at position offet + nfloat*narr +13
    pos = offset + nbyte * narr + 13

    with open(mfile.file, "rb") as fhandle:
        fhandle.seek(pos)
        jmeta = fhandle.read().decode()

    meta = json.loads(jmeta, object_pairs_hook=OrderedDict)
    req = meta["_required_"]

    reqattrs = xtgeo.MetaDataCPProperty.REQUIRED

    for myattr in reqattrs:
        if "discrete" in myattr:
            self._isdiscrete = req[myattr]
        else:
            setattr(self, "_" + myattr, req[myattr])

    if ijrange:
        self._ncol = ncolnew
        self._nrow = nrownew

    self._values = np.ma.masked_equal(
        vals.reshape(self._ncol, self._nrow, self._nlay), self._undef)

    self._metadata.required = self