dotthz 0.3.0

Crate to open and write dotThz files in rust.
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use hdf5::file::{FileAccess, FileCreate};
use hdf5::types::VarLenUnicode;
use hdf5::{Dataset, File, Group, H5Type, OpenMode};
use indexmap::IndexMap;
use ndarray::ArrayView;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::str::FromStr;

/// Metadata associated with a dotThz measurement.
#[derive(Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DotthzMetaData {
    /// The user responsible for the measurement.
    pub user: String,

    /// The email of the user.
    pub email: String,

    /// The ORCID identifier for the user.
    pub orcid: String,

    /// The institution of the user.
    pub institution: String,

    /// The description of the measurement.
    pub description: String,

    /// Additional metadata stored as key-value pairs.
    pub md: IndexMap<String, String>,

    /// dsDescription stored as key-value pairs.
    pub ds_description: Vec<String>,

    /// dotThz version
    pub version: String,

    /// The mode of measurement.
    pub mode: String,

    /// The instrument used for measurement.
    pub instrument: String,

    /// The time of measurement.
    pub time: String,

    /// The date of measurement.
    pub date: String,
}

/// A structure representing a .thz file according to the dotThz standard
pub struct DotthzFile {
    /// contains the Group and Dataset names
    file: File, // Keep a reference to the underlying HDF5 file
}

impl DotthzFile {
    /// Create an empty `DotthzFile` to the specified path, truncates if exists.
    pub fn create(path: &PathBuf) -> Result<Self, Box<dyn Error>> {
        // Create a new HDF5 file at the specified path
        let file = File::create(path)?;
        Ok(Self { file })
    }

    /// Loads a `DotthzFile` from the specified path as read-only, file must exist.
    pub fn open(filename: &PathBuf) -> Result<Self, Box<dyn Error>> {
        let file = File::open(filename)?;
        Ok(DotthzFile { file })
    }

    /// Opens a file as read/write, file must exist.
    pub fn open_rw<P: AsRef<Path>>(filename: P) -> Result<Self, Box<dyn Error>> {
        let file = File::open_rw(filename)?;
        Ok(DotthzFile { file })
    }

    /// Creates a file, fails if exists.
    pub fn create_excl<P: AsRef<Path>>(filename: P) -> Result<Self, Box<dyn Error>> {
        let file = File::create_excl(filename)?;
        Ok(DotthzFile { file })
    }

    /// Opens a file as read/write if exists, creates otherwise.
    pub fn append<P: AsRef<Path>>(filename: P) -> Result<Self, Box<dyn Error>> {
        let file = File::append(filename)?;
        Ok(DotthzFile { file })
    }

    /// Opens a file in a given mode.
    pub fn open_as<P: AsRef<Path>>(filename: P, mode: OpenMode) -> Result<Self, Box<dyn Error>> {
        let file = File::open_as(filename, mode)?;
        Ok(DotthzFile { file })
    }

    /// Returns the file size in bytes (or 0 if the file handle is invalid).
    pub fn size(&self) -> u64 {
        self.file.size()
    }

    /// Returns the free space in the file in bytes (or 0 if the file handle is invalid).
    pub fn free_space(&self) -> u64 {
        self.file.free_space()
    }

    /// Returns true if the file was opened in a read-only mode.
    pub fn is_read_only(&self) -> bool {
        self.file.is_read_only()
    }

    /// Returns the userblock size in bytes (or 0 if the file handle is invalid).
    pub fn userblock(&self) -> u64 {
        self.file.userblock()
    }

    /// Flushes the file to the storage medium.
    pub fn flush(&self) -> Result<(), Box<dyn Error>> {
        self.file.flush()?;
        Ok(())
    }

    /// Closes the file and invalidates all open handles for contained objects.
    pub fn close(self) -> Result<(), Box<dyn Error>> {
        self.file.close()?;
        Ok(())
    }

    /// Returns a copy of the file access property list.
    pub fn access_plist(&self) -> hdf5::Result<FileAccess> {
        self.file.access_plist()
    }

    /// A short alias for `access_plist()`.
    pub fn fapl(&self) -> hdf5::Result<FileAccess> {
        self.file.access_plist()
    }
    /// Returns a copy of the file creation property list.
    pub fn create_plist(&self) -> hdf5::Result<FileCreate> {
        self.file.create_plist()
    }

    /// A short alias for `create_plist()`.
    pub fn fcpl(&self) -> hdf5::Result<FileCreate> {
        self.file.create_plist()
    }
    /// get group names
    pub fn get_group_names(&self) -> hdf5::Result<Vec<String>> {
        Ok(self
            .file
            .groups()?
            .iter()
            .map(|s| s.name())
            .collect::<Vec<String>>())
    }

