Import a PShell HDF5 data file in Python
The snippet can be accessed without any authentication.
Authored by
muntwiler_m
This code snippet shows how to access PShell data in Python using the h5py
library.
The essential part is to create a h5py.File
object.
The object exposes all datasets contained in the file in a dictionary (mapping) syntax.
The snippet also shows how to discover datasets in the file and how to plot a spectrum in matplotlib
.
Requirements:
- python >= 3.6
- numpy
- h5py
- matplotlib (optional, for plotting)
# this guide requires python 3.6
import sys
sys.version
import numpy as np
import h5py
# open the file read-only, with error handling
try:
h5 = h5py.File("pshell-20200824-191714-ManipulatorScan.h5", "r")
except OSError:
print("error opening file")
# just checkin'
print(h5.file.filename)
# what's inside the file at top level?
h5.keys()
# the general group contains the experiment metadata
general = h5['general']
general.keys()
# authors is an array, the other ones are singletons
# call the decode method to convert to string
print([v.decode() for v in general['authors']])
print(general['pgroup'][()].decode())
print(general['sample'][()].decode())
# most files contain just one scan group.
# XPS spectra, e.g., can contain multiple scans.
#
# what's inside a scan group?
h5['scan 1'].keys()
import matplotlib.pyplot as plt
# the datasets (all items except 'attrs' and 'meta') contain the measured data
# they are represented as numpy datasets and can be plotted directly
plt.plot(h5['scan 1']['ManipulatorPhi'], h5['scan 1']['Counts'])
# the attrs group contains beamline status
h5['scan 1']['attrs'].keys()
# create a shortcut to the first scan group
# scan1 and h5['scan 1'] can be used interchangeably
scan1 = h5['scan 1']
print(scan1)
# a scan group also contains direct 'attributes'
# note that 'attributes' is used with several different meanings - unfortunately:
# - the scan group contains another group called 'attrs'
# - the scan group has 'attributes', which are represented in h5py by the 'attrs' attribute :-o
#
scan1.attrs.keys()
# number of scan dimensions
print(scan1.attrs['Dimensions'])
# number of scan steps
print(scan1.attrs['Steps'])
# number of iterations (XPS spectra only)
try:
print(scan1.attrs['Iterations'])
except KeyError:
print("(no iterations)")
# number of passes
print(scan1.attrs['Passes'])
# start and end time in microseconds
print((scan1.attrs['End'] - scan1.attrs['Start']) / 1000 / 60)
# list of scan positioners
scan1.attrs['Writables']
# remember to cast to string
scan1.attrs['Writables'][0].decode()
# list of detectors
scan1.attrs['Readables']
# note - these are the names of the datasets in the scan group!
scan1.keys()
Please register or sign in to comment