Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ gwk_ess_evolution_plot = "gwkokab_scripts.ess_evolution_plot:main"
gwk_ess_plot = "gwkokab_scripts.ess_plot:main"
gwk_flowMC_cfg_template = "gwkokab.analysis.core.inference_io._sampler:_dump_flowMC_cfg"
gwk_flowMC_info = "gwkokab_scripts.flowMC_info:main"
gwk_h5repack = "gwkokab_scripts.h5repack:main"
gwk_hist_overplot = "gwkokab_scripts.hist_overplot:main"
gwk_joint_plot = "gwkokab_scripts.joint_plot:main"
gwk_numpyro_cfg_template = "gwkokab.analysis.core.inference_io._sampler:_dump_numpyro_cfg"
Expand Down
62 changes: 62 additions & 0 deletions src/gwkokab_scripts/h5repack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2023 The GWKokab Authors
# SPDX-License-Identifier: Apache-2.0


import argparse
import os
import shutil
import subprocess


def main():
description = r"""Repack an HDF5 file using `h5repack` if available.

It is equivalent to the following shell script:

```sh
#!/bin/bash

file=$1
temp_file="${file}.tmp"

h5repack [options] "$file" "$temp_file"

if [ $? -eq 0 ]; then
mv "$temp_file" "$file"
else
rm -f "$temp_file"
fi
```
"""
parser = argparse.ArgumentParser(
description=description,
epilog="Example usage: gwk_h5repack -- -f GZIP=9 -f SHUF data.h5",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"options",
nargs=argparse.REMAINDER,
help="Additional options to pass to h5repack (e.g., '-f GZIP=9 -f SHUF')",
)
parser.add_argument("file", help="Path to the HDF5 file to repack")
args, options = parser.parse_known_args()

filename = args.file

if not shutil.which("h5repack"):
raise OSError("'h5repack' command not found in PATH.")

if not os.path.exists(filename):
raise FileNotFoundError(f"File '{filename}' does not exist.")

temp_file = f"{filename}.tmp"

cmd = ["h5repack"] + options + [filename, temp_file]

try:
subprocess.run(cmd, check=True)
shutil.move(temp_file, filename)
except (subprocess.CalledProcessError, OSError) as e:
if os.path.exists(temp_file):
os.remove(temp_file)
raise e
Loading