diff --git a/src/fmu/sumo/sim2sumo/sim2sumo.py b/src/fmu/sumo/sim2sumo/sim2sumo.py index eda04852..0424d0dd 100644 --- a/src/fmu/sumo/sim2sumo/sim2sumo.py +++ b/src/fmu/sumo/sim2sumo/sim2sumo.py @@ -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 @@ -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 diff --git a/tests/test_functions.py b/tests/test_functions.py index 68dbdaf5..7f510b74 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -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" @@ -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 @@ -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))