Skip to content

Instantly share code, notes, and snippets.

@lgarrison
Created June 7, 2022 20:03
Show Gist options
  • Save lgarrison/9cbd65cf90b5687c0996c2314b3559ac to your computer and use it in GitHub Desktop.
Save lgarrison/9cbd65cf90b5687c0996c2314b3559ac to your computer and use it in GitHub Desktop.
Compress a density grid with lossy + lossless compression
#!/usr/bin/env python3
'''
Compress a density grid.
Usage: ./compress.py --help
'''
from pathlib import Path
import click
import numpy as np
import asdf
@click.command()
@click.argument('fn')
@click.option('--truncbits', default=10, help='Number of low bits to truncate in the density field')
def compress(fn, truncbits):
'''
Read the density file in FN (a npz file) and write a compressed version.
This uses the blosc ASDF compression, supplied by abacusutils on pip.
'''
fn = Path(fn)
d = np.load(fn)['arr_0']
d = d.astype(np.float32)
d = (d.view(np.uint32) & ~np.uint32((1<<truncbits)-1)).view(np.float32)
with asdf.AsdfFile(tree=dict(density=d)) as af:
af.write_to(fn.parent / (fn.stem + '_compressed.asdf'), all_array_compression='blsc',
compression_kwargs=dict(shuffle='shuffle')
)
if __name__ == '__main__':
compress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment