Skip to content

Commit

Permalink
bugfix:find_datafiles (#40)
Browse files Browse the repository at this point in the history
* Refactor function

Fix partially failure when running Drogon
  • Loading branch information
daniel-sol authored Mar 4, 2024
1 parent b8f4de0 commit 3205e84
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
44 changes: 18 additions & 26 deletions src/fmu/sumo/sim2sumo/sim2sumo.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ def find_datatypes(datatype, simconfig):
return submods


def is_datafile(results: PosixPath) -> bool:
"""Filter results based on suffix
Args:
results (PosixPath): path to file
Returns:
bool: true if correct suffix
"""
valid = [".afi", ".DATA", ".in"]
return results.suffix in valid


def find_datafiles(seedpoint, simconfig):
"""Find all relevant paths that can be datafiles
Expand All @@ -253,40 +266,19 @@ def find_datafiles(seedpoint, simconfig):

logger = logging.getLogger(__file__ + ".find_datafiles")
datafiles = []

seedpoint = simconfig.get("datafile", seedpoint)
if seedpoint is None:
seedpoint = simconfig.get(
"datafile",
[
"eclipse/model/",
"ix/model/",
"opm/model/",
"pflotran/model",
],
)
datafiles = list(filter(is_datafile, Path().cwd().glob("*/model/*.*")))

if isinstance(seedpoint, (str, PosixPath)):
elif isinstance(seedpoint, (str, PosixPath)):
logger.debug("Using this string %s to find datafile(s)", seedpoint)
seedpoint_posix = Path(seedpoint)

if seedpoint_posix.is_dir():
logger.debug("%s is directory, globbing for datafiles", seedpoint)
glob_list = (
list(seedpoint_posix.glob("*.DATA"))
+ list(seedpoint_posix.glob("*.afi"))
+ list(seedpoint_posix.glob("*.in"))
)

logger.debug("Results are %s", glob_list)
datafiles.extend(find_datafiles(glob_list, simconfig))

else:
if seedpoint_posix.is_file():
logger.debug("%s is file path, will just use this one", seedpoint)
datafiles.append(seedpoint)
else:
logger.debug("%s is list", seedpoint)
for item in seedpoint:
datafiles.extend(find_datafiles(item, simconfig))
datafiles.extend(seedpoint)
logger.debug("Datafile(s) to use %s", datafiles)
return datafiles

Expand Down
18 changes: 10 additions & 8 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

REEK_ROOT = Path(__file__).parent / "data/reek"
REAL_PATH = "realization-0/iter-0/"
REEK_REAL = REEK_ROOT / REAL_PATH
REEK_REAL0 = REEK_ROOT / "realization-0/iter-0/"
REEK_REAL1 = REEK_ROOT / "realization-1/iter-0/"
REEK_BASE = "2_R001_REEK"
REEK_ECL_MODEL = REEK_REAL / "eclipse/model/"
REEK_ECL_MODEL = REEK_REAL0 / "eclipse/model/"
REEK_DATA_FILE = REEK_ECL_MODEL / f"{REEK_BASE}-0.DATA"
CONFIG_OUT_PATH = REEK_REAL / "fmuconfig/output/"
CONFIG_OUT_PATH = REEK_REAL0 / "fmuconfig/output/"
CONFIG_PATH = CONFIG_OUT_PATH / "global_variables.yml"


Expand All @@ -38,13 +39,14 @@ def test_fix_suffix():
assert corrected_path.endswith(".DATA"), f"Didn't correct {corrected_path}"


def test_find_datafiles_reek():
os.chdir(REEK_REAL)
@pytest.mark.parametrize("real,nrdfiles", [(REEK_REAL0, 5), (REEK_REAL1, 1)])
def test_find_datafiles_reek(real, nrdfiles):
os.chdir(real)
datafiles = sim2sumo.find_datafiles(None, {})
expected_tools = ["eclipse", "opm", "ix", "pflotran"]
assert (
len(datafiles) == 5
), f"Haven't found all 5 files only {len(datafiles)} ({datafiles})"
len(datafiles) == nrdfiles
), f"Haven't found all {nrdfiles} files only {len(datafiles)} ({datafiles})"
for datafile in datafiles:
found_path = datafile
parent = found_path.parent.parent.name
Expand Down Expand Up @@ -200,7 +202,7 @@ def _assert_right_len(checks, key, to_messure, name):
@pytest.mark.parametrize("config_path", CONFIG_OUT_PATH.glob("*.yml"))
def test_read_config(config_path):
"""Test reading of config file via read_config function"""
os.chdir(REEK_REAL)
os.chdir(REEK_REAL0)
LOGGER.info(config_path)
config = sim2sumo.yaml_load(config_path)
assert isinstance(config, (dict, bool))
Expand Down

0 comments on commit 3205e84

Please sign in to comment.