Skip to content

Commit

Permalink
Rename in common.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngve S. Kristiansen committed Nov 14, 2023
1 parent 0606b7c commit 1888da7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 48 deletions.
32 changes: 15 additions & 17 deletions res2df/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
.splitlines()
)
]
ECLMONTH2NUM = {
MONTH2NUM = {
"JAN": 1,
"FEB": 2,
"MAR": 3,
Expand All @@ -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__)

Expand Down Expand Up @@ -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]:
Expand All @@ -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,
Expand Down Expand Up @@ -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]]:
Expand Down Expand Up @@ -521,7 +519,7 @@ def df2res(
This function hands over the actual text generation pr. keyword
to functions named df2res_<keywordname> 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:
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions res2df/equil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
)

Expand All @@ -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"
)

Expand All @@ -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"
)

Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions res2df/fipreports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion res2df/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions res2df/pvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
)

Expand All @@ -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"
)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion res2df/satfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
28 changes: 14 additions & 14 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1888da7

Please sign in to comment.