R/h5utils.R

Defines functions h5_names_exist h5_get_dataset

Documented in h5_get_dataset h5_names_exist

#' @title get dataset from HDF5 file
#' @param h5f HDF5 file opened with \code{H5Fopen}
#' @param key String, full path to dataset.
#' @export
h5_get_dataset <- function(h5f,key)
{
  requireNamespace("rhdf5")
  exists <- rhdf5::H5Lexists(h5f, key)
  if( exists ){
    h5d <- rhdf5::H5Dopen(h5f, key)
    rval <- rhdf5::H5Dread(h5d)
    rhdf5::H5Dclose(h5d)
  } else {
    stop(sprintf("Dataset %s could not be found!", key))
  }
  return(rval)
}

#' @title check if group names exist in HDF5 file
#' @description The group names in an HDF5 file are stored as full paths
#'              as well as a flat vector. It is thus possible to check
#'              if a particular set of group names exist in the file
#'              by parsing the \code{name} member of the output
#'              of \code{h5ls}. This function does just that.
#' @param h5f HDF5 file openend with \code{H5Fopen}
#' @param nms_to_find Vector of strings, group names (not full paths) which
#'                    are to be located in the file.
#' @return Vector of booleans of the same length as \code{nms_to_find}
#'         indicating whether the name at the same index position
#'         was located in the file.
h5_names_exist <- function(h5f, nms_to_find){
  nms <- h5ls(h5f)$name
  unlist( lapply( nms_to_find, function(x){ x %in% nms } ) )
}
etmc/hadron documentation built on March 12, 2019, 8:43 p.m.