"""
Simple HDF5 example using h5py (see www.h5py.org)

Reads an HDF5 file, exported from SIMA
Performs simple post processing
Writes the result to HDF5
"""

import h5py
import numpy as np


# Filename as exported from SIMA
fin = 'flexout.h5'

# Open HDF5-file from SIMA for reading
f = h5py.File(fin,'r')

# Find name of data set using e.g. ViTables (comes with h5py) or HDFView
dsetName = '/Flex/HDF5Example/Dynamic/RiserLine/segment_1/element_1/Axial force'

# Read data set 
data = f[dsetName]

# Read attributes 
attributes = f[dsetName].attrs

# Example of simple post processing
dataOut = data - np.mean(data)

# Use whatever structure you like
dsetOutName = '/PythonResults/Axial force'

# Name of new HDF5-file
fout = 'flexinPy.h5'

# Open file for writing
g = h5py.File(fout,'w')

# Write data set to HDF5
g[dsetOutName] = dataOut

# Write attributes to HDF5
for i,a in enumerate(attributes.keys()):
    g[dsetOutName].attrs[a] = attributes.values()[i]

# Close both files
g.close()
f.close()
