Skip to content

halocat.io

io

Reading GLAM CatshortV catalogues and writing halo HDF5 files.

read_catalogue

read_catalogue(path: str, columns: list[str] | None = None) -> dict[str, np.ndarray]

Read a CatshortV.*.DAT plain-text halo catalogue.

Returns a dict of 1D numpy arrays keyed by column name. Empty / missing files raise FileNotFoundError or yield empty arrays.

Source code in halocat/io.py
def read_catalogue(path: str,
                   columns: list[str] | None = None) -> dict[str, np.ndarray]:
    """Read a CatshortV.*.DAT plain-text halo catalogue.

    Returns a dict of 1D numpy arrays keyed by column name.
    Empty / missing files raise FileNotFoundError or yield empty arrays.
    """
    if not os.path.exists(path):
        raise FileNotFoundError(path)

    arr = np.loadtxt(path, skiprows=CATALOGUE_HEADER_LINES, dtype=np.float64)

    if arr.ndim == 1:
        arr = arr.reshape(1, -1) if arr.size else arr.reshape(0, len(CATALOGUE_COLUMNS))

    ncol = arr.shape[1] if arr.size else len(CATALOGUE_COLUMNS)
    if ncol < len(CATALOGUE_COLUMNS):
        # Some catalogues may have fewer trailing columns; truncate names.
        col_names = CATALOGUE_COLUMNS[:ncol]
    else:
        col_names = CATALOGUE_COLUMNS

    out: dict[str, np.ndarray] = {}
    for i, name in enumerate(col_names):
        out[name] = arr[:, i] if arr.size else np.empty(0, dtype=np.float64)

    if columns is not None:
        out = {k: out[k] for k in columns if k in out}

    return out

write_halo_hdf5

write_halo_hdf5(data: dict[str, ndarray], outpath: str, attrs: dict | None = None) -> None

Write the halo dict to an HDF5 file with optional attributes.

Source code in halocat/io.py
def write_halo_hdf5(data: dict[str, np.ndarray], outpath: str,
                    attrs: dict | None = None) -> None:
    """Write the halo dict to an HDF5 file with optional attributes."""
    os.makedirs(os.path.dirname(outpath), exist_ok=True)
    with h5py.File(outpath, "w") as f:
        for k, v in data.items():
            f.create_dataset(k, data=np.asarray(v), compression="gzip",
                             compression_opts=4, shuffle=True)
        if attrs:
            for k, v in attrs.items():
                f.attrs[k] = v

read_halo_hdf5

read_halo_hdf5(path: str) -> dict[str, np.ndarray]

Read back a halo.hdf5 file written by write_halo_hdf5.

Source code in halocat/io.py
def read_halo_hdf5(path: str) -> dict[str, np.ndarray]:
    """Read back a halo.hdf5 file written by write_halo_hdf5."""
    out: dict[str, np.ndarray] = {}
    with h5py.File(path, "r") as f:
        for k in f.keys():
            out[k] = f[k][...]
    return out