Versions Compared

Key

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

Below is a simple building block example of saving data to an hdf5 file while going through events.

We do not assume we know the final size of the dataset. The example demonstrates using the hdf5 chunked storage and resize functions to grow a dataset.

Code Block
languagepython
import numpy as np
import psana
import h5py

NUM_EVENTS_TO_WRITE=20

ds = psana.DataSource('exp=xpptut15:run=54:smd')

h5out = h5py.File("simple_saved_data_for_xppdaq12_run54_cspad.h5", 'w')
saved = h5out.create_dataset('saved',(0,), dtype='f8', chunks=True, maxshape=(None,))

cspad = psana.Detector('cspad', ds.env())

for idx, evt in enumerate(ds.events()):
    if idx > NUM_EVENTS_TO_WRITE: break
    calib = cspad.calib(evt)
    if (calib is None): continue
    saved.resize((idx+1,))
    saved[idx] = np.sum(calib)

h5out.close()

Good tools to inspect an h5 files are h5ls and h5dump. For example, doing:

 h5ls -d -r simple_saved_data_for_xppdaq12_run54_cspad.h5

shows the dataset and its values:

/                        Group
/saved                   Dataset {21/Inf}
    Data:
        (0) 23773.12109375, 135712.25, 65513.67578125, 16749.18359375, 141970.578125, -3539.29711914062, 11022.4033203125,
        (7) 27192.0703125, -6797.27734375, 39909.9140625, 18433.25, 3982.2890625, 20780.5, 43662.01953125, 81957.65625,
        (15) 86769.421875, 96112.8515625, 115826.53125, -22957.05078125, -99477.6875, 79700.59375
 

A more advanced tutorial on saving data to an hdf5 file can be found on the page: More Advanced Tutorial on Saving Output in Hdf5

 NOTE: psana now provides a more automated way to do this that most users use as shown here.