|
1 | 1 | //! Create, write, and read a chunked dataset |
2 | | -fn main() -> hdf5::Result<()> { |
3 | | - let file = hdf5::File::create("chunking.h5")?; |
4 | 2 |
|
5 | | - let ny = 100; |
6 | | - let nx = 100; |
7 | | - let arr = ndarray::Array2::from_shape_fn((ny, nx), |(j, i)| (1000 * j + i) as f32); |
| 3 | +use hdf5::{File, Result}; |
| 4 | +use ndarray::Array2; |
8 | 5 |
|
9 | | - { |
10 | | - let ds = file |
| 6 | +fn main() -> Result<()> { |
| 7 | + let file = File::create("chunking.h5")?; |
| 8 | + |
| 9 | + let (ny, nx) = (100, 100); |
| 10 | + let arr = Array2::from_shape_fn((ny, nx), |(j, i)| (1000 * j + i) as f32); |
| 11 | + |
| 12 | + let ds = file |
11 | 13 | .new_dataset::<f32>() |
12 | | - .chunk((1, ny, nx)) // nx*ny elements will be compressed as a single chunk |
13 | | - .shape((1.., ny, nx)) // Initial size of 1 on the unlimited dimension |
| 14 | + .chunk((1, ny, nx)) // each chunk contains ny * nx elements |
| 15 | + .shape((1.., ny, nx)) // first axis is unlimited with initial size of 1 |
14 | 16 | .deflate(3) |
15 | 17 | .create("variable")?; |
16 | 18 |
|
17 | | - // Writing a chunk at a time will be most efficient |
18 | | - ds.write_slice(&arr, (0, .., ..))?; |
| 19 | + // writing one chunk at a time is the most efficient |
| 20 | + ds.write_slice(&arr, (0, .., ..))?; |
19 | 21 |
|
20 | | - // Dataset can be resized along an unlimited dimension |
21 | | - ds.resize((10, ny, nx))?; |
22 | | - ds.write_slice(&arr, (1, .., ..))?; |
23 | | - } |
| 22 | + // dataset can be resized along an unlimited dimension |
| 23 | + ds.resize((10, ny, nx))?; |
| 24 | + ds.write_slice(&arr, (1, .., ..))?; |
24 | 25 |
|
25 | | - let ds = file.dataset("variable")?; |
26 | 26 | let chunksize = ds.chunk().unwrap(); |
27 | 27 | assert_eq!(chunksize, &[1, ny, nx]); |
28 | 28 |
|
29 | 29 | let shape = ds.shape(); |
30 | 30 | assert_eq!(shape, &[10, ny, nx]); |
31 | 31 |
|
32 | | - // Reading from a chunked dataset should be done in a chunk-wise order |
| 32 | + // it's best to read from a chunked dataset in a chunk-wise fashion |
33 | 33 | for k in 0..shape[0] { |
34 | | - let _arr: ndarray::Array2<f32> = ds.read_slice((k, .., ..))?; |
| 34 | + let _arr: Array2<f32> = ds.read_slice((k, .., ..))?; |
35 | 35 | } |
36 | 36 |
|
37 | 37 | Ok(()) |
|
0 commit comments