diff --git a/fluidsim/base/output/base.py b/fluidsim/base/output/base.py index bc86a540b0eb4f66c677f6cd0c1616fcb846832e_Zmx1aWRzaW0vYmFzZS9vdXRwdXQvYmFzZS5weQ==..f5c62706429b9d8cfd631ae786f03ffa8955b094_Zmx1aWRzaW0vYmFzZS9vdXRwdXQvYmFzZS5weQ== 100644 --- a/fluidsim/base/output/base.py +++ b/fluidsim/base/output/base.py @@ -152,7 +152,7 @@ if not params.NEW_DIR_RESULTS: try: - self.path_run = params.path_run + self.path_run = str(params.path_run) except AttributeError: params.NEW_DIR_RESULTS = True print( diff --git a/fluidsim/base/params.py b/fluidsim/base/params.py index bc86a540b0eb4f66c677f6cd0c1616fcb846832e_Zmx1aWRzaW0vYmFzZS9wYXJhbXMucHk=..f5c62706429b9d8cfd631ae786f03ffa8955b094_Zmx1aWRzaW0vYmFzZS9wYXJhbXMucHk= 100644 --- a/fluidsim/base/params.py +++ b/fluidsim/base/params.py @@ -11,7 +11,5 @@ """ -from __future__ import division, print_function - import os from glob import glob @@ -16,7 +14,6 @@ import os from glob import glob -from importlib import import_module -from builtins import map +from pathlib import Path import h5py @@ -217,7 +214,9 @@ if path_dir is None: path_dir = os.getcwd() - path_info_solver = os.path.join(path_dir, "info_solver.xml") - if os.path.exists(path_info_solver): - return Parameters(path_file=path_info_solver) + if not isinstance(path_dir, Path): + path_dir = Path(path_dir) + + if not path_dir.is_dir(): + raise ValueError(str(path_dir) + " is not a directory") @@ -223,5 +222,7 @@ - paths = glob(os.path.join(path_dir, "state_*")) - if paths: - path = sorted(paths)[0] + path_info_solver = path_dir / "info_solver.xml" + if path_info_solver.exists(): + return Parameters(path_file=str(path_info_solver)) + + paths = path_dir.glob("state_*") @@ -227,6 +228,6 @@ - if len(path) > 100: - str_path = "[...]" + path[-100:] - else: - str_path = path + if not paths: + raise ValueError("No result files in dir " + str(path_dir)) + + path = str(sorted(paths)[0]) @@ -232,5 +233,6 @@ - print("load params from file\n" + str_path) - with h5py.File(path) as h5file: - return Parameters(hdf5_object=h5file["/info_simul/solver"]) + if len(path) > 100: + str_path = "[...]" + path[-100:] + else: + str_path = path @@ -236,6 +238,7 @@ - else: - return ValueError + print("load params from file\n" + str_path) + with h5py.File(path) as h5file: + return Parameters(hdf5_object=h5file["/info_simul/solver"]) if __name__ == "__main__": diff --git a/fluidsim/util/util.py b/fluidsim/util/util.py index bc86a540b0eb4f66c677f6cd0c1616fcb846832e_Zmx1aWRzaW0vdXRpbC91dGlsLnB5..f5c62706429b9d8cfd631ae786f03ffa8955b094_Zmx1aWRzaW0vdXRpbC91dGlsLnB5 100644 --- a/fluidsim/util/util.py +++ b/fluidsim/util/util.py @@ -144,7 +144,7 @@ def pathdir_from_namedir(name_dir=None): - """Return the path if a result directory.""" + """Return the path of a result directory.""" if name_dir is None: return _os.getcwd() @@ -148,9 +148,21 @@ if name_dir is None: return _os.getcwd() - if name_dir[0] != "/" and name_dir[0] != "~": - name_dir = str(path_dir_results / name_dir) - return _os.path.expanduser(name_dir) + if not isinstance(name_dir, Path): + name_dir = Path(name_dir) + + name_dir = name_dir.expanduser() + + if name_dir.is_dir(): + return name_dir.absolute() + + if not name_dir.is_absolute(): + name_dir = path_dir_results / name_dir + + if not name_dir.is_dir(): + raise ValueError(str(name_dir) + " is not a directory") + + return name_dir class ModulesSolvers(dict): @@ -165,6 +177,9 @@ """Return the file name whose time is the closest to the given time. """ - path_files = _glob.glob(path_dir + "/state_phys_t*") + if not isinstance(path_dir, Path): + path_dir = Path(path_dir) + + path_files = tuple(path_dir.glob("state_phys_t*")) nb_files = len(path_files) if nb_files == 0 and mpi.rank == 0: @@ -169,4 +184,4 @@ nb_files = len(path_files) if nb_files == 0 and mpi.rank == 0: - raise ValueError("No state file in the dir\n" + path_dir) + raise ValueError("No state file in the dir\n" + str(path_dir)) @@ -172,5 +187,5 @@ - name_files = [_os.path.split(path)[-1] for path in path_files] + name_files = [path.name for path in path_files] if "state_phys_t=" in name_files[0]: ind_start_time = len("state_phys_t=") else: @@ -182,7 +197,7 @@ if t_approx is None: t_approx = times.max() i_file = abs(times - t_approx).argmin() - name_file = _os.path.split(path_files[i_file])[-1] + name_file = path_files[i_file].name return name_file @@ -348,7 +363,7 @@ def modif_resolution_all_dir(t_approx=None, coef_modif_resol=2, dir_base=None): """Save files with a modified resolution.""" path_base = pathdir_from_namedir(dir_base) - list_dir_results = _glob.glob(path_base + "/SE2D*") + list_dir_results = list(path_base.glob("SE2D*")) for path_dir in list_dir_results: modif_resolution_from_dir( name_dir=path_dir, @@ -379,7 +394,7 @@ print(sim2.params.path_run) - sim2.output.path_run = path_dir + "/State_phys_{0}x{1}".format( + sim2.output.path_run = str(path_dir) + "/State_phys_{0}x{1}".format( sim2.params.oper.nx, sim2.params.oper.ny ) print("Save file in directory\n" + sim2.output.path_run) @@ -442,7 +457,7 @@ def __init__(self, arg): if isinstance(arg, str): dir_base = pathdir_from_namedir(arg) - paths_results = _glob.glob(dir_base + "/SE2D_*") + paths_results = tuple(dir_base.glob("SE2D_*")) if len(paths_results) == 0: print("No result directory in the directory\n" + dir_base) else: