Decorate an HDF5-reading function to open a filepath if needed

    ``func`` should be written to presume an `h5py.Group` as the first
    positional argument.
495830
def with_read_hdf5(func):
    
    @wraps(func)
    def decorated_func(fobj, *args, **kwargs):
        # pylint: disable=missing-docstring
        if not isinstance(fobj, h5py.HLObject):
            if isinstance(fobj, FILE_LIKE):
                fobj = fobj.name
            with h5py.File(fobj, 'r') as h5f:
                return func(h5f, *args, **kwargs)
        return func(fobj, *args, **kwargs)

    return decorated_func
776801
def withFile(file, func, mode='r', expand=False):
    
    file = _normalizeToFile(file, mode=mode, expand=expand)
    try:      return func(file)
    finally:  file.close()
50335
def from_file(cls, fn, *args, **kwargs):
        
        return AmiraHeader(get_parsed_data(fn, *args, **kwargs))
409244
def hdf5write(h5file, obj, root=''):
    
    dic = node_to_dict(obj_to_node(obj))
    h5file.save(dic, root)
281728
def read_hdf (self, key, **kwargs):
        
        # This one needs special handling because of the "key" and path input.
        import pandas
        return pandas.read_hdf (text_type (self), key, **kwargs)
1290611
def read_dir(input_dir,input_ext,func):
    ''''''
    import os
    for dirpath, dnames, fnames in os.walk(input_dir):
        for fname in fnames:
            if not dirpath.endswith(os.sep):
                dirpath = dirpath + os.sep
            if fname.endswith(input_ext):
                func(read_file(dirpath + fname))
90797
def save_hdf_metadata(filename, metadata, groupname="data", mode="a"):
    """""""
    with _h5py.File(filename, mode) as f:
        for key, val in metadata.items():
            f[groupname].attrs[key] = val
868647
def hdf5_read_in(self):
        

        with h5py.File(self.WORKING_DIRECTORY + '/' + self.file_name) as f:

            # read in
            data = f['data']

            # find number of distinct x and y points.
            num_x_pts = len(np.unique(data[self.x_column_label][:]))
            num_y_pts = len(np.unique(data[self.y_column_label][:]))

            # create 2D arrays of x,y,z
            self.xvals = np.reshape(data[self.x_column_label][:], (num_y_pts, num_x_pts))
            self.yvals = np.reshape(data[self.y_column_label][:], (num_y_pts, num_x_pts))
            self.zvals = np.reshape(data[self.z_column_label][:], (num_y_pts, num_x_pts))
        return
1287734
def open(self, filepath):
        
        with io.open(filepath, 'r', encoding='utf-8') as fp:
            content = fp.read()
        return content
1288715
def _openResources(self):
        
        logger.info("Opening: {}".format(self._fileName))
        self._h5Group = h5py.File(self._fileName, 'r')
873918
def h5features_convert(self, infile):
        """"""
        with h5py.File(infile, 'r') as f:
            groups = list(f.keys())
        for group in groups:
            self._writer.write(
                Reader(infile, group).read(),
                self.groupname, append=True)
519414
def _make_read_func(file_obj):
    """"""
    @ffi.callback("cairo_read_func_t", error=constants.STATUS_READ_ERROR)
    def read_func(_closure, data, length):
        string = file_obj.read(length)
        if len(string) < length:  # EOF too early
            return constants.STATUS_READ_ERROR
        ffi.buffer(data, length)[:len(string)] = string
        return constants.STATUS_SUCCESS
    return read_func
1092776
def _read(filepath_or_buffer: FilePathOrBuffer, kwds):
    """"""
    encoding = kwds.get('encoding', None)
    if encoding is not None:
        encoding = re.sub('_', '-', encoding).lower()
        kwds['encoding'] = encoding

    compression = kwds.get('compression', 'infer')
    compression = _infer_compression(filepath_or_buffer, compression)

    # TODO: get_filepath_or_buffer could return
    # Union[FilePathOrBuffer, s3fs.S3File, gcsfs.GCSFile]
    # though mypy handling of conditional imports is difficult.
    # See https://github.com/python/mypy/issues/1297
    fp_or_buf, _, compression, should_close = get_filepath_or_buffer(
        filepath_or_buffer, encoding, compression)
    kwds['compression'] = compression

    if kwds.get('date_parser', None) is not None:
        if isinstance(kwds['parse_dates'], bool):
            kwds['parse_dates'] = True

    # Extract some of the arguments (pass chunksize on).
    iterator = kwds.get('iterator', False)
    chunksize = _validate_integer('chunksize', kwds.get('chunksize', None), 1)
    nrows = kwds.get('nrows', None)

    # Check for duplicates in names.
    _validate_names(kwds.get("names", None))

    # Create the parser.
    parser = TextFileReader(fp_or_buf, **kwds)

    if chunksize or iterator:
        return parser

    try:
        data = parser.read(nrows)
    finally:
        parser.close()

    if should_close:
        try:
            fp_or_buf.close()
        except ValueError:
            pass

    return data
1316939
def dict_to_hdf5(dic, endpoint):
    
    filename = gen_filename(endpoint)
    with h5py.File(filename, 'w') as handler:
        walk_dict_to_hdf5(dic, handler)
    print('dumped to', filename)
817096
def h5ToDict(h5, readH5pyDataset=True):
    
    h = h5py.File(h5, "r")
    ret = unwrapArray(h, recursive=True, readH5pyDataset=readH5pyDataset)
    if readH5pyDataset: h.close()
    return ret
118968
def to_hdf5(self, f):
        

        if isinstance(f, str):
            import h5py
            f = h5py.File(f)

        if self.frame is not None:
            frame_group = f.create_group('frame')
            frame_group.attrs['module'] = self.frame.__module__
            frame_group.attrs['class'] = self.frame.__class__.__name__

            units = [str(x).encode('utf8')
                     for x in self.frame.units.to_dict().values()]
            frame_group.create_dataset('units', data=units)

            d = frame_group.create_group('parameters')
            for k, par in self.frame.parameters.items():
                quantity_to_hdf5(d, k, par)

        cart = self.represent_as('cartesian')
        quantity_to_hdf5(f, 'pos', cart.xyz)
        quantity_to_hdf5(f, 'vel', cart.v_xyz)

        return f
54259
def dict_to_hdf5(dict_like, h5group):
    
    # Write attributes
    for key, value in dict_like.items():
        if value is not None:
            h5group.attrs[str(key)] = value