from tables import *

class Particle(IsDescription):
    _v_flavor   = "numpy"
    name        = StringCol(16, pos=1)   # 16-character String
    lati        = IntCol(pos=2)        # integer
    longi       = IntCol(pos=3)        # integer
    pressure    = Float32Col(pos=4)    # float  (single-precision)
    temperature = FloatCol(pos=5)      # double (double-precision)

# Open a file in "w"rite mode
fileh = openFile("table1-numpy.h5", mode = "w")
# Create a new group
group = fileh.createGroup(fileh.root, "newgroup")
# Create a new table in newgroup group
table = fileh.createTable(group, 'table', Particle, "A table", Filters(1))
particle = table.row
# Fill the table with 10 particles
for i in xrange(10):
    # First, assign the values to the Particle record
    particle['name']  = 'Particle: %6d' % (i)
    particle['lati'] = i
    particle['longi'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['temperature'] = float(i**2)
    # This injects the row values.
    particle.append()
# We need to flush the buffers in table in order to get an
# accurate number of records on it.
table.flush()

print "Read in an heterogeneous numpy:"
print table.read()

# Finally, close the file
fileh.close()
