Skip to content

Commit

Permalink
HDF5 file structure reorganisation, and InterfaceComponent readab…
Browse files Browse the repository at this point in the history
…ility (#280)

* Reorganise hdf5 unit tests

- Move uint16 to char/str functions to the unit test utils file
- Create hdf5_and_tdms_objects subdirectory of unit/ to hold unit tests on interaction between hdf5 and tdms classes
- Move the Matrix<double> test from test_hdf5_io into the new subdirectory
- Data files needed for unit tests are defined in a unit_test_utils namespace to avoid redefinition across multiple files

* Create file to test interface and hdf5 interactions

- Add docstrings to interface.h since these are missing and I've just had to work out what they do
- Create a matlab script that can reproduce the class_data.mat file which the hdf5 unit tests will try to create tdms objects from
- Create the barebones test_hdf5_interface file

* HDF5Reader can read from .mat file and produce an InterfaceComponent

* File restructure: accounting for how many tests we are going to have with HDF5

* Prune includes

* Add .mat file for HDF5-TDMS-object unit tests to run.

- Adds scripts to reproduce this data, so in theory a new user can run a short MATLAB script to reproduce this
- Had a play with trying to get setup-matlab to run these scripts before the unit tests, but alas, no.
  • Loading branch information
willGraham01 committed May 19, 2023
1 parent 5c032e9 commit 135c048
Show file tree
Hide file tree
Showing 18 changed files with 750 additions and 535 deletions.
217 changes: 0 additions & 217 deletions tdms/include/hdf5_io.h

This file was deleted.

92 changes: 92 additions & 0 deletions tdms/include/hdf5_io/hdf5_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @file hdf5_io.h
* @brief Helper classes for HDF5 file I/O.
* @details The main classes are `HDF5Reader` and `HDF5Writer` with the methods
* `HDF5Reader::read` and `HDF5Writer::write` respectively.
*/
#pragma once

#include <memory>
#include <string>
#include <vector>

#include <H5Cpp.h>

#include "cell_coordinate.h"

/**
* @brief Convert from a vector of HDF5's hsize_t back to our struct of ints.
* @note Local scope utility function as only this code needs to interact with
* the HDF5 H5Cpp library.
*
* @param dimensions a 1, 2, or 3 element vector of dimensions.
* @return ijk The dimensions in a struct.
*/
ijk to_ijk(const std::vector<hsize_t> dimensions);

/**
* @brief The base class for HDF5 I/O.
* @details Common functionality and wraps handling the std::unique_ptr to hold
* the H5::File object.
*/
class HDF5Base {

protected:
std::string filename_; /**< The name of the file. */
std::shared_ptr<H5::H5File> file_; /**< Pointer to the underlying H5::File. */

/**
* @brief Construct a new HDF5{Reader/Writer} for a named file.
* @param filename The name of the file.
* @param mode The H5 file access mode (RDONLY for a HDF5Reader, TRUNC for a
* HDF5Writer.)
* @throws H5::FileIException if the file doesn't exist or can't be created.
*/
HDF5Base(const std::string &filename, int mode = H5F_ACC_RDONLY)
: filename_(filename) {
file_ = std::make_unique<H5::H5File>(filename, mode);
}

/**
* @brief Destructor closes the file.
* @details Closes file when HDF5Reader(or HDF5Writer) goes out of scope.
* Since the file pointer is a smart pointer it is deallocated automatically.
*/
~HDF5Base() { file_->close(); }

public:
/**
* @brief Get the name of the file.
* @return std::string the filename.
*/
std::string get_filename() const { return filename_; }

/**
* @brief Get the names of all datasets (data tables) currently in the file.
* @return std::vector<std::string> A vector of their names.
*/
std::vector<std::string> get_datanames() const;

/**
* @brief Print the names of all datasets to std::out.
*/
void ls() const;

/**
* @brief Return shape/dimensionality information about the array data stored
* with `name`.
* @param dataname The name of the data table.
* @return IJKDimensions The dimensions of the data.
*/
// IJKDimensions shape_of(const std::string &dataname) const;
std::vector<hsize_t> shape_of(const std::string &dataname) const;

/**
* @brief Checks the file is a valid HDF5 file, and everything is OK.
* TODO: Can perhaps remove.
*
* @return true If all is well.
* @return false Otherwise.
*/
bool is_ok() const;
};
Loading

0 comments on commit 135c048

Please sign in to comment.