diff --git a/nonos/api/analysis.py b/nonos/api/analysis.py index 70f77812..044d8ed3 100644 --- a/nonos/api/analysis.py +++ b/nonos/api/analysis.py @@ -448,7 +448,7 @@ def __init__( *, inifile: str = "", code: str = "", - directory="", + directory: Optional[str] = None, rotate_by: Optional[float] = None, rotate_with: Optional[str] = None, rotate_grid: int = -1, # deprecated @@ -462,6 +462,8 @@ def __init__( self.inifile = inifile self.code = code + if directory is None: + directory = os.getcwd() self.directory = directory self._rotate_by = _parse_rotation_angle( rotate_by=rotate_by, @@ -1533,9 +1535,21 @@ def __init__( inifile: str = "", code: str = "", geometry: str = "unknown", - directory: str = "", + directory: Optional[str] = None, fluid: Optional[str] = None, ) -> None: + if isinstance(input_dataset, str): + directory_from_input = os.path.dirname(input_dataset) + if directory is None: + directory = directory_from_input + elif os.path.abspath(directory_from_input) != os.path.abspath(directory): + raise ValueError( + f"directory value {directory!r} does not match " + f"directory name from input_dataset ({directory_from_input!r})" + ) + del directory_from_input + input_dataset = os.path.basename(input_dataset) + self.params = Parameters(inifile=inifile, code=code, directory=directory) self._read = self.params.loadSimuFile( input_dataset, geometry=geometry, cell="edges", fluid=fluid diff --git a/nonos/api/from_simulation.py b/nonos/api/from_simulation.py index 4a3bfd8f..e9043751 100644 --- a/nonos/api/from_simulation.py +++ b/nonos/api/from_simulation.py @@ -43,7 +43,14 @@ def __init__( code: Optional[str] = None, directory: Optional[str] = None, ) -> None: - self.directory = directory or "" + if not directory: + directory = os.getcwd() + elif not os.path.exists(directory): + raise FileNotFoundError(f"No such file or directory {directory}") + elif not os.path.isdir(directory): + raise ValueError(f"{directory} is not a directory") + + self.directory = directory self.paramfile = inifile or "" self.code: Code if code: @@ -311,7 +318,7 @@ def funnel_on_type( elif _code is Code.IDEFIX or _code is Code.PLUTO: if isinstance(input_dataset, str): filename = os.path.join(directory, input_dataset) - if (m := re.search(r"\d+", filename)) is None: + if (m := re.search(r"\d+", input_dataset)) is None: raise ValueError("filename format is not correct") else: on = int(m.group()) @@ -441,7 +448,7 @@ def idfxReadVTK( if V.geometry == "unknown": raise RuntimeError( "Geometry couldn't be determined from data. " - "Try to set the geometry keyword argument explicitely." + "Try to set the geometry keyword argument explicitly." ) slist = s.split() # DIMENSIONS.... diff --git a/tests/test_load_data.py b/tests/test_load_data.py index 557504e9..5873d95c 100644 --- a/tests/test_load_data.py +++ b/tests/test_load_data.py @@ -57,18 +57,28 @@ def test_roundtrip_other_dir(test_data_dir, tmp_path): assert dsnpy.nfields == 1 -def test_api_vtk_by_name(test_data_dir): - os.chdir(test_data_dir / "idefix_spherical_planet3d") - - on = 500 - - ds = GasDataSet(f"data.{on:04d}.vtk") - assert ds.on == on +@pytest.mark.parametrize( + "from_abs_path", + [ + pytest.param(True, id="from absolute path"), + pytest.param(False, id="from relative path"), + ], +) +def test_api_vtk_by_name(test_data_dir, from_abs_path): + data_dir = test_data_dir / "idefix_spherical_planet3d" + if from_abs_path: + input_ = str((data_dir / "data.0500.vtk").absolute()) + else: + os.chdir(data_dir) + input_ = "data.0500.vtk" + + ds = GasDataSet(input_) + assert ds.on == 500 with pytest.raises( - FileNotFoundError, match="In idfxReadVTK: datawrong.0500.vtk not found." + FileNotFoundError, match=r"In idfxReadVTK: .*datawrong\.0500\.vtk not found\." ): - GasDataSet(f"datawrong.{on:04d}.vtk") + GasDataSet(input_.replace("data.0500", "datawrong.0500")) def test_api_vtk_by_name_fargo(test_data_dir):