convert snapshots
This commit is contained in:
parent
c58ec1de1b
commit
3aeef8fead
1 changed files with 66 additions and 7 deletions
|
@ -34,6 +34,55 @@ def convert_snapshot_to_density(snapshot_path, output_path, N=None, corner=(0.0,
|
||||||
F.write(output_path)
|
F.write(output_path)
|
||||||
print("Density written to", output_path)
|
print("Density written to", output_path)
|
||||||
print("Done.")
|
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():
|
def console_main():
|
||||||
|
@ -70,13 +119,23 @@ def console_main():
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
convert_snapshot_to_density(
|
if args.snapshot.endswith("h5") or args.snapshot.endswith("hdf5") or args.snapshot.endswith("gadget3"):
|
||||||
snapshot_path=args.snapshot,
|
# If the snapshot is a single file, convert it to density
|
||||||
output_path=args.output,
|
convert_snapshot_to_density(
|
||||||
N=args.N,
|
snapshot_path=args.snapshot,
|
||||||
corner=args.corner,
|
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__":
|
if __name__ == "__main__":
|
||||||
console_main()
|
console_main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue