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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use std::fmt::{self, Debug};
use std::mem;
use std::ops::Deref;

use ndarray::{Array, Array1, Array2, ArrayD, ArrayView, ArrayView1};
use ndarray::{SliceInfo, SliceOrIndex};

use hdf5_sys::h5a::{H5Aget_space, H5Aget_storage_size, H5Aget_type, H5Aread, H5Awrite};
use hdf5_sys::h5d::{H5Dget_space, H5Dget_storage_size, H5Dget_type, H5Dread, H5Dwrite};
use hdf5_sys::h5p::H5Pcreate;

use crate::internal_prelude::*;

#[derive(Debug)]
pub struct Reader<'a> {
    obj: &'a Container,
    conv: Conversion,
}

impl<'a> Reader<'a> {
    /// Creates a reader for a dataset/attribute.
    ///
    /// Any conversions (including hard/soft) are allowed by default.
    pub fn new(obj: &'a Container) -> Self {
        Self { obj, conv: Conversion::Soft }
    }

    /// Set maximum allowed conversion level.
    pub fn conversion(mut self, conv: Conversion) -> Self {
        self.conv = conv;
        self
    }

    /// Disallow all conversions.
    pub fn no_convert(mut self) -> Self {
        self.conv = Conversion::NoOp;
        self
    }

    fn read_into_buf<T: H5Type>(
        &self, buf: *mut T, fspace: Option<&Dataspace>, mspace: Option<&Dataspace>,
    ) -> Result<()> {
        let file_dtype = self.obj.dtype()?;
        let mem_dtype = Datatype::from_type::<T>()?;
        file_dtype.ensure_convertible(&mem_dtype, self.conv)?;
        let (obj_id, tp_id) = (self.obj.id(), mem_dtype.id());

        if self.obj.is_attr() {
            h5try!(H5Aread(obj_id, tp_id, buf as *mut _));
        } else {
            let fspace_id = fspace.map_or(H5S_ALL, |f| f.id());
            let mspace_id = mspace.map_or(H5S_ALL, |m| m.id());
            let xfer =
                PropertyList::from_id(h5call!(H5Pcreate(*crate::globals::H5P_DATASET_XFER))?)?;
            crate::hl::plist::set_vlen_manager_libc(xfer.id())?;
            h5try!(H5Dread(obj_id, tp_id, mspace_id, fspace_id, xfer.id(), buf as *mut _));
        }
        Ok(())
    }

    /// Reads a slice of an n-dimensional array.
    /// If the dimensionality `D` has a fixed number of dimensions, it must match the dimensionality of
    /// the slice, after singleton dimensions are dropped.
    /// Use the multi-dimensional slice macro `s![]` from `ndarray` to conveniently create
    /// a multidimensional slice.
    pub fn read_slice<T, S, D>(&self, slice: &SliceInfo<S, D>) -> Result<Array<T, D>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
        D: ndarray::Dimension,
    {
        ensure!(!self.obj.is_attr(), "slicing cannot be used on attribute datasets");

        let shape = self.obj.get_shape()?;

        let slice_s: &[SliceOrIndex] = slice.as_ref();
        let slice_dim = slice_s.len();
        if shape.ndim() != slice_dim {
            let obj_ndim = shape.ndim();
            ensure!(
                obj_ndim == slice_dim,
                "slice dimension mismatch: dataset has {} dims, slice has {} dims",
                obj_ndim,
                slice_dim
            );
        }

        if shape.ndim() == 0 {
            // Check that return dimensionality is 0.
            if let Some(ndim) = D::NDIM {
                let obj_ndim = 0;
                ensure!(
                    obj_ndim == ndim,
                    "ndim mismatch: slice outputs dims {}, output type dims {}",
                    obj_ndim,
                    ndim
                );
            }

            // Fall back to a simple read for the scalar case
            // Slicing has no effect
            self.read()
        } else {
            let fspace = self.obj.space()?;
            let out_shape = fspace.select_slice(slice)?;

            // Remove dimensions from out_shape that were SliceOrIndex::Index in the slice
            let reduced_shape: Vec<_> = slice_s
                .iter()
                .zip(out_shape.iter().cloned())
                .filter_map(|(slc, sz)| match slc {
                    SliceOrIndex::Index(_) => None,
                    _ => Some(sz),
                })
                .collect();

            // *Output* dimensionality must match the reduced shape,
            // (i.e. dimensionality after singleton 'SliceOrIndex::Index'
            // axes are dropped.
            if let Some(ndim) = D::NDIM {
                let obj_ndim = reduced_shape.len();
                ensure!(
                    obj_ndim == ndim,
                    "ndim mismatch: slice outputs dims {}, output type dims {}",
                    obj_ndim,
                    ndim
                );
            }

            let mspace = Dataspace::try_new(&out_shape, false)?;
            let size = out_shape.iter().product();
            let mut vec = Vec::with_capacity(size);

            self.read_into_buf(vec.as_mut_ptr(), Some(&fspace), Some(&mspace))?;
            unsafe {
                vec.set_len(size);
            }

            let arr = ArrayD::from_shape_vec(reduced_shape, vec)?;
            Ok(arr.into_dimensionality()?)
        }
    }

