diff --git a/res2df/common.py b/res2df/common.py index 164cbcb86..dffd79460 100644 --- a/res2df/common.py +++ b/res2df/common.py @@ -94,7 +94,7 @@ .splitlines() ) ] -ECLMONTH2NUM = { +MONTH2NUM = { "JAN": 1, "FEB": 2, "MAR": 3, @@ -109,7 +109,7 @@ "NOV": 11, "DEC": 12, } -NUM2ECLMONTH = {num: month for month, num in ECLMONTH2NUM.items()} +NUM2MONTH = {num: month for month, num in MONTH2NUM.items()} logger: logging.Logger = logging.getLogger(__name__) @@ -165,14 +165,12 @@ def write_inc_stdout_file(string: str, outputfilename: str) -> None: print(f"Wrote to {outputfilename}") -def parse_ecl_month(eclmonth: str) -> int: - """Translate Eclipse month strings to integer months""" - return ECLMONTH2NUM[eclmonth] +def parse_month(rdmonth: str) -> int: + """Translate resdata month strings to integer months""" + return MONTH2NUM[rdmonth] -def datetime_to_eclipsedate( - timestamp: Union[str, datetime.datetime, datetime.date] -) -> str: +def datetime_to_ecldate(timestamp: Union[str, datetime.datetime, datetime.date]) -> str: """Convert a Python timestamp or date to the Eclipse DATE format""" if isinstance(timestamp, str): if list(map(len, timestamp.split(" ")[0].split("-"))) != [4, 2, 2]: @@ -181,13 +179,13 @@ def datetime_to_eclipsedate( timestamp = dateutil.parser.parse(timestamp) # noqa (py36 flake8 bug) if not isinstance(timestamp, (datetime.datetime, datetime.date)): raise TypeError("Require string or datetime") - string = f"{timestamp.day} '{NUM2ECLMONTH[timestamp.month]}' {timestamp.year}" + string = f"{timestamp.day} '{NUM2MONTH[timestamp.month]}' {timestamp.year}" if isinstance(timestamp, datetime.datetime): string += " " + timestamp.strftime("%H:%M:%S") return string.replace("00:00:00", "").strip() -def ecl_keyworddata_to_df( +def keyworddata_to_df( deck, keyword: str, renamer: Optional[Dict[str, Union[str, List[str]]]] = None, @@ -353,7 +351,7 @@ def parse_opmio_date_rec(record: "opm.io.DeckRecord") -> datetime.date: day = record[0].get_int(0) month = record[1].get_str(0) year = record[2].get_int(0) - return datetime.date(year=year, month=parse_ecl_month(month), day=day) + return datetime.date(year=year, month=parse_month(month), day=day) def parse_opmio_tstep_rec(record: "opm.io.DeckRecord") -> List[Union[float, int]]: @@ -521,7 +519,7 @@ def df2res( This function hands over the actual text generation pr. keyword to functions named df2res_ in the calling module. - These functions may again use generic_ecltable() from this module + These functions may again use generic_deck_table() from this module for the actual string construction. Args: @@ -539,7 +537,7 @@ def df2res( to file. Returns: - string that can be used as an include file for Eclipse. + string that can be used as an include file for resdata. """ from_module = inspect.stack()[1] calling_module = inspect.getmodule(from_module[0]) @@ -624,15 +622,15 @@ def df2res( return string -def generic_ecltable( +def generic_deck_table( dframe: pd.DataFrame, keyword: str, comment: Optional[str] = None, renamer: Optional[Dict[str, str]] = None, drop_trailing_columns: bool = True, ) -> str: - """Construct a typical Eclipse table for data following - a keyword. Each row (record in Eclipse terms) ends with a slash. + """Construct a deck table for data following + a keyword. Each row ends with a slash. This function will *not* add a final slash after all rows, as this is keyword dependent. Some keywords require it, some keywords @@ -747,7 +745,7 @@ def generic_ecltable( return string + tablestring + "\n" -def runlength_eclcompress(string: str, sep: str = " ") -> str: +def runlength_compress(string: str, sep: str = " ") -> str: """Compress a string of space-separated elements so that 2 2 2 2 2 3 3 4 diff --git a/res2df/equil.py b/res2df/equil.py index a7f47ce33..05e6f2eb4 100644 --- a/res2df/equil.py +++ b/res2df/equil.py @@ -141,7 +141,7 @@ def rsvd_fromdeck( """ if "EQLDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("EQLDIMS", "NTEQUL", deck, ntequl) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "RSVD", renamer=RENAMERS["RSVD"], recordcountername="EQLNUM" ) @@ -158,7 +158,7 @@ def rvvd_fromdeck( """ if "EQLDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("EQLDIMS", "NTEQUL", deck, ntequl) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "RVVD", renamer=RENAMERS["RVVD"], recordcountername="EQLNUM" ) @@ -175,7 +175,7 @@ def pbvd_fromdeck( """ if "EQLDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("EQLDIMS", "NTEQUL", deck, ntequl) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "PBVD", renamer=RENAMERS["PBVD"], recordcountername="EQLNUM" ) @@ -192,7 +192,7 @@ def pdvd_fromdeck( """ if "EQLDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("EQLDIMS", "NTEQUL", deck, ntequl) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "PDVD", renamer=RENAMERS["PDVD"], recordcountername="EQLNUM" ) @@ -264,7 +264,7 @@ def equil_fromdeck( raise ValueError(f"Could not determine phase configuration, got '{phases}'") columnrenamer = RENAMERS[phases_from_deck(deck)] - dataframe = common.ecl_keyworddata_to_df( + dataframe = common.keyworddata_to_df( deck, "EQUIL", renamer=columnrenamer, recordcountername="EQLNUM" ) @@ -418,7 +418,7 @@ def df2res_equil(dframe: pd.DataFrame, comment: Optional[str] = None) -> str: phases = phases_from_columns(subset.columns) - return common.generic_ecltable( + return common.generic_deck_table( subset, "EQUIL", renamer=RENAMERS[phases], # type: ignore diff --git a/res2df/fipreports.py b/res2df/fipreports.py index 493a3073d..7274a05f3 100644 --- a/res2df/fipreports.py +++ b/res2df/fipreports.py @@ -11,7 +11,7 @@ import pandas as pd from res2df import ResdataFiles, getLogger_res2csv -from res2df.common import parse_ecl_month, write_dframe_stdout_file +from res2df.common import parse_month, write_dframe_stdout_file logger = logging.getLogger(__name__) @@ -152,7 +152,7 @@ def df(prtfile: Union[str, ResdataFiles], fipname: str = "FIPNUM") -> pd.DataFra if matcheddate is not None: newdate = datetime.date( year=int(matcheddate.group(3)), - month=parse_ecl_month(matcheddate.group(2).upper()), + month=parse_month(matcheddate.group(2).upper()), day=int(matcheddate.group(1)), ) if newdate != date: diff --git a/res2df/grid.py b/res2df/grid.py index 5f72aa7f3..e9e87d560 100644 --- a/res2df/grid.py +++ b/res2df/grid.py @@ -737,7 +737,7 @@ def df2res( ) logger.warning("Data will be dumped, but may error in simulator") strvector = " ".join([str(x) for x in vector]) - strvector = common.runlength_eclcompress(strvector) + strvector = common.runlength_compress(strvector) string += keyword + "\n" indent = " " * 5 diff --git a/res2df/pvt.py b/res2df/pvt.py index c10fc6355..904299449 100644 --- a/res2df/pvt.py +++ b/res2df/pvt.py @@ -81,7 +81,7 @@ def pvtw_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "PVTW", renamer=RENAMERS["PVTW"], recordcountername="PVTNUM" ) @@ -98,7 +98,7 @@ def density_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "DENSITY", renamer=RENAMERS["DENSITY"], recordcountername="PVTNUM" ) @@ -115,7 +115,7 @@ def rock_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - return common.ecl_keyworddata_to_df( + return common.keyworddata_to_df( deck, "ROCK", renamer=RENAMERS["ROCK"], recordcountername="PVTNUM" ) @@ -132,7 +132,7 @@ def pvto_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - pvto_df = common.ecl_keyworddata_to_df( + pvto_df = common.keyworddata_to_df( deck, "PVTO", renamer=RENAMERS["PVTO"], emptyrecordcountername="PVTNUM" ) return pvto_df @@ -150,7 +150,7 @@ def pvdo_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - pvdg_df = common.ecl_keyworddata_to_df( + pvdg_df = common.keyworddata_to_df( deck, "PVDO", renamer=RENAMERS["PVDO"], recordcountername="PVTNUM" ) return pvdg_df @@ -168,7 +168,7 @@ def pvdg_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - pvdg_df = common.ecl_keyworddata_to_df( + pvdg_df = common.keyworddata_to_df( deck, "PVDG", renamer=RENAMERS["PVDG"], recordcountername="PVTNUM" ) return pvdg_df @@ -186,7 +186,7 @@ def pvtg_fromdeck( """ if "TABDIMS" not in deck: deck = inferdims.inject_xxxdims_ntxxx("TABDIMS", "NTPVT", deck, ntpvt) - pvtg_df = common.ecl_keyworddata_to_df( + pvtg_df = common.keyworddata_to_df( deck, "PVTG", renamer=RENAMERS["PVTG"], emptyrecordcountername="PVTNUM" ) return pvtg_df diff --git a/res2df/satfunc.py b/res2df/satfunc.py index 849661a23..5fbbefb2c 100644 --- a/res2df/satfunc.py +++ b/res2df/satfunc.py @@ -104,7 +104,7 @@ def df( for keyword in wanted_keywords: frames.append( interpolate_defaults( - common.ecl_keyworddata_to_df( + common.keyworddata_to_df( deck, keyword, renamer=RENAMERS[keyword], recordcountername="SATNUM" ).assign(KEYWORD=keyword) ) diff --git a/tests/test_common.py b/tests/test_common.py index f09d0f681..65d6e63d5 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -250,24 +250,24 @@ def test_df2res(): ), ], ) -def test_datetime_to_eclipsedate(somedate, expected): +def test_datetime_to_ecldate(somedate, expected): """Test conversion of datetime to Eclipse date or datetime syntax""" - assert common.datetime_to_eclipsedate(somedate) == expected + assert common.datetime_to_ecldate(somedate) == expected def test_eclcompress(): """Test that we can compress string using Eclipse style run-length encoding""" - assert common.runlength_eclcompress("") == "" - assert common.runlength_eclcompress(" ") == "" - assert common.runlength_eclcompress("1 2") == "1 2" - assert common.runlength_eclcompress("1 2", sep=" ") == "1 2" - assert common.runlength_eclcompress("1 2", sep=" ") == "1 2" - assert common.runlength_eclcompress("1") == "1" - assert common.runlength_eclcompress("1 1") == "2*1" - assert common.runlength_eclcompress("1 1 1") == "3*1" - assert common.runlength_eclcompress("1 1 1") == "3*1" - assert common.runlength_eclcompress("1 \n 1 1 2") == "3*1 2" + assert common.runlength_compress("") == "" + assert common.runlength_compress(" ") == "" + assert common.runlength_compress("1 2") == "1 2" + assert common.runlength_compress("1 2", sep=" ") == "1 2" + assert common.runlength_compress("1 2", sep=" ") == "1 2" + assert common.runlength_compress("1") == "1" + assert common.runlength_compress("1 1") == "2*1" + assert common.runlength_compress("1 1 1") == "3*1" + assert common.runlength_compress("1 1 1") == "3*1" + assert common.runlength_compress("1 \n 1 1 2") == "3*1 2" @pytest.mark.parametrize( @@ -446,10 +446,10 @@ def test_well_matching_template(template, wells, output): ), ], ) -def test_generic_ecltable( +def test_generic_deck_table( dframe, keyword, comment, renamer, drop_trailing_columns, expected ): - stringtable = common.generic_ecltable( + stringtable = common.generic_deck_table( dframe, keyword, comment=comment,