Skip to content

Commit

Permalink
New fipname option
Browse files Browse the repository at this point in the history
  • Loading branch information
lilbe66 committed Jun 21, 2024
1 parent 9f70d93 commit cd78a43
Show file tree
Hide file tree
Showing 6 changed files with 7,049 additions and 22 deletions.
13 changes: 10 additions & 3 deletions docs/scripts/prtvol2csv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ The script will read numbers from the line with ``CURRENTLY IN PLACE`` in Eclips
====================================================================================================================================
Additionally, if the Eclipse DATA file includes::
Additionally, if the Eclipse DATA file includes

.. code-block:: none
RPTSOL
FIP=2 'FIPRESV' /
the PRT-file will also contain a table with pore volumes pr. phase and pr.
FIPNUM, which will be added to the exported table.
the PRT-file will also contain a table with pore volumes pr. phase and pr. FIPNUM,
which will be added to the exported table. FIPRESV will include reservoir volumes (RM3).

.. code-block:: none
Expand All @@ -56,6 +58,11 @@ FIPNUM, which will be added to the exported table.
: 2 : 79481140.: 0.: 79481140.: 0.: 0.:
===========================================================================================
If `FIP=3` in RPTSOL, the PRT file will also contain report of in-place volumes for any additional
FIP-vectors. Default is to export volumes per FIPNUM, but any FIP-vector can be specified
using the ``fipname`` option. However, the column name will always be FIPNUM in the csv-file,
as required by ``webviz-subsurface`` plugin "VolumetricAnalysis". An additional column with
the actual FIPNAME is included for information.


Region and zone support
Expand Down
60 changes: 46 additions & 14 deletions src/subscript/prtvol2csv/prtvol2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from subscript import __version__, getLogger

