Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#!/usr/bin/env python
import h5py
import numpy as np
import scipy as sp
import scipy.ndimage as spi
import matplotlib.pyplot as plt

Basic operations

Basic operations allows to access the dataset records from HDF5.

  • Open file, get dataset, get array for current event, and close file:
Code Block

    file    = h5py.File(hdf5_file_name, 'r')   # Open hdf5 file in read-only mode
    dataset = file[dataset_name]
    arr1ev  = dataset[event_number]
    file.close()

HDF5 file structure

Detailed desciption of the HDF5 file structure can be found in HDF5 or h5py
web sites. Breifly speaking, its structure resembles the file system directory tree. The top level of the HDF5 tree is a file;
file may contain groups and datasets; each group may contain other groups and datasets; each dataset contains the data objects, which in most cases can be associated with numpy types.
So, there are three type of items in HDF5 file: File, Group, and Dataset. Their names are used as an access keys.

Basic operations

Basic operations allows to access the dataset records from HDF5.
Here we assume that user knows explicitly the names of file and dataset and event number, for example

Code Block

    hdf5_file_name = '/reg/d/psdm/XPP/xppcom10/hdf5/xppcom10-r0546.h5'
    dataset_name   = '/Configure:0000/Run:0000/CalibCycle:0000/Camera::FrameV1/XppSb4Pim.1:Tm6740.1/image'
    event_number   = 5
  • Open file, get dataset, get array for current event, and close file:
Code Block

    file    = h5py.File(hdf5_file_name, 'r')   # 'r' means that hdf5 file is open in read-only mode
    dataset = file[dataset_name]
    arr1ev  = dataset[event_number]
    file.close()

The arr1ev is a NumPy object. There are many methods which allow to manipulate with this object. For example, one can

  • print array shape and content:
Code Block

    print 'arr1ev.shape =', arr1ev.shape
    print 'arr1ev =\n',     arr1ev

Advanced operations

As in previous case we assume thatwhere we assume that all necessary parameters were defined earlier, for example

Code Block
    hdf5_file_name = '/reg/d/psdm/XPP/xppcom10/hdf5/xppcom10-r0546.h5'
    dataset_name   = '/Configure:0000/Run:0000/CalibCycle:0000/Camera::FrameV1/XppSb4Pim.1:Tm6740.1/image'
    event_number   = 5

The arr1ev is returned as a NumPy object. There are many methods which allow to manipulate with this object. For example, one can

  • print array shape and content:
Code Block

    print 'arr1ev.shape =', arr1ev.shape
    print 'arr1ev =\n',     arr1ev

Advanced operations

As in previous examples we assume that

Code Block

    file = h5py.File(hdf5_file_name, 'r')
    ds   = file[dataset_name]

Check if the HDF5 item is "File", "Group", or "Data"

Code Block

isFile    = isinstance(ds,h5py.File)
isGroup   = isinstance(ds,h5py.Group)
isDataset = isinstance(ds,h5py.Dataset)

Get information about HDF5 item

  • For all HDF5 items:
    these parameters are available:
    Code Block
    
    ds.id
    ds.ref
    ds.parent
    ds.file
    ds.name
    
h5py.File(hdf5_file_name, 'r')
    ds   = file[dataset_name]

Check if the HDF5 item is "File", "Group", or "Data"

Code Block

isFile    = isinstance(item, h5py.File)
isGroup   = isinstance(item, h5py.Group)
isDataset = isinstance(item, h5py.Dataset)

This standard Python method returns True or False in each case, respectively.

Get information about HDF5 item

  • For all HDF5 items:
    these parameters are available:
    Code Block
    
    ds.id      # for example: <GroupID [1] (U) 33554473>
    ds.ref     # for example: <HDF5 object reference>
    ds.parent  # for example: <HDF5 group "/Configure:0000/Run:0000/CalibCycle:0000" (5 members)>
    ds.file    # for example: <HDF5 file "cxi80410-r0587.h5" (mode r, 3.5G)>
    ds.name    # for example: /Configure:0000/Run:0000/CalibCycle:0000/Camera::FrameV1
    
  • Dataset
    Code Block
    
    ds.dtype   # for example: ('seconds', '<u4'), ('nanoseconds', '<u4')]
    ds.shape   # for example: (1186,)
    ds.value   # for example: (1297610252L, 482193420L)
    Dataset
    Code Block
     
    dataset_type  = ds.dtype
    dataset_shape = ds.shape
    dataset_value = ds.value 
    
  • Get item attributes for File or Group (if attributes available)
    Code Block
                        #group or file
    dict_attributes    = group.attrs
    attrs_keys         = group.attrs.keys()
    attrs_values       = group.attrs.values()
    number_of_attrs    = len(group.attrs)
    
    # For example, one of the file attributes is a run number:
    run_number         = file.attrs['runNumber']
    

...