    /// Reads a dataset/attribute into an n-dimensional array.
    ///
    /// If the array has a fixed number of dimensions, it must match the dimensionality
    /// of the dataset/attribute.
    pub fn read<T: H5Type, D: ndarray::Dimension>(&self) -> Result<Array<T, D>> {
        let shape = self.obj.get_shape()?;
        if let Some(ndim) = D::NDIM {
            let obj_ndim = shape.ndim();
            ensure!(obj_ndim == ndim, "ndim mismatch: expected {}, got {}", ndim, obj_ndim);
        }
        let vec = self.read_raw()?;
        let arr = ArrayD::from_shape_vec(shape, vec)?;
        Ok(arr.into_dimensionality()?)
    }

    /// Reads a dataset/attribute into a vector in memory order.
    pub fn read_raw<T: H5Type>(&self) -> Result<Vec<T>> {
        let size = self.obj.space()?.size();
        let mut vec = Vec::with_capacity(size);
        self.read_into_buf(vec.as_mut_ptr(), None, None).map(|_| {
            unsafe {
                vec.set_len(size);
            };
            vec
        })
    }

    /// Reads a dataset/attribute into a 1-dimensional array.
    ///
    /// The dataset/attribute must be 1-dimensional.
    pub fn read_1d<T: H5Type>(&self) -> Result<Array1<T>> {
        self.read()
    }

    /// Reads the given `slice` of the dataset into a 1-dimensional array.
    /// The slice must yield a 1-dimensional result.
    pub fn read_slice_1d<T, S>(&self, slice: &SliceInfo<S, ndarray::Ix1>) -> Result<Array1<T>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
    {
        self.read_slice(slice)
    }

    /// Reads a dataset/attribute into a 2-dimensional array.
    ///
    /// The dataset/attribute must be 2-dimensional.
    pub fn read_2d<T: H5Type>(&self) -> Result<Array2<T>> {
        self.read()
    }

    /// Reads the given `slice` of the dataset into a 2-dimensional array.
    /// The slice must yield a 2-dimensional result.
    pub fn read_slice_2d<T, S>(&self, slice: &SliceInfo<S, ndarray::Ix2>) -> Result<Array2<T>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
    {
        self.read_slice(slice)
    }

    /// Reads a dataset/attribute into an array with dynamic number of dimensions.
    pub fn read_dyn<T: H5Type>(&self) -> Result<ArrayD<T>> {
        self.read()
    }

    /// Reads a scalar dataset/attribute.
    pub fn read_scalar<T: H5Type>(&self) -> Result<T> {
        let obj_ndim = self.obj.get_shape()?.ndim();
        ensure!(obj_ndim == 0, "ndim mismatch: expected scalar, got {}", obj_ndim);
        let mut val = mem::MaybeUninit::<T>::uninit();
        self.read_into_buf(val.as_mut_ptr(), None, None).map(|_| unsafe { val.assume_init() })
    }
}

#[derive(Debug)]
pub struct Writer<'a> {
    obj: &'a Container,
    conv: Conversion,
}

impl<'a> Writer<'a> {
    /// Creates a writer for a dataset/attribute.
    ///
    /// Any conversions (including hard/soft) are allowed by default.
    pub fn new(obj: &'a Container) -> Self {
        Self { obj, conv: Conversion::Soft }
    }

    /// Set maximum allowed conversion level.
    pub fn conversion(mut self, conv: Conversion) -> Self {
        self.conv = conv;
        self
    }

