few bug solving

This commit is contained in:
Noe Brucy
2023-07-12 11:13:03 +02:00
parent 29a9150534
commit a1e4f775b6
4 changed files with 40 additions and 18 deletions
+2 -2
View File
@@ -370,7 +370,7 @@ class HDF5Container(BaseProcessor):
for base_unit_str in unit: for base_unit_str in unit:
expo = unit[base_unit_str] expo = unit[base_unit_str]
base_unit = self._get_units(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 return new_unit
if (data is not None) and isinstance(data, dict) and list(unit)[0] in data: if (data is not None) and isinstance(data, dict) and list(unit)[0] in data:
for key in unit: for key in unit:
@@ -435,7 +435,7 @@ class HDF5Container(BaseProcessor):
self._save_data(name_full + "/" + key, data[key], "", unit) self._save_data(name_full + "/" + key, data[key], "", unit)
else: else:
try: try:
if len(data) == 0: if data is None or len(data) == 0:
return return
except TypeError: except TypeError:
data = np.array([data]) data = np.array([data])
+13 -13
View File
@@ -360,8 +360,11 @@ class SnapshotProcessor(HDF5Container):
# Create selector object # Create selector object
if selector is None: if selector is None:
dirpath = os.path.dirname(path)
if dirpath == "":
dirpath = "."
selector = RunSelector( selector = RunSelector(
os.path.dirname(path), dirpath,
self.run, self.run,
self.num, self.num,
self.params.input.nml_filename, self.params.input.nml_filename,
@@ -452,9 +455,6 @@ class SnapshotProcessor(HDF5Container):
if os.path.exists(part_filename): if os.path.exists(part_filename):
hydro_file = open(part_filename) hydro_file = open(part_filename)
part_file = open(f"{path}/output_{self.num:05}/part_file_descriptor.txt") 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() plines = part_file.readlines()
part_var = np.unique( part_var = np.unique(
list(map(lambda s: s.split(",")[1][1:].split("_")[0], plines[2:])) list(map(lambda s: s.split(",")[1][1:].split("_")[0], plines[2:]))
@@ -463,7 +463,7 @@ class SnapshotProcessor(HDF5Container):
part_var = [] part_var = []
has_grav = os.path.exists( 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): def is_available(available_vars, pymsesrc, var):
@@ -684,12 +684,13 @@ class SnapshotProcessor(HDF5Container):
if len(data[key] > 0): if len(data[key] > 0):
if f"/{group}/{key}" in hdf5 and overwrite: if f"/{group}/{key}" in hdf5 and overwrite:
hdf5.remove_node(f"/{group}/{key}") hdf5.remove_node(f"/{group}/{key}")
hdf5.create_array( elif f"/{group}/{key}" not in hdf5:
f"/{group}", key, data[key], "", createparents=True 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 unit = self._get_units(self.unit_key[key])
nb_written += 1 hdf5.get_node(f"/{group}/{key}").unit = unit
nb_written += 1
else: else:
self.logger.warning("Empty key") self.logger.warning("Empty key")
if "namelist" not in hdf5.root._v_attrs: if "namelist" not in hdf5.root._v_attrs:
@@ -747,7 +748,7 @@ class SnapshotProcessor(HDF5Container):
self.cells = self.load_data( self.cells = self.load_data(
cells_src, cells_src,
filename, filename,
True, save,
keys=keys, keys=keys,
group="cells", group="cells",
) )
@@ -1648,7 +1649,6 @@ class SnapshotProcessor(HDF5Container):
outfile = self.pspec_filename outfile = self.pspec_filename
if overwrite_file or not os.path.exists(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) pspec.pspec(repo=self.path, iouts=[self.num], outfile=outfile, **kwargs)
return np.array([self.pspec_filename])
def _write_particles(self): def _write_particles(self):
"""Ensure particles are written in the hdf5 file""" """Ensure particles are written in the hdf5 file"""
+20
View File
@@ -1,8 +1,28 @@
# coding: utf-8 # coding: utf-8
import argparse
import os
from snapshotprocessor import SnapshotProcessor from snapshotprocessor import SnapshotProcessor
def convert_to_hdf5(path, snap_num, path_out=".", filename=None, **kwargs): 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 = SnapshotProcessor(path=path, num=snap_num, path_out=path_out, **kwargs)
snap.convert_hdf5(filename) 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)
+5 -3
View File
@@ -40,7 +40,7 @@ class NamelistRecursive:
class RunSelector: class RunSelector:
def __init__( def __init__(
self, self,
path_in, path_in=".",
in_runs=None, in_runs=None,
in_nums="all", in_nums="all",
nml_filename="run.nml", 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) sort_run_by : str, a key from the namelist used to sort the runs (by ascending order)
""" """
# Initialize logger # Initialize logger
self.logger = logging.getLogger("run_self") self.logger = logging.getLogger("run_self")
self.logger.propagate = False self.logger.propagate = False
@@ -450,7 +450,9 @@ class RunSelector:
# Be sure we have a namelist # Be sure we have a namelist
if self.fallback_nml and run not in self.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" path = f"{self.path_in}/{run}/output_{nums[0]:05}/namelist.txt"
self.namelist[run] = self.load_namelist(run, path=path) self.namelist[run] = self.load_namelist(run, path=path)