    /// get group by name
    pub fn get_group(&self, group_name: &str) -> hdf5::Result<Group> {
        self.file.group(group_name)
    }

    /// get groups
    pub fn get_groups(&self) -> hdf5::Result<Vec<Group>> {
        self.file.groups()
    }

    /// get dataset names for a given group name
    pub fn get_dataset_names(&self, group_name: &str) -> hdf5::Result<Vec<String>> {
        Ok(self
            .file
            .group(group_name)?
            .datasets()?
            .iter()
            .map(|d| d.name())
            .collect::<Vec<String>>())
    }

    /// get dataset for a given group name by dataset name
    pub fn get_dataset(&self, group_name: &str, dataset_name: &str) -> hdf5::Result<Dataset> {
        self.file.group(group_name)?.dataset(dataset_name)
    }

    /// get datasets for a given group name
    pub fn get_datasets(&self, group_name: &str) -> hdf5::Result<Vec<Dataset>> {
        self.file.group(group_name)?.datasets()
    }

    /// clear the meta-data for a given group
    pub fn clear_meta_data(&self, group_name: &str) -> hdf5::Result<()> {
        // delete all existing attributes
        if let Ok(names) = self.get_group(group_name)?.attr_names() {
            for attr in names {
                self.get_group(group_name)?.delete_attr(&attr)?;
            }
        }
        Ok(())
    }

