@@ -7,6 +7,7 @@ HDF5 for Rust.
77[ ![ Documentation] ( https://docs.rs/hdf5/badge.svg )] ( https://docs.rs/hdf5 )
88[ ![ Changelog] ( https://img.shields.io/github/v/release/aldanor/hdf5-rust )] ( https://github.com/aldanor/hdf5-rust/blob/master/CHANGELOG.md )
99![ hdf5: rustc 1.51+] ( https://img.shields.io/badge/hdf5-rustc_1.51+-lightblue.svg )
10+ [ ![ Total Lines] ( https://tokei.rs/b1/github/aldanor/hdf5-rust )] ( https://github.com/aldanor/hdf5-rust )
1011[ ![ Apache 2.0] ( https://img.shields.io/badge/License-Apache%202.0-blue.svg )] ( https://opensource.org/licenses/Apache-2.0 )
1112[ ![ MIT] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( https://opensource.org/licenses/MIT )
1213
@@ -25,53 +26,77 @@ Requires HDF5 library of version 1.8.4 or later.
2526## Example
2627
2728``` rust
28- #[derive(hdf5:: H5Type , Clone , PartialEq , Debug )]
29+ #[cfg(feature = " blosc" )]
30+ use hdf5 :: filters :: blosc_set_nthreads;
31+ use hdf5 :: {File , H5Type , Result };
32+ use ndarray :: {arr2, s};
33+
34+ #[derive(H5Type , Clone , PartialEq , Debug )] // register with HDF5
2935#[repr(u8 )]
3036pub enum Color {
31- RED = 1 ,
32- GREEN = 2 ,
33- BLUE = 3 ,
37+ R = 1 ,
38+ G = 2 ,
39+ B = 3 ,
3440}
3541
36- #[derive(hdf5 :: H5Type , Clone , PartialEq , Debug )]
42+ #[derive(H5Type , Clone , PartialEq , Debug )] // register with HDF5
3743#[repr(C )]
3844pub struct Pixel {
3945 xy : (i64 , i64 ),
4046 color : Color ,
4147}
4248
43- fn main () -> hdf5 :: Result <()> {
44- use self :: Color :: * ;
45- use ndarray :: {arr1, arr2};
46-
47- {
48- // write
49- let file = hdf5 :: File :: create (" pixels.h5" )? ;
50- let colors = file . new_dataset :: <Color >(). shape (2 ). create (" colors" )? ;
51- colors . write (& [RED , BLUE ])? ;
52- let group = file . create_group (" dir" )? ;
53- let pixels = group . new_dataset :: <Pixel >(). shape ((2 , 2 )). create (" pixels" )? ;
54- pixels . write (& arr2 (& [
55- [Pixel { xy : (1 , 2 ), color : RED }, Pixel { xy : (3 , 4 ), color : BLUE }],
56- [Pixel { xy : (5 , 6 ), color : GREEN }, Pixel { xy : (7 , 8 ), color : RED }],
57- ]))? ;
58- }
59- {
60- // read
61- let file = hdf5 :: File :: open (" pixels.h5" )? ;
62- let colors = file . dataset (" colors" )? ;
63- assert_eq! (colors . read_1d :: <Color >()? , arr1 (& [RED , BLUE ]));
64- let pixels = file . dataset (" dir/pixels" )? ;
65- assert_eq! (
66- pixels . read_raw :: <Pixel >()? ,
67- vec! [
68- Pixel { xy : (1 , 2 ), color : RED },
69- Pixel { xy : (3 , 4 ), color : BLUE },
70- Pixel { xy : (5 , 6 ), color : GREEN },
71- Pixel { xy : (7 , 8 ), color : RED },
72- ]
73- );
49+ impl Pixel {
50+ pub fn new (x : i64 , y : i64 , color : Color ) -> Self {
51+ Self { xy : (x , y ), color }
7452 }
53+ }
54+
55+ fn write_hdf5 () -> Result <()> {
56+ use Color :: * ;
57+ let file = File :: create (" pixels.h5" )? ; // open for writing
58+ let group = file . create_group (" dir" )? ; // create a group
59+ #[cfg(feature = " blosc" )]
60+ blosc_set_nthreads (2 ); // set number of blosc threads
61+ let builder = group . new_dataset_builder ();
62+ #[cfg(feature = " blosc" )]
63+ let builder = builder . blosc_zstd (9 , true ); // zstd + shuffle
64+ let ds = builder
65+ . with_data (& arr2 (& [
66+ // write a 2-D array of data
67+ [Pixel :: new (1 , 2 , R ), Pixel :: new (2 , 3 , B )],
68+ [Pixel :: new (3 , 4 , G ), Pixel :: new (4 , 5 , R )],
69+ [Pixel :: new (5 , 6 , B ), Pixel :: new (6 , 7 , G )],
70+ ]))
71+ // finalize and write the dataset
72+ . create (" pixels" )? ;
73+ // create an attr with fixed shape but don't write the data
74+ let attr = ds . new_attr :: <Color >(). shape ([3 ]). create (" colors" )? ;
75+ // write the attr data
76+ attr . write (& [R , G , B ])? ;
77+ Ok (())
78+ }
79+
80+ fn read_hdf5 () -> Result <()> {
81+ use Color :: * ;
82+ let file = File :: open (" pixels.h5" )? ; // open for reading
83+ let ds = file . dataset (" dir/pixels" )? ; // open the dataset
84+ assert_eq! (
85+ // read a slice of the 2-D dataset and verify it
86+ ds . read_slice :: <Pixel , _ , _ >(s! [1 .. , .. ])? ,
87+ arr2 (& [
88+ [Pixel :: new (3 , 4 , G ), Pixel :: new (4 , 5 , R )],
89+ [Pixel :: new (5 , 6 , B ), Pixel :: new (6 , 7 , G )],
90+ ])
91+ );
92+ let attr = ds . attr (" colors" )? ; // open the attribute
93+ assert_eq! (attr . read_1d :: <Color >()? . as_slice (). unwrap (), & [R , G , B ]);
94+ Ok (())
95+ }
96+
97+ fn main () -> Result <()> {
98+ write_hdf5 ()? ;
99+ read_hdf5 ()? ;
75100 Ok (())
76101}
77102```
0 commit comments