aldanor / hdf5-rust

HDF5 for Rust
https://docs.rs/hdf5
Apache License 2.0
310 stars 85 forks source link

stuck reading hdf5 3D f32 points #171

Closed DevinBayly closed 3 years ago

DevinBayly commented 3 years ago

I'm guessing this is almost as simple as the material in the documentation but I can't get the "no conversion path" error to go away. My hdf5 files are in this format image and each one is formatted in this shape image

given this info can anyone say why this rust code fails to read into a 2d array?

#[derive(hdf5::H5Type, Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Point {
    x:f32,
    y:f32,
    z:f32
}

fn main() {
    let hfile = hdf5::File::open("./positions_chunk1.hdf5").unwrap();
    let names = hfile.member_names().unwrap();
    let data = hfile.dataset(&names[0]).unwrap();
    let data_array = data.read_2d::<Point>().unwrap();
}
aldanor commented 3 years ago
DevinBayly commented 3 years ago

Thanks for providing some extra info, I think I understand why the conversion error arises now. So is there a better way to read a dataset when my data is 2-D collection of f32? I'm not tied to using the Point struct, that was just my first guess when looking at the examples on crates.io.

Would a read_raw::<f32> be a better approach?

Sorry to ask you to spell things out for me.

aldanor commented 3 years ago

Well, yea - you can read_2d::<f32>(), that should work.

If you really want an x/y/z struct, you can probably pull the data out of ndarray as a vec and unsafely transmute it into a vec of points. This is something that is technically doable at the library level but I guess there haven't been enough use cases (or contributors) to implement it.

DevinBayly commented 3 years ago

oh wow nvm, just using


    let mut f = hdf5::File::open("./positions_chunk0.hdf5").unwrap();
    let mut dataset = f.dataset("snapshot_000").unwrap();
    let data = dataset.read_2d::<f32>().unwrap();
    println!("data read is {:?}",data);

did the trick. Thanks for the package!