DESCRIPTION = """
Extract reservoir volumes pr FIPNUM from Eclipse PRT files and dump to CSV.
Extract in-place volumes per FIPNUM, or any FIP vector specified, from
Eclipse PRT files and dump to CSV.
If a yaml file is specified through options, it is possible to add columns
with region and zone information to each FIPNUM. The YAML file must contain
Expand All @@ -24,6 +25,7 @@

CATEGORY = "utility.eclipse"


EXAMPLES = """
.. code-block:: console
Expand Down Expand Up @@ -64,6 +66,12 @@ def get_parser() -> argparse.ArgumentParser:
help="CSV filename to write, including path. Directory must exist.",
default="simulator_volume_fipnum.csv", # FMU standard
)
parser.add_argument(
"--fipname",
type=str,
help="Specify FIP-name, for an additional FIP vector.",
default="FIPNUM",
)
parser.add_argument(
"--yaml",
"--regions", # Deprecated option name
Expand Down Expand Up @@ -194,13 +202,13 @@ def currently_in_place_from_prt(
["DATATYPE", "TO_REGION", "FIPNAME", "DATE"], axis="columns", inplace=True
)
inplace_df.set_index("REGION", inplace=True)
inplace_df.index.name = "FIPNUM"
inplace_df.index.name = fipname # Use "FIPNUM" if not handled by Webviz

logger.info("Extracted CURRENTLY IN PLACE from %s at date %s", prt_file, date_str)
return inplace_df


def reservoir_volumes_from_prt(prt_file: str) -> pd.DataFrame:
def reservoir_volumes_from_prt(prt_file: str, fipname: str = "FIPNUM") -> pd.DataFrame:
"""Extracts numbers from the table "RESERVOIR VOLUMES" in an Eclipse PRT
file, example table is::
Expand Down Expand Up @@ -233,9 +241,16 @@ def reservoir_volumes_from_prt(prt_file: str) -> pd.DataFrame:
table_found = (
False # State determining if current line is in our interesting table or not.
)
# The Reservoir Volume table is not tagged with the "FIPNAME", but will appear
# after the in-place volume table (see the "BALANCE" report) in the PRT file.
fipname_found = fipname == "FIPNUM" # found the corrent fipname, FIPNUM OK

with Path(prt_file).open(encoding="utf8") as f_handle:
for line in f_handle:
if start_matcher.search(line) is not None:
if line.startswith(" " + "BAL" + fipname[3:6]):
fipname_found = True
continue
if fipname_found and start_matcher.search(line) is not None:
table_found = True
continue
if table_found and line.strip().startswith("======================="):
Expand All @@ -253,7 +268,7 @@ def reservoir_volumes_from_prt(prt_file: str) -> pd.DataFrame:
continue
records.append(
{
"FIPNUM": int(line_split[0]),
fipname: int(line_split[0]),
"PORV_TOTAL": float(line_split[1]),
"HCPV_OIL": float(line_split[2]),
"WATPV_TOTAL": float(line_split[3]),
Expand All @@ -264,10 +279,12 @@ def reservoir_volumes_from_prt(prt_file: str) -> pd.DataFrame:

if not records:
logger.warning("No RESERVOIR VOLUMES table found in PRT file %s", prt_file)
logger.warning("Include RPTSOL <newline> FIP=2 'FIPRESV' in Eclipse DATA file")
logger.warning(
"Include RPTSOL <newline> FIP=2 (or 3) 'FIPRESV' in Eclipse DATA file"
)
return pd.DataFrame()

return pd.DataFrame(records).set_index("FIPNUM")
return pd.DataFrame(records).set_index(fipname)


def main() -> None:
Expand All @@ -292,22 +309,27 @@ def main() -> None:
logger.error("PRT-file %s does not exist", prt_file)
return

simvolumes_df = currently_in_place_from_prt(prt_file, "FIPNUM")
simvolumes_df = currently_in_place_from_prt(prt_file, args.fipname)
simvolumes_df.to_csv(Path(tablesdir) / args.outputfilename)
logger.info(
"Written CURRENTLY_IN_PLACE data to %s",
str(Path(tablesdir) / args.outputfilename),
)

resvolumes_df = reservoir_volumes_from_prt(prt_file)
resvolumes_df = reservoir_volumes_from_prt(prt_file, args.fipname)

fipmapper: Optional[FipMapper]
if args.yaml:
fipmapper = FipMapper(yamlfile=args.yaml, skipstring="Totals")
if args.fipname != "FIPNUM":
logger.error("Cannot use yaml file if fipname is different from FIPNUM")
return
else:
fipmapper = None

volumes = prtvol2df(simvolumes_df, resvolumes_df, fipmapper=fipmapper)
volumes = prtvol2df(
simvolumes_df, resvolumes_df, fipmapper=fipmapper, fipname=args.fipname
)

volumes.to_csv(Path(tablesdir) / args.outputfilename)
logger.info("Written CSV file %s", str(Path(tablesdir) / args.outputfilename))
Expand All @@ -317,18 +339,28 @@ def prtvol2df(
simvolumes_df: pd.DataFrame,
resvolumes_df: pd.DataFrame,
fipmapper: Optional[FipMapper] = None,
fipname: str = "FIPNUM",
) -> pd.DataFrame:
"""
Concatenate two dataframes (with common index) horizontally,
and add REGION and ZONE parameter.
and if fipname="FIPNUM", add REGION and ZONE parameter.
"""
# Concatenate dataframes horizontally. Both are/must be indexed by FIPNUM:

# Remove extra empty 'regions' (from the reservoir volume table in .PRT)
# Concatenate dataframes horizontally. Both are/must be indexed by value
# of fipname (FIPNUM):
volumes = (
pd.concat([simvolumes_df, resvolumes_df], axis=1)
pd.concat([simvolumes_df, resvolumes_df[: len(simvolumes_df)]], axis=1)
.apply(pd.to_numeric)
.fillna(value=0.0)
)

# Rename the index to "FIPNUM", as required by webviz-subsurface
volumes.index = volumes.index.rename("FIPNUM")

# Add new column with the actual FIPNAME, for info and traceability
volumes["FIPNAME"] = fipname

if fipmapper is not None:
if fipmapper.has_fip2region:
volumes["REGION"] = [
Expand All @@ -341,7 +373,7 @@ def prtvol2df(
for fipnum in volumes.index
]
if any(volumes.index < 1):
logger.warning("FIPNUM values should be 1 or larger")
logger.warning("%s values should be 1 or larger", fipname)
return volumes


Expand Down
Loading

0 comments on commit cd78a43

Please sign in to comment.