    /// Disallow all conversions.
    pub fn no_convert(mut self) -> Self {
        self.conv = Conversion::NoOp;
        self
    }

    fn write_from_buf<T: H5Type>(
        &self, buf: *const T, fspace: Option<&Dataspace>, mspace: Option<&Dataspace>,
    ) -> Result<()> {
        let file_dtype = self.obj.dtype()?;
        let mem_dtype = Datatype::from_type::<T>()?;
        mem_dtype.ensure_convertible(&file_dtype, self.conv)?;
        let (obj_id, tp_id) = (self.obj.id(), mem_dtype.id());

        if self.obj.is_attr() {
            h5try!(H5Awrite(obj_id, tp_id, buf as *const _));
        } else {
            let fspace_id = fspace.map_or(H5S_ALL, |f| f.id());
            let mspace_id = mspace.map_or(H5S_ALL, |m| m.id());
            h5try!(H5Dwrite(obj_id, tp_id, mspace_id, fspace_id, H5P_DEFAULT, buf as *const _));
        }
        Ok(())
    }

    /// Writes all data from the array `arr` into the given `slice` of the target dataset.
    /// The shape of `arr` must match the shape the set of elements included in the slice.
    /// If the array has a fixed number of dimensions, it must match the dimensionality of
    /// dataset. Use the multi-dimensional slice macro `s![]` from `ndarray` to conveniently create
    /// a multidimensional slice.
    pub fn write_slice<'b, A, T, S, D>(&self, arr: A, slice: &SliceInfo<S, D>) -> Result<()>
    where
        A: Into<ArrayView<'b, T, D>>,
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
        D: ndarray::Dimension,
    {
        ensure!(!self.obj.is_attr(), "slicing cannot be used on attribute datasets");

        let shape = self.obj.get_shape()?;
        let slice_s: &[SliceOrIndex] = slice.as_ref();
        let slice_dim = slice_s.len();
        if shape.ndim() != slice_dim {
            let obj_ndim = shape.ndim();
            ensure!(
                obj_ndim == slice_dim,
                "slice dimension mismatch: dataset has {} dims, slice has {} dims",
                obj_ndim,
                slice_dim
            );
        }

        if shape.ndim() == 0 {
            // Fall back to a simple read for the scalar case
            // Slicing has no effect
            self.write(arr)
        } else {
            let fspace = self.obj.space()?;
            let slice_shape = fspace.select_slice(slice)?;

            let view = arr.into();
            let data_shape = view.shape();

            // Restore dimensions that are SliceOrIndex::Index in the slice.
            let mut data_shape_hydrated = Vec::new();
            let mut pos = 0;
            for s in slice_s {
                if let SliceOrIndex::Index(_) = s {
                    data_shape_hydrated.push(1);
                } else {
                    data_shape_hydrated.push(data_shape[pos]);
                    pos += 1;
                }
            }

            let mspace = Dataspace::try_new(&slice_shape, false)?;

            // FIXME - we can handle non-standard input arrays by creating a memory space
            // that reflects the same slicing/ordering that this ArrayView represents.
            // we could also convert the array into a standard layout, but this is probably expensive.
            ensure!(
                view.is_standard_layout(),
                "input array is not in standard layout or is not contiguous"
            );

            if slice_shape != data_shape_hydrated {
                fail!(
                    "shape mismatch when writing slice: memory = {:?}, destination = {:?}",
                    data_shape_hydrated,
                    slice_shape
                );
            }

            self.write_from_buf(view.as_ptr(), Some(&fspace), Some(&mspace))
        }
    }

    /// Writes an n-dimensional array view into a dataset/attribute.
    ///
    /// The shape of the view must match the shape of the dataset/attribute exactly.
    /// The input argument must be convertible to an array view (this includes slices).
    pub fn write<'b, A, T, D>(&self, arr: A) -> Result<()>
    where
        A: Into<ArrayView<'b, T, D>>,
        T: H5Type,
        D: ndarray::Dimension,
    {
        let view = arr.into();
        ensure!(
            view.is_standard_layout(),
            "input array is not in standard layout or is not contiguous"
        );

        let src = view.shape();
        let dst = &*self.obj.get_shape()?;
        if src != dst {
            fail!("shape mismatch when writing: memory = {:?}, destination = {:?}", src, dst);
        }

        self.write_from_buf(view.as_ptr(), None, None)
    }

