From a1e4f775b61c8d201717b8f78e7da9e9b4075b1a Mon Sep 17 00:00:00 2001 From: Noe Brucy Date: Wed, 12 Jul 2023 11:13:03 +0200 Subject: [PATCH] few bug solving --- baseprocessor.py | 4 ++-- snapshotprocessor.py | 26 +++++++++++++------------- utils/convert_to_hdf5.py | 20 ++++++++++++++++++++ utils/runselector.py | 8 +++++--- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/baseprocessor.py b/baseprocessor.py index b0e263d..833bbaa 100644 --- a/baseprocessor.py +++ b/baseprocessor.py @@ -370,7 +370,7 @@ class HDF5Container(BaseProcessor): for base_unit_str in unit: expo = unit[base_unit_str] base_unit = self._get_units(base_unit_str) - new_unit = new_unit * base_unit ** expo + new_unit = new_unit * base_unit**expo return new_unit if (data is not None) and isinstance(data, dict) and list(unit)[0] in data: for key in unit: @@ -435,7 +435,7 @@ class HDF5Container(BaseProcessor): self._save_data(name_full + "/" + key, data[key], "", unit) else: try: - if len(data) == 0: + if data is None or len(data) == 0: return except TypeError: data = np.array([data]) diff --git a/snapshotprocessor.py b/snapshotprocessor.py index 35f3d19..f022403 100644 --- a/snapshotprocessor.py +++ b/snapshotprocessor.py @@ -360,8 +360,11 @@ class SnapshotProcessor(HDF5Container): # Create selector object if selector is None: + dirpath = os.path.dirname(path) + if dirpath == "": + dirpath = "." selector = RunSelector( - os.path.dirname(path), + dirpath, self.run, self.num, self.params.input.nml_filename, @@ -452,9 +455,6 @@ class SnapshotProcessor(HDF5Container): if os.path.exists(part_filename): hydro_file = open(part_filename) part_file = open(f"{path}/output_{self.num:05}/part_file_descriptor.txt") - has_grav = os.path.exists( - f"{path}/output_{self.num:05}/grav_{self.num:05}.out00000" - ) plines = part_file.readlines() part_var = np.unique( list(map(lambda s: s.split(",")[1][1:].split("_")[0], plines[2:])) @@ -463,7 +463,7 @@ class SnapshotProcessor(HDF5Container): part_var = [] has_grav = os.path.exists( - f"{path}/output_{self.num:05}/grav_{self.num:05}.out00000" + f"{path}/output_{self.num:05}/grav_{self.num:05}.out00001" ) def is_available(available_vars, pymsesrc, var): @@ -684,12 +684,13 @@ class SnapshotProcessor(HDF5Container): if len(data[key] > 0): if f"/{group}/{key}" in hdf5 and overwrite: hdf5.remove_node(f"/{group}/{key}") - hdf5.create_array( - f"/{group}", key, data[key], "", createparents=True - ) - unit = self._get_units(self.unit_key[key]) - hdf5.get_node(f"/{group}/{key}").unit = unit - nb_written += 1 + elif f"/{group}/{key}" not in hdf5: + hdf5.create_array( + f"/{group}", key, data[key], "", createparents=True + ) + unit = self._get_units(self.unit_key[key]) + hdf5.get_node(f"/{group}/{key}").unit = unit + nb_written += 1 else: self.logger.warning("Empty key") if "namelist" not in hdf5.root._v_attrs: @@ -747,7 +748,7 @@ class SnapshotProcessor(HDF5Container): self.cells = self.load_data( cells_src, filename, - True, + save, keys=keys, group="cells", ) @@ -1648,7 +1649,6 @@ class SnapshotProcessor(HDF5Container): outfile = self.pspec_filename if overwrite_file or not os.path.exists(self.pspec_filename): pspec.pspec(repo=self.path, iouts=[self.num], outfile=outfile, **kwargs) - return np.array([self.pspec_filename]) def _write_particles(self): """Ensure particles are written in the hdf5 file""" diff --git a/utils/convert_to_hdf5.py b/utils/convert_to_hdf5.py index 4be4903..357cc62 100644 --- a/utils/convert_to_hdf5.py +++ b/utils/convert_to_hdf5.py @@ -1,8 +1,28 @@ # coding: utf-8 +import argparse +import os from snapshotprocessor import SnapshotProcessor def convert_to_hdf5(path, snap_num, path_out=".", filename=None, **kwargs): snap = SnapshotProcessor(path=path, num=snap_num, path_out=path_out, **kwargs) snap.convert_hdf5(filename) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Convert Ramses output into a list of cells" + ) + parser.add_argument( + "snapshots", help="path to the snapshots", nargs="+", default=[] + ) + parser.add_argument("-o", "--out", help="path to the output", default=".") + + args = parser.parse_args() + + for snap in args.snapshots: + path = os.path.dirname(snap) + numstem = os.path.basename(snap) + snap_num = int(numstem.split("_")[1]) + convert_to_hdf5(path, snap_num, args.out) diff --git a/utils/runselector.py b/utils/runselector.py index aa3d9b9..d0c3c03 100644 --- a/utils/runselector.py +++ b/utils/runselector.py @@ -40,7 +40,7 @@ class NamelistRecursive: class RunSelector: def __init__( self, - path_in, + path_in=".", in_runs=None, in_nums="all", nml_filename="run.nml", @@ -93,7 +93,7 @@ class RunSelector: sort_run_by : str, a key from the namelist used to sort the runs (by ascending order) """ - + # Initialize logger self.logger = logging.getLogger("run_self") self.logger.propagate = False @@ -450,7 +450,9 @@ class RunSelector: # Be sure we have a namelist if self.fallback_nml and run not in self.namelist: - self.logger.warning(f"Used fallback namelist for run {run} from output {nums[0]}") + self.logger.warning( + f"Used fallback namelist for run {run} from output {nums[0]}" + ) path = f"{self.path_in}/{run}/output_{nums[0]:05}/namelist.txt" self.namelist[run] = self.load_namelist(run, path=path)