import sys
import numpy
from tables import *

# Open a new empty HDF5 file
fileh = openFile("array1-numpy.h5", mode = "w")
# Get the root group
root = fileh.root

# Create an Array
a = numpy.array([-1, 2, 4], 'int16')
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_1', a, "Signed short array")

# Create a scalar Array
a = numpy.array(4, 'int16')
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_s', a, "Scalar signed short array")

# Create a 3-d array of floats
a = numpy.arange(120, dtype='float64')
a = a.reshape((20,3,2))
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_f', a, "3-D float array")
# Close the file
fileh.close()

# Open the file for reading
fileh = openFile("array1-numpy.h5", mode = "r")
# Get the root group
root = fileh.root

a = root.array_1.read()
print "Signed byte array -->", repr(a), a.shape

b = root.array_s.read()
print "Scalar array -->", type(b), repr(b)

print "Examples of extend slicing in Array objects:"
print "array_f[:,2:3,2::2] -->", repr(root.array_f[:,2:3,2::2])
print "array_f[1,2:] -->", repr(root.array_f[1,2:])
print "array_f[1] -->", repr(root.array_f[1])

# Close the file
fileh.close()
