postprocessing/offsets/offsets_NNN.hdf5
Files span snapshots 000–080. FileOffsets gives the global starting index for each chunk. Search the starts to find the chunk, then subtract its starting index.
File Reference
One offset file per snapshot maps global indices to rows in the snapshot, group catalog, and merger tree files. The datasets are organized in three groups.
Offset Datasets
| Attribute | Dimensions | Description |
|---|---|---|
| FileOffsets/SnapByType | (Nchuncks, 6) | The offset table (by type) for the snapshot files, giving the first particle index in each snap file chunk. Determines which files(s) a given offset+length will cover. A two-dimensional array, where the element (i,j) equals the cumulative sum (i.e. offset) of particles of type i in all snapshot file chunks prior to j. |
| FileOffsets/Group | Nchuncks | The offset table for groups in the group catalog files. A one-dimensional array, where the ith element equals the first group number in the ith groupcat file chunk. |
| FileOffsets/Subhalo | Nchuncks | The offset table for subhalos in the group catalog files. A one-dimensional array, where the ith element equals the first subgroup number in the ith groupcat file chunk |
| Group/SnapByType | (Ngroups_Total,6) | The offset table for a given group number (by type), into the snapshot files. That is, the global particle index (across all snap file chunks) of the first particle of this group. A two-dimensional array, where the element (i,j) equals the cumulative sum (i.e. offset) of particles of type i in all groups prior to group number j. |
| Subhalo/SnapByType | (Nsubgroups_Total,6) | The offset table for a given subhalo number (by type), into the snapshot files. That is, the global particle index (across all snap file chunks) of the first particle of this subhalo. A two-dimensional array, where the element (i,j) equals the cumulative sum (i.e. offset) of particles of type i in all subhalos prior to subhalo number j. |
| Subhalo/LHaloTree/File | Nsubgroups_Total | The LHaloTree file number with the tree which contains this subhalo. |
| Subhalo/LHaloTree/Num | Nsubgroups_Total | The number of the tree within the above file within which this subhalo is located (e.g. TreeX). |
| Subhalo/LHaloTree/Index | Nsubgroups_Total | The LHaloTree index within the above tree dataset at which this subhalo is located. |
Index Lookup Functions
These functions use the offsets and the header of chunk 0. For example, get_location_of_particles_in_halo("Thesan-1", 80, "subhalo", 43) returns the files and half-open row ranges for each particle type. Use dataset[start:stop] to read each range.
from pathlib import Path
import h5py
import numpy as np
def chunk_edges(simpath, snap, kind, ptype=None):
"""Global row boundaries, including the exclusive end of the last chunk."""
base = Path(simpath)
file = base / "postprocessing" / "offsets" / f"offsets_{snap:03d}.hdf5"
with h5py.File(file) as offsets:
if kind == "particle":
if ptype not in range(6):
raise ValueError("Specify the particle type (0 through 5)")
starts = offsets["FileOffsets/SnapByType"][:, ptype]
header_file = base / "output" / f"snapdir_{snap:03d}" / f"snap_{snap:03d}.0.hdf5"
with h5py.File(header_file) as chunk:
header = chunk["Header"].attrs
total = int(header["NumPart_Total"][ptype])
total += int(header["NumPart_Total_HighWord"][ptype]) << 32
else:
group, attr = {"group": ("Group", "Ngroups_Total"),
"subhalo": ("Subhalo", "Nsubgroups_Total")}[kind]
starts = offsets["FileOffsets/" + group][:]
directory = base / "output" / f"groups_{snap:03d}"
with h5py.File(directory / f"fof_subhalo_tab_{snap:03d}.0.hdf5") as chunk:
total = int(chunk["Header"].attrs[attr])
return np.append(starts.astype(np.int64), total)
def get_global_index(simpath, snap, offset_type, local_index, chunk, ptype=None):
edges = chunk_edges(simpath, snap, offset_type, ptype)
if not 0 <= chunk < len(edges) - 1 or not 0 <= local_index < edges[chunk+1] - edges[chunk]:
raise IndexError("Row outside the requested chunk")
return int(edges[chunk]) + local_index
def get_chunk_and_local_index(simpath, snap, offset_type, global_index, ptype=None):
edges = chunk_edges(simpath, snap, offset_type, ptype)
if not 0 <= global_index < edges[-1]:
raise IndexError("Row outside this catalog or particle type")
chunk = int(np.searchsorted(edges, global_index, side="right")) - 1
return chunk, global_index - int(edges[chunk])
def get_particles_global_index_for_halo(simpath, snap, halo_type, global_index):
"""First snapshot row of each particle type for one halo or subhalo."""
group = {"group": "Group", "subhalo": "Subhalo"}[halo_type]
file = Path(simpath) / "postprocessing" / "offsets" / f"offsets_{snap:03d}.hdf5"
with h5py.File(file) as f:
return f[group + "/SnapByType"][global_index]
def get_location_of_particles_in_halo(simpath, snap, halo_type, global_index, verbose=False):
"""File names, start rows, and exclusive stop rows, each grouped by particle type."""
base = Path(simpath) / "output"
chunk, row = get_chunk_and_local_index(simpath, snap, halo_type, global_index)
catalog = base / f"groups_{snap:03d}" / f"fof_subhalo_tab_{snap:03d}.{chunk}.hdf5"
field = {"group": "Group/GroupLenType", "subhalo": "Subhalo/SubhaloLenType"}[halo_type]
with h5py.File(catalog) as f:
lengths = f[field][row]
starts = get_particles_global_index_for_halo(simpath, snap, halo_type, global_index)
files, lower, upper = ([[] for _ in range(6)] for _ in range(3))
for ptype in np.flatnonzero(lengths):
edges = chunk_edges(simpath, snap, "particle", ptype)
start = int(starts[ptype])
stop = start + int(lengths[ptype])
# Intersect the halo's particle range with each nonempty snapshot chunk.
chunks = np.flatnonzero((edges[:-1] < stop) & (edges[1:] > start) & (np.diff(edges) > 0))
for part in chunks:
path = base / f"snapdir_{snap:03d}" / f"snap_{snap:03d}.{part}.hdf5"
files[ptype].append(str(path))
lower[ptype].append(max(start - int(edges[part]), 0))
upper[ptype].append(min(stop, int(edges[part+1])) - int(edges[part]))
if verbose:
print(ptype, path, lower[ptype][-1], upper[ptype][-1])
return files, lower, upper
Locate a subhalo in a split catalog
Paths in this example are relative to Thesan-1/postprocessing/.
import h5py
import numpy as np
subhalo = 100000
with h5py.File("offsets/offsets_080.hdf5") as f:
starts = f["FileOffsets/Subhalo"][:]
chunk = np.searchsorted(starts, subhalo, side="right") - 1
row = int(subhalo - starts[chunk])
print("catalog chunk and row:", chunk, row)
with h5py.File(f"../output/groups_080/fof_subhalo_tab_080.{chunk}.hdf5") as f:
print("host FoF group:", f["Subhalo/SubhaloGrNr"][row])
Output from THESAN-1
catalog chunk and row: 16 517 host FoF group: 4817