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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::path::{Path, PathBuf};
use hdf5::File;
use super::dataset::DatasetD;
use crate::reader::{Reader, Streamer};
#[derive(Debug, Serialize, Deserialize)]
pub struct Index<'a> {
path: Option<PathBuf>,
#[serde(borrow)]
datasets: HashMap<String, DatasetD<'a>>,
}
impl TryFrom<&Path> for Index<'_> {
type Error = anyhow::Error;
fn try_from(p: &Path) -> Result<Index<'static>, anyhow::Error> {
Index::index(p)
}
}
impl TryFrom<&hdf5::File> for Index<'_> {
type Error = anyhow::Error;
fn try_from(f: &hdf5::File) -> Result<Index<'static>, anyhow::Error> {
Index::index_file::<&str>(f, None)
}
}
impl Index<'_> {
pub fn index<P>(path: P) -> Result<Index<'static>, anyhow::Error>
where
P: AsRef<Path>,
{
let path = path.as_ref();
let hf = File::open(path)?;
Index::index_file(&hf, Some(path))
}
pub fn index_file<P>(hf: &hdf5::File, path: Option<P>) -> Result<Index<'static>, anyhow::Error>
where
P: Into<PathBuf>,
{
let datasets = hf
.group("/")?
.member_names()?
.iter()
.map(|m| hf.dataset(m).map(|d| (m, d)))
.filter_map(Result::ok)
.filter(|(_, d)| d.is_chunked() || d.offset().is_some())
.map(|(m, d)| DatasetD::index(&d).map(|d| (m.clone(), d)))
.collect::<Result<HashMap<String, DatasetD<'static>>, _>>()?;
Ok(Index {
path: path.map(|p| p.into()),
datasets,
})
}
#[must_use]
pub fn dataset(&self, s: &str) -> Option<&DatasetD> {
self.datasets.get(s)
}
pub fn datasets(&self) -> &HashMap<String, DatasetD> {
&self.datasets
}
#[must_use]
pub fn path(&self) -> Option<&Path> {
self.path.as_ref().map(|p| p.as_ref())
}
pub fn reader(&self, ds: &str) -> Result<Box<dyn Reader + '_>, anyhow::Error> {
let path = self.path().ok_or_else(|| anyhow!("missing path"))?;
match self.dataset(ds) {
Some(ds) => ds.as_reader(&path),
None => Err(anyhow!("dataset does not exist")),
}
}
pub fn streamer(&self, ds: &str) -> Result<Box<dyn Streamer + '_>, anyhow::Error> {
let path = self.path().ok_or_else(|| anyhow!("missing path"))?;
match self.dataset(ds) {
Some(ds) => ds.as_streamer(&path),
None => Err(anyhow!("dataset does not exist")),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn index_t_float32() {
let i = Index::index("tests/data/dmrpp/t_float.h5").unwrap();
println!("index: {:#?}", i);
}
#[test]
fn chunked_1d() {
let i = Index::index("tests/data/dmrpp/chunked_oneD.h5").unwrap();
println!("index: {:#?}", i);
}
#[test]
fn chunked_2d() {
let i = Index::index("tests/data/dmrpp/chunked_twoD.h5").unwrap();
println!("index: {:#?}", i);
}
#[ignore]
#[test]
fn index_meps() {
println!("indexing meps");
test::black_box(Index::index("tests/data/meps_det_vc_2_5km_latest.nc").unwrap());
}
#[test]
fn serialize() {
use flexbuffers::FlexbufferSerializer as ser;
let i = Index::index("tests/data/dmrpp/chunked_oneD.h5").unwrap();
println!("Original index: {:#?}", i);
println!("serialize");
let mut s = ser::new();
i.serialize(&mut s).unwrap();
println!("deserialize");
let r = flexbuffers::Reader::get_root(s.view()).unwrap();
let mi = Index::deserialize(r).unwrap();
println!("Deserialized Index: {:#?}", mi);
let s = bincode::serialize(&i).unwrap();
bincode::deserialize::<Index>(&s).unwrap();
}
}