Skip to content

Commit

Permalink
CLN: Clean up file opening in find_gridprops
Browse files Browse the repository at this point in the history
eivindjahren committed Nov 17, 2021
1 parent 4dda285 commit 37515d6
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/xtgeo/grid3d/_gridprop_import_eclrun.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
import functools
import itertools
import operator
import pathlib
import warnings
from typing import Dict, List, Union

@@ -558,7 +559,7 @@ def find_gridprops_from_restart_file(
List of GridProperty parameters matching the names and dates.
"""
close = False
try:
if isinstance(restart_filelike, (pathlib.Path, str)):
if fformat == eclio.Format.UNFORMATTED:
filehandle = open(restart_filelike, "rb")
close = True
@@ -567,20 +568,23 @@ def find_gridprops_from_restart_file(
close = True
else:
raise ValueError(f"Unsupported restart file format {fformat}")
except TypeError:
else:
# If restart_filelike is not a filename/path we assume
# its a stream
filehandle = restart_filelike
close = False
generator = section_generator(filter_lgr(eclio.lazy_read(filehandle)))
read_properties = find_gridprops_from_restart_file_sections(
generator,
names,
dates,
grid,
fracture,
)

if close:
filehandle.close()
try:
generator = section_generator(filter_lgr(eclio.lazy_read(filehandle)))
read_properties = find_gridprops_from_restart_file_sections(
generator,
names,
dates,
grid,
fracture,
)
finally:
if close:
filehandle.close()
return read_properties


0 comments on commit 37515d6

Please sign in to comment.