    /// Writes a 1-dimensional array view into a dataset/attribute in memory order.
    ///
    /// The number of elements in the view must match the number of elements in the
    /// destination dataset/attribute. The input argument must be convertible to a
    /// 1-dimensional array view (this includes slices).
    pub fn write_raw<'b, A, T>(&self, arr: A) -> Result<()>
    where
        A: Into<ArrayView1<'b, T>>,
        T: H5Type,
    {
        let view = arr.into();
        ensure!(
            view.is_standard_layout(),
            "input array is not in standard layout or is not contiguous"
        );

        let src = view.len();
        let dst = self.obj.get_shape()?.size();
        if src != dst {
            fail!("length mismatch when writing: memory = {:?}, destination = {:?}", src, dst);
        }
        self.write_from_buf(view.as_ptr(), None, None)
    }

    /// Writes a scalar dataset/attribute.
    pub fn write_scalar<T: H5Type>(&self, val: &T) -> Result<()> {
        let ndim = self.obj.get_shape()?.ndim();
        ensure!(ndim == 0, "ndim mismatch: expected scalar, got {}", ndim);
        self.write_from_buf(val as *const _, None, None)
    }
}

#[repr(transparent)]
#[derive(Clone)]
pub struct Container(Handle);

impl ObjectClass for Container {
    const NAME: &'static str = "container";
    const VALID_TYPES: &'static [H5I_type_t] = &[H5I_DATASET, H5I_ATTR];

    fn from_handle(handle: Handle) -> Self {
        Self(handle)
    }

    fn handle(&self) -> &Handle {
        &self.0
    }
}

impl Debug for Container {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.debug_fmt(f)
    }
}

impl Deref for Container {
    type Target = Location;

    fn deref(&self) -> &Location {
        unsafe { self.transmute() }
    }
}

impl Container {
    pub(crate) fn is_attr(&self) -> bool {
        get_id_type(self.id()) == H5I_ATTR
    }

    /// Creates a reader wrapper for this dataset/attribute, allowing to
    /// set custom type conversion options when reading.
    pub fn as_reader(&self) -> Reader {
        Reader::new(self)
    }

    /// Creates a writer wrapper for this dataset/attribute, allowing to
    /// set custom type conversion options when writing.
    pub fn as_writer(&self) -> Writer {
        Writer::new(self)
    }

    /// Returns the datatype of the dataset/attribute.
    pub fn dtype(&self) -> Result<Datatype> {
        if self.is_attr() {
            Datatype::from_id(h5try!(H5Aget_type(self.id())))
        } else {
            Datatype::from_id(h5try!(H5Dget_type(self.id())))
        }
    }

    /// Returns the dataspace of the dataset/attribute.
    pub fn space(&self) -> Result<Dataspace> {
        if self.is_attr() {
            Dataspace::from_id(h5try!(H5Aget_space(self.id())))
        } else {
            Dataspace::from_id(h5try!(H5Dget_space(self.id())))
        }
    }

    #[doc(hidden)]
    pub fn get_shape(&self) -> Result<Vec<Ix>> {
        self.space().map(|s| s.dims())
    }

    /// Returns the shape of the dataset/attribute.
    pub fn shape(&self) -> Vec<Ix> {
        self.space().ok().map_or_else(Vec::new, |s| s.dims())
    }

    /// Returns the number of dimensions in the dataset/attribute.
    pub fn ndim(&self) -> usize {
        self.space().ok().map_or(0, |s| s.ndim())
    }

    /// Returns the total number of elements in the dataset/attribute.
    pub fn size(&self) -> usize {
        self.shape().size()
    }

    /// Returns whether this dataset/attribute is a scalar.
    pub fn is_scalar(&self) -> bool {
        self.ndim() == 0
    }

    /// Returns the amount of file space required for the dataset/attribute. Note that this
    /// only accounts for the space which has actually been allocated (it can be equal to zero).
    pub fn storage_size(&self) -> u64 {
        if self.is_attr() {
            h5lock!(H5Aget_storage_size(self.id())) as _
        } else {
            h5lock!(H5Dget_storage_size(self.id())) as _
        }
    }

    /// Reads a dataset/attribute into an n-dimensional array.
    ///
    /// If the array has a fixed number of dimensions, it must match the dimensionality
    /// of the dataset/attribute.
    pub fn read<T: H5Type, D: ndarray::Dimension>(&self) -> Result<Array<T, D>> {
        self.as_reader().read()
    }

