Skip to content

Commit

Permalink
Support for WELOPEN and COMPDAT entries at the same date (#189)
Browse files Browse the repository at this point in the history
* Fix issues with WELOPEN and COMPDAT entries at the same date

* Remove blank line

* Drop duplicates

* Add tests

* KEYWORD_ID --> KEYWORD_IDX

* Black on tests

* Added requested tests

* Added two corner cases, fixed .all() in test
  • Loading branch information
wouterjdb authored Oct 29, 2020
1 parent 71d6fa9 commit 3ba94d2
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 6 deletions.
15 changes: 10 additions & 5 deletions ecl2df/compdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def deck2dfs(deck, start_date=None, unroll=True):
welopenrecords = []
welsegsrecords = []
date = start_date # DATE column will always be there, but can contain NaN/None
for kword in deck:
for idx, kword in enumerate(deck):
if kword.name == "DATES" or kword.name == "START":
for rec in kword:
date = parse_opmio_date_rec(rec)
Expand All @@ -93,6 +93,7 @@ def deck2dfs(deck, start_date=None, unroll=True):
rec, "COMPDAT", renamer=COMPDAT_RENAMER
)
rec_data["DATE"] = date
rec_data["KEYWORD_IDX"] = idx
compdatrecords.append(rec_data)
elif kword.name == "COMPSEGS":
wellname = parse_opmio_deckrecord(
Expand All @@ -110,6 +111,7 @@ def deck2dfs(deck, start_date=None, unroll=True):
for rec in kword:
rec_data = parse_opmio_deckrecord(rec, "WELOPEN")
rec_data["DATE"] = date
rec_data["KEYWORD_IDX"] = idx
if rec_data["STATUS"] not in ["OPEN", "SHUT", "STOP", "AUTO"]:
rec_data["STATUS"] = "SHUT"
logger.warning(
Expand Down Expand Up @@ -155,6 +157,9 @@ def deck2dfs(deck, start_date=None, unroll=True):
if unroll and not welsegs_df.empty:
welsegs_df = unrolldf(welsegs_df, "SEGMENT1", "SEGMENT2")

if "KEYWORD_IDX" in compdat_df.columns:
compdat_df.drop(["KEYWORD_IDX"], axis=1, inplace=True)

return dict(COMPDAT=compdat_df, COMPSEGS=compsegs_df, WELSEGS=welsegs_df)


Expand Down Expand Up @@ -294,7 +299,7 @@ def applywelopen(compdat_df, welopen_df):
if row["I"] and row["J"] and row["K"]:
previous_state = compdat_df[
(compdat_df["WELL"] == row["WELL"])
& (compdat_df["DATE"] <= row["DATE"])
& (compdat_df["KEYWORD_IDX"] < row["KEYWORD_IDX"])
& (compdat_df["I"] == row["I"])
& (compdat_df["J"] == row["J"])
& (compdat_df["K1"] == row["K"])
Expand All @@ -310,7 +315,7 @@ def applywelopen(compdat_df, welopen_df):
elif not (row["I"] and row["J"] and row["K"]):
previous_state = compdat_df[
(compdat_df["WELL"] == row["WELL"])
& (compdat_df["DATE"] <= row["DATE"])
& (compdat_df["KEYWORD_IDX"] < row["KEYWORD_IDX"])
].drop_duplicates(subset=["I", "J", "K1", "K2"], keep="last")
else:
raise ValueError(
Expand All @@ -333,14 +338,14 @@ def applywelopen(compdat_df, welopen_df):
# well in COMPDAT and WELOPEN are not identical. These translation steps can
# be dropped when unity in the opm-common keyword definitions is reached.
new_state["OP/SH"] = row["STATUS"]

new_state["KEYWORD_IDX"] = row["KEYWORD_IDX"]
new_state["DATE"] = row["DATE"]

compdat_df = compdat_df.append(new_state)

if not compdat_df.empty:
compdat_df = (
compdat_df.sort_values(by=["DATE", "WELL"])
compdat_df.sort_values(by=["KEYWORD_IDX"])
.drop_duplicates(subset=["I", "J", "K1", "K2", "DATE"], keep="last")
.reset_index(drop=True)
)
Expand Down
172 changes: 171 additions & 1 deletion tests/test_welopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,176 @@
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 1 'OPEN' /
/
WELOPEN
'OP1' 'SHUT' /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1"},
"I": {0: 1},
"J": {0: 1},
"K1": {0: 1},
"K2": {0: 1},
"OP/SH": {0: "SHUT"},
"DATE": {0: datetime.date(2001, 5, 1)},
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 1 'OPEN' /
/
DATES
2 MAY 2001 /
/
WELOPEN
'OP1' 'SHUT' /
/
COMPDAT
'OP1' 1 1 1 1 'OPEN' /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1", 1: "OP1"},
"I": {0: 1, 1: 1},
"J": {0: 1, 1: 1},
"K1": {0: 1, 1: 1},
"K2": {0: 1, 1: 1},
"OP/SH": {0: "OPEN", 1: "OPEN"},
"DATE": {0: datetime.date(2001, 5, 1), 1: datetime.date(2001, 5, 2)},
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 2 'OPEN' /
/
WELOPEN
'OP1' 'SHUT' 1 1 1 /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1", 1: "OP1"},
"I": {0: 1, 1: 1},
"J": {0: 1, 1: 1},
"K1": {0: 2, 1: 1},
"K2": {0: 2, 1: 1},
"OP/SH": {0: "OPEN", 1: "SHUT"},
"DATE": {0: datetime.date(2001, 5, 1), 1: datetime.date(2001, 5, 1)},
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 1 'OPEN' /
/
WELOPEN
'OP1' 'SHUT' /
/
WELOPEN
'OP1' 'OPEN' /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1"},
"I": {0: 1},
"J": {0: 1},
"K1": {0: 1},
"K2": {0: 1},
"OP/SH": {0: "OPEN"},
"DATE": {0: datetime.date(2001, 5, 1)},
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 1 'OPEN' /
/
WELOPEN
'OP1' 'OPEN' /
'OP1' 'SHUT' /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1"},
"I": {0: 1},
"J": {0: 1},
"K1": {0: 1},
"K2": {0: 1},
"OP/SH": {0: "SHUT"},
"DATE": {0: datetime.date(2001, 5, 1)},
}
),
),
(
"""
DATES
1 MAY 2001 /
/
COMPDAT
'OP1' 1 1 1 2 'SHUT' /
/
WELOPEN
'OP1' 'OPEN' /
'OP1' 'SHUT' 1 1 1 /
/
""",
pd.DataFrame(
{
"WELL": {0: "OP1", 1: "OP1"},
"I": {0: 1, 1: 1},
"J": {0: 1, 1: 1},
"K1": {0: 2, 1: 1},
"K2": {0: 2, 1: 1},
"OP/SH": {0: "OPEN", 1: "SHUT"},
"DATE": {0: datetime.date(2001, 5, 1), 1: datetime.date(2001, 5, 1)},
}
),
),
]


Expand All @@ -225,4 +395,4 @@ def test_welopen(test_input, expected):
compdf = compdat.deck2dfs(deck)["COMPDAT"]

columns_to_check = ["WELL", "I", "J", "K1", "K2", "OP/SH", "DATE"]
assert all(compdf[columns_to_check] == expected[columns_to_check])
assert (compdf[columns_to_check] == expected[columns_to_check]).all(axis=None)

0 comments on commit 3ba94d2

Please sign in to comment.