    /// set meta-data for a given group
    pub fn set_meta_data(
        &self,
        group: &mut Group,
        meta_data: &DotthzMetaData,
    ) -> Result<(), Box<dyn Error>> {
        // Save metadata attributes, create if they don't already exist
        if let Ok(attr) = group.attr("description") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.description)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("description")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.description)?)?;
        }

        if let Ok(attr) = group.attr("date") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.date)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("date")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.date)?)?;
        }

        if let Ok(attr) = group.attr("instrument") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.instrument)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("instrument")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.instrument)?)?;
        }

        if let Ok(attr) = group.attr("mode") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.mode)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("mode")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.mode)?)?;
        }

        if let Ok(attr) = group.attr("thzVer") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.version)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("thzVer")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.version)?)?;
        }

        if let Ok(attr) = group.attr("time") {
            attr.write_scalar(&VarLenUnicode::from_str(&meta_data.time)?)?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("time")?
                .write_scalar(&VarLenUnicode::from_str(&meta_data.time)?)?;
        }

        let entry = VarLenUnicode::from_str(
            format!(
                "{}/{}/{}/{}",
                meta_data.orcid, meta_data.user, meta_data.email, meta_data.institution
            )
            .as_str(),
        )
        .unwrap();

        if let Ok(attr) = group.attr("user") {
            attr.write_scalar(&entry)?;
        } else {
            let attr = group.new_attr::<VarLenUnicode>().create("user")?;
            attr.write_scalar(&entry)?;
        }

        // Save additional metadata
        let md_descriptions = meta_data
            .md
            .keys()
            .cloned()
            .collect::<Vec<String>>()
            .join(", ");

        if let Ok(attr) = group.attr("mdDescription") {
            attr.write_raw(&[VarLenUnicode::from_str(&md_descriptions)?])?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("mdDescription")?
                .write_raw(&[VarLenUnicode::from_str(&md_descriptions)?])?;
        }

        for (i, (_key, value)) in meta_data.md.iter().enumerate() {
            if let Ok(attr) = group.attr(format!("md{}", i + 1).as_str()) {
                if let Ok(parsed) = value.parse::<f32>() {
                    attr.write_scalar(&parsed)?;
                } else {
                    attr.write_scalar(&VarLenUnicode::from_str(value)?)?;
                }
            } else {
                group
                    .new_attr::<VarLenUnicode>()
                    .create(format!("md{}", i + 1).as_str())?
                    .write_scalar(&VarLenUnicode::from_str(value)?)?;
            }
        }

        // Save dsDescription
        let ds_descriptions = meta_data.ds_description.join(", ");

        if let Ok(attr) = group.attr("dsDescription") {
            attr.write_raw(&[VarLenUnicode::from_str(&ds_descriptions)?])?;
        } else {
            group
                .new_attr::<VarLenUnicode>()
                .create("dsDescription")?
                .write_raw(&[VarLenUnicode::from_str(&ds_descriptions)?])?;
        }
        Ok(())
    }

    /// extract meta-data for a given group by group name
    pub fn get_meta_data(&self, group_name: &str) -> hdf5::Result<DotthzMetaData> {
        let mut meta_data = DotthzMetaData::default();

        if let Ok(instrument) = self
            .file
            .group(group_name)?
            .attr("instrument")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = instrument.first() {
                meta_data.instrument = d.to_string();
            }
        }

        // Load dataset descriptions
        if let Ok(ds_description) = self
            .file
            .group(group_name)?
            .attr("dsDescription")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            let descriptions: Vec<String> = ds_description
                .iter()
                .flat_map(|s| s.split(", ").map(String::from).collect::<Vec<String>>())
                .collect();
            meta_data.ds_description = descriptions;
        }

        if let Ok(md_description) = self
            .file
            .group(group_name)?
            .attr("mdDescription")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            // Convert ds_description to a vector of strings, splitting any single entry by ", "
            let descriptions: Vec<String> = if md_description.len() == 1 {
                // If there's only one entry, split it by ", "
                md_description[0]
                    .split(", ")
                    .map(|s| s.to_string())
                    .collect()
            } else {
                // Otherwise, assume it's already in the correct format
                md_description.iter().map(|s| s.to_string()).collect()
            };

            for (i, description) in descriptions.iter().enumerate() {
                // now read the mds
                if let Ok(md) = self
                    .file
                    .group(group_name)?
                    .attr(format!("md{}", i + 1).as_str())
                    .and_then(|a| a.read_raw::<f32>())
                {
                    if let Some(md) = md.first() {
                        meta_data
                            .md
                            .insert(description.to_string(), format!("{}", md));
                    }
                }
                if let Ok(md) = self
                    .file
                    .group(group_name)?
                    .attr(format!("md{}", i + 1).as_str())
                    .and_then(|a| a.read_raw::<VarLenUnicode>())
                {
                    if let Some(md) = md.first() {
                        meta_data
                            .md
                            .insert(description.to_string(), format!("{}", md));
                    }
                }
            }
        }

        if let Ok(mode) = self
            .file
            .group(group_name)?
            .attr("mode")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = mode.first() {
                meta_data.mode = d.to_string();
            }
        }

        if let Ok(version) = self
            .file
            .group(group_name)?
            .attr("thzVer")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = version.first() {
                meta_data.version = d.to_string();
            }
        }

        if let Ok(description) = self
            .file
            .group(group_name)?
            .attr("description")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = description.first() {
                meta_data.description = d.to_string();
            }
        }

        if let Ok(time) = self
            .file
            .group(group_name)?
            .attr("time")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = time.first() {
                meta_data.time = d.to_string();
            }
        }

        if let Ok(date) = self
            .file
            .group(group_name)?
            .attr("date")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = date.first() {
                meta_data.date = d.to_string();
            }
        }

        if let Ok(user_info) = self
            .file
            .group(group_name)?
            .attr("user")
            .and_then(|a| a.read_raw::<VarLenUnicode>())
        {
            if let Some(d) = user_info.first() {
                let user_info_str = d.to_string();
                let user_parts: Vec<&str> = user_info_str.split('/').collect();

                // Check each part individually to handle cases where fewer than 4 parts are available
                if let Some(part) = user_parts.first() {
                    meta_data.orcid = part.trim().into();
                }
                if let Some(part) = user_parts.get(1) {
                    meta_data.user = part.trim().into();
                }
                if let Some(part) = user_parts.get(2) {
                    meta_data.email = part.trim().into();
                }
                if let Some(part) = user_parts.get(3) {
                    meta_data.institution = part.trim().into();
                }
            }
        }
        Ok(meta_data)
    }

    /// remove a meta_data attribute
    pub fn remove_meta_data_attribute(
        &mut self,
        group_name: &str,
        attr_name: &str,
    ) -> hdf5::Result<()> {
        self.file.group(group_name)?.delete_attr(attr_name)
    }

    /// Add a group with meta-data and group name to the `DotthzFile`.
    pub fn add_group(
        &mut self,
        group_name: &str,
        metadata: &DotthzMetaData,
    ) -> Result<Group, Box<dyn Error>> {
        let mut group = self.file.create_group(group_name)?;
        self.set_meta_data(&mut group, metadata)?;
        Ok(group)
    }

    /// Add a dataset to a given group by group name and dataset name.
    pub fn add_dataset<T, D>(
        &mut self,
        group_name: &str,
        dataset_name: &str,
        dataset: ArrayView<'_, T, D>,
    ) -> Result<(), Box<dyn Error>>
    where
        T: H5Type + Debug,
        D: ndarray::Dimension, // Ensure dimensions are compatible with HDF5
    {
        // Retrieve or create the group
        let group = self.file.group(group_name)?;
        // Create the dataset in the specified group with the shape from the ndarray
        let ds = group
            .new_dataset::<T>()
            .shape(dataset.shape())
            .create(dataset_name)?;

        // Write the data into the dataset
        ds.write(dataset)?;

        Ok(())
    }
}