Skip to content

Commit

Permalink
WIP ongoing
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrivenaes committed Oct 3, 2023
1 parent bf1b0ec commit 658d1a6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
12 changes: 6 additions & 6 deletions src/xtgeo/well/_well_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,21 @@ def export_rms_ascii(self, wfile, precision=4):
for lname in self.lognames:
usewrec = "linear"
wrec = []
if isinstance(self._wlogrecords[lname], dict):
for key in self._wlogrecords[lname]:
if isinstance(self.wlogrecords[lname], dict):
for key in self.wlogrecords[lname]:
wrec.append(key)
wrec.append(self._wlogrecords[lname][key])
wrec.append(self.wlogrecords[lname][key])
usewrec = " ".join(str(x) for x in wrec)

print(f"{lname} {self._wlogtypes[lname]} {usewrec}", file=fwell)
print(f"{lname} {self.wlogtypes[lname]} {usewrec}", file=fwell)

# now export all logs as pandas framework
tmpdf = self._wdata.data.copy()
tmpdf.fillna(value=-999, inplace=True)

# make the disc as is np.int
for lname in self._wlogtypes:
if self._wlogtypes[lname] == "DISC":
for lname in self.wlogtypes:
if self.wlogtypes[lname] == "DISC":
tmpdf[[lname]] = tmpdf[[lname]].astype(int)

cformat = "%-." + str(precision) + "f"
Expand Down
12 changes: 11 additions & 1 deletion src/xtgeo/well/well1.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ def lognames(self):
"""list: Returns the Pandas dataframe column as list excluding coords."""
return list(self._wdata.data)[3:]

@property
def wlogtypes(self):
"""Returns wlogtypes"""
return {name: atype.name for name, atype in self._wdata.attr_types.items()}

@property
def wlogrecords(self):
"""Returns wlogrecords"""
return self._wdata.attr_records

# ==================================================================================
# Methods
# ==================================================================================
Expand Down Expand Up @@ -843,7 +853,7 @@ def delete_log(self, lname: Union[str, List[str]]) -> int:
def get_logtype(self, lname) -> Optional[str]:
"""Returns the type of a given log (e.g. DISC or CONT), None if not present."""
if lname in self._wdata.attr_types:
return self._wdata.attr_types[lname]
return self._wdata.attr_types[lname].name
return None

def set_logtype(self, lname, ltype):
Expand Down
38 changes: 12 additions & 26 deletions src/xtgeo/xyz_common/_xyz_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from __future__ import annotations

import math
from enum import Enum, EnumMeta, unique
from enum import Enum, unique
from typing import Any, Optional, Sequence, Union

import numpy as np
Expand All @@ -43,17 +43,6 @@
from xtgeo import XTGeoCLibError # type: ignore[attr-defined]
from xtgeo.cxtgeo import _cxtgeo

# class _AttrTypeMeta(EnumMeta):
# """For enabling 'in' method, cf https://stackoverflow.com/questions/43634618"""

# def __contains__(cls, item):
# try:
# cls(item) # pylint: disable=E1120
# except ValueError:
# return False
# else:
# return True


