1"""
2An example of using MultiBlockSlice to interleave frames in a virtual dataset.
3"""
4import h5py
5
6
7# These files were written round-robin in blocks of 1000 frames from a single source
8# 1.h5 has 0-999, 4000-4999, ...; 2.h4 has 1000-1999, 5000-5999, ...; etc
9# The frames from each block are contiguous within each file
10# e.g. 1.h5 = [..., 998, 999, 4000, 4001, ... ]
11files = ["1.h5", "2.h5", "3.h5", "4.h5"]
12dataset_name = "data"
13dtype = "float"
14source_shape = (25000, 256, 512)
15target_shape = (100000, 256, 512)
16block_size = 1000
17
18v_layout = h5py.VirtualLayout(shape=target_shape, dtype=dtype)
19
20for file_idx, file_path in enumerate(files):
21    v_source = h5py.VirtualSource(
22        file_path, name=dataset_name, shape=source_shape, dtype=dtype
23    )
24    dataset_frames = v_source.shape[0]
25
26    # A single raw file maps to every len(files)th block of frames in the VDS
27    start = file_idx * block_size  # 0, 1000, 2000, 3000
28    stride = len(files) * block_size  # 4000
29    count = dataset_frames // block_size  # 25
30    block = block_size  # 1000
31
32    # MultiBlockSlice for frame dimension and full extent for height and width
33    v_layout[h5py.MultiBlockSlice(start, stride, count, block), :, :] = v_source
34
35with h5py.File("interleave_vds.h5", "w", libver="latest") as f:
36    f.create_virtual_dataset(dataset_name, v_layout, fillvalue=0)
37