From 658d1a6d950ebd2b773f70d3775deb1ebdd7c1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20C=2E=20Riven=C3=A6s?= Date: Tue, 3 Oct 2023 19:21:14 +0200 Subject: [PATCH] WIP ongoing --- src/xtgeo/well/_well_io.py | 12 ++++----- src/xtgeo/well/well1.py | 12 ++++++++- src/xtgeo/xyz_common/_xyz_data.py | 38 +++++++++----------------- tests/test_well/test_well.py | 45 ++++++++++++++++--------------- 4 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/xtgeo/well/_well_io.py b/src/xtgeo/well/_well_io.py index ddf333c75..ad5de5daf 100644 --- a/src/xtgeo/well/_well_io.py +++ b/src/xtgeo/well/_well_io.py @@ -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" diff --git a/src/xtgeo/well/well1.py b/src/xtgeo/well/well1.py index 1cbb105b5..fe16d8a95 100644 --- a/src/xtgeo/well/well1.py +++ b/src/xtgeo/well/well1.py @@ -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 # ================================================================================== @@ -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): diff --git a/src/xtgeo/xyz_common/_xyz_data.py b/src/xtgeo/xyz_common/_xyz_data.py index 293572c04..2061ab156 100644 --- a/src/xtgeo/xyz_common/_xyz_data.py +++ b/src/xtgeo/xyz_common/_xyz_data.py @@ -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 @@ -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): @@ -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 @@ -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 " @@ -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" ) @@ -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) diff --git a/tests/test_well/test_well.py b/tests/test_well/test_well.py index 69ec0c205..58ff74f32 100644 --- a/tests/test_well/test_well.py +++ b/tests/test_well/test_well.py @@ -1,3 +1,4 @@ +import sys from os.path import join import numpy as np @@ -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"), ], ) @@ -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): @@ -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" @@ -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):