From 3aeef8fead04d390d71f867cda4fb1e2d18363e4 Mon Sep 17 00:00:00 2001 From: Mayeul Aubin Date: Tue, 24 Jun 2025 11:42:54 +0200 Subject: [PATCH] convert snapshots --- .../scripts/convert_snapshot_to_density.py | 73 +++++++++++++++++-- 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/sbmy_control/scripts/convert_snapshot_to_density.py b/sbmy_control/scripts/convert_snapshot_to_density.py index dadefed..869ee90 100644 --- a/sbmy_control/scripts/convert_snapshot_to_density.py +++ b/sbmy_control/scripts/convert_snapshot_to_density.py @@ -34,6 +34,55 @@ def convert_snapshot_to_density(snapshot_path, output_path, N=None, corner=(0.0, F.write(output_path) print("Density written to", output_path) print("Done.") + + +def convert_snapshots_to_density(snapshot_base_path, output_path, N=None, corner=(0.0, 0.0, 0.0)): + """ + Convert multiple snapshots to density fields. + + Parameters + ---------- + snapshot_base_path : str + Base path for the snapshot files. + output_path : str + Path to the output density file. + N : int + Size of the density field grid (N x N x N). + corner : tuple of float + Corner of the box (x, y, z). + """ + # Get all snapshot files with path "snapshot_base_path*" + import glob + snapshot_files = glob.glob(snapshot_base_path + "*") + if not snapshot_files: + raise FileNotFoundError(f"No snapshot files found at {snapshot_base_path}") + + A = None + F = None + + for snapshot_path in snapshot_files: + # Read the snapshot + print(f"Reading snapshot {snapshot_path}...") + snap = read_snapshot(snapshot_path) + + if N is None: + N = snap.Np0 + + # Calculate density + print("Calculating density...") + F = get_density_pm_snapshot(snap, N, N, N, corner[0], corner[1], corner[2]) + + if A is None: + A = F.data + else: + A += F.data + + F.data = A # Combine densities from all snapshots + # Write density to file + print("Writing density...") + F.write(output_path) + print("Density written to", output_path) + print("Done.") def console_main(): @@ -70,13 +119,23 @@ def console_main(): args = parser.parse_args() - convert_snapshot_to_density( - snapshot_path=args.snapshot, - output_path=args.output, - N=args.N, - corner=args.corner, - ) + if args.snapshot.endswith("h5") or args.snapshot.endswith("hdf5") or args.snapshot.endswith("gadget3"): + # If the snapshot is a single file, convert it to density + convert_snapshot_to_density( + snapshot_path=args.snapshot, + output_path=args.output, + N=args.N, + corner=args.corner, + ) + else: + # If the snapshot is a base path, convert all snapshots to density + convert_snapshots_to_density( + snapshot_base_path=args.snapshot, + output_path=args.output, + N=args.N, + corner=args.corner, + ) if __name__ == "__main__": - console_main() \ No newline at end of file + console_main()