    /// Reads a dataset/attribute into a vector in memory order.
    pub fn read_raw<T: H5Type>(&self) -> Result<Vec<T>> {
        self.as_reader().read_raw()
    }

    /// Reads a dataset/attribute into a 1-dimensional array.
    ///
    /// The dataset/attribute must be 1-dimensional.
    pub fn read_1d<T: H5Type>(&self) -> Result<Array1<T>> {
        self.as_reader().read_1d()
    }

    /// Reads the given `slice` of the dataset into a 1-dimensional array.
    /// The slice must yield a 1-dimensional result.
    pub fn read_slice_1d<T, S>(&self, slice: &SliceInfo<S, ndarray::Ix1>) -> Result<Array1<T>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
    {
        self.as_reader().read_slice_1d(slice)
    }

    /// Reads a dataset/attribute into a 2-dimensional array.
    ///
    /// The dataset/attribute must be 2-dimensional.
    pub fn read_2d<T: H5Type>(&self) -> Result<Array2<T>> {
        self.as_reader().read_2d()
    }

    /// Reads the given `slice` of the dataset into a 2-dimensional array.
    /// The slice must yield a 2-dimensional result.
    pub fn read_slice_2d<T, S>(&self, slice: &SliceInfo<S, ndarray::Ix2>) -> Result<Array2<T>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
    {
        self.as_reader().read_slice_2d(slice)
    }

    /// Reads a dataset/attribute into an array with dynamic number of dimensions.
    pub fn read_dyn<T: H5Type>(&self) -> Result<ArrayD<T>> {
        self.as_reader().read_dyn()
    }

    /// Reads a slice of an n-dimensional array.
    /// If the dimensionality `D` has a fixed number of dimensions, it must match the dimensionality of
    /// the slice, after singleton dimensions are dropped.
    /// Use the multi-dimensional slice macro `s![]` from `ndarray` to conveniently create
    /// a multidimensional slice.
    pub fn read_slice<T, S, D>(&self, slice: &SliceInfo<S, D>) -> Result<Array<T, D>>
    where
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
        D: ndarray::Dimension,
    {
        self.as_reader().read_slice(slice)
    }

    /// Reads a scalar dataset/attribute.
    pub fn read_scalar<T: H5Type>(&self) -> Result<T> {
        self.as_reader().read_scalar()
    }

    /// Writes an n-dimensional array view into a dataset/attribute.
    ///
    /// The shape of the view must match the shape of the dataset/attribute exactly.
    /// The input argument must be convertible to an array view (this includes slices).
    pub fn write<'b, A, T, D>(&self, arr: A) -> Result<()>
    where
        A: Into<ArrayView<'b, T, D>>,
        T: H5Type,
        D: ndarray::Dimension,
    {
        self.as_writer().write(arr)
    }

    /// Writes a 1-dimensional array view into a dataset/attribute in memory order.
    ///
    /// The number of elements in the view must match the number of elements in the
    /// destination dataset/attribute. The input argument must be convertible to a
    /// 1-dimensional array view (this includes slices).
    pub fn write_raw<'b, A, T>(&self, arr: A) -> Result<()>
    where
        A: Into<ArrayView1<'b, T>>,
        T: H5Type,
    {
        self.as_writer().write_raw(arr)
    }

    /// Writes all data from the array `arr` into the given `slice` of the target dataset.
    /// The shape of `arr` must match the shape the set of elements included in the slice.
    /// If the array has a fixed number of dimensions, it must match the dimensionality of
    /// dataset. Use the multi-dimensional slice macro `s![]` from `ndarray` to conveniently create
    /// a multidimensional slice.
    pub fn write_slice<'b, A, T, S, D>(&self, arr: A, slice: &SliceInfo<S, D>) -> Result<()>
    where
        A: Into<ArrayView<'b, T, D>>,
        T: H5Type,
        S: AsRef<[SliceOrIndex]>,
        D: ndarray::Dimension,
    {
        self.as_writer().write_slice(arr, slice)
    }

    /// Writes a scalar dataset/attribute.
    pub fn write_scalar<T: H5Type>(&self, val: &T) -> Result<()> {
        self.as_writer().write_scalar(val)
    }
}