1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
mod chunk;
mod dataset;
mod index;
pub mod serde;

#[cfg(feature = "fast-index")]
mod chunks_iter;

pub use chunk::{Chunk, ULE};
pub use dataset::{Dataset, DatasetD, Datatype};
pub use index::Index;

/// Convenience trait for returning an index for an existing HDF5 file or dataset opened with
/// the standard rust HDF5 library.
///
/// ```
/// use hidefix::prelude::*;
///
/// let hf = hdf5::File::open("tests/data/coads_climatology.nc4").unwrap();
/// let idx = hf.index().unwrap();
///
/// let mut r = idx.reader("SST").unwrap();
/// let values = r.values::<f32>(None, None).unwrap();
///
/// println!("SST: {:?}", values);
/// ```
pub trait IntoIndex {
    type Indexed;

    fn index(&self) -> anyhow::Result<Self::Indexed>;
}

impl IntoIndex for hdf5::File {
    type Indexed = Index<'static>;

    fn index(&self) -> anyhow::Result<Self::Indexed> {
        use std::convert::TryInto;

        self.try_into()
    }
}

impl IntoIndex for hdf5::Dataset {
    type Indexed = DatasetD<'static>;

    fn index(&self) -> anyhow::Result<Self::Indexed> {
        // TODO: use H5get_name to get file name (not really any spot to store the name)
        DatasetD::index(self)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn into_idx_file() {
        let hf = hdf5::File::open("tests/data/dmrpp/chunked_twoD.h5").unwrap();
        let i = hf.index().unwrap();
        println!("index: {:#?}", i);
    }

    #[test]
    fn into_idx_dataset() {
        let hf = hdf5::File::open("tests/data/coads_climatology.nc4").unwrap();
        let ds = hf.dataset("SST").unwrap();
        let i = ds.index().unwrap();
        println!("dataset index: {:#?}", i);
    }
}