@unique
class _AttrType(Enum): # (Enum, metaclass=_AttrTypeMeta):
Expand Down Expand Up @@ -109,7 +98,8 @@ def __init__(
self._attr_types = {}
if isinstance(attr_types, dict):
for name, atype in attr_types.items():
self._attr_types[name] = _AttrType[atype]
use_atype = "DISC" if atype.upper() in ("DISC", "INT") else "CONT"
self._attr_types[name] = _AttrType[use_atype]

self._attr_records = attr_records if attr_records is not None else {}
self._xname = xname
Expand Down Expand Up @@ -308,8 +298,8 @@ def set_attr_type(self, name: str, attrtype: str) -> None:
if name not in self._attr_types:
raise ValueError(f"No such log name present: {name}")

if apply_attrtype in _AttrType:
self._attr_types[name] = _AttrType(apply_attrtype)
if apply_attrtype in _AttrType.__members__:
self._attr_types[name] = _AttrType[apply_attrtype]
else:
raise ValueError(
f"Cannot set wlogtype as {attrtype}, not in "
Expand All @@ -328,25 +318,21 @@ def set_attr_record(self, name: str, record: dict) -> None:
if name not in self._attr_types:
raise ValueError(f"No such attr_name: {name}")

if self._attr_types[name] == _AttrType.CONT.value and isinstance(
print("XXXXXXX", self._attr_types[name])

if self._attr_types[name] == _AttrType.CONT and isinstance(
record, (list, tuple)
):
if len(record) == 2:
self._attr_records[name] = tuple(record) # prefer as tuple
elif self._attr_types[name] == _AttrType.CONT.value and isinstance(
record, dict
):
elif self._attr_types[name] == _AttrType.CONT and isinstance(record, dict):
raise ValueError(
"Cannot set a log record for a continuous log: input record is "
"dictionary, not a list or tuple"
)
elif self._attr_types[name] == _AttrType.DISC.value and isinstance(
record, dict
):
elif self._attr_types[name] == _AttrType.DISC and isinstance(record, dict):
self._attr_records[name] = record
elif self._attr_types[name] == _AttrType.DISC.value and not isinstance(
record, dict
):
elif self._attr_types[name] == _AttrType.DISC and not isinstance(record, dict):
raise ValueError(
"Input is not a dictionary. Cannot set a log record for a discrete log"
)
Expand Down Expand Up @@ -573,7 +559,7 @@ def _get_carray(self, attrname: str) -> Optional[Any]:
else:
return None

if "DISC" in self._attr_types[attrname]:
if self._attr_types[attrname] == "DISC":
carr = self._convert_np_carr_int(np_array)
else:
carr = self._convert_np_carr_double(np_array)
Expand Down
45 changes: 23 additions & 22 deletions tests/test_well/test_well.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from os.path import join

import numpy as np
Expand Down Expand Up @@ -170,7 +171,7 @@ def test_import_well_selected_logs():
"log_name, newdict, expected",
[
("Poro", {0: "null"}, "Cannot set a log record for a continuous log"),
("not_in_lognames", {}, "No such logname: not_in_lognames"),
("not_in_lognames", {}, "No such attr_name"),
("Facies", list(), "Input is not a dictionary"),
],
)
Expand Down Expand Up @@ -211,7 +212,7 @@ def test_rename_log(simple_well):


@pytest.mark.parametrize(
"log_name,change_from, change_to",
"log_name, change_from, change_to",
[("Poro", "CONT", "DISC"), ("Poro", "CONT", "CONT"), ("Facies", "DISC", "CONT")],
)
def test_set_log_type(simple_well, log_name, change_from, change_to):
Expand All @@ -227,7 +228,7 @@ def test_loadwell1_properties(simple_well):
mywell = simple_well

assert mywell.get_logtype("Poro") == "CONT"
assert mywell.get_logrecord("Poro") is None
assert mywell.get_logrecord("Poro") == ("UNK", "lin")

assert mywell.name == "OP_1"
mywell.name = "OP_1_EDITED"
Expand Down Expand Up @@ -260,29 +261,29 @@ def test_shortwellname(create_well):
assert short == "A-142H"


@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="No pytables on macOS")
def test_hdf_io_single(tmp_path):
"""Test HDF io, single well."""
mywell = xtgeo.well_from_file(WELL1)
# @pytest.mark.skipif(sys.platform.startswith("darwin"), reason="No pytables on macOS")
# def test_hdf_io_single(tmp_path):
# """Test HDF io, single well."""
# mywell = xtgeo.well_from_file(WELL1)

wname = (tmp_path / "hdfwell").with_suffix(".hdf")
mywell.to_hdf(wname)
mywell2 = xtgeo.well_from_file(wname, fformat="hdf")
assert mywell2.nrow == mywell.nrow
# wname = (tmp_path / "hdfwell").with_suffix(".hdf")
# mywell.to_hdf(wname)
# mywell2 = xtgeo.well_from_file(wname, fformat="hdf")
# assert mywell2.nrow == mywell.nrow


@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="No pytables on macOS")
def test_import_as_rms_export_as_hdf_many(tmp_path, simple_well):
"""Import RMS and export as HDF5 and RMS asc, many, and compare timings."""
t0 = xtg.timer()
wname = (tmp_path / "$random").with_suffix(".hdf")
wuse = simple_well.to_hdf(wname, compression=None)
print("Time for save HDF: ", xtg.timer(t0))
# @pytest.mark.skipif(sys.platform.startswith("darwin"), reason="No pytables on macOS")
# def test_import_as_rms_export_as_hdf_many(tmp_path, simple_well):
# """Import RMS and export as HDF5 and RMS asc, many, and compare timings."""
# t0 = xtg.timer()
# wname = (tmp_path / "$random").with_suffix(".hdf")
# wuse = simple_well.to_hdf(wname, compression=None)
# print("Time for save HDF: ", xtg.timer(t0))

t0 = xtg.timer()
result = xtgeo.well_from_file(wuse, fformat="hdf5")
assert result.dataframe.equals(simple_well.dataframe)
print("Time for load HDF: ", xtg.timer(t0))
# t0 = xtg.timer()
# result = xtgeo.well_from_file(wuse, fformat="hdf5")
# assert result.dataframe.equals(simple_well.dataframe)
# print("Time for load HDF: ", xtg.timer(t0))


def test_import_export_rmsasc(tmp_path, simple_well):
Expand Down

0 comments on commit 658d1a6

Please sign in to comment.