@@ -3,53 +3,71 @@ use hdf5::filters::blosc_set_nthreads;
33use hdf5:: { File , H5Type , Result } ;
44use ndarray:: { arr2, s} ;
55
6- #[ derive( H5Type , Clone , PartialEq , Debug ) ] // map the HDF5 type for this enum
6+ #[ derive( H5Type , Clone , PartialEq , Debug ) ] // register with HDF5
77#[ repr( u8 ) ]
88pub enum Color {
99 R = 1 ,
1010 G = 2 ,
1111 B = 3 ,
1212}
1313
14- #[ derive( H5Type , Clone , PartialEq , Debug ) ] // register this struct with HDF5
14+ #[ derive( H5Type , Clone , PartialEq , Debug ) ] // register with HDF5
1515#[ repr( C ) ]
1616pub struct Pixel {
1717 xy : ( i64 , i64 ) ,
1818 color : Color ,
1919}
2020
21- fn main ( ) -> Result < ( ) > {
22- {
23- let file = File :: create ( "pixels.h5" ) ?; // open the file for writing
24- let group = file. create_group ( "dir" ) ?; // create a group
25- #[ cfg( feature = "blosc" ) ]
26- blosc_set_nthreads ( 2 ) ; // set number of threads for compressing/decompressing chunks
27- let builder = group. new_dataset_builder ( ) ;
28- #[ cfg( feature = "blosc" ) ]
29- let builder = builder. blosc_zstd ( 9 , true ) ; // enable zstd compression with shuffling
30- let ds = builder
31- . with_data ( & arr2 ( & [
32- // write a 2-D array of data
33- [ Pixel { xy : ( 1 , 2 ) , color : Color :: R } , Pixel { xy : ( 2 , 3 ) , color : Color :: B } ] ,
34- [ Pixel { xy : ( 3 , 4 ) , color : Color :: G } , Pixel { xy : ( 4 , 5 ) , color : Color :: R } ] ,
35- [ Pixel { xy : ( 5 , 6 ) , color : Color :: B } , Pixel { xy : ( 6 , 7 ) , color : Color :: G } ] ,
36- ] ) )
37- . create ( "pixels" ) ?; // finalize and write the dataset
38- let attr = ds. new_attr :: < Color > ( ) . shape ( [ 3 ] ) . create ( "colors" ) ?; // create an attribute
39- attr. write ( & [ Color :: R , Color :: G , Color :: B ] ) ?;
40- }
41- {
42- let file = File :: open ( "pixels.h5" ) ?; // open the file for reading
43- let ds = file. dataset ( "dir/pixels" ) ?; // open the dataset object
44- assert_eq ! (
45- ds. read_slice:: <Pixel , _, _>( s![ 1 .., ..] ) ?, // read a slice of the 2-D dataset
46- arr2( & [
47- [ Pixel { xy: ( 3 , 4 ) , color: Color :: G } , Pixel { xy: ( 4 , 5 ) , color: Color :: R } ] ,
48- [ Pixel { xy: ( 5 , 6 ) , color: Color :: B } , Pixel { xy: ( 6 , 7 ) , color: Color :: G } ] ,
49- ] )
50- ) ;
51- let attr = ds. attr ( "colors" ) ?; // open the attribute
52- assert_eq ! ( attr. read_1d:: <Color >( ) ?. as_slice( ) . unwrap( ) , & [ Color :: R , Color :: G , Color :: B ] ) ;
21+ impl Pixel {
22+ pub fn new ( x : i64 , y : i64 , color : Color ) -> Self {
23+ Self { xy : ( x, y) , color }
5324 }
25+ }
26+
27+ fn write_hdf5 ( ) -> Result < ( ) > {
28+ use Color :: * ;
29+ let file = File :: create ( "pixels.h5" ) ?; // open for writing
30+ let group = file. create_group ( "dir" ) ?; // create a group
31+ #[ cfg( feature = "blosc" ) ]
32+ blosc_set_nthreads ( 2 ) ; // set number of blosc threads
33+ let builder = group. new_dataset_builder ( ) ;
34+ #[ cfg( feature = "blosc" ) ]
35+ let builder = builder. blosc_zstd ( 9 , true ) ; // zstd + shuffle
36+ let ds = builder
37+ . with_data ( & arr2 ( & [
38+ // write a 2-D array of data
39+ [ Pixel :: new ( 1 , 2 , R ) , Pixel :: new ( 2 , 3 , B ) ] ,
40+ [ Pixel :: new ( 3 , 4 , G ) , Pixel :: new ( 4 , 5 , R ) ] ,
41+ [ Pixel :: new ( 5 , 6 , B ) , Pixel :: new ( 6 , 7 , G ) ] ,
42+ ] ) )
43+ // finalize and write the dataset
44+ . create ( "pixels" ) ?;
45+ // create an attr with fixed shape but don't write the data
46+ let attr = ds. new_attr :: < Color > ( ) . shape ( [ 3 ] ) . create ( "colors" ) ?;
47+ // write the attr data
48+ attr. write ( & [ R , G , B ] ) ?;
49+ Ok ( ( ) )
50+ }
51+
52+ fn read_hdf5 ( ) -> Result < ( ) > {
53+ use Color :: * ;
54+ let file = File :: open ( "pixels.h5" ) ?; // open for reading
55+ let ds = file. dataset ( "dir/pixels" ) ?; // open the dataset
56+ assert_eq ! (
57+ // read a slice of the 2-D dataset and verify it
58+ ds. read_slice:: <Pixel , _, _>( s![ 1 .., ..] ) ?,
59+ arr2( & [
60+ [ Pixel :: new( 3 , 4 , G ) , Pixel :: new( 4 , 5 , R ) ] ,
61+ [ Pixel :: new( 5 , 6 , B ) , Pixel :: new( 6 , 7 , G ) ] ,
62+ ] )
63+ ) ;
64+ let attr = ds. attr ( "colors" ) ?; // open the attribute
65+ assert_eq ! ( attr. read_1d:: <Color >( ) ?. as_slice( ) . unwrap( ) , & [ R , G , B ] ) ;
66+ Ok ( ( ) )
67+ }
68+
69+ fn main ( ) -> Result < ( ) > {
70+ write_hdf5 ( ) ?;
71+ read_hdf5 ( ) ?;
5472 Ok ( ( ) )
5573}
0 commit comments