hdf5 using h5py
Date: February 16th 2018
Last updated: February 16th 2018
For h5py documentation go here.
Python version
python3 --version
# Python 3.5.2
Install h5py
sudo apt-get install python-h5py
Virtualenv
mkvirtualenv hdf5test -p python3
h5py not immediately available
# (hdf5test) ray:~$ python
import h5py
#ImportError: No module named 'h5py'
Install (+additional libs)
pip install h5py
#pip install tables
#pip install cython
Import data
# python
import h5py
Create hdf5
f = h5py.File("uav.hdf5")
Add data
f["uavName"] = 'The Camel'
f["uavType"] = 'quadrotor'
f["uavWeight"] = 2.3 #kg
f["uavAspect"] = [150, 151] #degrees
f["uavAlt"] = [120, 119] #meters
f["uavRoll"] = [1, 1] #degrees
f["uavPitch"] = [-1, 2] #degrees
f["nGCP"] = 10
f["cameraBrand"] = 'Samsung'
f["cameraModel"] = 'SM-G920I'
f["cameraFocal"] = 4.3
f["cameraWidth"] = 5312
f["cameraHeight"] = 2988
f["cameraImg"] = [b'1.jpg', b'2.jpg']
f["cameraTime"] = [1, 2] #seconds from start
f["cameraRoll"] = [0, 0]
f["cameraPitch"] = [0, 0]
f["cameraLat"] = [31.23456, 31.23460]
f["cameraLon"] = [152.12367, 152.12367]
f["date"] = datetime.datetime.now().isoformat()
f["temp"] = 25
f["humidity"] = 74
f["windSpeed"] = 15
f["windDir"] = 'S'
f["locName"] = 'Test'
f["pilot"] = 'Ray'
f['team'] = [b'Ray', b'Spot', b'Helen']
Get value
f["windSpeed"].value
#returns: 15
Slice first image
f['cameraImg'].value[0]
#returns: b'1.jpg'
f['cameraImg'].value[:1]
#returns: array([b'1.jpg'], dtype='|S5')
Get keys
[x for x in f.keys()]
#returns: ['cameraBrand', 'cameraFocal', 'cameraHeight', 'cameraImg',
# 'cameraLat', 'cameraLon', 'cameraModel', 'cameraPitch', 'cameraRoll',
# 'cameraTime', 'cameraWidth', 'date', 'humidity', 'locName', 'nGCP',
# 'pilot', 'team', 'temp', 'uavAlt', 'uavAspect', 'uavName', 'uavPitch',
# 'uavRoll', 'uavThrust', 'uavType', 'uavWeight', 'windDir', 'windSpeed']
Get keys and values
for key, value in f.items():
print(key, value)
# returns (first 3 lines)
#cameraBrand <HDF5 dataset "cameraBrand": shape (), type "|O">
#cameraFocal <HDF5 dataset "cameraFocal": shape (), type "<f8">
#cameraHeight <HDF5 dataset "cameraHeight": shape (), type "<i8">
Update (fail)
You cannot overwrite data.
f["cameraImg"] = [b'1.jpg', b'2.jpg', b'3.jpg']
#RuntimeError: Unable to create link (name already exists)
Close file
f.close()
Change field
Data must be same shape to overwrite. Instead delete and recreate.
#read file
f1 = h5py.File("uav.hdf5", 'r+')
# delete data
del f1["cameraImg"]
# new data
nd = [b'1.jpg',b'2.jpg',b'3.jpg']
# update
ds = f1.create_dataset("cameraImg", data=nd)