|
20 | 20 | clevel = 5 # compression level, e.g., 0-9, where 0 is no compression and 9 is maximum compression |
21 | 21 | fname_in = "kevlar.h5" # input file with the kevlar dataset |
22 | 22 | fname_out = "kevlar-blosc2.h5" |
| 23 | +nframes = 1000 |
23 | 24 | if not os.path.exists(fname_in): |
24 | 25 | raise FileNotFoundError( |
25 | 26 | f"Input file {fname_in} does not exist\n" |
|
30 | 31 | # Example 1 |
31 | 32 | # hdf5plugin supports limited blosc2 compression with certain codecs |
32 | 33 | cname = "zstd" |
33 | | -with h5py.File(fname_in, "r") as fr: |
34 | | - dset = fr["/entry/data/data"][:] |
35 | | - with h5py.File(fname_out, "w") as fw: |
| 34 | + |
| 35 | +if not os.path.exists("STD" + fname_out): |
| 36 | + with h5py.File(fname_in, "r") as fr: |
| 37 | + dset = fr["/entry/data/data"][:nframes] |
| 38 | + with h5py.File("STD" + fname_out, "w") as fw: |
36 | 39 | g = fw.create_group("/data") |
37 | 40 | b2comp = hdf5plugin.Blosc2(cname=cname, clevel=clevel, filters=hdf5plugin.Blosc2.BITSHUFFLE) |
38 | 41 | dset_out = g.create_dataset( |
|
42 | 45 | chunks=(1,) + dset.shape[1:], # chunk size of 1 frame |
43 | 46 | **b2comp, |
44 | 47 | ) |
45 | | - print("Successfully compressed file with hdf5plugin") |
| 48 | +print("Successfully compressed file with hdf5plugin") |
46 | 49 |
|
47 | 50 | # Example 2 |
48 | 51 | # For other codecs (e.g grok) or for more custom compression such as with user-defined block shapes, one |
49 | 52 | # has to use a more involved route |
50 | 53 | blocks = (50, 80, 80) |
51 | | -chunks = (200, 240, 240) |
| 54 | +chunks = (100, 240, 240) |
52 | 55 | cparams = { |
53 | 56 | "codec": blosc2.Codec.LZ4, |
54 | 57 | "filters": [blosc2.Filter.BITSHUFFLE], |
|
58 | 61 |
|
59 | 62 | if os.path.exists("dset.b2nd"): # don't reload dset to blosc2 if already done so once |
60 | 63 | b2im = blosc2.open(urlpath="dset.b2nd", mode="r") |
61 | | - s, d = b2im.shape, b2im.dtype |
62 | 64 | else: |
63 | 65 | with h5py.File(fname_in, "r") as fr: # load file and process to blosc2 array |
64 | | - dset = fr["/entry/data/data"][:] |
| 66 | + dset = fr["/entry/data/data"][:nframes] |
65 | 67 | b2im = blosc2.asarray( |
66 | 68 | dset, chunks=chunks, blocks=blocks, cparams=cparams, urlpath="dset.b2nd", mode="w" |
67 | 69 | ) |
68 | | - d = dset.dtype |
69 | 70 | del dset |
70 | 71 |
|
| 72 | +s, d = b2im.shape, b2im.dtype |
71 | 73 | # Write to .h5 file # |
72 | | -with h5py.File(fname_out, "w") as fw: |
| 74 | +with h5py.File("Custom" + fname_out, "w") as fw: |
73 | 75 | g = fw.create_group("/data") |
74 | 76 | b2comp = hdf5plugin.Blosc2() # just for identification, no compression algorithm specified |
75 | 77 | dset_out = g.create_dataset( |
|
0 commit comments